All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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"
39 #include "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 <QtGui/QIcon>
51 #include <QtGui/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  m_datasets.insert(std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::value_type((*it), converter));
114  }
115 
116  for(std::list<te::da::DataSetTypePtr>::const_iterator it = datasets.begin(); it != datasets.end(); ++it)
117  {
118  QString title = QString::fromStdString((*it)->getTitle());
119 
120  QString name = QString::fromStdString((*it)->getName());
121 
122  if(title.isEmpty())
123  title = name;
124 
125  QListWidgetItem* item = new QListWidgetItem(title);
126 
127  m_ui->m_selectedDatasetListWidget->addItem(item);
128 
129  if(datasets.size() == 1)
130  {
131  item->setSelected(true);
132  datasetPressed(item);
133  }
134  }
135 
136  if(datasets.size() != 1)
137  setControlsEnabled(false);
138 }
139 
140 const std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>& te::qt::widgets::DataSetOptionsWizardPage::getDatasets() const
141 {
142  return m_datasets;
143 }
144 
146 {
147  m_ui->m_dataSetWidget->setVisible(!mode);
148  m_ui->m_constraintWidget->setVisible(!mode);
149 }
150 
152 {
153  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
154 
155  if(item == 0)
156  return;
157 
158  std::string dataSetAdapterName = item->text().toStdString();
159 
160  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = m_datasets.begin();
161 
162  while(it != m_datasets.end())
163  {
164  if(it->second->getConvertee()->getName() == dataSetAdapterName)
165  {
166  it->second->getResult()->setName(m_ui->m_datasetNameLineEdit->text().trimmed().toStdString());
167  it->second->getResult()->setTitle(m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString());
168 
169  if(it->second->getResult()->hasGeom())
170  {
171  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(it->second->getResult()->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
172 
173  if(geomProp)
174  geomProp->setSRID(boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString()));
175  }
176 
177  break;
178  }
179  ++it;
180  }
181 }
182 
184 {
185  te::qt::widgets::SRSManagerDialog srsDialog(this);
186  srsDialog.setWindowTitle(tr("Choose the SRS"));
187 
188  if(srsDialog.exec() != QDialog::Rejected)
189  {
190  std::pair<int, std::string> srid = srsDialog.getSelectedSRS();
191  m_ui->m_sridLineEdit->setText(QString::number(srid.first));
192  }
193 }
194 
196 {
197  if(item == 0)
198  return;
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  te::da::DataSetType* dataset = it->second->getResult();
209 
210  // fill line edits
211  m_ui->m_datasetNameLineEdit->setEnabled(true);
212  m_ui->m_datasetNameLineEdit->setText(QString::fromStdString(dataset->getName()));
213 
214  m_ui->m_datasetTitleLineEdit->setEnabled(true);
215  m_ui->m_datasetTitleLineEdit->setText(QString::fromStdString(dataset->getTitle()));
216 
217  if(dataset->hasGeom())
218  {
219  m_ui->m_sridSearchToolButton->setEnabled(true);
220  m_ui->m_sridLineEdit->setEnabled(true);
221 
223 
224  if(geomProp)
225  m_ui->m_sridLineEdit->setText(QString::fromStdString(boost::lexical_cast<std::string>(geomProp->getSRID())));
226  }
227  else
228  {
229  m_ui->m_sridSearchToolButton->setEnabled(false);
230  m_ui->m_sridLineEdit->setEnabled(false);
231  }
232 
233  // fill property table
234  m_dataSetAdapterWidget->setAdapterParameters(it->second->getConvertee(), it->second, m_targetDatasource);
235 
236  // fill constraints
237  m_constraintWidget->setDataSetType(dataset);
238  break;
239  }
240  ++it;
241  }
242 }
243 
244 //void te::qt::widgets::DataSetOptionsWizardPage::checkModificationsOnFocusChange(QWidget* old, QWidget* now)
245 //{
246 // if(old == m_ui->m_datasetNameLineEdit)
247 // {
248 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
249 //
250 // if(dataset.get() == 0)
251 // return;
252 //
253 // std::string name = m_ui->m_datasetNameLineEdit->text().trimmed().toStdString();
254 //
255 // if(name != dataset->getName())
256 // {
257 // dataset->setName(name);
258 // m_ui->m_applyChangesPushButton->setEnabled(true);
259 // }
260 // }
261 // else if(old == m_ui->m_datasetTitleLineEdit)
262 // {
263 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
264 //
265 // if(dataset.get() == 0)
266 // return;
267 //
268 // std::string title = m_ui->m_datasetTitleLineEdit->text().trimmed().toStdString();
269 //
270 // if(title != dataset->getTitle())
271 // {
272 // dataset->setTitle(title);
273 // m_ui->m_applyChangesPushButton->setEnabled(true);
274 // }
275 // }
276 // else if(old == m_ui->m_sridLineEdit)
277 // {
278 // te::da::DataSetTypePtr dataset = getSelectedDataSet();
279 //
280 // if(dataset.get() == 0)
281 // return;
282 //
283 // if(!dataset->hasGeom())
284 // return;
285 //
286 // te::gm::GeometryProperty* gp = dataset->getDefaultGeomProperty();
287 //
288 // int srid = boost::lexical_cast<int>(m_ui->m_sridLineEdit->text().trimmed().toStdString());
289 //
290 // if(srid != gp->getSRID())
291 // {
292 // gp->setSRID(srid);
293 // m_ui->m_applyChangesPushButton->setEnabled(true);
294 // }
295 // }
296 //}
297 
299 {
300  QListWidgetItem* item = m_ui->m_selectedDatasetListWidget->currentItem();
301 
302  if(item == 0)
303  return te::da::DataSetTypePtr();
304 
305  std::string dataSetAdapterName = item->text().toStdString();
306 
307  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::const_iterator it = m_datasets.begin();
308 
309  while(it != m_datasets.end())
310  {
311  if(it->second->getConvertee()->getName() == dataSetAdapterName)
312  {
313  return it->first;
314  }
315  ++it;
316  }
317 
318  return te::da::DataSetTypePtr();
319 }
320 
322 {
323  m_ui->m_datasetNameLineEdit->clear();
324  m_ui->m_datasetTitleLineEdit->clear();
325  m_ui->m_sridLineEdit->clear();
326 }
327 
329 {
330  m_ui->m_datasetNameLineEdit->setEnabled(enabled);
331  m_ui->m_datasetTitleLineEdit->setEnabled(enabled);
332  m_ui->m_sridLineEdit->setEnabled(enabled);
333  m_ui->m_sridSearchToolButton->setEnabled(enabled);
334 }
335 
337 {
338  m_name = name.toStdString();
339 }
340 
342 {
343  return dataset->getName() == m_name;
344 }
345 
std::auto_ptr< te::qt::widgets::DataSetAdapterWidget > m_dataSetAdapterWidget
A dialog for creating a data set adapter.
bool operator()(const te::da::DataSetTypePtr &dataset) const
const std::string & getTitle() const
A human descriptive title for the DataSetType.
Definition: DataSetType.h:130
std::auto_ptr< te::qt::widgets::ConstraintsIndexesListWidget > m_constraintWidget
This class is used to list the constraints and indexes of a datasetype.
An converter for DataSetType.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
void set(const std::list< te::da::DataSetTypePtr > &datasets, const te::da::DataSourceInfoPtr &datasource, const te::da::DataSourceInfoPtr &targetDatasource)
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
A dialog used to build a SRSManagerDialog element.
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
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...
const std::pair< int, std::string > & getSelectedSRS() const
Returns the selected SRS in the window.
std::auto_ptr< Ui::DataSetOptionsWizardPageForm > m_ui
This file has the ConstraintsIndexesListWidget class.
A class that models the description of a dataset.
Definition: DataSetType.h:72
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
A class used to define the DataSetAdapterWidget class.
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:116
te::da::DataSetTypePtr getSelectedDataSet() const
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
Geometric property.
const std::map< te::da::DataSetTypePtr, te::da::DataSetTypeConverter * > & getDatasets() const
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48