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/DataSetType.h"
28 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
29 #include "../../../dataaccess/dataset/Index.h"
30 #include "../../../dataaccess/dataset/PrimaryKey.h"
31 #include "../../../dataaccess/dataset/UniqueKey.h"
32 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
33 #include "../../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../../dataaccess/utils/Utils.h"
35 #include "../../../geometry/GeometryProperty.h"
36 #include "../../../qt/widgets/utils/ScopedCursor.h"
37 #include "../../../qt/widgets/srs/SRSManagerDialog.h"
38 #include "../property/ConstraintsIndexesListWidget.h"
39 #include "../property/DataSetAdapterWidget.h"
41 #include "ui_DataSetOptionsWizardPageForm.h"
42 
43 // STL
44 #include <algorithm>
45 
46 // Boost
47 #include <boost/algorithm/string/replace.hpp>
48 #include <boost/lexical_cast.hpp>
49 
50 // Qt
51 #include <QIcon>
52 #include <QMessageBox>
53 
55  : QWizardPage(parent),
56  m_ui(new Ui::DataSetOptionsWizardPageForm)
57 {
58 // setup controls
59  m_ui->setupUi(this);
60 
61 //build form
63  QGridLayout* constraintLayout = new QGridLayout(m_ui->m_constraintWidget);
64  constraintLayout->addWidget(m_constraintWidget.get());
65  constraintLayout->setContentsMargins(0,0,0,0);
66 
68  QGridLayout* dataSetLayout = new QGridLayout(m_ui->m_dataSetWidget);
69  dataSetLayout->addWidget(m_dataSetAdapterWidget.get());
70  dataSetLayout->setContentsMargins(0,0,0,0);
71 
72  m_ui->m_sridInputPushButton->setIcon(QIcon::fromTheme("srs"));
73  m_ui->m_sridOutputPushButton->setIcon(QIcon::fromTheme("srs"));
74 
75 // connect signals and slots
76  connect(m_ui->m_sridInputPushButton, SIGNAL(clicked()), this, SLOT(sridInputSearchToolButtonPressed()));
77  connect(m_ui->m_sridOutputPushButton, SIGNAL(clicked()), this, SLOT(sridOutputSearchToolButtonPressed()));
78  connect(m_ui->m_applyPushButton, SIGNAL(clicked()), this, SLOT(applyChanges()));
79 
80  connect(m_ui->m_selectedDatasetListWidget, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(datasetPressed(QListWidgetItem*)));
81 
82  //QCoreApplication* app = QCoreApplication::instance();
83  //connect(app, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(checkModificationsOnFocusChange(QWidget*, QWidget*)));
84 }
85 
87 {
88 }
89 
90 void te::qt::widgets::DataSetOptionsWizardPage::set(const std::list<te::da::DataSetTypePtr>& datasets,
91  const te::da::DataSourceInfoPtr& datasource,
92  const te::da::DataSourceInfoPtr& targetDatasource)
93 {
94  ScopedCursor wcursor(Qt::WaitCursor);
95 
96  m_ui->m_selectedDatasetListWidget->clear();
97 
98  clearForm();
99 
100  m_datasets.clear();
101 
102  m_datasource = datasource;
103 
104  m_targetDatasource = targetDatasource;
105 
106  te::da::DataSourcePtr targetDSPtr = te::da::DataSourceManager::getInstance().get(m_targetDatasource->getId(), m_targetDatasource->getType(), m_targetDatasource->getConnInfo());
107 
108  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
109  {
110  if(it->get() == 0)
111  continue;
112 
113  if((*it)->size() == 0)
114  te::da::LoadProperties((*it).get(), datasource->getId());
115 
116  //create dataset adapter
117  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter((*it).get(), targetDSPtr->getCapabilities(), targetDSPtr->getEncoding());
118 
119  //fix output dataset name
120  std::string name = converter->getResult()->getName();
121 
122  std::size_t idx = name.find(".");
123  if (idx != std::string::npos)
124  {
125  name = name.substr(idx + 1, name.size() - 1);
126  }
127 
128  converter->getResult()->setName(name);
129 
130  //fix primary key name
131  if(converter->getResult() && converter->getResult()->getPrimaryKey())
132  {
133  te::da::PrimaryKey* pk = converter->getResult()->getPrimaryKey();
134  pk->setName(converter->getResult()->getName() + "_pk");
135  }
136 
137  m_datasets.insert(std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::value_type((*it), converter));
138  }
139 
140  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
141  {
142  QString title = QString::fromStdString((*it)->getTitle());
143 
144  QString name = QString::fromStdString((*it)->getName());
145 
146  if(title.isEmpty())
147  title = name;
148 
149  QListWidgetItem* item = new QListWidgetItem(title);
150 
151  m_ui->m_selectedDatasetListWidget->addItem(item);
152 
153  if(datasets.size() == 1)
154  {
155  item->setSelected(true);
156  datasetPressed(item);
157  }
158  }
159 
160  if(datasets.size() != 1)
161  setControlsEnabled(false);
162 }
163 
164 const std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>& te::qt::widgets::DataSetOptionsWizardPage::getDatasets() const
165 {
166  return m_datasets;
167 }
168 
170 {
171  m_ui->m_dataSetWidget->setVisible(!mode);
172  m_ui->m_constraintWidget->setVisible(!mode);
173 }
174 
176 {
177  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
178 
179  if(item == 0)
180  return;
181 
182  if (!m_ui->m_sridInputLineEdit->text().isEmpty())
183  {
184  int inputSRID = m_ui->m_sridInputLineEdit->text().toInt();
185  int outputSRID = m_ui->m_sridOutputLineEdit->text().toInt();
186 
187  if (inputSRID != outputSRID && outputSRID == TE_UNKNOWN_SRS)
188  {
189  QMessageBox::warning(this, tr("Warning"), tr("Invalid output Layer SRID."));
190  return;
191  }
192 
193  if (inputSRID != outputSRID && inputSRID == TE_UNKNOWN_SRS)
194  {
195  QMessageBox::warning(this, tr("Warning"), tr("Invalid input Layer SRID."));
196  return;
197  }
198  }
199 
200  std::string dataSetAdapterName = item->text().toStdString();
201 
202  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
203 
204  while(it != m_datasets.end())
205  {
206  if(it->second->getConvertee()->getName() == dataSetAdapterName)
207  {
208  it->second->getResult()->setName(m_ui->m_datasetNameLineEdit->text().trimmed().toStdString());
209  it->second->getResult()->setTitle(m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString());
210 
211  //get srid information
212  if (!m_ui->m_sridInputLineEdit->text().isEmpty())
213  {
214  int inputSRID = m_ui->m_sridInputLineEdit->text().toInt();
215 
216  int outputSRID = TE_UNKNOWN_SRS;
217 
218  if (!m_ui->m_sridOutputLineEdit->text().isEmpty())
219  {
220  outputSRID = m_ui->m_sridOutputLineEdit->text().toInt();
221  }
222 
223  te::da::AssociateDataSetTypeConverterSRID(it->second, inputSRID, outputSRID);
224  }
225 
226  if(it->second->getResult()->getPrimaryKey())
227  {
228  te::da::PrimaryKey* pk = it->second->getResult()->getPrimaryKey();
229  pk->setName(it->second->getResult()->getName() + "_pk");
230 
231  // fill constraints
232  m_constraintWidget->setDataSetType(it->second->getResult());
233  }
234 
235  break;
236  }
237  ++it;
238  }
239 }
240 
242 {
243  te::qt::widgets::SRSManagerDialog srsDialog(this);
244  srsDialog.setWindowTitle(tr("Choose the SRS"));
245 
246  if(srsDialog.exec() != QDialog::Rejected)
247  {
248  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
249  m_ui->m_sridInputLineEdit->setText(QString::number(srid.first));
250  }
251 }
252 
254 {
255  te::qt::widgets::SRSManagerDialog srsDialog(this);
256  srsDialog.setWindowTitle(tr("Choose the SRS"));
257 
258  if (srsDialog.exec() != QDialog::Rejected)
259  {
260  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
261  m_ui->m_sridOutputLineEdit->setText(QString::number(srid.first));
262  }
263 }
264 
266 {
267  if(item == 0)
268  return;
269 
270  std::string dataSetAdapterName = item->text().toStdString();
271 
272  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
273 
274  while(it != m_datasets.end())
275  {
276  if(it->second->getConvertee()->getName() == dataSetAdapterName)
277  {
278  te::da::DataSetType* dataset = it->second->getResult();
279 
280  // fill line edits
281  m_ui->m_datasetNameLineEdit->setEnabled(true);
282  m_ui->m_datasetNameLineEdit->setText(QString::fromStdString(dataset->getName()));
283 
284  m_ui->m_datasetTitleLineEdit->setEnabled(true);
285  m_ui->m_datasetTitleLineEdit->setText(QString::fromStdString(dataset->getTitle()));
286 
287  if(dataset->hasGeom())
288  {
289  m_ui->m_sridInputPushButton->setEnabled(true);
290  m_ui->m_sridInputLineEdit->clear();
291  m_ui->m_sridInputLineEdit->setEnabled(true);
292 
293  m_ui->m_sridOutputPushButton->setEnabled(true);
294  m_ui->m_sridOutputLineEdit->clear();
295  m_ui->m_sridOutputLineEdit->setEnabled(true);
296 
297  te::gm::GeometryProperty* geomPropIn = dynamic_cast<te::gm::GeometryProperty*>(it->second->getConvertee()->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
298 
299  if (geomPropIn)
300  {
301  m_ui->m_sridInputLineEdit->setText(QString::fromStdString(boost::lexical_cast<std::string>(geomPropIn->getSRID())));
302  }
303 
305 
306  if (geomPropOut)
307  {
308  m_ui->m_sridOutputLineEdit->setText(QString::fromStdString(boost::lexical_cast<std::string>(geomPropOut->getSRID())));
309  }
310  }
311  else
312  {
313  m_ui->m_sridInputPushButton->setEnabled(false);
314  m_ui->m_sridInputLineEdit->clear();
315  m_ui->m_sridInputLineEdit->setEnabled(false);
316 
317  m_ui->m_sridOutputPushButton->setEnabled(false);
318  m_ui->m_sridOutputLineEdit->clear();
319  m_ui->m_sridOutputLineEdit->setEnabled(false);
320  }
321 
322  // fill property table
323  m_dataSetAdapterWidget->setAdapterParameters(it->second->getConvertee(), it->second, m_targetDatasource);
324 
325  // fill constraints
326  m_constraintWidget->setDataSetType(dataset);
327  break;
328  }
329  ++it;
330  }
331 }
332 
333 //void te::qt::widgets::DataSetOptionsWizardPage::checkModificationsOnFocusChange(QWidget* old, QWidget* now)
334 //{
335 // if(old == m_ui->m_datasetNameLineEdit)
336 // {
337 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
338 //
339 // if(dataset.get() == 0)
340 // return;
341 //
342 // std::string name = m_ui->m_datasetNameLineEdit->text().trimmed().toStdString();
343 //
344 // if(name != dataset->getName())
345 // {
346 // dataset->setName(name);
347 // m_ui->m_applyChangesPushButton->setEnabled(true);
348 // }
349 // }
350 // else if(old == m_ui->m_datasetTitleLineEdit)
351 // {
352 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
353 //
354 // if(dataset.get() == 0)
355 // return;
356 //
357 // std::string title = m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString();
358 //
359 // if(title != dataset->getTitle())
360 // {
361 // dataset->setTitle(title);
362 // m_ui->m_applyChangesPushButton->setEnabled(true);
363 // }
364 // }
365 // else if(old == m_ui->m_sridLineEdit)
366 // {
367 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
368 //
369 // if(dataset.get() == 0)
370 // return;
371 //
372 // if(!dataset->hasGeom())
373 // return;
374 //
375 // te::gm::GeometryProperty* gp = dataset->getDefaultGeomProperty();
376 //
377 // int srid = boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString());
378 //
379 // if(srid != gp->getSRID())
380 // {
381 // gp->setSRID(srid);
382 // m_ui->m_applyChangesPushButton->setEnabled(true);
383 // }
384 // }
385 //}
386 
388 {
389  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
390 
391  if(item == 0)
392  return te::da::DataSetTypePtr();
393 
394  std::string dataSetAdapterName = item->text().toStdString();
395 
396  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::const_iterator it = m_datasets.begin();
397 
398  while(it != m_datasets.end())
399  {
400  if(it->second->getConvertee()->getName() == dataSetAdapterName)
401  {
402  return it->first;
403  }
404  ++it;
405  }
406 
407  return te::da::DataSetTypePtr();
408 }
409 
411 {
412  m_ui->m_datasetNameLineEdit->clear();
413  m_ui->m_datasetTitleLineEdit->clear();
414  m_ui->m_sridInputLineEdit->clear();
415  m_ui->m_sridOutputLineEdit->clear();
416 }
417 
419 {
420  m_ui->m_datasetNameLineEdit->setEnabled(enabled);
421  m_ui->m_datasetTitleLineEdit->setEnabled(enabled);
422  m_ui->m_sridInputLineEdit->setEnabled(enabled);
423  m_ui->m_sridInputPushButton->setEnabled(enabled);
424  m_ui->m_sridOutputLineEdit->setEnabled(enabled);
425  m_ui->m_sridOutputPushButton->setEnabled(enabled);
426 }
427 
429 {
430  m_name = name.toStdString();
431 }
432 
434 {
435  return dataset->getName() == m_name;
436 }
437 
virtual void setName(const std::string &name)
It sets the constraint name.
Definition: Constraint.h:126
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)
Definition: Utils.cpp:120
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
Definition: DataSource.h:1435
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
Definition: Utils.cpp:670
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
void clear()
It clears the DataSetType definition.
const std::map< te::da::DataSetTypePtr, te::da::DataSetTypeConverter * > & getDatasets() const
An converter for DataSetType.
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
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.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:41
std::auto_ptr< Ui::DataSetOptionsWizardPageForm > m_ui
te::da::DataSetTypePtr getSelectedDataSet() const
std::auto_ptr< te::qt::widgets::ConstraintsIndexesListWidget > m_constraintWidget
Property * findFirstPropertyOfType(const int t) const
returns the first property of the given data type. Caller doesn't take ownership of the returned poin...
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
A dialog used to build a SRSManagerDialog element.
const std::pair< int, std::string > & getSelectedSRS() const
Returns the selected SRS in the window.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
std::auto_ptr< te::qt::widgets::DataSetAdapterWidget > m_dataSetAdapterWidget
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