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  // Setups the path style
49  m_pen.setColor(QColor(0, 0, 0));
50  m_pen.setWidth(2);
51  m_brush = QColor(233, 88, 63, 80);
52 
53  // Signals & slots
54  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
55 }
56 
58 {
59  QPixmap* draft = m_display->getDraftPixmap();
60  draft->fill(Qt::transparent);
61 }
62 
64 {
65  if(e->button() != Qt::LeftButton)
66  return false;
67 
68  if(m_isFinished) // Is Finished?! So, start again...
69  {
70  clear();
71  m_isFinished = false;
72  }
73 
74  QPointF pw = m_display->transform(e->pos());
75  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
76 
77  return true;
78 }
79 
81 {
82  if(m_coords.size() < 1 || m_isFinished)
83  return false;
84 
85 #if QT_VERSION >= 0x050000
86  QPointF pw = m_display->transform(e->localPos());
87 #else
88  QPointF pw = m_display->transform(e->posF());
89 #endif
90  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
91 
92 #if QT_VERSION >= 0x050000
93  QPointF pos = e->localPos() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
94 #else
95  QPointF pos = e->posF() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
96 #endif
97 
98  pw = m_display->transform(pos);
99  m_lastPos = te::gm::Coord2D(pw.x(), pw.y());
100 
101  drawGeometry();
102 
103  return false;
104 }
105 
107 {
108  return false;
109 }
110 
112 {
113  if(e->button() != Qt::LeftButton)
114  return false;
115 
116  if(m_coords.size() < 3) // Can not stop yet...
117  return false;
118 
119  m_isFinished = true;
120 
121  // Build the geometry
122  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
123  for(std::size_t i = 0; i < m_coords.size(); ++i)
124  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
125  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
126 
128  polygon->setRingN(0, ring);
129  polygon->setSRID(m_display->getSRID());
130 
131  emit polygonAquired(polygon);
132 
133  return true;
134 }
135 
137 {
138  const te::gm::Envelope& env = m_display->getExtent();
139  if(!env.isValid())
140  return;
141 
142  // Clear!
143  QPixmap* draft = m_display->getDraftPixmap();
144  draft->fill(Qt::transparent);
145 
146  // Prepares the canvas
147  Canvas canvas(m_display->width(), m_display->height());
148  canvas.setDevice(draft, false);
149  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
150  canvas.setRenderHint(QPainter::Antialiasing, true);
151 
152  // Let's draw!
153  drawPolygon(canvas);
154 
155  m_coords.pop_back();
156 
157  m_display->repaint();
158 }
159 
161 {
162  // Build the geometry
163  te::gm::LineString* line = new te::gm::LineString(m_coords.size(), te::gm::LineStringType);
164  for(std::size_t i = 0; i < m_coords.size(); ++i)
165  line->setPoint(i, m_coords[i].x, m_coords[i].y);
166 
167  // Setup canvas style
168  canvas.setLineColor(m_pen.color().rgba());
169  canvas.setLineWidth(m_pen.width());
170 
171  // Let's draw!
172  canvas.draw(line);
173 
174  delete line;
175 }
176 
178 {
179  if(m_coords.size() < 3)
180  return drawLine(canvas);
181 
182  // Build the geometry
183  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
184  for(std::size_t i = 0; i < m_coords.size(); ++i)
185  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
186  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
187 
189  polygon->setRingN(0, ring);
190 
191  // Setup canvas style
192  canvas.setPolygonContourColor(m_pen.color().rgba());
193  canvas.setPolygonContourWidth(m_pen.width());
194  canvas.setPolygonFillColor(m_brush.color().rgba());
195 
196  // Let's draw!
197  canvas.draw(polygon);
198 
199  delete polygon;
200 }
201 
203 {
204  m_coords.clear();
205 
206  QPixmap* draft = m_display->getDraftPixmap();
207  draft->fill(Qt::transparent);
208 
209  m_display->repaint();
210 }
211 
213 {
214  if(m_coords.empty())
215  return;
216 
217  m_coords.push_back(m_lastPos);
218  drawGeometry();
219 }
QPen m_pen
The pen used to draw the path.
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
Definition: Canvas.cpp:2104
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.
Definition: Envelope.h:346
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.
Definition: Canvas.cpp:1732
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
Definition: Canvas.cpp:1862
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
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:62
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
Definition: Canvas.cpp:296
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
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.
Definition: LineString.cpp:353
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void setLineWidth(int w)
It sets the line width.
Definition: Canvas.cpp:1725
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
Definition: Canvas.cpp:1620
void setPolygonContourWidth(int w)
It sets the polygon contour width.
Definition: Canvas.cpp:1919
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
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.
Definition: Envelope.h:345
A canvas built on top of Qt.
Definition: Canvas.h:54
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
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
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:160
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.