PolygonAcquire.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/PolygonAcquire.cpp
22 
23  \brief This class implements a concrete tool to acquire a polygon geometry
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../geometry/Envelope.h"
29 #include "../../../geometry/Geometry.h"
30 #include "../../../geometry/LinearRing.h"
31 #include "../../../geometry/Polygon.h"
32 #include "../canvas/Canvas.h"
33 #include "../canvas/MapDisplay.h"
34 #include "PolygonAcquire.h"
35 
36 // Qt
37 #include <QMouseEvent>
38 #include <QPainter>
39 #include <QPixmap>
40 
41 // STL
42 #include <cassert>
43 
45  : AbstractTool(display, parent),
46  m_isFinished(false)
47 {
48  setCursor(Qt::ArrowCursor);
49 
50  // Setups the path style
51  m_pen.setColor(QColor(0, 0, 0));
52  m_pen.setWidth(2);
53  m_brush = QColor(233, 88, 63, 80);
54 
55  // Signals & slots
56  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
57 }
58 
60 {
61  QPixmap* draft = m_display->getDraftPixmap();
62  draft->fill(Qt::transparent);
63 }
64 
66 {
67  if(e->button() != Qt::LeftButton)
68  return false;
69 
70  if(m_isFinished) // Is Finished?! So, start again...
71  {
72  clear();
73  m_isFinished = false;
74  }
75 
76  QPointF pw = m_display->transform(e->pos());
77  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
78 
79  return true;
80 }
81 
83 {
84  if(m_coords.size() < 1 || m_isFinished)
85  return false;
86 
87 #if QT_VERSION >= 0x050000
88  QPointF pw = m_display->transform(e->localPos());
89 #else
90  QPointF pw = m_display->transform(e->posF());
91 #endif
92  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
93 
94 #if QT_VERSION >= 0x050000
95  QPointF pos = e->localPos() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
96 #else
97  QPointF pos = e->posF() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
98 #endif
99 
100  pw = m_display->transform(pos);
101  m_lastPos = te::gm::Coord2D(pw.x(), pw.y());
102 
103  drawGeometry();
104 
105  return false;
106 }
107 
109 {
110  return false;
111 }
112 
114 {
115  if(e->button() != Qt::LeftButton)
116  return false;
117 
118  if(m_coords.size() < 3) // Can not stop yet...
119  return false;
120 
121  m_isFinished = true;
122 
123  // Build the geometry
125  for(std::size_t i = 0; i < m_coords.size(); ++i)
126  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
127  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
128 
130  polygon->setRingN(0, ring);
131  polygon->setSRID(m_display->getSRID());
132 
133  emit polygonAquired(polygon);
134 
135  return true;
136 }
137 
139 {
140  const te::gm::Envelope& env = m_display->getExtent();
141  if(!env.isValid())
142  return;
143 
144  // Clear!
145  QPixmap* draft = m_display->getDraftPixmap();
146  draft->fill(Qt::transparent);
147 
148  // Prepares the canvas
149  Canvas canvas(m_display->width(), m_display->height());
150  canvas.setDevice(draft, false);
151  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
152  canvas.setRenderHint(QPainter::Antialiasing, true);
153 
154  // Let's draw!
155  drawPolygon(canvas);
156 
157  m_coords.pop_back();
158 
159  m_display->repaint();
160 }
161 
163 {
164  // Build the geometry
166  for(std::size_t i = 0; i < m_coords.size(); ++i)
167  line->setPoint(i, m_coords[i].x, m_coords[i].y);
168 
169  // Setup canvas style
170  canvas.setLineColor(m_pen.color().rgba());
171  canvas.setLineWidth(m_pen.width());
172 
173  // Let's draw!
174  canvas.draw(line);
175 
176  delete line;
177 }
178 
180 {
181  if(m_coords.size() < 3)
182  return drawLine(canvas);
183 
184  // Build the geometry
186  for(std::size_t i = 0; i < m_coords.size(); ++i)
187  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
188  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
189 
191  polygon->setRingN(0, ring);
192 
193  // Setup canvas style
194  canvas.setPolygonContourColor(m_pen.color().rgba());
195  canvas.setPolygonContourWidth(m_pen.width());
196  canvas.setPolygonFillColor(m_brush.color().rgba());
197 
198  // Let's draw!
199  canvas.draw(polygon);
200 
201  delete polygon;
202 }
203 
205 {
206  m_coords.clear();
207 
208  QPixmap* draft = m_display->getDraftPixmap();
209  draft->fill(Qt::transparent);
210 
211  m_display->repaint();
212 }
213 
215 {
216  if(m_coords.empty())
217  return;
218 
219  m_coords.push_back(m_lastPos);
220 }
QPen m_pen
The pen used to draw the path.
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
void drawPolygon(Canvas &canvas)
double m_urx
Upper right corner x-coordinate.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
void setPolygonFillColor(const te::color::RGBAColor &color)
It sets the color used to fill the draw of polygon geometries.
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
unsigned int line
void polygonAquired(te::gm::Polygon *poly)
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
double m_llx
Lower left corner x-coordinate.
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
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.
void setLineWidth(int w)
It sets the line width.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
void setPolygonContourWidth(int w)
It sets the polygon contour width.
virtual void setCursor(const QCursor &cursor)
It sets the tool cursor.
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.
This class implements a concrete tool to acquire a polygon geometry.
QBrush m_brush
The brush used to draw the path.
double m_lly
Lower left corner y-coordinate.
te::gm::Coord2D m_lastPos
The last position captured on mouse move event.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
bool m_isFinished
A flag that indicates if the operations was finished.
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
double m_ury
Upper right corner y-coordinate.
PolygonAcquire(MapDisplay *display, QObject *parent=0)
It constructs a PolygonAcquire tool associated with the given map display.
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
std::vector< te::gm::Coord2D > m_coords
The coord list managed by the measure tool.
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.