All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SHP2ADODialog.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/qt/widgets/exchanger/SHP2ADODialog.h
22 
23  \brief A exchanger dialog from SHP to ADO operation
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSetAdapter.h"
28 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
29 #include "../../../dataaccess/datasource/DataSourceInfo.h"
30 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
31 #include "../../../dataaccess/datasource/DataSourceManager.h"
32 #include "../../../dataaccess/utils/Utils.h"
33 #include "../../../geometry/GeometryProperty.h"
34 #include "../../../maptools/DataSetLayer.h"
35 #include "SHP2ADODialog.h"
36 #include "ui_SHP2ADODialogForm.h"
37 
38 // Qt
39 #include <QMessageBox>
40 
43 
44 te::qt::widgets::SHP2ADODialog::SHP2ADODialog(QWidget* parent, Qt::WindowFlags f)
45  : QDialog(parent, f),
46  m_ui(new Ui::SHP2ADODialogForm)
47 {
48 // setup widget
49  m_ui->setupUi(this);
50 
51 // add icons
52  m_ui->m_imgLabel->setPixmap(QIcon::fromTheme("data-exchange-shp-ado-hint").pixmap(112,48));
53 
54 //connectors
55  connect(m_ui->m_helpPushButton, SIGNAL(clicked()), this, SLOT(onHelpPushButtonClicked()));
56  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
57 
58 //set list of data sources
60 }
61 
63 {
64 }
65 
66 void te::qt::widgets::SHP2ADODialog::setLayers(std::list<te::map::AbstractLayerPtr> layers)
67 {
68  std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
69 
70  while(it != layers.end())
71  {
73 
74  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
75 
76  if(!dsLayer)
77  continue;
78 
79  std::string dsName = dsLayer->getDataSourceId();
80 
82 
83  if(dsPtr->getType() == "OGR") // TENSO
84  m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
85 
86  ++it;
87  }
88 
89  if(m_ui->m_inputLayerComboBox->count() > 0)
90  {
91  QString s = m_ui->m_inputLayerComboBox->currentText();
92 
93  m_ui->m_dataSetLineEdit->setText(s);
94  }
95 }
96 
98 {
99  m_ui->m_outputDataSourceComboBox->clear();
100 
101  std::vector<te::da::DataSourceInfoPtr> datasources;
102 
103  te::da::DataSourceInfoManager::getInstance().getByType("ADO", datasources); // TENSO
104 
105  for(std::size_t i = 0; i < datasources.size(); ++i)
106  {
107  const te::da::DataSourceInfoPtr& datasource = datasources[i];
108 
109  if(datasource.get() == 0)
110  continue;
111 
112  const std::string& title = datasource->getTitle();
113 
114  m_ui->m_outputDataSourceComboBox->addItem(title.c_str(), QVariant::fromValue(datasource));
115  }
116 }
117 
119 {
120  QMessageBox::information(this, "Help", "Under development");
121 }
122 
124 {
125  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
126 
127  if(idxLayer == -1)
128  {
129  QMessageBox::warning(this, tr("Warning"), tr("Input layer not selected."));
130  return;
131  }
132 
133  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
134  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
135 
136  if(!layer.get())
137  {
138  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected layer."));
139  return;
140  }
141 
142  int idxDataSource = m_ui->m_outputDataSourceComboBox->currentIndex();
143 
144  if(idxLayer == -1)
145  {
146  QMessageBox::warning(this, tr("Warning"), tr("Output data source not selected."));
147  return;
148  }
149 
150  QVariant varDataSource = m_ui->m_outputDataSourceComboBox->itemData(idxDataSource, Qt::UserRole);
151  te::da::DataSourceInfoPtr dsInfo = varDataSource.value<te::da::DataSourceInfoPtr>();
152 
153  if(!dsInfo.get())
154  {
155  QMessageBox::warning(this, tr("Warning"), tr("Error getting selected data source."));
156  return;
157  }
158 
159  if(m_ui->m_dataSetLineEdit->text().isEmpty())
160  {
161  QMessageBox::warning(this, tr("Warning"), tr("Data Set name not defined."));
162  return;
163  }
164 
165  try
166  {
167  //create adapter
168  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
169 
170  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(layer.get());
171 
172  if(dsType->size() == 0)
173  te::da::LoadProperties(dsType.get(), dsLayer->getDataSourceId());
174 
175  te::da::DataSourcePtr targetDSPtr = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getType(), dsInfo->getConnInfo());
176 
177  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), targetDSPtr->getCapabilities());
178 
179  te::da::DataSetType* dsTypeResult = converter->getResult();
180 
181  dsTypeResult->setName(m_ui->m_dataSetLineEdit->text().toStdString());
182 
183  //exchange
184  std::map<std::string,std::string> nopt;
185 
186  std::auto_ptr<te::da::DataSet> dataset = layer->getData();
187 
188  targetDSPtr->createDataSet(dsTypeResult, nopt);
189 
190  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
191 
192  if(dataset->moveBeforeFirst())
193  targetDSPtr->add(dsTypeResult->getName(), dsAdapter.get(), targetDSPtr->getConnectionInfo());
194 
195  QMessageBox::information(this, tr("Exchanger"), tr("Layer exported successfully."));
196  }
197  catch(const std::exception& e)
198  {
199  QString errMsg(tr("Error during exchanger. The reported error is: %1"));
200 
201  errMsg = errMsg.arg(e.what());
202 
203  QMessageBox::information(this, tr("Exchanger"), errMsg);
204  }
205 
206  accept();
207 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:120
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
void setDataSources()
Set the list of data sources that can be used.
A class that models the description of a dataset.
Definition: DataSetType.h:72
An converter for DataSetType.
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
std::auto_ptr< Ui::SHP2ADODialogForm > m_ui
Definition: SHP2ADODialog.h:79
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
const std::string & getDataSourceId() const
A exchanger dialog from SHP to ADO operation.
SHP2ADODialog(QWidget *parent=0, Qt::WindowFlags f=0)
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr