All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GDALConnectorDialog.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/gdal/GDALConnectorDialog.cpp
22 
23  \brief A dialog window for showing the GDAL 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/DataSourceManager.h"
31 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
32 #include "../../../af/Utils.h"
33 #include "../../../widgets/Exception.h"
34 #include "../../../widgets/Utils.h"
35 #include "GDALConnectorDialog.h"
36 #include "ui_GDALConnectorDialogForm.h"
37 
38 // Boost
39 #include <boost/uuid/random_generator.hpp>
40 #include <boost/uuid/uuid_io.hpp>
41 #include <boost/filesystem.hpp>
42 #include <boost/lexical_cast.hpp>
43 
44 // Qt
45 #include <QFileInfo>
46 #include <QFileDialog>
47 #include <QMessageBox>
48 
50  : QDialog(parent, f),
51  m_ui(new Ui::GDALConnectorDialogForm)
52 {
53 // add controls
54  m_ui->setupUi(this);
55 
56 // connect signal and slots
57  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
58  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
59  connect(m_ui->m_searchDatasetToolButton, SIGNAL(pressed()), this, SLOT(searchDatasetToolButtonPressed()));
60 
61  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
62  m_ui->m_helpPushButton->setPageReference("plugins/gdal/gdal.html");
63 }
64 
66 {
67 }
68 
70 {
71  return m_datasource;
72 }
73 
75 {
76  return m_driver;
77 }
78 
80 {
81  m_datasource = ds;
82 
83  if(m_datasource.get() != 0)
84  {
85  setConnectionInfo(m_datasource->getConnInfo());
86 
87  m_ui->m_datasourceTitleLineEdit->setText(QString::fromStdString(m_datasource->getTitle()));
88 
89  m_ui->m_datasourceDescriptionTextEdit->setText(QString::fromStdString(m_datasource->getDescription()));
90  }
91 }
92 
94 {
95  try
96  {
97 // check if driver is loaded
98  if(te::da::DataSourceFactory::find("GDAL") == 0)
99  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for GDAL data sources!"));
100 
101 // get data source connection info based on form data
102  std::map<std::string, std::string> dsInfo;
103 
104  getConnectionInfo(dsInfo);
105 
106 // perform connection
107  //m_driver.reset(te::da::DataSourceFactory::open("GDAL", dsInfo));
108  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL");
109  ds->setConnectionInfo(dsInfo);
110  ds->open();
111  m_driver.reset(ds.release());
112 
113  if(m_driver.get() == 0)
114  throw te::qt::widgets::Exception(TE_TR("Could not open dataset via GDAL due to an unknown error!"));
115 
116  QString title = m_ui->m_datasourceTitleLineEdit->text();
117 
118  if(title.isEmpty())
119  title = m_ui->m_datasetLineEdit->text();
120 
121  if(m_datasource.get() == 0)
122  {
123 // create a new data source based on form data
124  m_datasource.reset(new te::da::DataSourceInfo);
125 
126  m_datasource->setConnInfo(dsInfo);
127 
128  boost::uuids::basic_random_generator<boost::mt19937> gen;
129  boost::uuids::uuid u = gen();
130  std::string dsId = boost::uuids::to_string(u);
131 
132  m_datasource->setId(dsId);
133  m_driver->setId(dsId);
134  m_datasource->setTitle(title.toStdString());
135  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().toStdString());
136  m_datasource->setAccessDriver("GDAL");
137  m_datasource->setType("GDAL");
138  }
139  else
140  {
141  m_driver->setId(m_datasource->getId());
142  m_datasource->setConnInfo(dsInfo);
143  m_datasource->setTitle(title.toStdString());
144  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().toStdString());
145  }
146  }
147  catch(const std::exception& e)
148  {
149  QMessageBox::warning(this,
150  tr("TerraLib Qt Components"),
151  tr(e.what()));
152  return;
153  }
154  catch(...)
155  {
156  QMessageBox::warning(this,
157  tr("TerraLib Qt Components"),
158  tr("Unknown error while opening dataset via GDAL!"));
159  return;
160  }
161 
162  accept();
163 }
164 
166 {
167  try
168  {
169 // check if driver is loaded
170  if(te::da::DataSourceFactory::find("GDAL") == 0)
171  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for GDAL data sources!"));
172 
173 // get data source connection info based on form data
174  std::map<std::string, std::string> dsInfo;
175 
176  getConnectionInfo(dsInfo);
177 
178 // perform connection
179  //std::auto_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::open("GDAL", dsInfo));
180  std::auto_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("GDAL"));
181  ds->setConnectionInfo(dsInfo);
182  ds->open();
183 
184  if(ds.get() == 0)
185  throw te::qt::widgets::Exception(TE_TR("Could not open dataset via GDAL!"));
186 
187  QMessageBox::warning(this,
188  tr("TerraLib Qt Components"),
189  tr("Data source is ok!"));
190  }
191  catch(const std::exception& e)
192  {
193  QMessageBox::warning(this,
194  tr("TerraLib Qt Components"),
195  tr(e.what()));
196  }
197  catch(...)
198  {
199  QMessageBox::warning(this,
200  tr("TerraLib Qt Components"),
201  tr("Unknown error while testing GDAL data source!"));
202  }
203 }
204 
206 {
207  if(m_ui->m_fileRadioButton->isChecked())
208  {
209  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Geo Spatial File"), te::qt::widgets::GetFilePathFromSettings("raster"),
210  tr("Image File (*.png *.jpg *.jpeg *.tif *.tiff *.geotif *.geotiff);; Web Map Service - WMS (*.xml *.wms);; Web Coverage Service - WCS (*.xml *.wcs);; All Files (*.*)"), 0, QFileDialog::ReadOnly);
211 
212  if(fileName.isEmpty())
213  return;
214 
215  QFileInfo info(fileName);
216 
217  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "raster");
218 
219  m_ui->m_datasetLineEdit->setText(fileName);
220  }
221  else if(m_ui->m_dirRadioButton->isChecked())
222  {
223  QString dirName = QFileDialog::getExistingDirectory(this, tr("Select a directory with image files"), te::qt::widgets::GetFilePathFromSettings("raster"), QFileDialog::ShowDirsOnly);
224 
225  if(dirName.isEmpty())
226  return;
227 
228  te::qt::widgets::AddFilePathToSettings(dirName, "raster");
229 
230  m_ui->m_datasetLineEdit->setText(dirName);
231  }
232  else
233  {
234  QMessageBox::warning(this,
235  tr("TerraLib Qt Components"),
236  tr("Sorry, network files are not implemented yet!\nWe will provide it soon!"));
237  }
238 }
239 
240 void te::qt::plugins::gdal::GDALConnectorDialog::getConnectionInfo(std::map<std::string, std::string>& connInfo) const
241 {
242  connInfo.clear();
243 
244  QString qstr = m_ui->m_datasetLineEdit->text().trimmed();
245 
246  if(qstr.isEmpty())
247  throw te::qt::widgets::Exception(TE_TR("Please select a dataset first!"));
248 
249  if(boost::filesystem::is_directory(qstr.toUtf8().data()))
250  connInfo["SOURCE"] = qstr.toUtf8().data();
251  else
252  connInfo["URI"] = qstr.toUtf8().data();
253 }
254 
255 void te::qt::plugins::gdal::GDALConnectorDialog::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
256 {
257  std::map<std::string, std::string>::const_iterator it = connInfo.find("URI");
258  std::map<std::string, std::string>::const_iterator itend = connInfo.end();
259 
260  if(it != itend)
261  {
262  m_ui->m_datasetLineEdit->setText(QString::fromUtf8(it->second.c_str()));
263  m_ui->m_dirRadioButton->setChecked(true);
264  return;
265  }
266 
267  it = connInfo.find("SOURCE");
268 
269  if(it != itend)
270  {
271  m_ui->m_datasetLineEdit->setText(QString::fromUtf8(it->second.c_str()));
272  m_ui->m_fileRadioButton->setChecked(true);
273  }
274 }
275 
static bool find(const std::string &dsType)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:367
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void getConnectionInfo(std::map< std::string, std::string > &connInfo) const
GDALConnectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
const te::da::DataSourceInfoPtr & getDataSource() const
void set(const te::da::DataSourceInfoPtr &ds)
A dialog window for showing the GDAL connector widget.
static std::auto_ptr< DataSource > make(const std::string &dsType)
const te::da::DataSourcePtr & getDriver() const
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
A class that represents a data source component.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:376
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
std::auto_ptr< Ui::GDALConnectorDialogForm > m_ui