QueryLayerBuilderWizard.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/query/QueryLayerBuilderWizard.cpp
22 
23  \brief A Qt dialog that allows users to create query builder based on TerraLib query framework.
24 */
25 
26 // TerraLib
28 
29 // TerraLib
30 #include "../../../dataaccess/dataset/DataSet.h"
31 #include "../../../dataaccess/dataset/DataSetType.h"
32 #include "../../../dataaccess/datasource/DataSource.h"
33 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
34 #include "../../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../../dataaccess/query/QueryCapabilities.h"
36 #include "../../../dataaccess/query/Select.h"
37 #include "../../../dataaccess/utils/Utils.h"
38 #include "../../../geometry/GeometryProperty.h"
39 #include "../../../maptools/QueryLayer.h"
40 #include "../../../se/Utils.h"
41 #include "../../widgets/help/HelpPushButton.h"
42 #include "../datasource/selector/DataSourceSelectorWizardPage.h"
43 #include "../datasource/selector/DataSourceSelectorWidget.h"
44 #include "../utils/DoubleListWidget.h"
45 #include "DataSetWidget.h"
46 #include "DataSetWizardPage.h"
47 #include "FieldsWizardPage.h"
48 #include "GroupByWizardPage.h"
50 #include "OrderByWidget.h"
51 #include "OrderByWizardPage.h"
52 #include "WhereClauseWidget.h"
53 #include "WhereClauseWizardPage.h"
54 
55 // STL
56 #include <cassert>
57 
58 // Qt
59 #include <QMessageBox>
60 #include <QIcon>
61 
62 // Boost
63 #include <boost/uuid/random_generator.hpp>
64 #include <boost/uuid/uuid_io.hpp>
65 
66 
68  : QWizard(parent)
69 {
70  m_ds.reset();
71 
72  //configure the wizard
73  this->setWizardStyle(QWizard::ModernStyle);
74  //this->setFixedSize(640, 480);
75  this->setWindowTitle(tr("Query Layer Builder"));
76 
77  this->setOption(QWizard::HaveHelpButton, true);
78  this->setOption(QWizard::HelpButtonOnRight, false);
79 
81 
82  this->setButton(QWizard::HelpButton, helpButton);
83 
84  helpButton->setPageReference("widgets/query/query_layer.html");
85 
86  addPages();
87 }
88 
90 
92 {
93  if(currentPage() == m_dataSourcePage.get())
94  {
95  std::list<te::da::DataSourceInfoPtr> list = m_dataSourcePage->getSelectorWidget()->getSelecteds();
96 
97  if(list.empty())
98  {
99  return false;
100  }
101 
102  te::da::DataSourceInfoPtr dsInfo = *list.begin();
103  te::da::DataSourcePtr dataSource = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getType(), dsInfo->getConnInfo());
104 
105  if(!dataSource->isOpened())
106  dataSource->open();
107 
108  setDataSource(dataSource);
109 
110  return m_dataSourcePage->isComplete();
111  }
112  else if(currentPage() == m_dataSetPage.get())
113  {
114  getProperties();
115 
116  //used to get distinct values from a selected property
117  std::vector<std::pair<std::string, std::string> > vec;
118  m_dataSetPage->getWidget()->getDataSetNames(vec);
119  m_whereClausePage->getWidget()->setFromItems(vec);
120 
121  return m_dataSetPage->isComplete();
122  }
123  else if(currentPage() == m_fieldPage.get())
124  {
125  return m_fieldPage->isComplete();
126  }
127  else if(currentPage() == m_whereClausePage.get())
128  {
129  return m_whereClausePage->isComplete();
130  }
131  else if(currentPage() == m_groupByPage.get())
132  {
133  return m_groupByPage->isComplete();
134  }
135  else if(currentPage() == m_orderByPage.get())
136  {
137  return m_orderByPage->isComplete();
138  }
139  else if(currentPage() == m_layerAttrPage.get())
140  {
141  bool res = m_layerAttrPage->isComplete();
142 
143  if(!res)
144  QMessageBox::warning(this, tr("Warning"), tr("Layer name not defined."));
145 
146  return res;
147  }
148 
149  return false;
150 }
151 
153 {
154  assert(ds);
155 
156  m_ds = ds;
157 
158  m_whereClausePage->getWidget()->setDataSource(m_ds);
159 
160  getDataSets();
161 
163 }
164 
165 void te::qt::widgets::QueryLayerBuilderWizard::setLayerList(std::list<te::map::AbstractLayerPtr>& layerList)
166 {
167  if(m_whereClausePage.get())
168  m_whereClausePage->getWidget()->setLayerList(layerList);
169 }
170 
172 {
173  //fields
174  te::da::Fields* fields = m_fieldPage->getFields();
175 
176  //from
177  te::da::From* from = m_dataSetPage->getWidget()->getFrom();
178 
179  //where
180  te::da::Where* w = m_whereClausePage->getWidget()->getWhere();
181 
182  //groupby
183  te::da::GroupBy* groupBy = m_groupByPage->getGroupBy();
184 
185  //order
186  te::da::OrderBy* orderBy = m_orderByPage->getWidget()->gerOrderBy();
187 
188  //build the select object
189  te::da::Select s(fields, from, w, groupBy, orderBy);
190 
191  return s;
192 }
193 
195 {
196  static boost::uuids::basic_random_generator<boost::mt19937> gen;
197  boost::uuids::uuid u = gen();
198  std::string id = boost::uuids::to_string(u);
199 
200  std::string title = m_layerAttrPage->getLayerName();
201 
203 
204  te::map::QueryLayerPtr layer(new te::map::QueryLayer(id, title));
205  layer->setDataSourceId(m_ds->getId());
206  layer->setRendererType("QUERY_LAYER_RENDERER");
207  layer->setQuery(s);
208 
209  // SRID
210  std::unique_ptr<const te::map::LayerSchema> schema(layer->getSchema());
212  if (gp)
213  {
214  layer->computeExtent();
215  layer->setSRID(gp->getSRID());
216 
217  // style
218  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
219  }
220 
221  return layer;
222 }
223 
225 {
233 
234  addPage(m_dataSourcePage.get());
235  addPage(m_dataSetPage.get());
236  addPage(m_fieldPage.get());
237  addPage(m_whereClausePage.get());
238  addPage(m_groupByPage.get());
239  addPage(m_orderByPage.get());
240  addPage(m_layerAttrPage.get());
241 }
242 
244 {
245  std::string dsId = m_ds->getId();
246 
247  std::vector<std::string> datasetNames;
248 
249  te::da::GetDataSetNames(datasetNames, dsId);
250 
251  m_dataSetPage->getWidget()->setDataSetNames(datasetNames);
252 }
253 
255 {
256  //get the dataset names
257  std::vector<std::string> datasetNames = m_ds->getDataSetNames();
258 
259  std::vector<std::pair<std::string, std::string> > dataSetSelecteds;
260 
261  m_dataSetPage->getWidget()->getDataSetNames(dataSetSelecteds);
262 
263  std::vector<std::string> inputProperties;
264  std::vector<std::string> geomProperties;
265 
266  int srid = 0;
267 
268  //get properties for each data set
269  for(size_t t = 0; t < dataSetSelecteds.size(); ++t)
270  {
271  //alias name
272  std::string alias = dataSetSelecteds[t].second;
273 
274  //data set name
275  std::string dataSetName = dataSetSelecteds[t].first;
276 
277  //get datasettype
278  std::unique_ptr<te::da::DataSetType> dsType;
279 
280  for(unsigned int i = 0; i < datasetNames.size(); ++i)
281  {
282  if(datasetNames[i] == dataSetName)
283  dsType = m_ds->getDataSetType(datasetNames[i]);
284  }
285 
286  if(dsType.get())
287  {
288  for(size_t i = 0; i < dsType->size(); ++i)
289  {
290  std::string propName = dsType->getProperty(i)->getName();
291  std::string fullName = alias + "." + propName;
292 
293  if(dsType->getProperty(i)->getType() == te::dt::GEOMETRY_TYPE)
294  {
295  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(dsType->getProperty(i));
296 
297  if(geomProp)
298  srid = geomProp->getSRID();
299 
300  geomProperties.push_back(fullName);
301  }
302  else
303  inputProperties.push_back(fullName);
304  }
305  }
306  }
307 
308  //set values in other pages
309  m_fieldPage->getWidget()->setInputValues(inputProperties);
310  m_fieldPage->getWidget()->clearOutputValues();
311  m_fieldPage->getWidget()->setFixedOutputValues(geomProperties, "geometry");
312  m_groupByPage->getWidget()->setInputValues(inputProperties);
313  m_whereClausePage->getWidget()->setAttributeList(inputProperties);
314  m_whereClausePage->getWidget()->setGeomAttributeList(geomProperties, srid);
315  m_orderByPage->getWidget()->setAttributeList(inputProperties);
316 }
317 
319 {
320 
321  te::da::DataSourceCapabilities dsCap = m_ds->getCapabilities();
322 
324 
325  std::vector<std::string> vecOperators;
326 
327  std::set<std::string>::iterator it;
328 
329  //Arithmetic Operators
330  it = queryCap.getArithmeticOperators().begin();
331 
332  while(it != queryCap.getArithmeticOperators().end())
333  {
334  vecOperators.push_back(*it);
335 
336  ++it;
337  }
338 
339  //Comparsion Operators
340  it = queryCap.getComparsionOperators().begin();
341 
342  while(it != queryCap.getComparsionOperators().end())
343  {
344  vecOperators.push_back(*it);
345 
346  ++it;
347  }
348 
349  m_whereClausePage->getWidget()->setOperatorsList(vecOperators);
350 
351  //Spatial Operators
352  std::vector<std::string> vecSpatialOperators;
353 
354  it = queryCap.getSpatialTopologicOperators().begin();
355 
356  while(it != queryCap.getSpatialTopologicOperators().end())
357  {
358  vecSpatialOperators.push_back(*it);
359 
360  ++it;
361  }
362 
363  m_whereClausePage->getWidget()->setSpatialOperatorsList(vecSpatialOperators);
364 
365  //Logical Operators
366  std::vector<std::string> vecConnectors;
367 
368  it = queryCap.getLogicalOperators().begin();
369 
370  while(it != queryCap.getLogicalOperators().end())
371  {
372  vecConnectors.push_back(*it);
373 
374  ++it;
375  }
376 
377  m_whereClausePage->getWidget()->setConnectorsList(vecConnectors);
378 }
const std::set< std::string > & getComparsionOperators() const
This class is a wizard page with the DoubleListWidget component, used to create the FIELD object of a...
This file has the WhereClauseWizardPage class.
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
Geometric property.
std::unique_ptr< te::qt::widgets::WhereClauseWizardPage > m_whereClausePage
std::unique_ptr< te::qt::widgets::FieldsWizardPage > m_fieldPage
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
boost::shared_ptr< DataSource > DataSourcePtr
A Qt dialog that allows users to create query builder based on TerraLib query framework.
void setPageReference(const QString &ref)
Sets the documentation page reference.
This class is a wizard page with the WhereClauseWidget component.
This file has the FieldsWizardPage class.
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
const std::set< std::string > & getArithmeticOperators() const
A layer resulting from a query.
Definition: QueryLayer.h:50
static te::dt::Date ds(2010, 01, 01)
This file has the OrderByWizardPage class.
const QueryCapabilities & getQueryCapabilities() const
std::unique_ptr< te::qt::widgets::LayerAttributesWizardPage > m_layerAttrPage
std::unique_ptr< te::qt::widgets::DataSetWizardPage > m_dataSetPage
void setDataSource(const te::da::DataSourcePtr &ds)
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
const std::set< std::string > & getLogicalOperators() const
std::unique_ptr< te::qt::widgets::DataSourceSelectorWizardPage > m_dataSourcePage
This file has the GroupByWizardPage class.
A class that informs the query support of a given data source.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
This file has the OrderByWidget class.
This file has the DataSetWidget class.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
std::unique_ptr< te::qt::widgets::GroupByWizardPage > m_groupByPage
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
const std::set< std::string > & getSpatialTopologicOperators() const
This class is used to define the layer attributes information.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
This file has the DataSetWidget class.
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
void setLayerList(std::list< te::map::AbstractLayerPtr > &layerList)
boost::intrusive_ptr< QueryLayer > QueryLayerPtr
Definition: QueryLayer.h:170
This class is a wizard page with the DoubleListWidget component, used to create the GROUPBY object of...
This class is a wizard page with the DataSetWidget component.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
std::unique_ptr< te::qt::widgets::OrderByWizardPage > m_orderByPage
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
This file has the DataSetWizardPage class.
This file has the LayerAttributesWizardPage class.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr