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"
55 #include <QtGui/QIcon>
56 #include <QtGui/QMessageBox>
57 #include <QtGui/QToolButton>
64 m_ui(new Ui::WhereClauseWidgetForm)
71 m_ui->m_addWhereClausePushButton->setIcon(QIcon::fromTheme(
"list-add"));
72 m_ui->m_clearAllPushButton->setIcon(QIcon::fromTheme(
"edit-clear"));
96 int row = m_ui->m_whereClauseTableWidget->rowCount();
102 std::string lastConnectorName =
"";
104 for(
int i = 0; i < row; ++i)
107 std::string propName =
"";
108 QTableWidgetItem* itemPropName = m_ui->m_whereClauseTableWidget->item(i, 1);
110 propName = itemPropName->text().toStdString();
113 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 1);
114 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
116 propName = cmbBox->currentText().toStdString();
119 std::string funcName =
"";
120 QTableWidgetItem* itemFuncName = m_ui->m_whereClauseTableWidget->item(i, 2);
122 funcName = itemFuncName->text().toStdString();
125 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 2);
126 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
128 funcName = cmbBox->currentText().toStdString();
132 QTableWidgetItem* itemValue = m_ui->m_whereClauseTableWidget->item(i, 3);
135 expIdx = itemValue->data(Qt::UserRole).toInt();
139 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 3);
140 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
142 expIdx = m_comboMap[cmbBox].first;
154 if(leftSide != 0 && lastConnectorName.empty() ==
false)
157 leftSide = connectFunc;
165 QTableWidgetItem* itemConnectorName = m_ui->m_whereClauseTableWidget->item(i, 4);
166 if(itemConnectorName)
168 if(itemPropName->text().isEmpty() ==
false)
169 lastConnectorName = itemConnectorName->text().toStdString();
171 lastConnectorName =
"";
175 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 4);
176 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(w);
178 lastConnectorName = cmbBox->currentText().toStdString();
189 std::string sql =
"";
221 m_ui->m_layerComboBox->clear();
223 std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
225 while(it != layerList.end())
229 m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
242 m_ui->m_restrictValueComboBox->clear();
243 m_ui->m_valuePropertyComboBox->clear();
245 for(
size_t t = 0; t <vec.size(); ++t)
247 m_ui->m_restrictValueComboBox->addItem(vec[t].c_str());
248 m_ui->m_valuePropertyComboBox->addItem(vec[t].c_str());
256 m_ui->m_geomAttrComboBox->clear();
258 for(
size_t t = 0; t <vec.size(); ++t)
260 m_ui->m_geomAttrComboBox->addItem(vec[t].c_str());
266 m_ui->m_OperatorComboBox->clear();
268 for(
size_t t = 0; t <vec.size(); ++t)
270 m_ui->m_OperatorComboBox->addItem(vec[t].c_str());
276 m_ui->m_SpatialOperatorComboBox->clear();
278 for(
size_t t = 0; t <vec.size(); ++t)
280 m_ui->m_SpatialOperatorComboBox->addItem(vec[t].c_str());
286 m_connectorsList.clear();
288 m_connectorsList.append(
"");
290 for(
size_t t = 0; t <vec.size(); ++t)
292 m_connectorsList.append(vec[t].c_str());
304 m_ui->m_whereClauseTableWidget->setRowCount(0);
311 std::string restrictValue =
"";
312 std::string operatorStr =
"";
313 std::string valueStr =
"";
315 if(m_ui->m_criteriaTabWidget->currentIndex() == 0)
317 int expId = ++m_count;
319 if(m_ui->m_restrictValueComboBox->currentText().isEmpty())
321 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Restrict value not defined."));
325 if(m_ui->m_valuePropertyRadioButton->isChecked() ==
false &&
326 m_ui->m_valueValueRadioButton->isChecked() ==
false)
328 if(m_ui->m_valueValueComboBox->currentText().isEmpty())
330 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Value not defined."));
335 if(m_ui->m_valueValueRadioButton->isChecked())
337 if(m_ui->m_valueValueComboBox->currentText().isEmpty())
339 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Value not defined."));
344 restrictValue = m_ui->m_restrictValueComboBox->currentText().toStdString();
346 if(m_ui->m_OperatorComboBox->currentText().isEmpty())
348 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Operator not defined."));
351 operatorStr = m_ui->m_OperatorComboBox->currentText().toStdString();
358 if(m_ui->m_valuePropertyRadioButton->isChecked())
360 valueStr = m_ui->m_valuePropertyComboBox->currentText().toStdString();
368 m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
370 else if(m_ui->m_valueValueRadioButton->isChecked())
372 valueStr = m_ui->m_valueValueComboBox->currentText().toStdString();
374 te::da::Expression* exp = getExpression(m_ui->m_valueValueComboBox->currentText(), restrictValue);
380 m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
384 int newrow = m_ui->m_whereClauseTableWidget->rowCount();
386 m_ui->m_whereClauseTableWidget->insertRow(newrow);
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);
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);
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);
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);
420 cmbValue->setEditable(
true);
421 copyCombo(m_ui->m_valueValueComboBox, cmbValue, valueStr);
423 m_ui->m_whereClauseTableWidget->setCellWidget(newrow, 3, cmbValue);
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);
436 if(m_ui->m_SpatialOperatorComboBox->currentText().isEmpty())
438 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Operator not defined."));
441 operatorStr = m_ui->m_SpatialOperatorComboBox->currentText().toStdString();
443 restrictValue = m_ui->m_geomAttrComboBox->currentText().toStdString();
446 int layerIdx = m_ui->m_layerComboBox->currentIndex();
447 QVariant varLayer = m_ui->m_layerComboBox->itemData(layerIdx, Qt::UserRole);
450 std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
456 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Selected layer has no geometry property."));
460 std::auto_ptr<te::da::DataSet> ds;
462 if(m_ui->m_selectedObjCheckBox->isChecked())
466 if(!selecteds || selecteds->
size() == 0)
468 QMessageBox::warning(
this, tr(
"Query Builder"), tr(
"Selected layer has no selected geometries."));
472 ds = layer->getData(selecteds);
476 ds = layer->getData();
479 ds->moveBeforeFirst();
481 size_t dsSize = ds->size();
485 while(ds->moveNext())
488 int expId = ++m_count;
492 geom->
setSRID(layer->getSRID());
495 if(layer->getSRID() != m_srid)
501 valueStr = tr(
"Geometry Value").toStdString();
513 m_mapExp.insert(std::map<int, ExpressionProperty*>::value_type(expId, ep));
516 std::string connector =
"";
518 if(count < dsSize - 1)
519 connector = tr(
"or").toStdString();
524 int newrow = m_ui->m_whereClauseTableWidget->rowCount();
526 m_ui->m_whereClauseTableWidget->insertRow(newrow);
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);
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);
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);
551 QTableWidgetItem* itemValue =
new QTableWidgetItem(valueStr.c_str());
552 itemValue->setData(Qt::UserRole, QVariant(expId));
553 m_ui->m_whereClauseTableWidget->setItem(newrow, 3, itemValue);
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);
563 for(
int i = 0; i < connectorCmbBox->count(); ++i)
565 if(connectorCmbBox->itemText(i).toStdString() == connector)
567 connectorCmbBox->setCurrentIndex(i);
574 m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
577 std::string sql = getWhereString();
579 m_ui->m_sqlTextEdit->setText(sql.c_str());
584 QToolButton* button =
dynamic_cast<QToolButton*
>(sender());
589 for(
int i = 0; i < m_ui->m_whereClauseTableWidget->rowCount(); ++i)
591 QWidget* w = m_ui->m_whereClauseTableWidget->cellWidget(i, 0);
592 QToolButton* btn =
dynamic_cast<QToolButton*
>(w);
601 m_ui->m_whereClauseTableWidget->removeRow(row);
603 m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
607 std::string sql = getWhereString();
609 m_ui->m_sqlTextEdit->setText(sql.c_str());
614 std::string propertyName = m_ui->m_restrictValueComboBox->currentText().toStdString();
616 m_ui->m_valueValueComboBox->clear();
617 m_ui->m_valueValueComboBox->addItems(getPropertyValues(propertyName));
622 m_ui->m_whereClauseTableWidget->setRowCount(0);
623 m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
628 if(value.isEmpty() || !m_ui->m_valueValueRadioButton->isChecked())
631 onValuePropertyRadioButtonClicked();
636 QComboBox* cmbBox =
dynamic_cast<QComboBox*
>(sender());
640 int expId = m_comboMap[cmbBox].first;
641 int column = m_comboMap[cmbBox].second;
668 ep->
m_value = value.toStdString();
690 std::string sql = getWhereString();
692 m_ui->m_sqlTextEdit->setText(sql.c_str());
697 std::string dataSetName;
698 std::string dataSetAliasName;
699 std::string propertyName = propName;
701 std::size_t pos = propName.find(
".");
702 if(pos != std::string::npos)
704 dataSetAliasName = propName.substr(0, pos);
705 propertyName = propName.substr(pos + 1, propName.size() - 1);
709 if(m_fromItems.size() == 1)
711 dataSetName = m_fromItems[0].first;
715 for(
size_t t = 0; t < m_fromItems.size(); ++t)
717 if(m_fromItems[t].second == dataSetAliasName)
719 dataSetName = m_fromItems[t].first;
725 if(dataSetName.empty())
729 std::auto_ptr<te::da::DataSetType> dsType = m_ds->getDataSetType(dataSetName);
767 for(
int i = 0; i < input->count(); ++i)
769 output->addItem(input->itemText(i));
771 if(!curValue.empty() && input->itemText(i).toStdString() == curValue)
775 output->setCurrentIndex(idx);
787 fields->push_back(f);
793 for(
size_t t = 0; t < m_fromItems.size(); ++t)
805 std::auto_ptr<te::da::DataSet> dataset;
809 dataset = m_ds->query(*select);
811 catch(
const std::exception& e)
813 std::string msg =
"An exception has occuried: ";
816 QMessageBox::warning(0,
"Query Error: ", msg.c_str());
822 std::string msg =
"An unexpected exception has occuried!";
824 QMessageBox::warning(0,
"Query Error: ", msg.c_str());
829 std::size_t pos = propertyName.find(
".");
830 if(pos != std::string::npos)
832 std::string dataSetAliasName = propertyName.substr(0, pos);
833 propertyName = propertyName.substr(pos + 1, propertyName.size() - 1);
836 std::set<std::string> values;
840 while(dataset->moveNext())
842 if(!dataset->isNull(propertyName))
844 std::string value = dataset->getAsString(propertyName);
846 std::set<std::string>::iterator it = values.find(value);
848 if(it == values.end())
849 values.insert(value);
854 std::set<std::string>::iterator it = values.begin();
856 while(it != values.end())
858 list.append((*it).c_str());
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...
A class that models the name of a dataset used in a From clause.
A visitor for building an SQL statement from a given Query hierarchy.
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)
A Select models a query to be used when retrieving data from a DataSource.
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...
void onClearAllPushButtonClicked()
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...
void setAttributeList(const std::vector< std::string > &vec)
This class models a string Literal value.
te::da::Where * getWhere()
void onAddWhereClausePushButtonClicked()
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
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.
bool m_isAttributeCriteria
A class that models a literal for Geometry values.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
void onRemoveWhereClausePushButtonClicked()
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.
te::da::Expression * m_expression
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.
This file has the DataSetWidget class.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void onRestrictValueComboBoxActivated(QString value)
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
Ui::WhereClauseWidgetForm * getForm() const
boost::shared_ptr< DataSource > DataSourcePtr
This class models a literal value.
void copyCombo(QComboBox *input, QComboBox *output, std::string curValue="")
void setSpatialOperatorsList(const std::vector< std::string > &vec)
Expression * getExp() const
std::string getWhereString()
It models a property definition.
This is an abstract class that models a query expression.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
void onComboBoxActivated(QString value)
int getType() const
It returns the property data type.
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.
te::da::DataSourcePtr m_ds
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.
QComboBox * m_valuesComboBox
A class that models a literal for double values.
void setFrom(From *f)
It sets the list of source information.
void onValuePropertyRadioButtonClicked()
void setOperatorsList(const std::vector< std::string > &vec)