DataSetOptionsWizardPage.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/plugins/exchanger/DataSetOptionsWizardPage.cpp
22 
23  \brief A Qt dialog for ....
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSetAdapter.h"
28 #include "../../../dataaccess/dataset/DataSetType.h"
29 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
30 #include "../../../dataaccess/dataset/Index.h"
31 #include "../../../dataaccess/dataset/PrimaryKey.h"
32 #include "../../../dataaccess/dataset/UniqueKey.h"
33 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
34 #include "../../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../../dataaccess/utils/Utils.h"
36 #include "../../../geometry/GeometryProperty.h"
37 #include "../../../qt/widgets/utils/ScopedCursor.h"
38 #include "../../../qt/widgets/srs/SRSManagerDialog.h"
39 #include "../../../qt/widgets/table/DataSetTableView.h"
40 #include "../property/ConstraintsIndexesListWidget.h"
41 #include "../property/DataSetAdapterWidget.h"
43 #include "ui_DataSetOptionsWizardPageForm.h"
44 
45 // STL
46 #include <algorithm>
47 
48 // Boost
49 #include <boost/algorithm/string/replace.hpp>
50 #include <boost/lexical_cast.hpp>
51 
52 // Qt
53 #include <QIcon>
54 #include <QMessageBox>
55 
57  : QWizardPage(parent),
58  m_ui(new Ui::DataSetOptionsWizardPageForm)
59 {
60 // setup controls
61  m_ui->setupUi(this);
62 
63 //build form
65  QGridLayout* constraintLayout = new QGridLayout(m_ui->m_constraintWidget);
66  constraintLayout->addWidget(m_constraintWidget.get());
67  constraintLayout->setContentsMargins(0,0,0,0);
68 
70  QGridLayout* dataSetLayout = new QGridLayout(m_ui->m_dataSetWidget);
71  dataSetLayout->addWidget(m_dataSetAdapterWidget.get());
72  dataSetLayout->setContentsMargins(0,0,0,0);
73 
74  m_ui->m_sridInputPushButton->setIcon(QIcon::fromTheme("srs"));
75  m_ui->m_sridOutputPushButton->setIcon(QIcon::fromTheme("srs"));
76 
77  //Adjusting the dataSetTableView that will be used to display the dataset's data
78  m_tblView.reset(new DataSetTableView(m_ui->m_dataPreviewFrame));
79  QGridLayout* dataPreviewLayout = new QGridLayout(m_ui->m_dataPreviewFrame);
80  dataPreviewLayout->addWidget(m_tblView.get());
81  dataPreviewLayout->setContentsMargins(0, 0, 0, 0);
82 
83  m_tblView->setAlternatingRowColors(true);
84  m_tblView->verticalHeader()->setVisible(false);
85  m_tblView->setSelectionMode(QAbstractItemView::NoSelection);
86 
88 
89 // connect signals and slots
90  connect(m_ui->m_sridInputPushButton, SIGNAL(clicked()), this, SLOT(sridInputSearchToolButtonPressed()));
91  connect(m_ui->m_sridOutputPushButton, SIGNAL(clicked()), this, SLOT(sridOutputSearchToolButtonPressed()));
92  connect(m_ui->m_applyPushButton, SIGNAL(clicked()), this, SLOT(applyChanges()));
93  connect(m_ui->m_dataPreviewGroupBox, SIGNAL(clicked(bool)), this, SLOT(onDataPreviewGroupBoxClicked()));
94  connect(m_constraintWidget.get(), SIGNAL(constraintsChanged()), this, SLOT(onDataPreviewGroupBoxClicked()));
95  connect(m_dataSetAdapterWidget.get(), SIGNAL(dataSetAdapterChanged()), this, SLOT(onDataPreviewGroupBoxClicked()));
96  connect(m_ui->m_selectedDatasetListWidget, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(datasetPressed(QListWidgetItem*)));
97 
98  //QCoreApplication* app = QCoreApplication::instance();
99  //connect(app, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(checkModificationsOnFocusChange(QWidget*, QWidget*)));
100 }
101 
103  default;
104 
105 void te::qt::widgets::DataSetOptionsWizardPage::set(const std::list<te::da::DataSetTypePtr>& datasets,
106  const te::da::DataSourceInfoPtr& datasource,
107  const te::da::DataSourceInfoPtr& targetDatasource)
108 {
109  ScopedCursor wcursor(Qt::WaitCursor);
110 
111  m_ui->m_selectedDatasetListWidget->clear();
112 
113  clearForm();
114 
115  m_datasets.clear();
116 
117  m_datasource = datasource;
118 
119  m_targetDatasource = targetDatasource;
120 
122 
123  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
124  {
125  if(it->get() == nullptr)
126  continue;
127 
128  if((*it)->size() == 0)
129  te::da::LoadProperties((*it).get(), datasource->getId());
130 
131  //create dataset adapter
132  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter((*it).get(), targetDSPtr->getCapabilities(), targetDSPtr->getEncoding());
133 
134  //fix output dataset name
135  std::string name = converter->getResult()->getName();
136 
137  std::size_t idx = name.find(".");
138  if (idx != std::string::npos)
139  {
140  name = name.substr(idx + 1, name.size() - 1);
141  }
142 
143  converter->getResult()->setName(name);
144 
145  //fix primary key name
146  if(converter->getResult() && converter->getResult()->getPrimaryKey())
147  {
148  te::da::PrimaryKey* pk = converter->getResult()->getPrimaryKey();
149  pk->setName(converter->getResult()->getName() + "_pk");
150  }
151 
152  m_datasets.insert(std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::value_type((*it), converter));
153  }
154 
155  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
156  {
157  QString title = QString::fromUtf8((*it)->getTitle().c_str());
158 
159  QString name = QString::fromUtf8((*it)->getName().c_str());
160 
161  if(title.isEmpty())
162  title = name;
163 
164  QListWidgetItem* item = new QListWidgetItem(title);
165 
166  m_ui->m_selectedDatasetListWidget->addItem(item);
167 
168  if(datasets.size() == 1)
169  {
170  m_ui->m_selectedDatasetListWidget->setCurrentItem(item);
171  item->setSelected(true);
172  datasetPressed(item);
173  }
174  }
175 
176  if(datasets.size() != 1)
177  setControlsEnabled(false);
178 }
179 
180 const std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>& te::qt::widgets::DataSetOptionsWizardPage::getDatasets() const
181 {
182  return m_datasets;
183 }
184 
186 {
187  m_ui->m_dataSetWidget->setVisible(!mode);
188  m_ui->m_constraintWidget->setVisible(!mode);
189 }
190 
192 {
193  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
194 
195  if (item == nullptr)
196  {
197  QMessageBox::warning(this, tr("Warning"), tr("Select a dataset first."));
198  return;
199  }
200 
201  if (!m_ui->m_sridInputLineEdit->text().isEmpty())
202  {
203  int inputSRID = m_ui->m_sridInputLineEdit->text().toInt();
204  int outputSRID = m_ui->m_sridOutputLineEdit->text().toInt();
205 
206  if (inputSRID != outputSRID && outputSRID == TE_UNKNOWN_SRS)
207  {
208  QMessageBox::warning(this, tr("Warning"), tr("Invalid output Layer SRID."));
209  return;
210  }
211 
212  if (inputSRID != outputSRID && inputSRID == TE_UNKNOWN_SRS)
213  {
214  QMessageBox::warning(this, tr("Warning"), tr("Invalid input Layer SRID."));
215  return;
216  }
217  }
218 
219  std::string dataSetAdapterName = item->text().toUtf8().data();
220 
221  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
222 
223  while(it != m_datasets.end())
224  {
225  if(it->second->getConvertee()->getName() == dataSetAdapterName)
226  {
227  it->second->getResult()->setName(m_ui->m_datasetNameLineEdit->text().trimmed().toUtf8().data());
228  it->second->getResult()->setTitle(m_ui->m_datasetTitleLineEdit->text().trimmed().toUtf8().data());
229 
230  //get srid information
231  if (!m_ui->m_sridInputLineEdit->text().isEmpty())
232  {
233  int inputSRID = m_ui->m_sridInputLineEdit->text().toInt();
234 
235  int outputSRID = TE_UNKNOWN_SRS;
236 
237  if (!m_ui->m_sridOutputLineEdit->text().isEmpty())
238  {
239  outputSRID = m_ui->m_sridOutputLineEdit->text().toInt();
240  }
241 
242  te::da::AssociateDataSetTypeConverterSRID(it->second, inputSRID, outputSRID);
243  }
244 
245  if(it->second->getResult()->getPrimaryKey())
246  {
247  te::da::PrimaryKey* pk = it->second->getResult()->getPrimaryKey();
248  pk->setName(it->second->getResult()->getName() + "_pk");
249 
250  // fill constraints
251  m_constraintWidget->setDataSetType(it->second->getResult());
252  }
253 
254  //char encoding
255  std::string charEncodingStr = m_ui->m_encodingComboBox->currentText().toUtf8().data();
257  it->second->setEncodingType(et);
258 
259  previewData(item, true);
260 
261  break;
262  }
263  ++it;
264  }
265 }
266 
268 {
269  te::qt::widgets::SRSManagerDialog srsDialog(this);
270  srsDialog.setWindowTitle(tr("Choose the SRS"));
271 
272  if(srsDialog.exec() != QDialog::Rejected)
273  {
274  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
275  m_ui->m_sridInputLineEdit->setText(QString::number(srid.first));
276  }
277 }
278 
280 {
281  te::qt::widgets::SRSManagerDialog srsDialog(this);
282  srsDialog.setWindowTitle(tr("Choose the SRS"));
283 
284  if (srsDialog.exec() != QDialog::Rejected)
285  {
286  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
287  m_ui->m_sridOutputLineEdit->setText(QString::number(srid.first));
288  }
289 }
290 
292 {
293  if(item == nullptr)
294  return;
295 
296  std::string dataSetAdapterName = item->text().toUtf8().data();
297 
298  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
299 
300  while(it != m_datasets.end())
301  {
302  if(it->second->getConvertee()->getName() == dataSetAdapterName)
303  {
304  te::da::DataSetType* dataset = it->second->getResult();
305 
306  // fill line edits
307  m_ui->m_datasetNameLineEdit->setEnabled(true);
308  m_ui->m_datasetNameLineEdit->setText(QString::fromUtf8(dataset->getName().c_str()));
309 
310  m_ui->m_datasetTitleLineEdit->setEnabled(true);
311  m_ui->m_datasetTitleLineEdit->setText(QString::fromUtf8(dataset->getTitle().c_str()));
312 
313  if(dataset->hasGeom())
314  {
315  m_ui->m_sridInputPushButton->setEnabled(true);
316  m_ui->m_sridInputLineEdit->clear();
317  m_ui->m_sridInputLineEdit->setEnabled(true);
318 
319  m_ui->m_sridOutputPushButton->setEnabled(true);
320  m_ui->m_sridOutputLineEdit->clear();
321  m_ui->m_sridOutputLineEdit->setEnabled(true);
322 
323  te::gm::GeometryProperty* geomPropIn = dynamic_cast<te::gm::GeometryProperty*>(it->second->getConvertee()->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
324 
325  if (geomPropIn)
326  {
327  m_ui->m_sridInputLineEdit->setText(QString::fromUtf8(boost::lexical_cast<std::string>(geomPropIn->getSRID()).c_str()));
328  }
329 
331 
332  if (geomPropOut)
333  {
334  m_ui->m_sridOutputLineEdit->setText(QString::fromUtf8(boost::lexical_cast<std::string>(geomPropOut->getSRID()).c_str()));
335  }
336  }
337  else
338  {
339  m_ui->m_sridInputPushButton->setEnabled(false);
340  m_ui->m_sridInputLineEdit->clear();
341  m_ui->m_sridInputLineEdit->setEnabled(false);
342 
343  m_ui->m_sridOutputPushButton->setEnabled(false);
344  m_ui->m_sridOutputLineEdit->clear();
345  m_ui->m_sridOutputLineEdit->setEnabled(false);
346  }
347 
348  //set char encoding
349  te::core::EncodingType et = it->second->getEncodingType();
350  int etIndex = m_ui->m_encodingComboBox->findText(te::core::CharEncoding::getEncodingName(et).c_str());
351  m_ui->m_encodingComboBox->setCurrentIndex(etIndex);
352 
353  // fill property table
354  m_dataSetAdapterWidget->setAdapterParameters(it->second->getConvertee(), it->second, m_targetDatasource);
355 
356  // fill constraints
357  m_constraintWidget->setDataSetType(dataset);
358 
359  previewData(item, false);
360 
361  break;
362  }
363  ++it;
364  }
365 }
366 
368 {
369  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
370 
371  if (!item)
372  return;
373 
374  previewData(item, true);
375 }
376 
377 //void te::qt::widgets::DataSetOptionsWizardPage::checkModificationsOnFocusChange(QWidget* old, QWidget* now)
378 //{
379 // if(old == m_ui->m_datasetNameLineEdit)
380 // {
381 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
382 //
383 // if(dataset.get() == 0)
384 // return;
385 //
386 // std::string name = m_ui->m_datasetNameLineEdit->text().trimmed().toUtf8().data();
387 //
388 // if(name != dataset->getName())
389 // {
390 // dataset->setName(name);
391 // m_ui->m_applyChangesPushButton->setEnabled(true);
392 // }
393 // }
394 // else if(old == m_ui->m_datasetTitleLineEdit)
395 // {
396 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
397 //
398 // if(dataset.get() == 0)
399 // return;
400 //
401 // std::string title = m_ui->m_datasetTitleLineEdit->text().trimmed().toUtf8().data();
402 //
403 // if(title != dataset->getTitle())
404 // {
405 // dataset->setTitle(title);
406 // m_ui->m_applyChangesPushButton->setEnabled(true);
407 // }
408 // }
409 // else if(old == m_ui->m_sridLineEdit)
410 // {
411 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
412 //
413 // if(dataset.get() == 0)
414 // return;
415 //
416 // if(!dataset->hasGeom())
417 // return;
418 //
419 // te::gm::GeometryProperty* gp = dataset->getDefaultGeomProperty();
420 //
421 // int srid = boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toUtf8().data());
422 //
423 // if(srid != gp->getSRID())
424 // {
425 // gp->setSRID(srid);
426 // m_ui->m_applyChangesPushButton->setEnabled(true);
427 // }
428 // }
429 //}
430 
432 {
433  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
434 
435  if(item == nullptr)
436  return te::da::DataSetTypePtr();
437 
438  std::string dataSetAdapterName = item->text().toUtf8().data();
439 
440  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::const_iterator it = m_datasets.begin();
441 
442  while(it != m_datasets.end())
443  {
444  if(it->second->getConvertee()->getName() == dataSetAdapterName)
445  {
446  return it->first;
447  }
448  ++it;
449  }
450 
451  return te::da::DataSetTypePtr();
452 }
453 
455 {
456  m_ui->m_datasetNameLineEdit->clear();
457  m_ui->m_datasetTitleLineEdit->clear();
458  m_ui->m_sridInputLineEdit->clear();
459  m_ui->m_sridOutputLineEdit->clear();
460 }
461 
463 {
464  m_ui->m_datasetNameLineEdit->setEnabled(enabled);
465  m_ui->m_datasetTitleLineEdit->setEnabled(enabled);
466  m_ui->m_sridInputLineEdit->setEnabled(enabled);
467  m_ui->m_sridInputPushButton->setEnabled(enabled);
468  m_ui->m_sridOutputLineEdit->setEnabled(enabled);
469  m_ui->m_sridOutputPushButton->setEnabled(enabled);
470 }
471 
473 {
474  m_ui->m_encodingComboBox->clear();
475 
476  std::vector<std::string> etNames = te::core::CharEncoding::getEncodingList();
477 
478  for (std::size_t t = 0; t < etNames.size(); ++t)
479  m_ui->m_encodingComboBox->addItem(etNames[t].c_str());
480 }
481 
482 void te::qt::widgets::DataSetOptionsWizardPage::previewData(QListWidgetItem* item, bool isResult)
483 {
484  if (m_ui->m_dataPreviewGroupBox->isChecked() == false)
485  {
486  m_tblView->setDataSet(nullptr);
487  return;
488  }
489 
490 
491  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_datasource->getId(), m_datasource->getAccessDriver(), m_datasource->getConnInfo());
492 
493  if (ds.get() == nullptr)
494  return;
495 
496  if (!ds->isOpened())
497  ds->open();
498 
499  ScopedCursor wcursor(Qt::WaitCursor);
500 
501  te::da::DataSet* datasetPreview = nullptr;
502 
503  std::string dataSetAdapterName = item->text().toUtf8().data();
504 
505  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
506 
507  while (it != m_datasets.end())
508  {
509  if (it->second->getConvertee()->getName() == dataSetAdapterName)
510  {
511  if (isResult)
512  {
513  te::da::DataSet* dataset = ds->getDataSet(it->first->getName()).release();
514 
515  std::unique_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset, it->second, true));
516 
517  datasetPreview = dsAdapter.release();
518  }
519  else
520  {
521  datasetPreview = ds->getDataSet(it->first->getName()).release();
522  }
523 
524  break;
525  }
526  ++it;
527  }
528 
529  if (datasetPreview)
530  {
531  m_tblView->setDataSet(datasetPreview);
532  m_tblView->resizeColumnsToContents();
533  m_tblView->show();
534  }
535 }
536 
538 {
539  m_name = name.toUtf8().data();
540 }
541 
543 {
544  return dataset->getName() == m_name;
545 }
546 
virtual void setName(const std::string &name)
It sets the constraint name.
Definition: Constraint.h:126
std::unique_ptr< te::qt::widgets::DataSetAdapterWidget > m_dataSetAdapterWidget
bool operator()(const te::da::DataSetTypePtr &dataset) const
Geometric property.
void set(const std::list< te::da::DataSetTypePtr > &datasets, const te::da::DataSourceInfoPtr &datasource, const te::da::DataSourceInfoPtr &targetDatasource)
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
This class is used to list the constraints and indexes of a datasetype.
A dialog for creating a data set adapter.
boost::shared_ptr< DataSource > DataSourcePtr
std::map< te::da::DataSetTypePtr, te::da::DataSetTypeConverter * > m_datasets
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
A class that models the description of a dataset.
Definition: DataSetType.h:72
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
static te::dt::Date ds(2010, 01, 01)
std::unique_ptr< te::qt::widgets::ConstraintsIndexesListWidget > m_constraintWidget
const std::map< te::da::DataSetTypePtr, te::da::DataSetTypeConverter * > & getDatasets() const
EncodingType
Supported character encodings.
Definition: CharEncoding.h:50
An converter for DataSetType.
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
static std::string getEncodingName(EncodingType et)
Retrive a string from a given character encoding type enum.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
std::unique_ptr< te::qt::widgets::DataSetTableView > m_tblView
std::unique_ptr< Ui::DataSetOptionsWizardPageForm > m_ui
te::da::DataSetTypePtr getSelectedDataSet() const
A dataset is the unit of information manipulated by the data access module of TerraLib.
void previewData(QListWidgetItem *item, bool isResult)
Property * findFirstPropertyOfType(const int t) const
returns the first property of the given data type. Caller doesn&#39;t take ownership of the returned poin...
static te::core::EncodingType getEncodingType(const std::string &name)
Retrive an EncodingType from a given character encoding name.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
A customized table view for te::map::AbstractLayer objects. Uses a te::qt::widgets::DataSetModel as i...
A dialog used to build a SRSManagerDialog element.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
const std::pair< int, std::string > & getSelectedSRS() const
Returns the selected SRS in the window.
static std::vector< std::string > getEncodingList()
Retrive a vector of string with all available encoding types name.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
const std::string & getTitle() const
A human descriptive title for the DataSetType.
Definition: DataSetType.h:130
const std::string & getName() const
It returns the property name.
Definition: Property.h:127