All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TableLinkDialog.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/externaltable/TableLinkDialog.h
22 
23  \brief A Qt dialog that allows users to create a new query layer based on the information of two distinct datasets
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/PrimaryKey.h"
28 #include "../../../dataaccess/datasource/DataSourceManager.h"
29 #include "../../../dataaccess/Enums.h"
30 #include "../../../dataaccess/query/BinaryFunction.h"
31 #include "../../../dataaccess/query/DataSetName.h"
32 #include "../../../dataaccess/query/Expression.h"
33 #include "../../../dataaccess/query/Field.h"
34 #include "../../../dataaccess/query/Join.h"
35 #include "../../../dataaccess/query/JoinConditionOn.h"
36 #include "../../../dataaccess/query/PropertyName.h"
37 #include "../../../dataaccess/utils/Utils.h"
38 #include "../../../geometry/GeometryProperty.h"
39 #include "../../../maptools/QueryLayer.h"
40 #include "../../../memory/DataSet.h"
41 #include "../../../se/Utils.h"
42 #include "../table/DataSetTableView.h"
43 #include "FieldsDialog.h"
44 #include "TableLinkDialog.h"
45 #include "ui_TableLinkDialogForm.h"
46 
47 // Boost
48 #include <boost/uuid/random_generator.hpp>
49 #include <boost/uuid/uuid_io.hpp>
50 
51 //QT
52 #include <QMessageBox>
53 
54 // STL
55 #include <cassert>
56 
57 te::qt::widgets::TableLinkDialog::TableLinkDialog(QWidget* parent, Qt::WindowFlags f)
58  : QDialog(parent, f),
59  m_ui(new Ui::TableLinkDialogForm)
60 {
61  m_ui->setupUi(this);
62  m_ui->m_dataToolButton->setIcon(QIcon::fromTheme("view-data-table"));
63  m_ui->m_dataToolButton->setToolTip(tr("View dataset rows"));
64  m_ui->m_advancedToolButton->setIcon(QIcon::fromTheme("preferences-system"));
65  m_ui->m_advancedToolButton->setToolTip(tr("View advanced options"));
66 
67  //Adjusting the dataSetTableView that will be used to display the tabular dataset's data
68  m_tabularView.reset(new DataSetTableView(m_ui->m_tabularFrame));
69  QGridLayout* tabularLayout = new QGridLayout(m_ui->m_tabularFrame);
70  tabularLayout->addWidget(m_tabularView.get());
71  tabularLayout->setContentsMargins(0, 0, 0, 0);
72 
73  m_tabularView->setAlternatingRowColors(true);
74  m_tabularView->verticalHeader()->setVisible(false);
75  m_tabularView->setSelectionMode(QAbstractItemView::NoSelection);
76  m_tabularView->hide();
77  m_ui->m_dataPreviewGroupBox->hide();
78 
79  //Adjusting the doubleListWidget that will be used to configure the query's fields.
81  m_ui->m_tabularFrame->hide();
82  m_ui->m_helpPushButton->setPageReference("widgets/external_table/table_link_dialog.html");
83 
84  //Currently, this function is disabled, further enhancements to the linking functions are required to make such options available
85  m_ui->m_advancedToolButton->hide();
86 
87  //Connecting signals and slots
88  connect(m_ui->m_dataSet2ComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onDataCBIndexChanged(int)));
89  connect(m_ui->m_dataToolButton, SIGNAL(clicked()), this, SLOT(onDataToolButtonnClicked()));
90 // connect(m_ui->m_advancedToolButton, SIGNAL(clicked()), this, SLOT(onAdvancedToolButtonnClicked()));
91  connect(m_ui->m_OkPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
92 }
93 
95 {
96 }
97 
99 {
100  m_inputLayer.reset((te::map::DataSetLayer*)inLayer.get());
101  m_ui->m_dataSet1LineEdit->setText(QString::fromStdString(m_inputLayer->getDataSetName()));
102  m_ds = te::da::DataSourceManager::getInstance().find(m_inputLayer->getDataSourceId());
103 
104  if(!m_ds->isOpened())
105  m_ds->open();
106 
107  getDataSets();
108 }
109 
111 {
112  size_t pos = m_inputLayer->getDataSetName().find(".");
113  std::string inputAlias = m_inputLayer->getDataSetName().substr(pos + 1, m_inputLayer->getDataSetName().size() - 1);
114 
115  if(pos != std::string::npos)
116  inputAlias = m_inputLayer->getDataSetName().substr(pos + 1, m_inputLayer->getDataSetName().size() - 1);
117  else
118  inputAlias = m_inputLayer->getDataSetName();
119 
120  te::da::DataSetName* inField = new te::da::DataSetName(m_inputLayer->getDataSetName(), inputAlias);
121  te::da::DataSetName* tabField = new te::da::DataSetName(m_ui->m_dataSet2ComboBox->currentText().toStdString(), m_ui->m_dataSetAliasLineEdit->text().toStdString());
122 
123  te::da::Expression* exp1 = new te::da::PropertyName(m_ui->m_dataset1ColumnComboBox->currentText().toStdString());
124  te::da::Expression* exp2 = new te::da::PropertyName(m_ui->m_dataset2ColumnComboBox->currentText().toStdString());
125  te::da::Expression* expression = new te::da::BinaryFunction("=", exp1, exp2);
126 
127  te::da::JoinType type = m_fieldsDialog->getJoinType();
128 
129  te::da::Join* join = new te::da::Join(inField, tabField, type, new te::da::JoinConditionOn(expression));
130  return join;
131 }
132 
134 {
135  //fields
136  te::da::Fields* fields = m_fieldsDialog->getFields();
137 
138  //from
139  te::da::From* from = new te::da::From;
140 
141  //Join
142  from->push_back(getJoin());
143 
144  //build the select object
145  te::da::Select s(fields, from);
146 
147  return s;
148 }
149 
151 {
152  static boost::uuids::basic_random_generator<boost::mt19937> gen;
153  boost::uuids::uuid u = gen();
154  std::string id = boost::uuids::to_string(u);
155 
156  std::string title = m_ui->m_layerTitleLineEdit->text().toStdString();
157 
158  te::map::QueryLayerPtr layer(new te::map::QueryLayer(id, title));
159  layer->setDataSourceId(m_ds->getId());
160  layer->setRendererType("QUERY_LAYER_RENDERER");
161  layer->setQuery(new te::da::Select(getSelectQuery()));
162  layer->computeExtent();
163 
164  // SRID
165  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
167  layer->setSRID(gp->getSRID());
168 
169  // style
170  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
171 
172  return layer;
173 }
174 
176 {
177  QApplication::setOverrideCursor(Qt::WaitCursor);
178 
179  std::string dsId = m_ds->getId();
180 
181  std::vector<std::string> datasetNames;
182 
183  te::da::GetDataSetNames(datasetNames, dsId);
184 
185  for (size_t i = 0; i < datasetNames.size(); i++)
186  {
187  if(datasetNames[i] != m_inputLayer->getDataSetName())
188  m_ui->m_dataSet2ComboBox->addItem(QString::fromStdString(datasetNames[i]));
189  }
190 
191  std::string DsName = m_ui->m_dataSet2ComboBox->currentText().toStdString();
192  size_t pos = DsName.find(".");
193  if(pos != std::string::npos)
194  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName.substr(pos + 1, DsName.size() - 1)));
195  else
196  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName));
197 
198  QApplication::restoreOverrideCursor();
199 }
200 
202 {
203  //Clearing contents
204  int index = m_ui->m_dataset1ColumnComboBox->currentIndex();
205  m_ui->m_dataset1ColumnComboBox->clear();
206  m_ui->m_dataset2ColumnComboBox->clear();
207 
208  m_fieldsDialog->clearInputValues();
209  m_fieldsDialog->clearOutputValues();
210 
211  //get the dataset names
212  std::vector<std::string> datasetNames = m_ds->getDataSetNames();
213  std::vector<std::pair<std::string, std::string> > dataSetSelecteds;
214  std::string inputAlias;
215  size_t pos = m_inputLayer->getDataSetName().find(".");
216 
217  if(pos != std::string::npos)
218  inputAlias = m_inputLayer->getDataSetName().substr(pos + 1, m_inputLayer->getDataSetName().size() - 1);
219  else
220  inputAlias = m_inputLayer->getDataSetName();
221 
222  dataSetSelecteds.push_back(std::make_pair(m_inputLayer->getDataSetName(), inputAlias));
223  dataSetSelecteds.push_back(std::make_pair(m_ui->m_dataSet2ComboBox->currentText().toStdString(), m_ui->m_dataSetAliasLineEdit->text().toStdString()));
224 
225  std::vector<std::string> propertyNames;
226  std::vector<std::string> fixedProperties;
227 
228  //get properties for each data set
229  for(size_t t = 0; t < dataSetSelecteds.size(); ++t)
230  {
231  //alias name
232  std::string alias = dataSetSelecteds[t].second;
233 
234  //data set name
235  std::string dataSetName = dataSetSelecteds[t].first;
236 
237  //get datasettype
238  std::auto_ptr<te::da::DataSetType> dsType(0);
239 
240  //Acquiring the dataSet properties
241  std::vector<std::size_t> dataSetProperties;
242 
243 
244  for(unsigned int i = 0; i < datasetNames.size(); ++i)
245  {
246  if(datasetNames[i] == dataSetName)
247  {
248  dsType = m_ds->getDataSetType(datasetNames[i]);
249  break;
250  }
251  }
252 
253  if(dsType.get())
254  {
255  //Acquiring the primary key's properties
256  te::da::PrimaryKey* pk = dsType->getPrimaryKey();
257 
258  if(!pk)
259  return;
260 
261  for(size_t i = 0; i < dsType->size(); ++i)
262  {
263  std::string propName = dsType->getProperty(i)->getName();
264  std::string fullName = alias + "." + propName;
265 
266  std::auto_ptr<te::da::DataSet> dataSet = m_ds->getDataSet(alias);
267 
268  dataSetProperties.push_back(i);
269 
270  if((dsType->getProperty(i)->getType() == te::dt::GEOMETRY_TYPE) || (pk->has(dsType->getProperty(i))))
271  fixedProperties.push_back(fullName);
272  else
273  propertyNames.push_back(fullName);
274 
275  if(t == 0)
276  m_ui->m_dataset1ColumnComboBox->addItem(QString::fromStdString(fullName), QVariant(dsType->getProperty(i)->getType()));
277  else
278  {
279  m_ui->m_dataset2ColumnComboBox->addItem(QString::fromStdString(fullName), QVariant(dsType->getProperty(i)->getType()));
280 
281  if(i == dsType->size() - 1)
282  {
283  //Adjusting the table that will display the tabular dataset
284  m_tabularView->setDataSet(new te::mem::DataSet(*dataSet.get(), dataSetProperties, 5), m_ds->getEncoding());
285  m_tabularView->resizeColumnsToContents();
286  }
287  }
288  }
289  }
290  }
291 
292  if(index != -1)
293  m_ui->m_dataset1ColumnComboBox->setCurrentIndex(index);
294 
295  //Adjusting the widget that is used to configure the query's field
296  m_fieldsDialog->setLeftLabel("Non-selected fields");
297  m_fieldsDialog->setRightLabel("Selected fields");
298  m_fieldsDialog->setOutputValues(propertyNames);
299  m_fieldsDialog->setFixedOutputValues(fixedProperties, "geometry");
300 }
301 
303 {
304  if(QDialog::Accepted == r)
305  {
306  QVariant dsv1, dsv2;
307  dsv1 = m_ui->m_dataset1ColumnComboBox->itemData(m_ui->m_dataset1ColumnComboBox->currentIndex());
308  dsv2 = m_ui->m_dataset2ColumnComboBox->itemData(m_ui->m_dataset2ColumnComboBox->currentIndex());
309 
310  if(dsv1 != dsv2)
311  {
312  QMessageBox::warning(this, tr("Tabular File"), "The types of the selected columns do not match.");
313  return;
314  }
315  else if(dsv1.toInt() != te::dt::STRING_TYPE && (dsv1.toInt() < te::dt::INT16_TYPE || dsv1.toInt() > te::dt::UINT64_TYPE))
316  {
317  QMessageBox::warning(this, tr("Tabular File"), "The types of the selected columns must be either an integer or a string.");
318  return;
319  }
320  else
321  {
322  QDialog::done(r);
323  return;
324  }
325  }
326  else
327  {
328  QDialog::done(r);
329  return;
330  }
331 }
332 
334 {
335  if(m_ds->getType() == "OGR")
336  {
337  QMessageBox::information(this, tr("Table link error"),
338  tr("This function is not available for the selected datasource"));
339  return QDialog::Rejected;
340  }
341  else if(!m_ds->getDataSetType(m_inputLayer->getDataSetName())->getPrimaryKey())
342  {
343  QMessageBox::information(this, tr("Table link error"),
344  tr("This function is not available for datasets without a primary key"));
345  return QDialog::Rejected;
346  }
347  else
348  return QDialog::exec();
349 }
350 
352 {
353  std::string DsName = m_ui->m_dataSet2ComboBox->currentText().toStdString();
354  size_t pos = DsName.find(".");
355  if(pos != std::string::npos)
356  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName.substr(pos + 1, DsName.size() - 1)));
357  else
358  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName));
359 
360  getProperties();
361 }
362 
364 {
365  if(m_ui->m_tabularFrame->isHidden())
366  {
367  m_tabularView->show();
368  m_ui->m_tabularFrame->show();
369  m_ui->m_dataPreviewGroupBox->show();
370  }
371  else
372  {
373  m_ui->m_tabularFrame->hide();
374  m_tabularView->hide();
375  m_ui->m_dataPreviewGroupBox->hide();
376  }
377 }
378 
380 {
381  int res = m_fieldsDialog->exec();
382  if(res != QDialog::Accepted)
383  {
384  getProperties();
385  }
386 }
387 
389 {
390  this->accept();
391 }
Geometric property.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
TableLinkDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Constructor.
void setInputLayer(te::map::AbstractLayerPtr inLayer)
It sets the Datasource that is being used to generate the new Layer.
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
A class that models the name of any property of an object.
Definition: PropertyName.h:50
bool has(const te::dt::Property *p) const
It verifies if Property is associated to the primary key.
Definition: PrimaryKey.cpp:66
std::auto_ptr< FieldsDialog > m_fieldsDialog
The widget used to select which fields will be added to the query.
A layer resulting from a query.
Definition: QueryLayer.h:50
std::auto_ptr< Ui::TableLinkDialogForm > m_ui
The widget's form.
void onAdvancedToolButtonnClicked()
Displays or hides the widget's used to configure the query's fields.
te::map::AbstractLayerPtr getQueryLayer()
Returns a new query layer based on the requested parameters.
This is an abstract class that models a query expression.
Definition: Expression.h:47
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
Definition: Utils.cpp:284
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
te::da::Join * getJoin()
Returns the generated Join.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
JoinType
The type of join in a query.
Definition: Enums.h:49
te::da::Select getSelectQuery()
Returns a new query based on the requested parameters.
const Fields * getFields() const
It returns the list of output expressions used to form the result set.
Definition: Select.cpp:932
A Join clause combines two FromItems.
Definition: Join.h:50
std::auto_ptr< DataSetTableView > m_tabularView
The widget used to preview the data of the tabular dataset.
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:153
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
void onDataToolButtonnClicked()
Displays or hides the data of the tabular Dataset.
A Qt Dialog that allows users to modify which fields will be included in a query. ...
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
A customized table view for te::map::AbstractLayer objects. Uses a te::qt::widgets::DataSetModel as i...
JoinConditionOn is a boolean expression and it specifies which items in a join are considered to matc...
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
boost::intrusive_ptr< QueryLayer > QueryLayerPtr
Definition: QueryLayer.h:176
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
A base class for binary functions.
A Qt dialog that allows users to create a new query layer based on the information of two distinct da...