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 "../../../qt/widgets/utils/ScopedCursor.h"
42 #include "../../../se/Utils.h"
43 #include "../table/DataSetTableView.h"
44 #include "FieldsDialog.h"
45 #include "TableLinkDialog.h"
46 #include "ui_TableLinkDialogForm.h"
47 
48 // Boost
49 #include <boost/uuid/random_generator.hpp>
50 #include <boost/uuid/uuid_io.hpp>
51 
52 //QT
53 #include <QMessageBox>
54 
55 // STL
56 #include <cassert>
57 
58 te::qt::widgets::TableLinkDialog::TableLinkDialog(QWidget* parent, Qt::WindowFlags f)
59  : QDialog(parent, f),
60  m_ui(new Ui::TableLinkDialogForm)
61 {
62  m_ui->setupUi(this);
63  m_ui->m_dataToolButton->setIcon(QIcon::fromTheme("view-data-table"));
64  m_ui->m_dataToolButton->setToolTip(tr("View dataset rows"));
65  m_ui->m_advancedToolButton->setIcon(QIcon::fromTheme("preferences-system"));
66  m_ui->m_advancedToolButton->setToolTip(tr("View advanced options"));
67 
68  //Adjusting the dataSetTableView that will be used to display the tabular dataset's data
69  m_tabularView.reset(new DataSetTableView(m_ui->m_tabularFrame));
70  QGridLayout* tabularLayout = new QGridLayout(m_ui->m_tabularFrame);
71  tabularLayout->addWidget(m_tabularView.get());
72  tabularLayout->setContentsMargins(0, 0, 0, 0);
73 
74  m_tabularView->setAlternatingRowColors(true);
75  m_tabularView->verticalHeader()->setVisible(false);
76  m_tabularView->setSelectionMode(QAbstractItemView::NoSelection);
77  m_tabularView->hide();
78  m_ui->m_dataPreviewGroupBox->hide();
79 
80  //Adjusting the doubleListWidget that will be used to configure the query's fields.
82  m_ui->m_tabularFrame->hide();
83  m_ui->m_helpPushButton->setPageReference("widgets/external_table/table_link_dialog.html");
84 
85  //Currently, this function is disabled, further enhancements to the linking functions are required to make such options available
86  m_ui->m_advancedToolButton->hide();
87 
88  //Connecting signals and slots
89  connect(m_ui->m_dataSet2ComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onDataCBIndexChanged(int)));
90  connect(m_ui->m_dataToolButton, SIGNAL(clicked()), this, SLOT(onDataToolButtonnClicked()));
91 // connect(m_ui->m_advancedToolButton, SIGNAL(clicked()), this, SLOT(onAdvancedToolButtonnClicked()));
92  connect(m_ui->m_OkPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
93 }
94 
96 {
97 }
98 
100 {
101  m_inputLayer.reset((te::map::DataSetLayer*)inLayer.get());
102  m_ui->m_dataSet1LineEdit->setText(QString::fromStdString(m_inputLayer->getDataSetName()));
103  m_ds = te::da::DataSourceManager::getInstance().find(m_inputLayer->getDataSourceId());
104 
105  if(!m_ds->isOpened())
106  m_ds->open();
107 
108  getDataSets();
109 }
110 
112 {
113  size_t pos = m_inputLayer->getDataSetName().find(".");
114  std::string inputAlias = m_inputLayer->getDataSetName().substr(pos + 1, m_inputLayer->getDataSetName().size() - 1);
115 
116  if(pos != std::string::npos)
117  inputAlias = m_inputLayer->getDataSetName().substr(pos + 1, m_inputLayer->getDataSetName().size() - 1);
118  else
119  inputAlias = m_inputLayer->getDataSetName();
120 
121  te::da::DataSetName* inField = new te::da::DataSetName(m_inputLayer->getDataSetName(), inputAlias);
122  te::da::DataSetName* tabField = new te::da::DataSetName(m_ui->m_dataSet2ComboBox->currentText().toStdString(), m_ui->m_dataSetAliasLineEdit->text().toStdString());
123 
124  te::da::Expression* exp1 = new te::da::PropertyName(m_ui->m_dataset1ColumnComboBox->currentText().toStdString());
125  te::da::Expression* exp2 = new te::da::PropertyName(m_ui->m_dataset2ColumnComboBox->currentText().toStdString());
126  te::da::Expression* expression = new te::da::BinaryFunction("=", exp1, exp2);
127 
128  te::da::JoinType type = m_fieldsDialog->getJoinType();
129 
130  te::da::Join* join = new te::da::Join(inField, tabField, type, new te::da::JoinConditionOn(expression));
131  return join;
132 }
133 
135 {
136  //fields
137  te::da::Fields* fields = m_fieldsDialog->getFields();
138 
139  //from
140  te::da::From* from = new te::da::From;
141 
142  //Join
143  from->push_back(getJoin());
144 
145  //build the select object
146  te::da::Select s(fields, from);
147 
148  return s;
149 }
150 
152 {
153  te::qt::widgets::ScopedCursor c(Qt::WaitCursor);
154 
155  static boost::uuids::basic_random_generator<boost::mt19937> gen;
156  boost::uuids::uuid u = gen();
157  std::string id = boost::uuids::to_string(u);
158 
159  std::string title = m_ui->m_layerTitleLineEdit->text().toStdString();
160 
161  te::map::QueryLayerPtr layer(new te::map::QueryLayer(id, title));
162  layer->setDataSourceId(m_ds->getId());
163  layer->setRendererType("QUERY_LAYER_RENDERER");
164  layer->setQuery(new te::da::Select(getSelectQuery()));
165  layer->computeExtent();
166 
167  // SRID
168  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
170  layer->setSRID(gp->getSRID());
171 
172  // style
173  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
174 
175  return layer;
176 }
177 
179 {
180  te::qt::widgets::ScopedCursor c(Qt::WaitCursor);
181 
182  std::string dsId = m_ds->getId();
183 
184  std::vector<std::string> datasetNames;
185 
186  te::da::GetDataSetNames(datasetNames, dsId);
187 
188  for (size_t i = 0; i < datasetNames.size(); i++)
189  {
190  if(datasetNames[i] != m_inputLayer->getDataSetName())
191  m_ui->m_dataSet2ComboBox->addItem(QString::fromStdString(datasetNames[i]));
192  }
193 
194  std::string DsName = m_ui->m_dataSet2ComboBox->currentText().toStdString();
195  size_t pos = DsName.find(".");
196  if(pos != std::string::npos)
197  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName.substr(pos + 1, DsName.size() - 1)));
198  else
199  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName));
200 }
201 
203 {
204  te::qt::widgets::ScopedCursor c(Qt::WaitCursor);
205 
206  //Clearing contents
207  int index = m_ui->m_dataset1ColumnComboBox->currentIndex();
208  m_ui->m_dataset1ColumnComboBox->clear();
209  m_ui->m_dataset2ColumnComboBox->clear();
210 
211  m_fieldsDialog->clearInputValues();
212  m_fieldsDialog->clearOutputValues();
213 
214  //get the dataset names
215  std::vector<std::string> datasetNames = m_ds->getDataSetNames();
216  std::vector<std::pair<std::string, std::string> > dataSetSelecteds;
217  std::string inputAlias;
218  size_t pos = m_inputLayer->getDataSetName().find(".");
219 
220  if(pos != std::string::npos)
221  inputAlias = m_inputLayer->getDataSetName().substr(pos + 1, m_inputLayer->getDataSetName().size() - 1);
222  else
223  inputAlias = m_inputLayer->getDataSetName();
224 
225  dataSetSelecteds.push_back(std::make_pair(m_inputLayer->getDataSetName(), inputAlias));
226  dataSetSelecteds.push_back(std::make_pair(m_ui->m_dataSet2ComboBox->currentText().toStdString(), m_ui->m_dataSetAliasLineEdit->text().toStdString()));
227 
228  std::vector<std::string> propertyNames;
229  std::vector<std::string> fixedProperties;
230 
231  //get properties for each data set
232  for(size_t t = 0; t < dataSetSelecteds.size(); ++t)
233  {
234  //alias name
235  std::string alias = dataSetSelecteds[t].second;
236 
237  //data set name
238  std::string dataSetName = dataSetSelecteds[t].first;
239 
240  //get datasettype
241  std::auto_ptr<te::da::DataSetType> dsType(0);
242 
243  //Acquiring the dataSet properties
244  std::vector<std::size_t> dataSetProperties;
245 
246 
247  for(unsigned int i = 0; i < datasetNames.size(); ++i)
248  {
249  if(datasetNames[i] == dataSetName)
250  {
251  dsType = m_ds->getDataSetType(datasetNames[i]);
252  break;
253  }
254  }
255 
256  if(dsType.get())
257  {
258  //Acquiring the primary key's properties
259  te::da::PrimaryKey* pk = dsType->getPrimaryKey();
260 
261  if(!pk)
262  return;
263 
264  for(size_t i = 0; i < dsType->size(); ++i)
265  {
266  std::string propName = dsType->getProperty(i)->getName();
267  std::string fullName = alias + "." + propName;
268 
269  if((dsType->getProperty(i)->getType() == te::dt::GEOMETRY_TYPE) || (pk->has(dsType->getProperty(i))))
270  fixedProperties.push_back(fullName);
271  else
272  propertyNames.push_back(fullName);
273 
274  if(t == 0)
275  m_ui->m_dataset1ColumnComboBox->addItem(QString::fromStdString(fullName), QVariant(dsType->getProperty(i)->getType()));
276  else
277  {
278  m_ui->m_dataset2ColumnComboBox->addItem(QString::fromStdString(fullName), QVariant(dsType->getProperty(i)->getType()));
279  }
280  }
281  }
282  }
283 
284  if(index != -1)
285  m_ui->m_dataset1ColumnComboBox->setCurrentIndex(index);
286 
287  //Adjusting the widget that is used to configure the query's field
288  m_fieldsDialog->setLeftLabel("Non-selected fields");
289  m_fieldsDialog->setRightLabel("Selected fields");
290  m_fieldsDialog->setOutputValues(propertyNames);
291  m_fieldsDialog->setFixedOutputValues(fixedProperties, "geometry");
292 }
293 
295 {
296  if(QDialog::Accepted == r)
297  {
298  QVariant dsv1, dsv2;
299  dsv1 = m_ui->m_dataset1ColumnComboBox->itemData(m_ui->m_dataset1ColumnComboBox->currentIndex());
300  dsv2 = m_ui->m_dataset2ColumnComboBox->itemData(m_ui->m_dataset2ColumnComboBox->currentIndex());
301  std::string title = m_ui->m_layerTitleLineEdit->text().toStdString();
302 
303  if(dsv1 != dsv2)
304  {
305  QMessageBox::warning(this, tr("Tabular File"), "The types of the selected columns do not match.");
306  return;
307  }
308  else if(dsv1.toInt() != te::dt::STRING_TYPE && (dsv1.toInt() < te::dt::INT16_TYPE || dsv1.toInt() > te::dt::UINT64_TYPE))
309  {
310  QMessageBox::warning(this, tr("Tabular File"), "The types of the selected columns must be either an integer or a string.");
311  return;
312  }
313  else if (title.empty())
314  {
315  QMessageBox::warning(this, tr("Tabular File"), "The new layer must have a title.");
316  return;
317  }
318  else
319  {
320  QDialog::done(r);
321  return;
322  }
323  }
324  else
325  {
326  QDialog::done(r);
327  return;
328  }
329 }
330 
332 {
333  if(m_ds->getType() == "OGR")
334  {
335  QMessageBox::information(this, tr("Table link error"),
336  tr("This function is not available for the selected datasource"));
337  return QDialog::Rejected;
338  }
339  else if(!m_ds->getDataSetType(m_inputLayer->getDataSetName())->getPrimaryKey())
340  {
341  QMessageBox::information(this, tr("Table link error"),
342  tr("This function is not available for datasets without a primary key"));
343  return QDialog::Rejected;
344  }
345  else
346  return QDialog::exec();
347 }
348 
350 {
351  std::string DsName = m_ui->m_dataSet2ComboBox->currentText().toStdString();
352  size_t pos = DsName.find(".");
353  if(pos != std::string::npos)
354  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName.substr(pos + 1, DsName.size() - 1)));
355  else
356  m_ui->m_dataSetAliasLineEdit->setText(QString::fromStdString(DsName));
357 
358  getProperties();
359  m_ui->m_tabularFrame->hide();
360  m_tabularView->hide();
361  m_ui->m_dataPreviewGroupBox->hide();
362 }
363 
365 {
366  std::string aux = m_ui->m_dataset2ColumnComboBox->currentText().toStdString();
367  std::string alias = "";
368  size_t pos = aux.find(".");
369 
370  if (pos != std::string::npos)
371  alias = aux.substr(0, pos);
372  else
373  alias = aux;
374 
375  //get datasettype
376  std::auto_ptr<te::da::DataSetType> dsType(0);
377  dsType = m_ds->getDataSetType(alias);
378 
379  //Get Dataset
380 
381  std::auto_ptr<te::da::DataSet> dataSet = m_ds->getDataSet(alias);
382 
383  //Acquiring the dataSet properties
384  std::vector<std::size_t> dataSetProperties;
385 
386  for (size_t i = 0; i < dsType->size(); ++i)
387  {
388  dataSetProperties.push_back(i);
389  }
390 
391  //Adjusting the table that will display the tabular dataset
392  m_tabularView->setDataSet(new te::mem::DataSet(*dataSet.get(), dataSetProperties, 5), m_ds->getEncoding());
393  m_tabularView->resizeColumnsToContents();
394 
395  if(m_ui->m_tabularFrame->isHidden())
396  {
397  m_tabularView->show();
398  m_ui->m_tabularFrame->show();
399  m_ui->m_dataPreviewGroupBox->show();
400  }
401  else
402  {
403  m_ui->m_tabularFrame->hide();
404  m_tabularView->hide();
405  m_ui->m_dataPreviewGroupBox->hide();
406  }
407 }
408 
410 {
411  int res = m_fieldsDialog->exec();
412  if(res != QDialog::Accepted)
413  {
414  getProperties();
415  }
416 }
417 
419 {
420  this->accept();
421 }
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.
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:183
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.
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48