All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Selection.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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 <QtGui/QKeyEvent>
45 #include <QtGui/QMessageBox>
46 #include <QtGui/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  m_rect = QRectF(e->posF() - pixelOffset, e->posF() + pixelOffset);
117  }
118 
119  // Converts rect boundary to world coordinates
120  QPointF ll(m_rect.left(), m_rect.bottom());
121  QPointF ur(m_rect.right(), m_rect.top());
122  ll = m_display->transform(ll);
123  ur = m_display->transform(ur);
124 
125  te::gm::Envelope envelope(ll.x(), ll.y(), ur.x(), ur.y());
126 
127  // Select the layers
128  std::list<te::map::AbstractLayerPtr>::iterator it;
129  for(it = m_layers.begin(); it != m_layers.end(); ++it)
130  executeSelection(*it, envelope);
131 
132  return true;
133 }
134 
135 void te::qt::widgets::Selection::setLayers(const std::list<te::map::AbstractLayerPtr>& layers)
136 {
137  m_layers = layers;
138 }
139 
141 {
142  if(layer->getVisibility() != te::map::VISIBLE)
143  return;
144 
145  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
146 
147  if(!dsType->hasGeom())
148  return;
149 
150  te::gm::Envelope reprojectedEnvelope(e);
151 
152  if((layer->getSRID() != TE_UNKNOWN_SRS) && (m_display->getSRID() != TE_UNKNOWN_SRS) && (layer->getSRID() != m_display->getSRID()))
153  reprojectedEnvelope.transform(m_display->getSRID(), layer->getSRID());
154 
155  // Clear previous selection?
156  if(!m_keepPreviousSelection)
157  {
158  layer->clearSelected();
159  emit layerSelectedObjectsChanged(layer);
160  }
161 
162  if(!reprojectedEnvelope.intersects(layer->getExtent()))
163  return;
164 
165  try
166  {
167  // Gets the layer schema
168  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
169  if(!schema->hasGeom())
170  return;
171 
173 
174  // Gets the dataset
175  std::auto_ptr<te::da::DataSet> dataset = layer->getData(gp->getName(), &reprojectedEnvelope, te::gm::INTERSECTS);
176  assert(dataset.get());
177 
178  // Let's generate the oids
179  te::da::ObjectIdSet* oids = 0;
180  te::da::GetEmptyOIDSet(schema.get(), oids);
181  assert(oids);
182 
183  std::vector<std::string> pnames;
184  te::da::GetOIDPropertyNames(schema.get(), pnames);
185 
186  // Generates a geometry from the given extent. It will be used to refine the results
187  std::auto_ptr<te::gm::Geometry> geometryFromEnvelope(te::gm::GetGeomFromEnvelope(&reprojectedEnvelope, layer->getSRID()));
188 
189  if(m_selectionByPointing == false)
190  {
191  while(dataset->moveNext())
192  {
193  std::auto_ptr<te::gm::Geometry> g(dataset->getGeometry(gp->getName()));
194 
195  if(!g->intersects(geometryFromEnvelope.get()))
196  continue;
197 
198  // Feature found!
199  oids->add(te::da::GenerateOID(dataset.get(), pnames));
200  }
201  }
202  else
203  {
204  // The restriction point. It will be used to refine the results
205  te::gm::Coord2D center = reprojectedEnvelope.getCenter();
206  te::gm::Point point(center.x, center.y, layer->getSRID());
207 
208  while(dataset->moveNext())
209  {
210  std::auto_ptr<te::gm::Geometry> g(dataset->getGeometry(gp->getName()));
211 
212  if(g->contains(&point) || g->crosses(geometryFromEnvelope.get()) || geometryFromEnvelope->contains(g.get()))
213  {
214  // Feature found!
215  oids->add(te::da::GenerateOID(dataset.get(), pnames));
216  break;
217  }
218  }
219  }
220 
221  assert(oids);
222 
223  // Adjusts the layer selection
224 
225  const te::da::ObjectIdSet* currentOids = layer->getSelected();
226  if(currentOids == 0 || !m_keepPreviousSelection)
227  {
228  layer->select(oids);
229  emit layerSelectedObjectsChanged(layer);
230  return;
231  }
232 
233  te::da::ObjectIdSet* finalSelected = currentOids->clone();
234  finalSelected->symDifference(oids);
235 
236  delete oids;
237 
238  layer->clearSelected();
239 
240  layer->select(finalSelected);
241 
242  emit layerSelectedObjectsChanged(layer);
243  }
244  catch(std::exception& e)
245  {
246  QMessageBox::critical(m_display, tr("Error"), QString(tr("The selection cannot be retrieved from the layer. Details:") + " %1.").arg(e.what()));
247  // TODO: catch the exceptions...
248  }
249 }
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:35
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
virtual bool mouseReleaseEvent(QMouseEvent *e)
Definition: RubberBand.cpp:72
This class implements a concrete tool to select layer features using an extent.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:504
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
bool mouseMoveEvent(QMouseEvent *e)
Definition: Selection.cpp:84
double y
y-coordinate.
Definition: Coord2D.h:87
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:282
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:72
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual void setCursor(const QCursor &cursor)
virtual bool mouseMoveEvent(QMouseEvent *e)
Definition: RubberBand.cpp:55
virtual bool mousePressEvent(QMouseEvent *e)
Definition: RubberBand.cpp:49
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:393
bool mouseReleaseEvent(QMouseEvent *e)
Definition: Selection.cpp:92
A point with x and y coordinate values.
Definition: Point.h:50
void symDifference(const ObjectIdSet *rhs)
It performs the symmetric difference operation between this ObjectIdSet and the given ObjectIdSet...
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:65
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
Definition: Envelope.cpp:48
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:89
bool intersects(const Envelope &rhs) const
It returns true if the envelopes &quot;spatially intersects&quot;.
Definition: Envelope.h:493
This class provides a rectangle that can indicate a boundary.
Definition: RubberBand.h:52
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:305
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
QBrush m_brush
The brush used to draw the rubber band shape.
Definition: RubberBand.h:95
double x
x-coordinate.
Definition: Coord2D.h:86
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void executeSelection(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &e)
Definition: Selection.cpp:140
ObjectIdSet * clone() const
Geometric property.
~Selection()
Destructor.
Definition: Selection.cpp:69
void setLayers(const std::list< te::map::AbstractLayerPtr > &layers)
Definition: Selection.cpp:135
bool mousePressEvent(QMouseEvent *e)
Definition: Selection.cpp:73
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48