All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Selection.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/tools/Selection.cpp
22 
23  \brief This class implements a concrete tool to select layer features using an extent.
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/utils/Utils.h"
32 #include "../../../geometry/Coord2D.h"
33 #include "../../../geometry/Geometry.h"
34 #include "../../../geometry/GeometryProperty.h"
35 #include "../../../geometry/Point.h"
36 #include "../../../geometry/Utils.h"
37 #include "../../../srs/Config.h"
38 #include "../canvas/MapDisplay.h"
39 #include "../utils/ScopedCursor.h"
40 #include "Selection.h"
41 
42 // Qt
43 #include <QtCore/QString>
44 #include <QKeyEvent>
45 #include <QMessageBox>
46 #include <QMouseEvent>
47 
48 // STL
49 #include <cassert>
50 #include <exception>
51 #include <memory>
52 
53 te::qt::widgets::Selection::Selection(te::qt::widgets::MapDisplay* display, const QCursor& cursor, const std::list<te::map::AbstractLayerPtr>& layers, QObject* parent)
54  : RubberBand(display, parent),
55  m_layers(layers),
56  m_selectionStarted(false),
57  m_keepPreviousSelection(false),
58  m_selectionByPointing(false)
59 {
60  setCursor(cursor);
61 
62  // Setups the rubber band style
63  m_pen.setStyle(Qt::DashLine);
64  m_pen.setColor(QColor(255, 255, 100));
65  m_pen.setWidth(2);
66  m_brush = QColor(255, 255, 100, 80);
67 }
68 
70 {
71 }
72 
74 {
75  if(e->button() != Qt::LeftButton)
76  return false;
77 
78  m_selectionStarted = true;
79  m_rect = QRectF();
80 
82 }
83 
85 {
86  if(!m_selectionStarted)
87  return false;
88 
90 }
91 
93 {
94  ScopedCursor cursor(Qt::WaitCursor);
95 
96  m_selectionStarted = false;
97 
98  if(e->button() != Qt::LeftButton)
99  return false;
100 
102 
103  m_keepPreviousSelection = false;
104 
105  Qt::KeyboardModifiers keys = e->modifiers();
106 
107  if(keys & Qt::ControlModifier || keys & Qt::ShiftModifier)
108  m_keepPreviousSelection = true;
109 
110  m_selectionByPointing = false;
111 
112  if(m_rect.isNull())
113  {
114  m_selectionByPointing = true;
115  QPointF pixelOffset(4.0, 4.0);
116 #if QT_VERSION >= 0x050000
117  m_rect = QRectF(e->localPos() - pixelOffset, e->localPos() + pixelOffset);
118 #else
119  m_rect = QRectF(e->posF() - pixelOffset, e->posF() + pixelOffset);
120 #endif
121  }
122 
123  // Converts rect boundary to world coordinates
124  QPointF ll(m_rect.left(), m_rect.bottom());
125  QPointF ur(m_rect.right(), m_rect.top());
126  ll = m_display->transform(ll);
127  ur = m_display->transform(ur);
128 
129  te::gm::Envelope envelope(ll.x(), ll.y(), ur.x(), ur.y());
130 
131  // Select the layers
132  std::list<te::map::AbstractLayerPtr>::iterator it;
133  for(it = m_layers.begin(); it != m_layers.end(); ++it)
134  executeSelection(*it, envelope);
135 
136  return true;
137 }
138 
139 void te::qt::widgets::Selection::setLayers(const std::list<te::map::AbstractLayerPtr>& layers)
140 {
141  m_layers = layers;
142 }
143 
145 {
146  if(layer->getVisibility() != te::map::VISIBLE || !layer->isValid() || layer->getType() == "FOLDERLAYER")
147  return;
148 
149  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
150 
151  if(!dsType->hasGeom())
152  return;
153 
154  te::gm::Envelope reprojectedEnvelope(e);
155 
156  if((layer->getSRID() != TE_UNKNOWN_SRS) && (m_display->getSRID() != TE_UNKNOWN_SRS) && (layer->getSRID() != m_display->getSRID()))
157  reprojectedEnvelope.transform(m_display->getSRID(), layer->getSRID());
158 
159  // Clear previous selection?
160  if(!m_keepPreviousSelection)
161  {
162  layer->clearSelected();
163  emit layerSelectedObjectsChanged(layer);
164  }
165 
166  if(!reprojectedEnvelope.intersects(layer->getExtent()))
167  return;
168 
169  try
170  {
171  // Gets the layer schema
172  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
173  if(!schema->hasGeom())
174  return;
175 
177 
178  // Gets the dataset
179  std::auto_ptr<te::da::DataSet> dataset = layer->getData(gp->getName(), &reprojectedEnvelope, te::gm::INTERSECTS);
180  assert(dataset.get());
181 
182  // Let's generate the oids
183  te::da::ObjectIdSet* oids = 0;
184  te::da::GetEmptyOIDSet(schema.get(), oids);
185  assert(oids);
186 
187  std::vector<std::string> pnames;
188  te::da::GetOIDPropertyNames(schema.get(), pnames);
189 
190  // Generates a geometry from the given extent. It will be used to refine the results
191  std::auto_ptr<te::gm::Geometry> geometryFromEnvelope(te::gm::GetGeomFromEnvelope(&reprojectedEnvelope, layer->getSRID()));
192 
193  if(m_selectionByPointing == false)
194  {
195  while(dataset->moveNext())
196  {
197  std::auto_ptr<te::gm::Geometry> g(dataset->getGeometry(gp->getName()));
198 
199  if(g->getSRID() == TE_UNKNOWN_SRS)
200  g->setSRID(layer->getSRID());
201 
202  if(!g->intersects(geometryFromEnvelope.get()))
203  continue;
204 
205  // Feature found!
206  oids->add(te::da::GenerateOID(dataset.get(), pnames));
207  }
208 
209  oids->setExpressionByIntersection(gp->getName(), reprojectedEnvelope, layer->getSRID());
210  }
211  else
212  {
213  // The restriction point. It will be used to refine the results
214  te::gm::Coord2D center = reprojectedEnvelope.getCenter();
215  te::gm::Point point(center.x, center.y, layer->getSRID());
216 
217  while(dataset->moveNext())
218  {
219  std::auto_ptr<te::gm::Geometry> g(dataset->getGeometry(gp->getName()));
220 
221  if(g->getSRID() == TE_UNKNOWN_SRS)
222  g->setSRID(layer->getSRID());
223 
224  if(g->contains(&point) || g->crosses(geometryFromEnvelope.get()) || geometryFromEnvelope->contains(g.get()))
225  {
226  // Feature found!
227  oids->add(te::da::GenerateOID(dataset.get(), pnames));
228  }
229  }
230 
231  oids->setExpressionByInClause();
232  }
233 
234  assert(oids);
235 
236  // Adjusts the layer selection
237 
238  const te::da::ObjectIdSet* currentOids = layer->getSelected();
239  if(currentOids == 0 || !m_keepPreviousSelection)
240  {
241  layer->select(oids);
242  emit layerSelectedObjectsChanged(layer);
243  return;
244  }
245 
246  te::da::ObjectIdSet* finalSelected = currentOids->clone();
247  finalSelected->symDifference(oids);
248 
249  delete oids;
250 
251  layer->clearSelected();
252 
253  layer->select(finalSelected);
254 
255  emit layerSelectedObjectsChanged(layer);
256  }
257  catch(std::exception& e)
258  {
259  QMessageBox::critical(m_display, tr("Error"), QString(tr("The selection cannot be retrieved from the layer. Details:") + " %1.").arg(e.what()));
260  // TODO: catch the exceptions...
261  }
262 }
virtual bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Definition: RubberBand.cpp:55
Geometric property.
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:286
double y
y-coordinate.
Definition: Coord2D.h:114
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
Definition: Envelope.h:493
virtual bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
Definition: RubberBand.cpp:72
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:446
double x
x-coordinate.
Definition: Coord2D.h:113
virtual bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Definition: RubberBand.cpp:49
QPen m_pen
The pen used to draw the rubber band shape.
Definition: RubberBand.h:94
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
Definition: Selection.cpp:92
void setLayers(const std::list< te::map::AbstractLayerPtr > &layers)
Definition: Selection.cpp:139
QBrush m_brush
The brush used to draw the rubber band shape.
Definition: RubberBand.h:95
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Definition: Selection.cpp:84
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
virtual void setCursor(const QCursor &cursor)
It sets the tool cursor.
Selection(MapDisplay *display, const QCursor &cursor, const std::list< te::map::AbstractLayerPtr > &layers, QObject *parent=0)
It constructs a selection tool associated with the given map display.
Definition: Selection.cpp:53
void symDifference(const ObjectIdSet *rhs)
It performs the symmetric difference operation between this ObjectIdSet and the given ObjectIdSet...
This class provides a rectangle that can indicate a boundary.
Definition: RubberBand.h:52
~Selection()
Destructor.
Definition: Selection.cpp:69
void executeSelection(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &e)
Definition: Selection.cpp:144
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:309
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
Definition: Envelope.cpp:92
ObjectIdSet * clone() const
This class implements a concrete tool to select layer features using an extent.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Definition: Selection.cpp:73
const std::string & getName() const
It returns the property name.
Definition: Property.h:127