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 "GDALConnectorDialog.h"
28 #include "../../../../core/translator/Translator.h"
29 #include "../../../../core/filesystem/FileSystem.h"
30 #include "../../../../core/uri/URI.h"
31 #include "../../../../core/uri/Utils.h"
32 #include "../../../../dataaccess/datasource/DataSource.h"
33 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
34 #include "../../../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
36 #include "../../../af/Utils.h"
37 #include "../../../widgets/Exception.h"
38 #include "../../../widgets/Utils.h"
39 #include "../../../widgets/raster/RasterInfoDialog.h"
40 #include "../../../widgets/raster/RasterInfoWidget.h"
41 #include "ui_GDALConnectorDialogForm.h"
42 
43 // Boost
44 #include <boost/uuid/random_generator.hpp>
45 #include <boost/uuid/uuid_io.hpp>
46 #include <boost/lexical_cast.hpp>
47 
48 // Qt
49 #include <QFileInfo>
50 #include <QFileDialog>
51 #include <QMessageBox>
52 
53 #include <memory>
54 
56  : QDialog(parent, f),
57  m_ui(new Ui::GDALConnectorDialogForm)
58 {
59 // add controls
60  m_ui->setupUi(this);
61 
62 // connect signal and slots
63  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
64  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
65  connect(m_ui->m_searchDatasetToolButton, SIGNAL(pressed()), this, SLOT(searchDatasetToolButtonPressed()));
66 
67  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
68  m_ui->m_helpPushButton->setPageReference("plugins/gdal/gdal.html");
69 }
70 
72 
74 {
75  return m_datasource;
76 }
77 
79 {
80  return m_driver;
81 }
82 
84 {
85  m_datasource = ds;
86 
87  if(m_datasource.get() != nullptr)
88  {
89  setConnectionInfo(m_datasource->getConnInfoAsString());
90 
91  m_ui->m_datasourceTitleLineEdit->setText(QString::fromUtf8(m_datasource->getTitle().c_str()));
92 
93  m_ui->m_datasourceDescriptionTextEdit->setText(QString::fromUtf8(m_datasource->getDescription().c_str()));
94  }
95 }
96 
98 {
99  try
100  {
101 // check if driver is loaded
102  if(te::da::DataSourceFactory::find("GDAL") == 0)
103  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for GDAL data sources!"));
104 
105 // get data source connection info based on form data
106  std::string dsInfo = getConnectionInfo();
107 
108 // perform connection
109  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL", dsInfo);
110  ds->open();
111  m_driver.reset(ds.release());
112 
113  if(m_driver.get() == nullptr)
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() == nullptr)
122  {
123 // create a new data source based on form data
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.toUtf8().data());
135  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().toUtf8().data());
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.toUtf8().data());
144  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().toUtf8().data());
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  // perform connection
174  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("GDAL", getConnectionInfo()));
175  ds->open();
176 
177  if(ds.get() == nullptr)
178  throw te::qt::widgets::Exception(TE_TR("Could not open dataset via GDAL!"));
179 
180  QMessageBox::warning(this,
181  tr("TerraLib Qt Components"),
182  tr("Data source is ok!"));
183  }
184  catch(const std::exception& e)
185  {
186  QMessageBox::warning(this,
187  tr("TerraLib Qt Components"),
188  tr(e.what()));
189  }
190  catch(...)
191  {
192  QMessageBox::warning(this,
193  tr("TerraLib Qt Components"),
194  tr("Unknown error while testing GDAL data source!"));
195  }
196 }
197 
199 {
200  if(m_ui->m_fileRadioButton->isChecked())
201  {
202  std::unique_ptr< te::qt::widgets::RasterInfoDialog > diagPtr(
204  this, nullptr ) );
205  diagPtr->exec();
206  if( diagPtr->getWidget()->getFullName().empty() )
207  {
208  return;
209  }
210 
211  te::qt::widgets::AddFilePathToSettings(QString( diagPtr->getWidget()->getPath().c_str() ), "raster");
212 
213  m_ui->m_datasetLineEdit->setText(QString(diagPtr->getWidget()->getFullName().c_str()));
214  }
215  else if(m_ui->m_dirRadioButton->isChecked())
216  {
217  QString dirName = QFileDialog::getExistingDirectory(this, tr("Select a directory with image files"), te::qt::widgets::GetFilePathFromSettings("raster"), QFileDialog::ShowDirsOnly);
218 
219  if(dirName.isEmpty())
220  return;
221 
222  te::qt::widgets::AddFilePathToSettings(dirName, "raster");
223 
224  m_ui->m_datasetLineEdit->setText(dirName);
225  }
226  else
227  {
228  QMessageBox::warning(this,
229  tr("TerraLib Qt Components"),
230  tr("Sorry, network files are not implemented yet!\nWe will provide it soon!"));
231  }
232 }
233 
235 {
236  QString qstr; // Auxiliary string used to hold temporary data
237 
238  std::string strURI = "file://"; // The base of the URI
239 
240  qstr = m_ui->m_datasetLineEdit->text().trimmed();
241 
242  if(qstr.isEmpty())
243  throw te::qt::widgets::Exception(TE_TR("Please select a dataset first!"));
244 
245  strURI += qstr.toUtf8().data();
246 
247  return strURI;
248 }
249 
251 {
252  const te::core::URI uri(connInfo);
253 
254  std::string path = te::core::URIDecode(uri.host() + uri.path());
255 
256  if(!path.empty())
257  {
258  m_ui->m_datasetLineEdit->setText(QString::fromUtf8(path.c_str()));
259 
261  m_ui->m_dirRadioButton->setChecked(true);
263  m_ui->m_fileRadioButton->setChecked(true);
264  }
265 }
266 
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
boost::shared_ptr< DataSource > DataSourcePtr
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
void setConnectionInfo(const std::string &connInfo)
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
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.
const te::da::DataSourcePtr & getDriver() const
std::string host() const
Retrieving the host.
Definition: URI.cpp:108
std::unique_ptr< Ui::GDALConnectorDialogForm > m_ui
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
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.
TECOREEXPORT std::string URIDecode(const std::string &srcUri)
Decodes an encoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
This class is a dialog for the RasterInfoWidget.