All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeoFileConnectorDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/plugins/datasource/geofile/GeoFileConnectorDialog.cpp
22 
23  \brief A dialog window for showing the GeoFile connector widget.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSource.h"
29 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
31 #include "../../../widgets/Exception.h"
32 #include "GeoFileConnectorDialog.h"
33 #include "ui_GeoFileConnectorDialogForm.h"
34 
35 // Boost
36 #include <boost/algorithm/string/case_conv.hpp>
37 #include <boost/uuid/random_generator.hpp>
38 #include <boost/uuid/uuid_io.hpp>
39 #include <boost/filesystem.hpp>
40 
41 // Qt
42 #include <QtGui/QFileDialog>
43 #include <QtGui/QIcon>
44 #include <QtGui/QMessageBox>
45 
47  : QDialog(parent, f),
48  m_ui(new Ui::GeoFileConnectorDialogForm)
49 {
50 // add controls
51  m_ui->setupUi(this);
52 
53 // add button icons
54  m_ui->m_addDatasourceToolButton->setIcon(QIcon::fromTheme("list-add"));
55  m_ui->m_removeDatasourceToolButton->setIcon(QIcon::fromTheme("list-remove"));
56  m_ui->m_updateDatasourceToolButton->setIcon(QIcon::fromTheme("view-refresh"));
57 
58 // connect signal and slots
59  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
60  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
61  connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
62  connect(m_ui->m_fileSearchToolButton, SIGNAL(pressed()), this, SLOT(fileSearchToolButtonPressed()));
63  connect(m_ui->m_addDatasourceToolButton, SIGNAL(pressed()), this, SLOT(addDatasourceToolButtonPressed()));
64  connect(m_ui->m_removeDatasourceToolButton, SIGNAL(pressed()), this, SLOT(removeDatasourceToolButtonPressed()));
65  connect(m_ui->m_updateDatasourceToolButton, SIGNAL(pressed()), this, SLOT(updateDatasourceToolButtonPressed()));
66 
67  m_ui->m_openPushButton->setEnabled(false);
68 }
69 
71 {
72 }
73 
74 const std::list<te::da::DataSourceInfoPtr>& te::qt::plugins::geofile::GeoFileConnectorDialog::getDataSources() const
75 {
76  return m_datasources;
77 }
78 
79 void te::qt::plugins::geofile::GeoFileConnectorDialog::set(const std::list<te::da::DataSourceInfoPtr>& datasources)
80 {
81  m_datasources = datasources;
82 
83  m_ui->m_datasourceListWidget->clear();
84 
85  for(std::list<te::da::DataSourceInfoPtr>::iterator it = m_datasources.begin(); it != m_datasources.end(); ++it)
86  {
87  QString id = QString::fromStdString((*it)->getId());
88  QString title = QString::fromStdString((*it)->getTitle());
89 
90  QListWidgetItem* item = new QListWidgetItem(title);
91  item->setData(Qt::UserRole, QVariant(id));
92 
93  m_ui->m_datasourceListWidget->addItem(item);
94  }
95 
96  if(m_ui->m_datasourceListWidget->count() != 0)
97  {
98  QListWidgetItem* item = m_ui->m_datasourceListWidget->item(0);
99  m_ui->m_datasourceListWidget->setCurrentItem(item);
100  dataSourcePressed(item);
101 
102  m_ui->m_openPushButton->setEnabled(true);
103  }
104 }
105 
107 {
108  accept();
109 }
110 
112 {
113  try
114  {
115  test();
116 
117  QMessageBox::warning(this,
118  tr("TerraLib Qt Components"),
119  tr("Data source is ok!"));
120  }
121  catch(const std::exception& e)
122  {
123  QMessageBox::warning(this,
124  tr("TerraLib Qt Components"),
125  tr(e.what()));
126  }
127  catch(...)
128  {
129  QMessageBox::warning(this,
130  tr("TerraLib Qt Components"),
131  tr("Unknown error when probing data source!"));
132  }
133 }
134 
136 {
137  QMessageBox::warning(this,
138  tr("TerraLib Qt Components"),
139  tr("Not implemented yet!\nWe will provide it soon!"));
140 }
141 
143 {
144  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Geo Spatial File"), QString(""), tr("Image File (*.png, *.jpg, *.jpeg, *.tif, *.tiff, *.geotif, *.geotiff);; Vector File (*.shp, *.geojson, *.kml);; All Files (*.*)"), 0, QFileDialog::ReadOnly);
145 
146  if(fileName.isEmpty())
147  return;
148 
149  m_ui->m_datasourceLineEdit->setText(fileName);
150 }
151 
153 {
154  try
155  {
156  te::da::DataSourcePtr driver = test();
157 
158  assert(driver.get());
159 
160  if(driver.get() != 0)
161  {
163 
164  nds->setConnInfo(driver->getConnectionInfo());
165 
166  boost::uuids::basic_random_generator<boost::mt19937> gen;
167  boost::uuids::uuid u = gen();
168  std::string dsId = boost::uuids::to_string(u);
169 
170  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
171 
172  if(title.isEmpty())
173  title = m_ui->m_datasourceLineEdit->text().trimmed();
174 
175  nds->setId(dsId);
176  nds->setTitle(title.toUtf8().data());
177  nds->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
178  nds->setAccessDriver(driver->getType());
179  nds->setType("GEOFILE");
180 
181  m_datasources.push_back(nds);
182 
183  QString id = QString::fromStdString(nds->getId());
184 
185  QListWidgetItem* item = new QListWidgetItem(title);
186  item->setData(Qt::UserRole, QVariant(id));
187 
188  m_ui->m_datasourceListWidget->addItem(item);
189 
190  m_ui->m_openPushButton->setEnabled(true);
191  }
192  }
193  catch(const std::exception& e)
194  {
195  QMessageBox::warning(this,
196  tr("TerraLib Qt Components"),
197  tr(e.what()));
198  }
199  catch(...)
200  {
201  QMessageBox::warning(this,
202  tr("TerraLib Qt Components"),
203  tr("Unknown error when probing data source!"));
204  }
205 }
206 
208 {
209  QListWidgetItem* item = m_ui->m_datasourceListWidget->currentItem();
210 
211  if(item == 0)
212  return;
213 
214  QVariant udata = item->data(Qt::UserRole);
215 
216  QString id = udata.toString();
217 
218  if(id.isEmpty())
219  return;
220 
221  std::string dsId = id.toStdString();
222 
223  std::list<te::da::DataSourceInfoPtr>::iterator it = std::find_if(m_datasources.begin(), m_datasources.end(), FindById(dsId));
224 
225  if(it == m_datasources.end())
226  return;
227 
228  m_datasources.erase(it);
229 
230  int itemRow = m_ui->m_datasourceListWidget->row(item);
231 
232  item = m_ui->m_datasourceListWidget->takeItem(itemRow);
233 
234  delete item;
235 
236  if(m_datasources.empty())
237  m_ui->m_openPushButton->setEnabled(false);
238 }
239 
241 {
242  QMessageBox::warning(this,
243  tr("TerraLib Qt Components"),
244  tr("Not implemented yet!\nWe will provide it soon!"));
245 }
246 
248 {
249  m_ui->m_datasourceLineEdit->setText("");
250  m_ui->m_datasourceTitleLineEdit->setText("");
251  m_ui->m_datasourceDescriptionTextEdit->setText("");
252 
253  if(item == 0)
254  return;
255 
256  QVariant udata = item->data(Qt::UserRole);
257 
258  QString id = udata.toString();
259 
260  if(id.isEmpty())
261  return;
262 
263  std::string dsId = id.toStdString();
264 
265  std::list<te::da::DataSourceInfoPtr>::iterator it = std::find_if(m_datasources.begin(), m_datasources.end(), FindById(dsId));
266 
267  if(it == m_datasources.end())
268  return;
269 
270  std::map<std::string, std::string>::const_iterator itdsinfo = (*it)->getConnInfo().find("SOURCE"); // this can change!
271 
272  if(itdsinfo == (*it)->getConnInfo().end())
273  return;
274 
275  // in the future we must check the underlying driver!
276  //if((*it)->getAccessDriver() == "OGR")
277  //{
278  //}
279  //else if((*it)->getAccessDriver() == "GEOFILE")
280  //{
281  //}
282 
283  m_ui->m_datasourceLineEdit->setText(QString::fromStdString(itdsinfo->second));
284  m_ui->m_datasourceTitleLineEdit->setText(QString::fromStdString((*it)->getTitle()));
285  m_ui->m_datasourceDescriptionTextEdit->setText(QString::fromStdString((*it)->getDescription()));
286 }
287 
289 {
290  std::map<std::string, std::string> dsInfo;
291 
292 // check data acccess driver to be used
293  std::string driverType;
294 
295  boost::filesystem::path fp = m_ui->m_datasourceLineEdit->text().trimmed().toUtf8().data();
296 
297  std::string fileExtension = boost::to_upper_copy(fp.extension().string());
298 
299  if((fileExtension == ".TIF") || (fileExtension == ".TIFF") || (fileExtension == ".GEOTIF") || (fileExtension == ".GEOTIFF") ||
300  (fileExtension == ".PNG") || (fileExtension == ".JPG") || (fileExtension == ".JPEG"))
301  {
302  if(te::da::DataSourceFactory::find("GEOFILE") == 0)
303  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Sorry! No data access driver loaded for GEOFILE data sources!"));
304 
305  dsInfo["SOURCE"] = m_ui->m_datasourceLineEdit->text().trimmed().toStdString();
306 
307  driverType = "GEOFILE";
308  }
309  else if((fileExtension == ".SHP") || (fileExtension == ".GEOJSON") || (fileExtension == ".KML"))
310  {
311  if(te::da::DataSourceFactory::find("OGR") == 0)
312  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Sorry! No data access driver loaded for OGR data sources!"));
313 
314  dsInfo["SOURCE"] = m_ui->m_datasourceLineEdit->text().trimmed().toStdString();
315 
316  driverType = "OGR";
317  }
318  else
319  {
320  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Sorry! Unknown data access driver for the selected file!"));
321  }
322 
324 
325  if(ds.get() == 0)
326  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Could not open data source due to an unexpected error!"));
327 
328  ds->setConnectionInfo(dsInfo);
329  ds->open();
330 
331  return ds;
332 }
333 
335 {
336  return (ds->getId() == m_id);
337 }
338 
void set(const std::list< te::da::DataSourceInfoPtr > &datasources)
static bool find(const std::string &dsType)
bool operator()(const te::da::DataSourceInfoPtr &ds) const
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
const std::list< te::da::DataSourceInfoPtr > & getDataSources() const
static std::auto_ptr< DataSource > make(const std::string &dsType)
GeoFileConnectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
A dialog window for showing the GeoFile connector widget.
A class that represents a data source component.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
std::auto_ptr< Ui::GeoFileConnectorDialogForm > m_ui