SubtractAreaTool.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/edit/qt/tools/SubtratAreaTool.cpp
22 
23  \brief This class implements a concrete tool to subtract geometries.
24 */
25 
26 //TerraLib
27 #include "../../../dataaccess/utils/Utils.h"
28 #include "../../../geometry/GeometryProperty.h"
29 #include "../../../geometry/Utils.h"
30 #include "../../../qt/widgets/canvas/MapDisplay.h"
31 #include "../../Feature.h"
32 #include "../../RepositoryManager.h"
33 #include "../../Utils.h"
34 #include "../Renderer.h"
35 #include "../Utils.h"
36 #include "SubtractAreaTool.h"
37 
38 // Qt
39 #include <QMessageBox>
40 #include <QMouseEvent>
41 #include <QPainter>
42 #include <QPixmap>
43 
44 // STL
45 #include <cassert>
46 #include <memory>
47 #include <iostream>
48 
50  : CreateLineTool(display, layer, Qt::ArrowCursor, parent),
51  m_stack(UndoStackManager::getInstance())
52 {
53  setCursor(Qt::ArrowCursor);
54 }
55 
57 
59 {
60  if (e->button() != Qt::LeftButton)
61  return false;
62 
63  if (m_isFinished) // Is Finished?! So, start again...
64  {
66  m_isFinished = false;
67  }
68 
70 
72 }
73 
75 {
77 }
78 
80 {
81  try
82  {
83  if (e->button() != Qt::LeftButton)
84  return false;
85 
86  return editingFinished();
87  }
88  catch (std::exception& e)
89  {
90  QMessageBox::critical(m_display, tr("Error"), QString(tr("Could not subtract.") + " %1.").arg(e.what()));
91  return false;
92  }
93 }
94 
96 {
97  try
98  {
99  if (e->button() != Qt::RightButton)
100  return false;
101 
102  return editingFinished();
103  }
104  catch (std::exception& e)
105  {
106  QMessageBox::critical(m_display, tr("Error"), QString(tr("Could not subtract.") + " %1.").arg(e.what()));
107  return false;
108  }
109 }
110 
112 {
113  if (m_coords.size() < 3) // Can not stop yet...
114  return false;
115 
116  if (m_feature == nullptr) // Can not stop yet...
117  {
119  QMessageBox::critical(m_display, tr("Error"), QString(tr("Error subtracting area to the polygon")));
120  return false;
121  }
122 
123  setCursor(Qt::WaitCursor);
124 
125  m_isFinished = true;
126 
127  draw();
128 
129  storeFeature();
130 
132 
133  clear();
134 
135  setCursor(Qt::ArrowCursor);
136 
137  emit geometriesEdited();
138 
139  return true;
140 }
141 
143 {
144  const te::gm::Envelope& env = m_display->getExtent();
145  if (!env.isValid())
146  return;
147 
148  // Clear!
149  QPixmap* draft = m_display->getDraftPixmap();
150  draft->fill(Qt::transparent);
151 
152  // Initialize the renderer
153  Renderer& renderer = Renderer::getInstance();
154  renderer.begin(draft, env, m_display->getSRID());
155 
156  // Draw the layer edited geometries
157  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
158 
159  if (!m_coords.empty())
160  {
161  if (m_coords.size() > 3)
162  drawPolygon();
163 
164  if (m_continuousMode == false)
165  m_coords.pop_back();
166  }
167 
168  renderer.end();
169 
170  m_display->repaint();
171 
172 }
173 
175 {
176  // Build the geometry
178 
179  // Draw the current geometry and the vertexes
180  Renderer& renderer = Renderer::getInstance();
181  renderer.draw(m_feature->getGeometry(), true);
182 }
183 
185 {
186  te::gm::Geometry* geom = nullptr;
187 
188  // Build the geometry
190  for (std::size_t i = 0; i < m_coords.size(); ++i)
191  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
192  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
193 
195  polygon->setRingN(0, ring);
196  polygon->setSRID(m_display->getSRID());
197 
198  te::gm::Geometry* hole = te::gm::Validate(polygon);
199 
202 
203  if (!hole->intersects(m_feature->getGeometry()) || hole->covers(m_feature->getGeometry()))
204  {
205  m_isFinished = false;
206  return dynamic_cast<te::gm::Geometry*>(m_feature->getGeometry()->clone());
207  }
208 
209  geom = m_feature->getGeometry()->difference(hole);
210 
211  return geom;
212 }
213 
215 {
216  QPointF pixelOffset(4.0, 4.0);
217 
218  QRectF rect(pos - pixelOffset, pos + pixelOffset);
219 
220  // Converts rect boundary to world coordinates
221  QPointF ll(rect.left(), rect.bottom());
222  QPointF ur(rect.right(), rect.top());
223  ll = m_display->transform(ll);
224  ur = m_display->transform(ur);
225 
226  te::gm::Envelope env(ll.x(), ll.y(), ur.x(), ur.y());
227 
228  return env;
229 }
230 
232 {
234 }
235 
237 {
238  setCursor(Qt::WaitCursor);
239 
240  te::gm::Envelope env = buildEnvelope(pos);
241 
242  try
243  {
244  if (m_feature == nullptr)
246 
247  setCursor(Qt::ArrowCursor);
248  }
249  catch (std::exception& e)
250  {
251  setCursor(Qt::ArrowCursor);
252  QMessageBox::critical(m_display, tr("Error"), QString(tr("The geometry cannot be selected from the layer. Details:") + " %1.").arg(e.what()));
253  }
254 }
255 
257 {
258  if (m_feature == nullptr)
259  return;
260 
261  if (!m_isFinished)
262  return;
263 
265 
266  QUndoCommand* command = new AddCommand(m_display, m_layer, m_feature->clone()->getId());
267  m_stack.addUndoStack(command);
268 }
269 
271 {
273 
274  m_feature = nullptr;
275 }
virtual const std::string & getId() const
It returns the layer id.
virtual Geometry * difference(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns a geometric object that represents the point set difference with another geometry...
bool m_isFinished
A flag that indicates if the operations was finished.
Undo/Redo for add one components.
Definition: AddCommand.h:52
UndoStackManager & m_stack
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
te::da::ObjectId * getId() const
Definition: Feature.cpp:134
This class implements a concrete tool to create lines.
Feature * clone() const
Definition: Feature.cpp:182
void addWatch(Feature *feature)
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:139
A widget to control the display of a set of layers.
virtual bool intersects(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if the geometry object spatially intersects rhs geometry.
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
void addUndoStack(QUndoCommand *command)
Method that insert command Undo/Redo of type AddCommand in the Undo/Redo stack.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
std::vector< te::gm::Coord2D > m_coords
The coord list managed by this tool.
TEEDITEXPORT Feature * PickFeature(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &env, int srid)
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
void draw(te::gm::Geometry *geom, bool showVertexes=false)
Definition: Renderer.cpp:260
virtual AbstractData * clone() const =0
It returns a clone of this object.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
te::gm::Geometry * buildPolygon()
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
te::map::AbstractLayer * m_layer
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:59
void drawRepository(const std::string &source, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:78
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
te::gm::Envelope buildEnvelope(const QPointF &pos)
void setCursor(const QCursor &cursor)
It sets the tool cursor.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
SubtractAreaTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, QObject *parent=0)
bool m_continuousMode
A flag that indicates if the tool is working in &#39;continuous mode&#39;. i.e. the coordinates will be acqui...
void setGeometry(te::gm::Geometry *geom)
Definition: Feature.cpp:95
virtual void transform(int srid) _NOEXCEPT_OP(false)=0
It converts the coordinate values of the geometry to the new spatial reference system.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void pickFeature(const QPointF &pos)
TEGEOMEXPORT te::gm::Geometry * Validate(te::gm::Geometry *geom)
Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon...
bool isValid() const
It tells if the rectangle is valid or not.
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
virtual bool covers(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if this geometry object spatially covers the rhs geometry.