All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WhereClauseWidget.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 terralib/qt/widgets/query/WhereClauseWidget.cpp
22 
23  \brief This file has the WhereClauseWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/dataset/DataSetType.h"
30 #include "../../../dataaccess/dataset/ObjectIdSet.h"
31 #include "../../../dataaccess/datasource/DataSourceCatalog.h"
32 #include "../../../dataaccess/query/BinaryFunction.h"
33 #include "../../../dataaccess/query/DataSetName.h"
34 #include "../../../dataaccess/query/Distinct.h"
35 #include "../../../dataaccess/query/Field.h"
36 #include "../../../dataaccess/query/Fields.h"
37 #include "../../../dataaccess/query/From.h"
38 #include "../../../dataaccess/query/LiteralDouble.h"
39 #include "../../../dataaccess/query/LiteralGeom.h"
40 #include "../../../dataaccess/query/LiteralInt16.h"
41 #include "../../../dataaccess/query/LiteralInt32.h"
42 #include "../../../dataaccess/query/LiteralString.h"
43 #include "../../../dataaccess/query/OrderByItem.h"
44 #include "../../../dataaccess/query/PropertyName.h"
45 #include "../../../dataaccess/query/Select.h"
46 #include "../../../dataaccess/query/SQLVisitor.h"
47 #include "../../../dataaccess/utils/Utils.h"
48 #include "../../../geometry/Geometry.h"
49 #include "../../../geometry/GeometryCollection.h"
50 #include "../../../geometry/GeometryProperty.h"
51 #include "ui_WhereClauseWidgetForm.h"
52 #include "WhereClauseWidget.h"
53 
54 // Qt
55 #include <QtGui/QIcon>
56 #include <QtGui/QMessageBox>
57 #include <QtGui/QToolButton>
58 
60 
61 
62 te::qt::widgets::WhereClauseWidget::WhereClauseWidget(QWidget* parent, Qt::WindowFlags f)
63  : QWidget(parent, f),
64  m_ui(new Ui::WhereClauseWidgetForm)
65 {
66  m_ui->setupUi(this);
67 
68  m_ds.reset();
69 
70  // set icons
71  m_ui->m_addWhereClausePushButton->setIcon(QIcon::fromTheme("list-add"));
72  m_ui->m_clearAllPushButton->setIcon(QIcon::fromTheme("edit-clear"));
73 
74  //connects
75  connect(m_ui->m_addWhereClausePushButton, SIGNAL(clicked()), this, SLOT(onAddWhereClausePushButtonClicked()));
76  connect(m_ui->m_clearAllPushButton, SIGNAL(clicked()), this, SLOT(onClearAllPushButtonClicked()));
77  connect(m_ui->m_valueValueRadioButton, SIGNAL(clicked()), this, SLOT(onValuePropertyRadioButtonClicked()));
78  connect(m_ui->m_restrictValueComboBox, SIGNAL(activated(QString)), this, SLOT(onRestrictValueComboBoxActivated(QString)));
79 
80  m_count = 0;
81  m_srid = 0;
82 }
83 
85 {
86  clear();
87 }
88 
89 Ui::WhereClauseWidgetForm* te::qt::widgets::WhereClauseWidget::getForm() const
90 {
91  return m_ui.get();
92 }
93 
95 {
96  int row = m_ui->m_whereClauseTableWidget->rowCount();
97 
98  if(row == 0)
99  return 0;
100 
101  te::da::Expression* leftSide = 0;
102  std::string lastConnectorName = "";
103 
104  for(int i = 0; i < row; ++i)
105  {
106  //create binary function
107  std::string propName = "";
108  QTableWidgetItem* itemPropName = m_ui->m_whereClauseTableWidget->item(i, 1);
109  if(itemPropName)
110  propName = itemPropName->text().toStdString();
111  else
112  {
113  QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 1);
114  QComboBox* cmbBox = dynamic_cast<QComboBox*>(w);
115  if(cmbBox)
116  propName = cmbBox->currentText().toStdString();
117  }
118 
119  std::string funcName = "";
120  QTableWidgetItem* itemFuncName = m_ui->m_whereClauseTableWidget->item(i, 2);
121  if(itemFuncName)
122  funcName = itemFuncName->text().toStdString();
123  else
124  {
125  QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 2);
126  QComboBox* cmbBox = dynamic_cast<QComboBox*>(w);
127  if(cmbBox)
128  funcName = cmbBox->currentText().toStdString();
129  }
130 
131  int expIdx = -1;
132  QTableWidgetItem* itemValue = m_ui->m_whereClauseTableWidget->item(i, 3);
133  if(itemValue)
134  {
135  expIdx = itemValue->data(Qt::UserRole).toInt();
136  }
137  else
138  {
139  QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 3);
140  QComboBox* cmbBox = dynamic_cast<QComboBox*>(w);
141  if(cmbBox)
142  expIdx = m_comboMap[cmbBox].first;
143  else
144  return 0;
145  }
146 
147 
148  te::da::Expression* exp1 = new te::da::PropertyName(propName);
149  te::da::Expression* exp2 = m_mapExp[expIdx]->m_expression->clone();
150 
151  te::da::BinaryFunction* func = new te::da::BinaryFunction(funcName, exp1, exp2);
152 
153  //check left side expression
154  if(leftSide != 0 && lastConnectorName.empty() == false)
155  {
156  te::da::BinaryFunction* connectFunc = new te::da::BinaryFunction(lastConnectorName, leftSide, func);
157  leftSide = connectFunc;
158  }
159  else
160  {
161  leftSide = func;
162  }
163 
164  //check connector
165  QTableWidgetItem* itemConnectorName = m_ui->m_whereClauseTableWidget->item(i, 4);
166  if(itemConnectorName)
167  {
168  if(itemPropName->text().isEmpty() == false)
169  lastConnectorName = itemConnectorName->text().toStdString();
170  else
171  lastConnectorName = "";
172  }
173  else
174  {
175  QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 4);
176  QComboBox* cmbBox = dynamic_cast<QComboBox*>(w);
177  if(cmbBox)
178  lastConnectorName = cmbBox->currentText().toStdString();
179  }
180  }
181 
182  te::da::Where* w = new te::da::Where(leftSide);
183 
184  return w;
185 }
186 
188 {
189  std::string sql = "";
190 
191  te::da::Where* w = getWhere();
192 
193  if(w)
194  {
195  te::da::SQLVisitor visitor(*m_ds->getDialect(), sql);
196  te::da::Expression* exp = w->getExp();
197 
198  try
199  {
200  exp->accept(visitor);
201  }
202  catch(...)
203  {
204  delete w;
205  return "";
206  }
207  }
208 
209  delete w;
210 
211  return sql;
212 }
213 
215 {
216  m_ds = ds;
217 }
218 
219 void te::qt::widgets::WhereClauseWidget::setLayerList(std::list<te::map::AbstractLayerPtr>& layerList)
220 {
221  m_ui->m_layerComboBox->clear();
222 
223  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
224 
225  while(it != layerList.end())
226  {
228 
229  m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
230 
231  ++it;
232  }
233 }
234 
235 void te::qt::widgets::WhereClauseWidget::setFromItems(std::vector<std::pair<std::string, std::string> > vec)
236 {
237  m_fromItems = vec;
238 }
239 
240 void te::qt::widgets::WhereClauseWidget::setAttributeList(const std::vector<std::string>& vec)
241 {
242  m_ui->m_restrictValueComboBox->clear();
243  m_ui->m_valuePropertyComboBox->clear();
244 
245  for(size_t t = 0; t <vec.size(); ++t)
246  {
247  m_ui->m_restrictValueComboBox->addItem(vec[t].c_str());
248  m_ui->m_valuePropertyComboBox->addItem(vec[t].c_str());
249  }
250 }
251 
252 void te::qt::widgets::WhereClauseWidget::setGeomAttributeList(const std::vector<std::string>& vec, int srid)
253 {
254  m_srid = srid;
255 
256  m_ui->m_geomAttrComboBox->clear();
257 
258  for(size_t t = 0; t <vec.size(); ++t)
259  {
260  m_ui->m_geomAttrComboBox->addItem(vec[t].c_str());
261  }
262 }
263 
264 void te::qt::widgets::WhereClauseWidget::setOperatorsList(const std::vector<std::string>& vec)
265 {
266  m_ui->m_OperatorComboBox->clear();
267 
268  for(size_t t = 0; t <vec.size(); ++t)
269  {
270  m_ui->m_OperatorComboBox->addItem(vec[t].c_str());
271  }
272 }
273 
274 void te::qt::widgets::WhereClauseWidget::setSpatialOperatorsList(const std::vector<std::string>& vec)
275 {
276  m_ui->m_SpatialOperatorComboBox->clear();
277 
278  for(size_t t = 0; t <vec.size(); ++t)
279  {
280  m_ui->m_SpatialOperatorComboBox->addItem(vec[t].c_str());
281  }
282 }
283 
284 void te::qt::widgets::WhereClauseWidget::setConnectorsList(const std::vector<std::string>& vec)
285 {
286  m_connectorsList.clear();
287 
288  m_connectorsList.append("");
289 
290  for(size_t t = 0; t <vec.size(); ++t)
291  {
292  m_connectorsList.append(vec[t].c_str());
293  }
294 }
295 
297 {
298  te::common::FreeContents(m_mapExp);
299 
300  m_mapExp.clear();
301 
302  m_comboMap.clear();
303 
304  m_ui->m_whereClauseTableWidget->setRowCount(0);
305 
306  m_count = 0;
307 }
308 
310 {
311  std::string restrictValue = "";
312  std::string operatorStr = "";
313  std::string valueStr = "";
314 
315  if(m_ui->m_criteriaTabWidget->currentIndex() == 0) // criteria by attribute restriction
316  {
317  int expId = ++m_count;
318 
319  if(m_ui->m_restrictValueComboBox->currentText().isEmpty())
320  {
321  QMessageBox::warning(this, tr("Query Builder"), tr("Restrict value not defined."));
322  return;
323  }
324 
325  if(m_ui->m_valuePropertyRadioButton->isChecked() == false &&
326  m_ui->m_valueValueRadioButton->isChecked() == false)
327  {
328  if(m_ui->m_valueValueComboBox->currentText().isEmpty())
329  {
330  QMessageBox::warning(this, tr("Query Builder"), tr("Value not defined."));
331  return;
332  }
333  }
334 
335  if(m_ui->m_valueValueRadioButton->isChecked())
336  {
337  if(m_ui->m_valueValueComboBox->currentText().isEmpty())
338  {
339  QMessageBox::warning(this, tr("Query Builder"), tr("Value not defined."));
340  return;
341  }
342  }
343 
344  restrictValue = m_ui->m_restrictValueComboBox->currentText().toStdString();
345 
346  if(m_ui->m_OperatorComboBox->currentText().isEmpty())
347  {
348  QMessageBox::warning(this, tr("Query Builder"), tr("Operator not defined."));
349  return;
350  }
351  operatorStr = m_ui->m_OperatorComboBox->currentText().toStdString();
352 
354  ep->m_isAttributeCriteria = true;
355  ep->m_property = restrictValue;
356  ep->m_operator = operatorStr;
357 
358  if(m_ui->m_valuePropertyRadioButton->isChecked())
359  {
360  valueStr = m_ui->m_valuePropertyComboBox->currentText().toStdString();
361 
362  te::da::Expression* exp = new te::da::PropertyName(valueStr);
363 
364  ep->m_isPropertyValue = true;
365  ep->m_value = valueStr;
366  ep->m_expression = exp;
367 
368  m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
369  }
370  else if(m_ui->m_valueValueRadioButton->isChecked())
371  {
372  valueStr = m_ui->m_valueValueComboBox->currentText().toStdString();
373 
374  te::da::Expression* exp = getExpression(m_ui->m_valueValueComboBox->currentText(), restrictValue);
375 
376  ep->m_isValueValue = true;
377  ep->m_value = valueStr;
378  ep->m_expression = exp;
379 
380  m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
381  }
382 
383  //new entry
384  int newrow = m_ui->m_whereClauseTableWidget->rowCount();
385 
386  m_ui->m_whereClauseTableWidget->insertRow(newrow);
387 
388  //remove button
389  QToolButton* removeBtn = new QToolButton(m_ui->m_whereClauseTableWidget);
390  removeBtn->setIcon(QIcon::fromTheme("list-remove"));
391  connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveWhereClausePushButtonClicked()));
392  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 0, removeBtn);
393 
394  //property combo
395  QComboBox* cmbProperty = new QComboBox(m_ui->m_whereClauseTableWidget);
396  connect(cmbProperty, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
397  std::pair<int, int> pairProperty(expId, 1);
398  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbProperty, pairProperty));
399  copyCombo(m_ui->m_restrictValueComboBox, cmbProperty, restrictValue);
400  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 1, cmbProperty);
401 
402  //operator combo
403  QComboBox* cmbOperator = new QComboBox(m_ui->m_whereClauseTableWidget);
404  connect(cmbOperator, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
405  std::pair<int, int> pairOperator(expId, 2);
406  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbOperator, pairOperator));
407  copyCombo(m_ui->m_OperatorComboBox, cmbOperator, operatorStr);
408  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 2, cmbOperator);
409 
410  //value combo
411  QComboBox* cmbValue = new QComboBox(m_ui->m_whereClauseTableWidget);
412  connect(cmbValue, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
413  connect(cmbValue, SIGNAL(editTextChanged(QString)), this, SLOT(onComboBoxActivated(QString)));
414  std::pair<int, int> pairValue(expId, 3);
415  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbValue, pairValue));
416  if(m_ui->m_valuePropertyRadioButton->isChecked())
417  copyCombo(m_ui->m_valuePropertyComboBox, cmbValue, valueStr);
418  else
419  {
420  cmbValue->setEditable(true);
421  copyCombo(m_ui->m_valueValueComboBox, cmbValue, valueStr);
422  }
423  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 3, cmbValue);
424  ep->m_valuesComboBox = cmbValue;
425 
426  //connector information
427  QComboBox* connectorCmbBox = new QComboBox(m_ui->m_whereClauseTableWidget);
428  connect(connectorCmbBox, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
429  std::pair<int, int> pairConnector(expId, 4);
430  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(connectorCmbBox, pairConnector));
431  connectorCmbBox->addItems(m_connectorsList);
432  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 4, connectorCmbBox);
433  }
434  else // criteria by spatial restriction
435  {
436  if(m_ui->m_SpatialOperatorComboBox->currentText().isEmpty())
437  {
438  QMessageBox::warning(this, tr("Query Builder"), tr("Operator not defined."));
439  return;
440  }
441  operatorStr = m_ui->m_SpatialOperatorComboBox->currentText().toStdString();
442 
443  restrictValue = m_ui->m_geomAttrComboBox->currentText().toStdString();
444 
445  //get layer
446  int layerIdx = m_ui->m_layerComboBox->currentIndex();
447  QVariant varLayer = m_ui->m_layerComboBox->itemData(layerIdx, Qt::UserRole);
448  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
449 
450  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
451 
453 
454  if(!prop)
455  {
456  QMessageBox::warning(this, tr("Query Builder"), tr("Selected layer has no geometry property."));
457  return;
458  }
459 
460  std::auto_ptr<te::da::DataSet> ds;
461 
462  if(m_ui->m_selectedObjCheckBox->isChecked())
463  {
464  const te::da::ObjectIdSet* selecteds = layer->getSelected();
465 
466  if(!selecteds || selecteds->size() == 0)
467  {
468  QMessageBox::warning(this, tr("Query Builder"), tr("Selected layer has no selected geometries."));
469  return;
470  }
471 
472  ds = layer->getData(selecteds);
473  }
474  else
475  {
476  ds = layer->getData();
477  }
478 
479  ds->moveBeforeFirst();
480 
481  size_t dsSize = ds->size();
482  size_t count = 0;
483 
484  //get all geometries
485  while(ds->moveNext())
486  {
487 
488  int expId = ++m_count;
489 
490  te::gm::Geometry* geom = ds->getGeometry(prop->getName()).release();
491 
492  geom->setSRID(layer->getSRID());
493 
494  //convert
495  if(layer->getSRID() != m_srid)
496  {
497  geom->transform(m_srid);
498  }
499 
500  //valueStr = geom->toString();
501  valueStr = tr("Geometry Value").toStdString();
502 
503  //create expression
504  te::da::LiteralGeom* lGeom = new te::da::LiteralGeom(geom);
505 
507  ep->m_isSpatialCriteria = true;
508  ep->m_property = restrictValue;
509  ep->m_operator = operatorStr;
510  ep->m_value = valueStr;
511  ep->m_expression = lGeom;
512 
513  m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
514 
515  //set connector
516  std::string connector = "";
517 
518  if(count < dsSize - 1)
519  connector = tr("or").toStdString();
520 
521  ++count;
522 
523  //new entry
524  int newrow = m_ui->m_whereClauseTableWidget->rowCount();
525 
526  m_ui->m_whereClauseTableWidget->insertRow(newrow);
527 
528  //remove button
529  QToolButton* removeBtn = new QToolButton(m_ui->m_whereClauseTableWidget);
530  removeBtn->setIcon(QIcon::fromTheme("list-remove"));
531  connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveWhereClausePushButtonClicked()));
532  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 0, removeBtn);
533 
534  //property combo
535  QComboBox* cmbProperty = new QComboBox(m_ui->m_whereClauseTableWidget);
536  connect(cmbProperty, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
537  std::pair<int, int> pairProperty(expId, 1);
538  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbProperty, pairProperty));
539  copyCombo(m_ui->m_geomAttrComboBox, cmbProperty, restrictValue);
540  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 1, cmbProperty);
541 
542  //operator combo
543  QComboBox* cmbOperator = new QComboBox(m_ui->m_whereClauseTableWidget);
544  connect(cmbOperator, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
545  std::pair<int, int> pairOperator(expId, 2);
546  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(cmbOperator, pairOperator));
547  copyCombo(m_ui->m_SpatialOperatorComboBox, cmbOperator, operatorStr);
548  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 2, cmbOperator);
549 
550  //value combo
551  QTableWidgetItem* itemValue = new QTableWidgetItem(valueStr.c_str());
552  itemValue->setData(Qt::UserRole, QVariant(expId));
553  m_ui->m_whereClauseTableWidget->setItem(newrow, 3, itemValue);
554 
555  //connector information
556  QComboBox* connectorCmbBox = new QComboBox(m_ui->m_whereClauseTableWidget);
557  connect(connectorCmbBox, SIGNAL(activated(QString)), this, SLOT(onComboBoxActivated(QString)));
558  std::pair<int, int> pairConnector(expId, 4);
559  m_comboMap.insert(std::map< QComboBox*, std::pair<int, int> >::value_type(connectorCmbBox, pairConnector));
560  connectorCmbBox->addItems(m_connectorsList);
561  m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 4, connectorCmbBox);
562 
563  for(int i = 0; i < connectorCmbBox->count(); ++i)
564  {
565  if(connectorCmbBox->itemText(i).toStdString() == connector)
566  {
567  connectorCmbBox->setCurrentIndex(i);
568  break;
569  }
570  }
571  }
572  }
573 
574  m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
575 
576  //get string sql
577  std::string sql = getWhereString();
578 
579  m_ui->m_sqlTextEdit->setText(sql.c_str());
580 }
581 
583 {
584  QToolButton* button = dynamic_cast<QToolButton*>(sender());
585  if(button)
586  {
587  int row = -1;
588 
589  for(int i = 0; i < m_ui->m_whereClauseTableWidget->rowCount(); ++i)
590  {
591  QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 0);
592  QToolButton* btn = dynamic_cast<QToolButton*>(w);
593  if(button == w)
594  {
595  row = i;
596  break;
597  }
598  }
599 
600  if(row >= 0)
601  m_ui->m_whereClauseTableWidget->removeRow(row);
602 
603  m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
604  }
605 
606  //get string sql
607  std::string sql = getWhereString();
608 
609  m_ui->m_sqlTextEdit->setText(sql.c_str());
610 }
611 
613 {
614  std::string propertyName = m_ui->m_restrictValueComboBox->currentText().toStdString();
615 
616  m_ui->m_valueValueComboBox->clear();
617  m_ui->m_valueValueComboBox->addItems(getPropertyValues(propertyName));
618 }
619 
621 {
622  m_ui->m_whereClauseTableWidget->setRowCount(0);
623  m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
624 }
625 
627 {
628  if(value.isEmpty() || !m_ui->m_valueValueRadioButton->isChecked())
629  return;
630 
631  onValuePropertyRadioButtonClicked();
632 }
633 
635 {
636  QComboBox* cmbBox = dynamic_cast<QComboBox*>(sender());
637  if(!cmbBox)
638  return;
639 
640  int expId = m_comboMap[cmbBox].first;
641  int column = m_comboMap[cmbBox].second;
642 
643  ExpressionProperty* ep = m_mapExp[expId];
644 
645  if(!ep)
646  return;
647 
648  if(ep->m_isAttributeCriteria)
649  {
650  //update expression property
651  if(column == 1) //property
652  {
653  ep->m_property = value.toStdString();
654 
655  if(ep->m_isValueValue)
656  {
657  ep->m_valuesComboBox->clear();
658  ep->m_valuesComboBox->addItems(getPropertyValues(ep->m_property));
659  ep->m_value = ep->m_valuesComboBox->currentText().toStdString();
660  }
661  }
662  else if(column == 2) //operator
663  {
664  ep->m_operator = value.toStdString();
665  }
666  else if(column == 3) //value
667  {
668  ep->m_value = value.toStdString();
669  }
670 
671  //re-create expression
672  te::da::Expression* exp = 0;
673 
674  if(ep->m_isValueValue)
675  {
676  exp = getExpression(ep->m_value.c_str(), ep->m_property);
677  }
678 
679  if(ep->m_isPropertyValue)
680  {
681  exp = new te::da::PropertyName(ep->m_property);
682  }
683 
684  delete ep->m_expression;
685 
686  ep->m_expression = exp;
687  }
688 
689  //get string sql
690  std::string sql = getWhereString();
691 
692  m_ui->m_sqlTextEdit->setText(sql.c_str());
693 }
694 
695 te::da::Expression* te::qt::widgets::WhereClauseWidget::getExpression(const QString& value, const std::string& propName)
696 {
697  std::string dataSetName;
698  std::string dataSetAliasName;
699  std::string propertyName = propName;
700 
701  std::size_t pos = propName.find(".");
702  if(pos != std::string::npos)
703  {
704  dataSetAliasName = propName.substr(0, pos);
705  propertyName = propName.substr(pos + 1, propName.size() - 1);
706  }
707 
708  //get the dataset name
709  if(m_fromItems.size() == 1)
710  {
711  dataSetName = m_fromItems[0].first;
712  }
713  else
714  {
715  for(size_t t = 0; t < m_fromItems.size(); ++t)
716  {
717  if(m_fromItems[t].second == dataSetAliasName)
718  {
719  dataSetName = m_fromItems[t].first;
720  break;
721  }
722  }
723  }
724 
725  if(dataSetName.empty())
726  return 0;
727 
728  //get the dataset property type
729  std::auto_ptr<te::da::DataSetType> dsType = m_ds->getDataSetType(dataSetName);
730 
731  if(dsType.get())
732  {
733  te::dt::Property* prop = dsType->getProperty(propertyName);
734 
735  te::da::Literal* l = 0;
736 
737  if(prop)
738  {
739  if(prop->getType() == te::dt::DOUBLE_TYPE)
740  {
741  l = new te::da::LiteralDouble(value.toDouble());
742  }
743  else if(prop->getType() == te::dt::INT16_TYPE)
744  {
745  l = new te::da::LiteralInt16(value.toInt());
746  }
747  else if(prop->getType() == te::dt::INT32_TYPE)
748  {
749  l = new te::da::LiteralInt32(value.toInt());
750  }
751  else if(prop->getType() == te::dt::STRING_TYPE)
752  {
753  l = new te::da::LiteralString(value.toStdString());
754  }
755  }
756 
757  return l;
758  }
759 
760  return 0;
761 }
762 
763 void te::qt::widgets::WhereClauseWidget::copyCombo(QComboBox* input, QComboBox* output, std::string curValue)
764 {
765  int idx = 0;
766 
767  for(int i = 0; i < input->count(); ++i)
768  {
769  output->addItem(input->itemText(i));
770 
771  if(!curValue.empty() && input->itemText(i).toStdString() == curValue)
772  idx = i;
773  }
774 
775  output->setCurrentIndex(idx);
776 }
777 
778 QStringList te::qt::widgets::WhereClauseWidget::getPropertyValues(std::string propertyName)
779 {
780  QStringList list;
781 
782  if(m_ds.get() == 0)
783  return list;
784 
785  te::da::Fields* fields = new te::da::Fields;
786  te::da::Field* f = new te::da::Field(new te::da::PropertyName(propertyName));
787  fields->push_back(f);
788 
789  te::da::PropertyName* name = new te::da::PropertyName(propertyName);
790 
791  te::da::From* from = new te::da::From;
792 
793  for(size_t t = 0; t < m_fromItems.size(); ++t)
794  {
795  te::da::FromItem* fi = new te::da::DataSetName(m_fromItems[t].first/*, m_fromItems[t].second*/);
796 
797  from->push_back(fi);
798  }
799 
800  te::da::Select* select = new te::da::Select();
801 
802  select->setFields(fields);
803  select->setFrom(from);
804 
805  std::auto_ptr<te::da::DataSet> dataset;
806 
807  try
808  {
809  dataset = m_ds->query(*select);
810  }
811  catch(const std::exception& e)
812  {
813  std::string msg = "An exception has occuried: ";
814  msg += e.what();
815 
816  QMessageBox::warning(0, "Query Error: ", msg.c_str());
817 
818  return list;
819  }
820  catch(...)
821  {
822  std::string msg = "An unexpected exception has occuried!";
823 
824  QMessageBox::warning(0, "Query Error: ", msg.c_str());
825 
826  return list;
827  }
828 
829  std::size_t pos = propertyName.find(".");
830  if(pos != std::string::npos)
831  {
832  std::string dataSetAliasName = propertyName.substr(0, pos);
833  propertyName = propertyName.substr(pos + 1, propertyName.size() - 1);
834  }
835 
836  std::set<std::string> values;
837 
838  if(dataset.get())
839  {
840  while(dataset->moveNext())
841  {
842  if(!dataset->isNull(propertyName))
843  {
844  std::string value = dataset->getAsString(propertyName);
845 
846  std::set<std::string>::iterator it = values.find(value);
847 
848  if(it == values.end())
849  values.insert(value);
850  }
851  }
852  }
853 
854  std::set<std::string>::iterator it = values.begin();
855 
856  while(it != values.end())
857  {
858  list.append((*it).c_str());
859 
860  ++it;
861  }
862 
863  return list;
864 }
void setConnectorsList(const std::vector< std::string > &vec)
void setFromItems(std::vector< std::pair< std::string, std::string > > vec)
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
A visitor for building an SQL statement from a given Query hierarchy.
Definition: SQLVisitor.h:58
WhereClauseWidget(QWidget *parent=0, Qt::WindowFlags f=0)
virtual void setSRID(int srid)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:504
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
A base class for binary functions.
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
void setAttributeList(const std::vector< std::string > &vec)
This class models a string Literal value.
Definition: LiteralString.h:46
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
virtual Expression * clone() const =0
It creates a new copy of this expression.
A class that models the name of any property of an object.
Definition: PropertyName.h:50
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
void setGeomAttributeList(const std::vector< std::string > &vec, int srid)
void setDataSource(const te::da::DataSourcePtr &ds)
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
void setLayerList(std::list< te::map::AbstractLayerPtr > &layerList)
std::size_t size() const
It returns the object id set size.
te::da::Expression * getExpression(const QString &value, const std::string &propName)
void setFields(Fields *f)
It sets the list of output expressions used to form the result set.
Definition: Select.cpp:927
This file has the DataSetWidget class.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
void onRestrictValueComboBoxActivated(QString value)
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
Ui::WhereClauseWidgetForm * getForm() const
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
This class models a literal value.
Definition: Literal.h:53
void copyCombo(QComboBox *input, QComboBox *output, std::string curValue="")
void setSpatialOperatorsList(const std::vector< std::string > &vec)
Expression * getExp() const
Definition: Where.cpp:60
It models a property definition.
Definition: Property.h:59
This is an abstract class that models a query expression.
Definition: Expression.h:47
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
int getType() const
It returns the property data type.
Definition: Property.h:143
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
QStringList getPropertyValues(std::string propertyName)
virtual void transform(int srid)=0
It converts the coordinate values of the geometry to the new spatial reference system.
std::auto_ptr< Ui::WhereClauseWidgetForm > m_ui
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
A class that models a literal for double values.
Definition: LiteralDouble.h:43
Geometric property.
void setFrom(From *f)
It sets the list of source information.
Definition: Select.cpp:937
void setOperatorsList(const std::vector< std::string > &vec)