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) 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 // connect signals and slots
73  connect(m_ui->m_sridSearchToolButton, SIGNAL(pressed()), this, SLOT(sridSearchToolButtonPressed()));
74 
75  connect(m_ui->m_selectedDatasetListWidget, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(datasetPressed(QListWidgetItem*)));
76 
77  //QCoreApplication* app = QCoreApplication::instance();
78  //connect(app, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(checkModificationsOnFocusChange(QWidget*, QWidget*)));
79 }
80 
82 {
83 }
84 
85 void te::qt::widgets::DataSetOptionsWizardPage::set(const std::list<te::da::DataSetTypePtr>& datasets,
86  const te::da::DataSourceInfoPtr& datasource,
87  const te::da::DataSourceInfoPtr& targetDatasource)
88 {
89  ScopedCursor wcursor(Qt::WaitCursor);
90 
91  m_ui->m_selectedDatasetListWidget->clear();
92 
93  clearForm();
94 
95  m_datasets.clear();
96 
97  m_datasource = datasource;
98 
99  m_targetDatasource = targetDatasource;
100 
101  te::da::DataSourcePtr targetDSPtr = te::da::DataSourceManager::getInstance().get(m_targetDatasource->getId(), m_targetDatasource->getType(), m_targetDatasource->getConnInfo());
102 
103  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
104  {
105  if(it->get() == 0)
106  continue;
107 
108  if((*it)->size() == 0)
109  te::da::LoadProperties((*it).get(), datasource->getId());
110 
111  //create dataset adapter
112  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter((*it).get(), targetDSPtr->getCapabilities(), targetDSPtr->getEncoding());
113 
114  //fix output dataset name
115  std::string name = converter->getResult()->getName();
116 
117  std::size_t idx = name.find(".");
118  if (idx != std::string::npos)
119  {
120  name = name.substr(idx + 1, name.size() - 1);
121  }
122 
123  converter->getResult()->setName(name);
124 
125  //fix primary key name
126  if(converter->getResult() && converter->getResult()->getPrimaryKey())
127  {
128  te::da::PrimaryKey* pk = converter->getResult()->getPrimaryKey();
129  pk->setName(converter->getResult()->getName() + "_pk");
130  }
131 
132  m_datasets.insert(std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::value_type((*it), converter));
133  }
134 
135  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
136  {
137  QString title = QString::fromStdString((*it)->getTitle());
138 
139  QString name = QString::fromStdString((*it)->getName());
140 
141  if(title.isEmpty())
142  title = name;
143 
144  QListWidgetItem* item = new QListWidgetItem(title);
145 
146  m_ui->m_selectedDatasetListWidget->addItem(item);
147 
148  if(datasets.size() == 1)
149  {
150  item->setSelected(true);
151  datasetPressed(item);
152  }
153  }
154 
155  if(datasets.size() != 1)
156  setControlsEnabled(false);
157 }
158 
159 const std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>& te::qt::widgets::DataSetOptionsWizardPage::getDatasets() const
160 {
161  return m_datasets;
162 }
163 
165 {
166  m_ui->m_dataSetWidget->setVisible(!mode);
167  m_ui->m_constraintWidget->setVisible(!mode);
168 }
169 
171 {
172  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
173 
174  if(item == 0)
175  return;
176 
177  std::string dataSetAdapterName = item->text().toStdString();
178 
179  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
180 
181  while(it != m_datasets.end())
182  {
183  if(it->second->getConvertee()->getName() == dataSetAdapterName)
184  {
185  it->second->getResult()->setName(m_ui->m_datasetNameLineEdit->text().trimmed().toStdString());
186  it->second->getResult()->setTitle(m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString());
187 
188  if(it->second->getResult()->hasGeom())
189  {
190  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(it->second->getResult()->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
191 
192  if(geomProp)
193  geomProp->setSRID(boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString()));
194  }
195 
196  if(it->second->getResult()->getPrimaryKey())
197  {
198  te::da::PrimaryKey* pk = it->second->getResult()->getPrimaryKey();
199  pk->setName(it->second->getResult()->getName() + "_pk");
200 
201  // fill constraints
202  m_constraintWidget->setDataSetType(it->second->getResult());
203  }
204 
205  break;
206  }
207  ++it;
208  }
209 }
210 
212 {
213  te::qt::widgets::SRSManagerDialog srsDialog(this);
214  srsDialog.setWindowTitle(tr("Choose the SRS"));
215 
216  if(srsDialog.exec() != QDialog::Rejected)
217  {
218  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
219  m_ui->m_sridLineEdit->setText(QString::number(srid.first));
220  }
221 }
222 
224 {
225  if(item == 0)
226  return;
227 
228  std::string dataSetAdapterName = item->text().toStdString();
229 
230  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
231 
232  while(it != m_datasets.end())
233  {
234  if(it->second->getConvertee()->getName() == dataSetAdapterName)
235  {
236  te::da::DataSetType* dataset = it->second->getResult();
237 
238  // fill line edits
239  m_ui->m_datasetNameLineEdit->setEnabled(true);
240  m_ui->m_datasetNameLineEdit->setText(QString::fromStdString(dataset->getName()));
241 
242  m_ui->m_datasetTitleLineEdit->setEnabled(true);
243  m_ui->m_datasetTitleLineEdit->setText(QString::fromStdString(dataset->getTitle()));
244 
245  if(dataset->hasGeom())
246  {
247  m_ui->m_sridSearchToolButton->setEnabled(true);
248  m_ui->m_sridLineEdit->setEnabled(true);
249 
251 
252  if(geomProp)
253  m_ui->m_sridLineEdit->setText(QString::fromStdString(boost::lexical_cast<std::string>(geomProp->getSRID())));
254  }
255  else
256  {
257  m_ui->m_sridSearchToolButton->setEnabled(false);
258  m_ui->m_sridLineEdit->setEnabled(false);
259  }
260 
261  // fill property table
262  m_dataSetAdapterWidget->setAdapterParameters(it->second->getConvertee(), it->second, m_targetDatasource);
263 
264  // fill constraints
265  m_constraintWidget->setDataSetType(dataset);
266  break;
267  }
268  ++it;
269  }
270 }
271 
272 //void te::qt::widgets::DataSetOptionsWizardPage::checkModificationsOnFocusChange(QWidget* old, QWidget* now)
273 //{
274 // if(old == m_ui->m_datasetNameLineEdit)
275 // {
276 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
277 //
278 // if(dataset.get() == 0)
279 // return;
280 //
281 // std::string name = m_ui->m_datasetNameLineEdit->text().trimmed().toStdString();
282 //
283 // if(name != dataset->getName())
284 // {
285 // dataset->setName(name);
286 // m_ui->m_applyChangesPushButton->setEnabled(true);
287 // }
288 // }
289 // else if(old == m_ui->m_datasetTitleLineEdit)
290 // {
291 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
292 //
293 // if(dataset.get() == 0)
294 // return;
295 //
296 // std::string title = m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString();
297 //
298 // if(title != dataset->getTitle())
299 // {
300 // dataset->setTitle(title);
301 // m_ui->m_applyChangesPushButton->setEnabled(true);
302 // }
303 // }
304 // else if(old == m_ui->m_sridLineEdit)
305 // {
306 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
307 //
308 // if(dataset.get() == 0)
309 // return;
310 //
311 // if(!dataset->hasGeom())
312 // return;
313 //
314 // te::gm::GeometryProperty* gp = dataset->getDefaultGeomProperty();
315 //
316 // int srid = boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString());
317 //
318 // if(srid != gp->getSRID())
319 // {
320 // gp->setSRID(srid);
321 // m_ui->m_applyChangesPushButton->setEnabled(true);
322 // }
323 // }
324 //}
325 
327 {
328  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
329 
330  if(item == 0)
331  return te::da::DataSetTypePtr();
332 
333  std::string dataSetAdapterName = item->text().toStdString();
334 
335  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::const_iterator it = m_datasets.begin();
336 
337  while(it != m_datasets.end())
338  {
339  if(it->second->getConvertee()->getName() == dataSetAdapterName)
340  {
341  return it->first;
342  }
343  ++it;
344  }
345 
346  return te::da::DataSetTypePtr();
347 }
348 
350 {
351  m_ui->m_datasetNameLineEdit->clear();
352  m_ui->m_datasetTitleLineEdit->clear();
353  m_ui->m_sridLineEdit->clear();
354 }
355 
357 {
358  m_ui->m_datasetNameLineEdit->setEnabled(enabled);
359  m_ui->m_datasetTitleLineEdit->setEnabled(enabled);
360  m_ui->m_sridLineEdit->setEnabled(enabled);
361  m_ui->m_sridSearchToolButton->setEnabled(enabled);
362 }
363 
365 {
366  m_name = name.toStdString();
367 }
368 
370 {
371  return dataset->getName() == m_name;
372 }
373 
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
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.
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.
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