CreatePolygonTool.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/CreatePolygonTool.cpp
22 
23  \brief This class implements a concrete tool to create polygons.
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../geometry.h"
29 #include "../../../dataaccess/dataset/ObjectId.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../qt/widgets/canvas/MapDisplay.h"
32 #include "../../../qt/widgets/Utils.h"
33 #include "../../RepositoryManager.h"
34 #include "../../Utils.h"
35 #include "../Renderer.h"
36 #include "../Utils.h"
37 #include "CreatePolygonTool.h"
38 
39 // Qt
40 #include <QMessageBox>
41 #include <QMouseEvent>
42 #include <QPainter>
43 #include <QPixmap>
44 
45 // STL
46 #include <cassert>
47 #include <memory>
48 
50  bool showVertexes, QObject* parent)
51  : GeometriesUpdateTool(display, layer.get(), parent),
52  m_continuousMode(false),
53  m_isFinished(false),
54  m_showVertexes(showVertexes),
55  m_stack(UndoStackManager::getInstance())
56 {
57  setCursor(cursor);
58 
59  // Signals & slots
60  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
61 
62  draw();
63 }
64 
66 
68 {
69  if (e->button() != Qt::LeftButton)
70  return false;
71 
72  if(m_isFinished) // Is Finished?! So, start again...
73  {
74  clear();
75  m_isFinished = false;
76  }
77 
78  if (m_coords.empty())
79  emit geometriesStarted();
80 
81  QPointF pw = m_display->transform(GetPosition(e));
82 
83  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
84 
85  TrySnap(coord, m_display->getSRID());
86 
87  m_coords.push_back(coord);
88 
90 
91  return true;
92 }
93 
95 {
96  if(m_coords.size() < 1 || m_isFinished)
97  return false;
98 
99  QPointF pos = GetPosition(e);
100  pos += QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
101 
102  QPointF pw = m_display->transform(pos);
103 
104  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
105 
106  TrySnap(coord, m_display->getSRID());
107 
108  m_lastPos = te::gm::Coord2D(coord.x, coord.y);
109 
110  if (e->buttons() & Qt::LeftButton)
111  {
112  double tol = 0.0;
113 
114  m_continuousMode = true;
115 
116  double scaleValue = m_display->getScale();
117  double digitValue = m_display->getDigitalization();
118 
119  // Calculate of the digitalization (Code from Old Version: TA4)
120  tol = (((scaleValue*digitValue) / 1000) / 1000);
121  if (tol <= 0.0001347)
122  tol = 0.0001347;
123 
124  if (GetDistance(m_coords.at(m_coords.size() - 1), coord) >= tol)
125  {
126  m_coords.push_back(coord);
128  }
129  }
130  else
131  {
132  m_coords.push_back(coord);
133 
134  m_continuousMode = false;
135  }
136 
137  draw();
138 
139  return false;
140 }
141 
143 {
144  try
145  {
146  if (e->button() != Qt::LeftButton)
147  return false;
148 
149  return editingFinished();
150  }
151  catch (std::exception& e)
152  {
153  QMessageBox::critical(m_display, tr("Error"), QString(tr("Could not create.") + " %1.").arg(e.what()));
154  return false;
155  }
156 }
157 
159 {
160  try
161  {
162  if (e->button() != Qt::RightButton)
163  return false;
164 
165  return editingFinished();
166  }
167  catch (std::exception& e)
168  {
169  QMessageBox::critical(m_display, tr("Error"), QString(tr("Could not create.") + " %1.").arg(e.what()));
170  return false;
171  }
172 }
173 
175 {
176  const te::gm::Envelope& env = m_display->getExtent();
177  if(!env.isValid())
178  return;
179 
180  // Clear!
181  QPixmap* draft = m_display->getDraftPixmap();
182  draft->fill(Qt::transparent);
183 
184  // Initialize the renderer
185  Renderer& renderer = Renderer::getInstance();
186  renderer.begin(draft, env, m_display->getSRID());
187 
188  // Draw the layer edited geometries
189  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
190 
191  if(!m_coords.empty())
192  {
193  // Draw the geometry being created
194  if(m_coords.size() < 3)
195  drawLine();
196  else if (m_stack.m_currentIndex > -1)
197  drawPolygon();
198 
199  if(m_continuousMode == false)
200  m_coords.pop_back();
201  }
202 
203  renderer.end();
204 
205  m_display->repaint();
206 }
207 
209 {
210  // Build the geometry
211  te::gm::Geometry* polygon = buildPolygon();
212 
213  // Draw the current geometry and the vertexes
214  Renderer& renderer = Renderer::getInstance();
215  renderer.draw(polygon, m_showVertexes);
216 
217  delete polygon;
218 }
219 
221 {
222  // Build the geometry
224 
225  // Draw the current geometry and the vertexes
226  Renderer& renderer = Renderer::getInstance();
227  renderer.draw(line, m_showVertexes);
228 
229  delete line;
230 }
231 
233 {
234  delete m_feature;
235  m_feature = nullptr;
236 
237  m_coords.clear();
238 }
239 
241 {
242  // Build the geometry
244  for(std::size_t i = 0; i < m_coords.size(); ++i)
245  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
246  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
247 
249  polygon->setRingN(0, ring);
250  polygon->setSRID(m_display->getSRID());
251 
252  return polygon;
253 }
254 
256 {
258  for(std::size_t i = 0; i < m_coords.size(); ++i)
259  line->setPoint(i, m_coords[i].x, m_coords[i].y);
260 
261  return line;
262 }
263 
265 {
266  if(m_coords.empty())
267  return;
268 
269  m_coords.push_back(m_lastPos);
270 }
271 
273 {
274  if (m_coords.size() < 3)
275  return;
276 
277  if (m_feature == nullptr)
278  m_feature = new Feature();
279 
282 
284 
285  QUndoCommand* command = new AddCommand(m_display, m_layer, m_feature->clone()->getId());
286  connect(dynamic_cast<AddCommand*>(command), SIGNAL(commandFeedback()), SLOT(onCommandFeedback()));
287 
288  m_stack.addUndoStack(command);
289 }
290 
292 {
293  m_isFinished = false;
294 
296 
297  if (repo.hasIdentify(m_layer->getId(), m_feature->getId()) == true)
298  {
299  m_feature = repo.getFeature(m_layer->getId(), m_feature->getId())->clone();
301  }
302 
303  m_coords.clear();
304 
306  m_coords.pop_back();
307 }
308 
310 {
311  clear();
312 }
313 
315 {
316  if (m_coords.size() < 3) // Can not stop yet...
317  return false;
318 
319  m_isFinished = true;
320 
321  if (m_feature)
322  storeFeature();
323 
324  emit geometriesEdited();
325 
326  return true;
327 }
328 
330 {
331  if (m_feature == nullptr)
332  return;
333 
335 }
virtual const std::string & getId() const
It returns the layer id.
virtual double getDigitalization() const
Gets the digitalization factor.
bool m_isFinished
A flag that indicates if the operations was finished.
This class implements a concrete tool to create polygons.
Undo/Redo for add one components.
Definition: AddCommand.h:52
double y
y-coordinate.
Definition: Coord2D.h:114
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
te::gm::Coord2D m_lastPos
The last position captured on mouse move event.
double x
x-coordinate.
Definition: Coord2D.h:113
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
te::da::ObjectId * getId() const
Definition: Feature.cpp:134
Feature * clone() const
Definition: Feature.cpp:182
void addWatch(Feature *feature)
virtual double getScale() const
Calculates and return the current scale.
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:139
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
TEEDITEXPORT void GetCoordinates(te::gm::Geometry *geom, std::vector< te::gm::Coord2D > &coords)
unsigned int line
This is a singleton for managing edit repositories.
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.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
CreatePolygonTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, const QCursor &cursor, bool showVertexes=true, QObject *parent=0)
It constructs a create polygon tool associated with the given map display.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
void setOperationTypeId(const te::edit::OperationType &currentOperationType)
Definition: Feature.cpp:124
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.
TEEDITEXPORT double GetDistance(const te::gm::Coord2D &c1, const te::gm::Coord2D &c2)
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
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
te::gm::Geometry * buildPolygon()
te::gm::Geometry * buildLine()
Feature * getFeature(const std::string &source, const te::gm::Envelope &e, int srid) const
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void removeFeature(const std::string &source, te::da::ObjectId *id)
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
te::map::AbstractLayer * m_layer
bool hasIdentify(const std::string &source, te::da::ObjectId *id)
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
TEEDITEXPORT void TrySnap(te::gm::Coord2D &coord, int srid)
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
void setCursor(const QCursor &cursor)
It sets the tool cursor.
std::vector< te::gm::Coord2D > m_coords
The coord list managed by this tool.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
void setGeometry(te::gm::Geometry *geom)
Definition: Feature.cpp:95
~CreatePolygonTool()
Destructor.
bool m_continuousMode
A flag that indicates if the tool is working in &#39;continuous mode&#39;. i.e. the coordinates will be acqui...
bool m_showVertexes
A flag that indicates whether the tool will display the vertices.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
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.