All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
130  emit polygonAquired(polygon);
131 
132  return true;
133 }
134 
136 {
137  const te::gm::Envelope& env = m_display->getExtent();
138  if(!env.isValid())
139  return;
140 
141  // Clear!
142  QPixmap* draft = m_display->getDraftPixmap();
143  draft->fill(Qt::transparent);
144 
145  // Prepares the canvas
146  Canvas canvas(m_display->width(), m_display->height());
147  canvas.setDevice(draft, false);
148  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
149  canvas.setRenderHint(QPainter::Antialiasing, true);
150 
151  // Let's draw!
152  drawPolygon(canvas);
153 
154  m_coords.pop_back();
155 
156  m_display->repaint();
157 }
158 
160 {
161  // Build the geometry
162  te::gm::LineString* line = new te::gm::LineString(m_coords.size(), te::gm::LineStringType);
163  for(std::size_t i = 0; i < m_coords.size(); ++i)
164  line->setPoint(i, m_coords[i].x, m_coords[i].y);
165 
166  // Setup canvas style
167  canvas.setLineColor(m_pen.color().rgba());
168  canvas.setLineWidth(m_pen.width());
169 
170  // Let's draw!
171  canvas.draw(line);
172 
173  delete line;
174 }
175 
177 {
178  if(m_coords.size() < 3)
179  return drawLine(canvas);
180 
181  // Build the geometry
182  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
183  for(std::size_t i = 0; i < m_coords.size(); ++i)
184  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
185  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
186 
188  polygon->setRingN(0, ring);
189 
190  // Setup canvas style
191  canvas.setPolygonContourColor(m_pen.color().rgba());
192  canvas.setPolygonContourWidth(m_pen.width());
193  canvas.setPolygonFillColor(m_brush.color().rgba());
194 
195  // Let's draw!
196  canvas.draw(polygon);
197 
198  delete polygon;
199 }
200 
202 {
203  m_coords.clear();
204 
205  QPixmap* draft = m_display->getDraftPixmap();
206  draft->fill(Qt::transparent);
207 
208  m_display->repaint();
209 }
210 
212 {
213  if(m_coords.empty())
214  return;
215 
216  m_coords.push_back(m_lastPos);
217  drawGeometry();
218 }
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 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.