All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryDialog.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/QueryDialog.h
22 
23  \brief This file defines a class for a Query Dialog Dialog.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSet.h"
28 #include "../../../dataaccess/dataset/DataSetType.h"
29 #include "../../../dataaccess/dataset/ObjectIdSet.h"
30 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
31 #include "../../../dataaccess/query/FunctionNames.h"
32 #include "../../../dataaccess/query/QueryCapabilities.h"
33 #include "../../../dataaccess/utils/Utils.h"
34 #include "../../../maptools/DataSetLayer.h"
35 #include "../utils/ColorPickerToolButton.h"
36 #include "QueryDialog.h"
37 #include "WhereClauseWidget.h"
38 #include "ui_QueryDialogForm.h"
39 
40 // Qt
41 #include <QtGui/QGridLayout>
42 #include <QtGui/QMessageBox>
43 
44 // STL
45 #include <cassert>
46 #include <memory>
47 
49 
50 te::qt::widgets::QueryDialog::QueryDialog(QWidget* parent, Qt::WindowFlags f)
51  : QDialog(parent, f),
52  m_ui(new Ui::QueryDialogForm)
53 {
54  m_ui->setupUi(this);
55 
56  // Build form
57  QGridLayout* layout = new QGridLayout(m_ui->m_widget);
59  layout->addWidget(m_whereClauseWidget.get());
60  layout->setContentsMargins(0, 0, 0, 0);
61 
62  // Color Picker
64  m_colorPicker->setFixedSize(70, 24);
65  m_colorPicker->setColor(QColor(255, 255, 0, 128));
66 
67  // Adjusting...
68  QGridLayout* colorPickerLayout = new QGridLayout(m_ui->m_colorPickerFrame);
69  colorPickerLayout->setContentsMargins(0, 0, 0, 0);
70  colorPickerLayout->addWidget(m_colorPicker);
71 
72  // Signals¨& slots
73  connect(m_ui->m_inputLayerComboBox, SIGNAL(activated(QString)), this, SLOT(onInputLayerActivated(QString)));
74  connect(m_ui->m_applyPushButton, SIGNAL(clicked()), this, SLOT(onApplyPushButtonClicked()));
75 
76  //
77  m_ui->m_helpPushButton->setPageReference("widgets/query/query_dialog.html");
78 }
79 
81 {
82 }
83 
85 {
86  return m_whereClauseWidget.get();
87 }
88 
89 void te::qt::widgets::QueryDialog::setList(std::list<te::map::AbstractLayerPtr>& layerList)
90 {
91  m_whereClauseWidget->setLayerList(layerList);
92 
93  m_ui->m_inputLayerComboBox->clear();
94 
95  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
96 
97  while(it != layerList.end())
98  {
100 
101  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(l.get());
102 
103  if(dsLayer)
104  m_ui->m_inputLayerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
105 
106  ++it;
107  }
108 
109  if(m_ui->m_inputLayerComboBox->count() > 0)
110  {
111  QString s = m_ui->m_inputLayerComboBox->currentText();
112 
113  onInputLayerActivated(s);
114  }
115 }
116 
118 {
119  for(int i = 0; i < m_ui->m_inputLayerComboBox->count(); ++i)
120  {
121  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(i, Qt::UserRole);
123 
124  if(layer == l)
125  {
126  m_ui->m_inputLayerComboBox->setCurrentIndex(i);
127  break;
128  }
129  }
130 
131  if(m_ui->m_inputLayerComboBox->count() > 0)
132  {
133  QString s = m_ui->m_inputLayerComboBox->currentText();
134 
135  onInputLayerActivated(s);
136  }
137 }
138 
140 {
141  return m_whereClauseWidget->getWhere();
142 }
143 
145 {
146  m_whereClauseWidget->clear();
147 
148  // Gets the input layer
149  int idxLayer = m_ui->m_inputLayerComboBox->currentIndex();
150 
151  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(idxLayer, Qt::UserRole);
152 
153  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
154 
155  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(layer.get());
156 
157  // Sets the DataSource
158  std::string dataSourceName = dsLayer->getDataSourceId();
159 
160  te::da::DataSourcePtr dsPtr = te::da::GetDataSource(dataSourceName);
161 
162  if(!dsPtr->isOpened())
163  dsPtr->open();
164 
165  m_whereClauseWidget->setDataSource(dsPtr);
166 
167  // Gets the capabilities
168  std::vector<std::string> vecOperators;
169  std::vector<std::string> vecSpatialOperators;
170  std::vector<std::string> vecConnectors;
171 
172  te::da::DataSourceCapabilities dsCap = dsPtr->getCapabilities();
174 
175  std::set<std::string>::iterator it;
176 
177  it = queryCap.getArithmeticOperators().begin();
178  while(it != queryCap.getArithmeticOperators().end())
179  {
180  vecOperators.push_back(*it);
181  ++it;
182  }
183 
184  it = queryCap.getComparsionOperators().begin();
185  while(it != queryCap.getComparsionOperators().end())
186  {
187  vecOperators.push_back(*it);
188  ++it;
189  }
190 
191  m_whereClauseWidget->setOperatorsList(vecOperators);
192 
193  it = queryCap.getSpatialTopologicOperators().begin();
194  while(it != queryCap.getSpatialTopologicOperators().end())
195  {
196  vecSpatialOperators.push_back(*it);
197  ++it;
198  }
199 
200  // For while! TODO: Revision!!!
201  if(vecSpatialOperators.size() == 1 && vecSpatialOperators[0] == te::da::FunctionNames::sm_ST_EnvelopeIntersects)
202  {
203  vecSpatialOperators.clear();
204  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Intersects);
205  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Disjoint);
206  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Touches);
207  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Overlaps);
208  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Crosses);
209  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Within);
210  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Contains);
211  vecSpatialOperators.push_back(te::da::FunctionNames::sm_ST_Equals);
212  }
213 
214  m_whereClauseWidget->setSpatialOperatorsList(vecSpatialOperators);
215 
216  it = queryCap.getLogicalOperators().begin();
217  while(it != queryCap.getLogicalOperators().end())
218  {
219  vecConnectors.push_back(*it);
220  ++it;
221  }
222 
223  m_whereClauseWidget->setConnectorsList(vecConnectors);
224 
225  // Gets data set information
226  std::auto_ptr<te::da::DataSetType> dsType = dsLayer->getSchema();
227 
228  std::string dsName = dsType->getName();
229  std::string aliasName = dsName;
230 
231  std::vector<std::pair<std::string, std::string> > list;
232  list.push_back(std::pair<std::string, std::string>(dsName, aliasName));
233 
234  m_whereClauseWidget->setFromItems(list);
235 
236  // Get properties
237  std::vector<std::string> inputProperties;
238  std::vector<std::string> geomProperties;
239 
240  for(size_t i = 0; i < dsType->size(); ++i)
241  {
242  std::string propName = dsType->getProperty(i)->getName();
243 
244  if(dsType->getProperty(i)->getType() == te::dt::GEOMETRY_TYPE)
245  geomProperties.push_back(propName);
246  else
247  inputProperties.push_back(propName);
248  }
249 
250  m_whereClauseWidget->setAttributeList(inputProperties);
251  m_whereClauseWidget->setGeomAttributeList(geomProperties, dsLayer->getSRID());
252 }
253 
255 {
256  // Gets the defined restriction
257  te::da::Where* wh = getWhere();
258  if(wh == 0 || wh->getExp() == 0)
259  {
260  QMessageBox::information(this, tr("Query"), tr("Add a restriction expression first."));
261  return;
262  }
263 
264  // Gets the selected layer
265  int index = m_ui->m_inputLayerComboBox->currentIndex();
266 
267  QVariant varLayer = m_ui->m_inputLayerComboBox->itemData(index, Qt::UserRole);
268 
269  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
270 
271  assert(layer.get());
272 
273  // Let's execute the query
274  try
275  {
276  setCursor(Qt::WaitCursor);
277 
278  // The filter expression
279  te::da::Expression* e = wh->getExp()->clone();
280 
281  // Gets the layer schema
282  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
283 
284  // Gets the dataset
285  std::auto_ptr<te::da::DataSet> dataset = layer->getData(e);
286  assert(dataset.get());
287 
288  if(m_ui->m_newSelRadioButton->isChecked())
289  {
290  // Generates the oids
291  dataset->moveBeforeFirst();
292  te::da::ObjectIdSet* oids = te::da::GenerateOIDSet(dataset.get(), schema.get());
293 
294  layer->clearSelected();
295  layer->select(oids);
296 
297  emit layerSelectedObjectsChanged(layer);
298  }
299  else if(m_ui->m_addSelRadioButton->isChecked())
300  {
301  // Generates the oids
302  dataset->moveBeforeFirst();
303  te::da::ObjectIdSet* oids = te::da::GenerateOIDSet(dataset.get(), schema.get());
304 
305  layer->select(oids);
306 
307  emit layerSelectedObjectsChanged(layer);
308  }
309 
310  emit highlightLayerObjects(layer, dataset.get(), m_colorPicker->getColor());
311 
312  setCursor(Qt::ArrowCursor);
313 
314  //QMessageBox::information(this, tr("Query"), tr("Query executed with successfully."));
315  }
316  catch(std::exception& e)
317  {
318  setCursor(Qt::ArrowCursor);
319  QMessageBox::information(this, tr("Query"), e.what());
320  }
321 }
static const std::string sm_ST_Contains
Definition: FunctionNames.h:77
static const std::string sm_ST_Intersects
Definition: FunctionNames.h:80
static const std::string sm_ST_Disjoint
Definition: FunctionNames.h:70
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:362
This file defines a class for a Query Dialog Dialog.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
ColorPickerToolButton * m_colorPicker
Definition: QueryDialog.h:106
te::da::Where * getWhere()
static const std::string sm_ST_Crosses
Definition: FunctionNames.h:76
static const std::string sm_ST_Equals
Definition: FunctionNames.h:69
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:258
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
This method is used to set the list of layers.
Definition: QueryDialog.cpp:89
void setCurrentLayer(te::map::AbstractLayerPtr layer)
const QueryCapabilities & getQueryCapabilities() const
virtual Expression * clone() const =0
It creates a new copy of this expression.
const std::set< std::string > & getLogicalOperators() const
static const std::string sm_ST_Overlaps
Definition: FunctionNames.h:75
const std::string & getDataSourceId() const
std::auto_ptr< Ui::QueryDialogForm > m_ui
Definition: QueryDialog.h:104
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
const std::set< std::string > & getSpatialTopologicOperators() const
This file has the DataSetWidget class.
This class is used to define the WHERE object of select operation.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
QueryDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: QueryDialog.cpp:50
A class that informs the query support of a given data source.
static const std::string sm_ST_Touches
Definition: FunctionNames.h:73
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
const std::set< std::string > & getArithmeticOperators() const
void onInputLayerActivated(QString value)
Expression * getExp() const
Definition: Where.cpp:60
This is an abstract class that models a query expression.
Definition: Expression.h:47
Custom tool button used to pick a color.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
te::qt::widgets::WhereClauseWidget * getWidget()
Definition: QueryDialog.cpp:84
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
static const std::string sm_ST_EnvelopeIntersects
Definition: FunctionNames.h:81
std::auto_ptr< te::qt::widgets::WhereClauseWidget > m_whereClauseWidget
Definition: QueryDialog.h:105
static const std::string sm_ST_Within
Definition: FunctionNames.h:74
const std::set< std::string > & getComparsionOperators() const