All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSetOptionsWizardPage.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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/lexical_cast.hpp>
48 
49 // Qt
50 #include <QIcon>
51 #include <QMessageBox>
52 
54  : QWizardPage(parent),
55  m_ui(new Ui::DataSetOptionsWizardPageForm)
56 {
57 // setup controls
58  m_ui->setupUi(this);
59 
60 //build form
62  QGridLayout* constraintLayout = new QGridLayout(m_ui->m_constraintWidget);
63  constraintLayout->addWidget(m_constraintWidget.get());
64  constraintLayout->setContentsMargins(0,0,0,0);
65 
67  QGridLayout* dataSetLayout = new QGridLayout(m_ui->m_dataSetWidget);
68  dataSetLayout->addWidget(m_dataSetAdapterWidget.get());
69  dataSetLayout->setContentsMargins(0,0,0,0);
70 
71 // connect signals and slots
72  connect(m_ui->m_sridSearchToolButton, SIGNAL(pressed()), this, SLOT(sridSearchToolButtonPressed()));
73 
74  connect(m_ui->m_selectedDatasetListWidget, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(datasetPressed(QListWidgetItem*)));
75 
76  //QCoreApplication* app = QCoreApplication::instance();
77  //connect(app, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(checkModificationsOnFocusChange(QWidget*, QWidget*)));
78 }
79 
81 {
82 }
83 
84 void te::qt::widgets::DataSetOptionsWizardPage::set(const std::list<te::da::DataSetTypePtr>& datasets,
85  const te::da::DataSourceInfoPtr& datasource,
86  const te::da::DataSourceInfoPtr& targetDatasource)
87 {
88  ScopedCursor wcursor(Qt::WaitCursor);
89 
90  m_ui->m_selectedDatasetListWidget->clear();
91 
92  clearForm();
93 
94  m_datasets.clear();
95 
96  m_datasource = datasource;
97 
98  m_targetDatasource = targetDatasource;
99 
100  te::da::DataSourcePtr targetDSPtr = te::da::DataSourceManager::getInstance().get(m_targetDatasource->getId(), m_targetDatasource->getType(), m_targetDatasource->getConnInfo());
101 
102  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
103  {
104  if(it->get() == 0)
105  continue;
106 
107  if((*it)->size() == 0)
108  te::da::LoadProperties((*it).get(), datasource->getId());
109 
110  //create dataset adapter
111  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter((*it).get(), targetDSPtr->getCapabilities());
112 
113  if(converter->getResult() && converter->getResult()->getPrimaryKey())
114  {
115  te::da::PrimaryKey* pk = converter->getResult()->getPrimaryKey();
116  pk->setName(converter->getResult()->getName() + "_" + pk->getName() + "_pk");
117  }
118 
119  m_datasets.insert(std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::value_type((*it), converter));
120  }
121 
122  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
123  {
124  QString title = QString::fromStdString((*it)->getTitle());
125 
126  QString name = QString::fromStdString((*it)->getName());
127 
128  if(title.isEmpty())
129  title = name;
130 
131  QListWidgetItem* item = new QListWidgetItem(title);
132 
133  m_ui->m_selectedDatasetListWidget->addItem(item);
134 
135  if(datasets.size() == 1)
136  {
137  item->setSelected(true);
138  datasetPressed(item);
139  }
140  }
141 
142  if(datasets.size() != 1)
143  setControlsEnabled(false);
144 }
145 
146 const std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>& te::qt::widgets::DataSetOptionsWizardPage::getDatasets() const
147 {
148  return m_datasets;
149 }
150 
152 {
153  m_ui->m_dataSetWidget->setVisible(!mode);
154  m_ui->m_constraintWidget->setVisible(!mode);
155 }
156 
158 {
159  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
160 
161  if(item == 0)
162  return;
163 
164  std::string dataSetAdapterName = item->text().toStdString();
165 
166  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
167 
168  while(it != m_datasets.end())
169  {
170  if(it->second->getConvertee()->getName() == dataSetAdapterName)
171  {
172  it->second->getResult()->setName(m_ui->m_datasetNameLineEdit->text().trimmed().toStdString());
173  it->second->getResult()->setTitle(m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString());
174 
175  if(it->second->getResult()->hasGeom())
176  {
177  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(it->second->getResult()->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
178 
179  if(geomProp)
180  geomProp->setSRID(boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString()));
181  }
182 
183  if(it->second->getResult()->getPrimaryKey())
184  {
185  te::da::PrimaryKey* pk = it->second->getResult()->getPrimaryKey();
186  pk->setName(it->second->getResult()->getName() + "_" + pk->getName() + "_pk");
187 
188  // fill constraints
189  m_constraintWidget->setDataSetType(it->second->getResult());
190  }
191 
192  break;
193  }
194  ++it;
195  }
196 }
197 
199 {
200  te::qt::widgets::SRSManagerDialog srsDialog(this);
201  srsDialog.setWindowTitle(tr("Choose the SRS"));
202 
203  if(srsDialog.exec() != QDialog::Rejected)
204  {
205  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
206  m_ui->m_sridLineEdit->setText(QString::number(srid.first));
207  }
208 }
209 
211 {
212  if(item == 0)
213  return;
214 
215  std::string dataSetAdapterName = item->text().toStdString();
216 
217  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
218 
219  while(it != m_datasets.end())
220  {
221  if(it->second->getConvertee()->getName() == dataSetAdapterName)
222  {
223  te::da::DataSetType* dataset = it->second->getResult();
224 
225  // fill line edits
226  m_ui->m_datasetNameLineEdit->setEnabled(true);
227  m_ui->m_datasetNameLineEdit->setText(QString::fromStdString(dataset->getName()));
228 
229  m_ui->m_datasetTitleLineEdit->setEnabled(true);
230  m_ui->m_datasetTitleLineEdit->setText(QString::fromStdString(dataset->getTitle()));
231 
232  if(dataset->hasGeom())
233  {
234  m_ui->m_sridSearchToolButton->setEnabled(true);
235  m_ui->m_sridLineEdit->setEnabled(true);
236 
238 
239  if(geomProp)
240  m_ui->m_sridLineEdit->setText(QString::fromStdString(boost::lexical_cast<std::string>(geomProp->getSRID())));
241  }
242  else
243  {
244  m_ui->m_sridSearchToolButton->setEnabled(false);
245  m_ui->m_sridLineEdit->setEnabled(false);
246  }
247 
248  // fill property table
249  m_dataSetAdapterWidget->setAdapterParameters(it->second->getConvertee(), it->second, m_targetDatasource);
250 
251  // fill constraints
252  m_constraintWidget->setDataSetType(dataset);
253  break;
254  }
255  ++it;
256  }
257 }
258 
259 //void te::qt::widgets::DataSetOptionsWizardPage::checkModificationsOnFocusChange(QWidget* old, QWidget* now)
260 //{
261 // if(old == m_ui->m_datasetNameLineEdit)
262 // {
263 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
264 //
265 // if(dataset.get() == 0)
266 // return;
267 //
268 // std::string name = m_ui->m_datasetNameLineEdit->text().trimmed().toStdString();
269 //
270 // if(name != dataset->getName())
271 // {
272 // dataset->setName(name);
273 // m_ui->m_applyChangesPushButton->setEnabled(true);
274 // }
275 // }
276 // else if(old == m_ui->m_datasetTitleLineEdit)
277 // {
278 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
279 //
280 // if(dataset.get() == 0)
281 // return;
282 //
283 // std::string title = m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString();
284 //
285 // if(title != dataset->getTitle())
286 // {
287 // dataset->setTitle(title);
288 // m_ui->m_applyChangesPushButton->setEnabled(true);
289 // }
290 // }
291 // else if(old == m_ui->m_sridLineEdit)
292 // {
293 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
294 //
295 // if(dataset.get() == 0)
296 // return;
297 //
298 // if(!dataset->hasGeom())
299 // return;
300 //
301 // te::gm::GeometryProperty* gp = dataset->getDefaultGeomProperty();
302 //
303 // int srid = boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString());
304 //
305 // if(srid != gp->getSRID())
306 // {
307 // gp->setSRID(srid);
308 // m_ui->m_applyChangesPushButton->setEnabled(true);
309 // }
310 // }
311 //}
312 
314 {
315  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
316 
317  if(item == 0)
318  return te::da::DataSetTypePtr();
319 
320  std::string dataSetAdapterName = item->text().toStdString();
321 
322  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::const_iterator it = m_datasets.begin();
323 
324  while(it != m_datasets.end())
325  {
326  if(it->second->getConvertee()->getName() == dataSetAdapterName)
327  {
328  return it->first;
329  }
330  ++it;
331  }
332 
333  return te::da::DataSetTypePtr();
334 }
335 
337 {
338  m_ui->m_datasetNameLineEdit->clear();
339  m_ui->m_datasetTitleLineEdit->clear();
340  m_ui->m_sridLineEdit->clear();
341 }
342 
344 {
345  m_ui->m_datasetNameLineEdit->setEnabled(enabled);
346  m_ui->m_datasetTitleLineEdit->setEnabled(enabled);
347  m_ui->m_sridLineEdit->setEnabled(enabled);
348  m_ui->m_sridSearchToolButton->setEnabled(enabled);
349 }
350 
352 {
353  m_name = name.toStdString();
354 }
355 
357 {
358  return dataset->getName() == m_name;
359 }
360 
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:117
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
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
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
const std::map< te::da::DataSetTypePtr, te::da::DataSetTypeConverter * > & getDatasets() const
An converter for DataSetType.
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::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
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
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:126