ImportTableDialog.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/addressgeocoding/ImportTableDialog.cpp
22 
23  \brief A dialog to import table with address to geocoding operation
24 */
25 
26 // TerraLib
27 #include "../../core/logger/Logger.h"
28 #include "../../common/progress/ProgressManager.h"
29 #include "../../core/translator/Translator.h"
30 #include "../../common/STLUtils.h"
31 #include "../../dataaccess/dataset/DataSetType.h"
32 #include "../../dataaccess/dataset/DataSetTypeConverter.h"
33 #include "../../dataaccess/dataset/ObjectIdSet.h"
34 #include "../../dataaccess/datasource/DataSourceCapabilities.h"
35 #include "../../dataaccess/datasource/DataSource.h"
36 #include "../../dataaccess/datasource/DataSourceInfo.h"
37 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
38 #include "../../dataaccess/datasource/DataSourceFactory.h"
39 #include "../../dataaccess/datasource/DataSourceManager.h"
40 #include "../../dataaccess/utils/Utils.h"
41 #include "../../datatype/Enums.h"
42 #include "../../datatype/Property.h"
43 #include "../../maptools/AbstractLayer.h"
44 #include "../../memory/DataSet.h"
45 #include "../../qt/af/Utils.h"
46 #include "../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
47 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
48 #include "../../qt/widgets/progress/ProgressViewerDialog.h"
49 #include "../../qt/widgets/property/DataSetAdapterWidget.h"
50 #include "../../qt/widgets/table/DataSetTableView.h"
51 #include "../../qt/widgets/Utils.h"
52 #include "../../statistics/core/Utils.h"
53 #include "../Config.h"
54 #include "../Exception.h"
55 #include "ImportTableDialog.h"
56 #include "ui_ImportTableDialogForm.h"
57 
58 // Qt
59 #include <QCheckBox>
60 #include <QFileDialog>
61 #include <QGridLayout>
62 #include <QMessageBox>
63 
64 // Boost
65 #include <boost/algorithm/string.hpp>
66 #include <boost/filesystem.hpp>
67 #include <boost/uuid/random_generator.hpp>
68 #include <boost/uuid/uuid_io.hpp>
69 
71  : QDialog(parent, f),
72  m_ui(new Ui::ImportTableDialogForm)
73 {
74 // add controls
75  m_ui->setupUi(this);
76 
77  //Adjusting the dataSetTableView that will be used to display the address data
78  m_tblView.reset(new te::qt::widgets::DataSetTableView(m_ui->m_dataPreviewFrame));
79  QGridLayout* dataPreviewLayout = new QGridLayout(m_ui->m_dataPreviewFrame);
80  dataPreviewLayout->addWidget(m_tblView.get());
81  dataPreviewLayout->setContentsMargins(0, 0, 0, 0);
82 
83  m_tblView->setAlternatingRowColors(true);
84  m_tblView->verticalHeader()->setVisible(false);
85  m_tblView->setSelectionMode(QAbstractItemView::NoSelection);
86 
87 
88  connect(m_ui->m_filePushButton, SIGNAL(clicked()), this, SLOT(onInputDataToolButtonTriggered()));
89 
90  connect(m_ui->m_helpPushButton, SIGNAL(clicked()), this, SLOT(onHelpPushButtonClicked()));
91  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
92  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
93 
94 }
95 
97 {
98 }
99 
101 {
102  return m_dataSource;
103 }
104 
106 {
107  return *m_dataSet;
108 }
109 
111 {
112  try
113  {
114  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Textual File"), te::qt::widgets::GetFilePathFromSettings("tabular"), tr("Comma Separated Value (*.csv *.CSV);; dBASE (*.dbf *.DBF)"),
115  0, QFileDialog::ReadOnly);
116 
117  if(fileName.isEmpty())
118  return;
119 
120  QFileInfo info(fileName);
121  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "tabular");
122 
123  //Getting the connection info
124  std::string ogrInfo("connection_string=" + std::string(fileName.toUtf8().data()));
125  std::string connInfo("file://");
126  connInfo += fileName.toUtf8().data();
127 
128  boost::filesystem::path uri(fileName.toUtf8().data());
129  std::string file = uri.stem().string();
130 
131  //Creating a DataSource
132  static boost::uuids::basic_random_generator<boost::mt19937> gen;
133  boost::uuids::uuid u = gen();
134 
136  dsInfo->setConnInfo(connInfo);
137  dsInfo->setId(boost::uuids::to_string(u));
138  dsInfo->setTitle(fileName.toUtf8().data());
139  dsInfo->setDescription("");
140  dsInfo->setAccessDriver("OGR");
141  dsInfo->setType("OGR");
142 
144 
145  m_dataSource = te::da::DataSourceFactory::make(dsInfo->getAccessDriver(), connInfo);
146 
147  m_dataSource->setId(boost::uuids::to_string(u));
148  m_dataSource->open();
149 
150  //Creating the DataSet and DataType
151  m_dataSet = m_dataSource->getDataSet(file);
152  std::vector<std::string> datasetNames = m_dataSource->getDataSetNames();
153 
154  if(!datasetNames.empty())
155  m_dataType = m_dataSource->getDataSetType(datasetNames[0]);
156  else
157  {
158  QMessageBox::critical( this, tr("Error"),tr("The file could not be read!"));
159  return;
160  }
161 
162  m_ui->m_fileLineEdit->setText(fileName);
163 
164  //Creating the DataSetConverter
166 
167  //Filling the data preview table
168  std::vector<std::size_t> properties;
169  for(std::size_t i = 0; i < m_dataSet->getNumProperties(); ++i)
170  properties.push_back(i);
171 
172  //The table will display 10 rows of the data for previewing purposes
173  std::unique_ptr<te::mem::DataSet> memFeature((new te::mem::DataSet(*m_dataSet.get(), properties, 10)));
174 
175  m_tblView->setDataSet(memFeature.release());
176  m_tblView->resizeColumnsToContents();
177  m_tblView->show();
178  }
179  catch(const std::exception& e)
180  {
181  QMessageBox::warning(this, tr("Tabular File"), e.what());
182  }
183 }
184 
186 {
187  QMessageBox::information(this, "Help", "Under development");
188 }
189 
191 {
192  this->close();
193 }
194 
196 {
197  reject();
198 }
199 
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
A dialog to import table with address to geocoding operation.
ImportTableDialog(QWidget *parent=0, Qt::WindowFlags f=0)
boost::shared_ptr< DataSource > DataSourcePtr
std::unique_ptr< te::da::DataSet > m_dataSet
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
An converter for DataSetType.
std::unique_ptr< te::qt::widgets::DataSetTableView > m_tblView
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
std::unique_ptr< Ui::ImportTableDialogForm > m_ui
std::unique_ptr< te::da::DataSetType > m_dataType
std::unique_ptr< te::da::DataSetTypeConverter > m_dsConverter
A dataset is the unit of information manipulated by the data access module of TerraLib.
A customized table view for te::map::AbstractLayer objects. Uses a te::qt::widgets::DataSetModel as i...
A class that represents a data source component.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr