GeoPackageSynchronizerDialog.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 src/terraMobilePlugin/qt/GeoPackageSynchronizerDialog.cpp
22 
23 \brief This interface is used to get the input parameters for GeoPackage Synchronizer operation.
24 */
25 
26 #include "../../../../dataaccess/datasource/DataSource.h"
27 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
28 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
29 #include "../../../../dataaccess/datasource/DataSourceInfoManager.h"
30 #include "../../../../dataaccess/utils/Utils.h"
31 #include "../core/GeopackageSynchronizer.h"
33 #include "ui_GeoPackageSynchronizerDialogForm.h"
34 
35 // Qt
36 #include <QFileDialog>
37 #include <QMessageBox>
38 
40 
42  : QDialog(parent, f),
43  m_ui(new Ui::GeoPackageSynchronizerDialogForm)
44 {
45  // add controls
46  m_ui->setupUi(this);
47 
48  //connects
49  connect(m_ui->m_geopackageToolButton, SIGNAL(pressed()), this, SLOT(onGeopackageToolButtonClicked()));
50  connect(m_ui->m_synchPushButton, SIGNAL(clicked()), this, SLOT(onSynchronizePushButtonClicked()));
51  connect(m_ui->m_gatheringComboBox, SIGNAL(activated(int)), this, SLOT(onGatheringComboBoxActivated(int)));
52 }
53 
55 {
56 
57 }
58 
59 
61 {
62  QString fileName = QFileDialog::getOpenFileName(this, tr("Open GeoPackage File"), "", tr("GeoPackage File (*.gpkg *.GPKG)"));
63 
64  if (fileName.isEmpty())
65  {
66  return;
67  }
68 
69  m_ui->m_geopackageLineEdit->setText(fileName);
70 
71  //list gpkg datasets
72  std::map<std::string, std::string> connInfo;
73  connInfo["URI"] = fileName.toUtf8().data();
74 
75  std::auto_ptr<te::da::DataSource> dsGPKG = te::da::DataSourceFactory::make("GPKG");
76  dsGPKG->setConnectionInfo(connInfo);
77  dsGPKG->open();
78 
79  std::vector<std::string> dsNames = dsGPKG->getDataSetNames();
80 
81  m_ui->m_gatheringComboBox->clear();
82 
83  for (std::size_t t = 0; t < dsNames.size(); ++t)
84  {
85  std::string connInfo = "";
86  std::string sql = "SELECT datasource_uri FROM tm_layer_settings WHERE layer_name = '" + dsNames[t] + "';";
87  std::auto_ptr<te::da::DataSet> dataSetQuery = dsGPKG->query(sql);
88 
89  if (!dataSetQuery->isEmpty())
90  {
91  dataSetQuery->moveFirst();
92  connInfo = dataSetQuery->getAsString(0);
93  }
94 
95  m_ui->m_gatheringComboBox->addItem(dsNames[t].c_str(), QVariant(connInfo.c_str()));
96  }
97 }
98 
100 {
101  std::string connInfo = m_ui->m_gatheringComboBox->itemData(index).toString().toUtf8().data();
102 
103  for (int i = 0; i < m_ui->m_layerComboBox->count(); ++i)
104  {
105  QVariant varLayer = m_ui->m_layerComboBox->itemData(i, Qt::UserRole);
107 
108  std::string dsId = l->getDataSourceId();
109 
111  std::string dsConnInfo = dsInfoPtr->getConnInfoAsString();
112 
113  if (dsConnInfo == connInfo)
114  {
115  m_ui->m_layerComboBox->setCurrentIndex(i);
116  break;
117  }
118  }
119 }
120 
122 {
123  //input ds
124  std::map<std::string, std::string> connInfo;
125  connInfo["URI"] = m_ui->m_geopackageLineEdit->text().toUtf8().data();
126 
127  std::auto_ptr<te::da::DataSource> dsGPKG = te::da::DataSourceFactory::make("GPKG");
128  dsGPKG->setConnectionInfo(connInfo);
129  dsGPKG->open();
130 
131  //input dataset
132  std::string inputDs = m_ui->m_gatheringComboBox->currentText().toUtf8().data();
133 
134  //output ds
135  QVariant varLayer = m_ui->m_layerComboBox->currentData(Qt::UserRole);
137 
138  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
139 
140  std::string outputDataSet = dsType->getTitle();
141 
142  te::da::DataSourcePtr outDs = te::da::GetDataSource(l->getDataSourceId());
143 
144  try
145  {
147 
148  gpSync.setInputParameters(dsGPKG.get(), inputDs, outDs.get(), outputDataSet);
149 
150  gpSync.synchronize();
151  }
152  catch (const std::exception& e)
153  {
154  QMessageBox::warning(this, tr("Warning"), e.what());
155 
156  return;
157  }
158  catch(...)
159  {
160  QMessageBox::warning(this, tr("Warning"), tr("Internal Error."));
161 
162  return;
163  }
164 
165  QMessageBox::information(this, tr("Information"), tr("Synchronizer Done."));
166 }
167 
169 {
170  m_ui->m_layerComboBox->clear();
171 
172  std::list<te::map::AbstractLayerPtr>::iterator it = list.begin();
173 
174  while (it != list.end())
175  {
177 
178  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
179 
180  if (dsType->hasGeom())
181  m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
182 
183  ++it;
184  }
185 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
This file is used to Synchronizer operation.
boost::shared_ptr< DataSource > DataSourcePtr
void setInputParameters(te::da::DataSource *inputDataSource, std::string inputDataSet, te::da::DataSource *outputDataSource, std::string outputDataset)
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
void setLayerList(std::list< te::map::AbstractLayerPtr > list)
std::auto_ptr< Ui::GeoPackageSynchronizerDialogForm > m_ui
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr