All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PolygonAcquire.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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 <QtGui/QMouseEvent>
38 #include <QtGui/QPainter>
39 #include <QtGui/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  QPointF pw = m_display->transform(e->posF());
86  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
87 
88  QPointF pos = e->posF() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
89  pw = m_display->transform(pos);
90  m_lastPos = te::gm::Coord2D(pw.x(), pw.y());
91 
92  drawGeometry();
93 
94  return false;
95 }
96 
98 {
99  return false;
100 }
101 
103 {
104  if(e->button() != Qt::LeftButton)
105  return false;
106 
107  if(m_coords.size() < 3) // Can not stop yet...
108  return false;
109 
110  m_isFinished = true;
111 
112  // Build the geometry
113  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
114  for(std::size_t i = 0; i < m_coords.size(); ++i)
115  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
116  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
117 
119  polygon->setRingN(0, ring);
120 
121  emit polygonAquired(polygon);
122 
123  return true;
124 }
125 
127 {
128  const te::gm::Envelope& env = m_display->getExtent();
129  if(!env.isValid())
130  return;
131 
132  // Clear!
133  QPixmap* draft = m_display->getDraftPixmap();
134  draft->fill(Qt::transparent);
135 
136  // Prepares the canvas
137  Canvas canvas(m_display->width(), m_display->height());
138  canvas.setDevice(draft, false);
139  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
140  canvas.setRenderHint(QPainter::Antialiasing, true);
141 
142  // Let's draw!
143  drawPolygon(canvas);
144 
145  m_coords.pop_back();
146 
147  m_display->repaint();
148 }
149 
151 {
152  // Build the geometry
153  te::gm::LineString* line = new te::gm::LineString(m_coords.size(), te::gm::LineStringType);
154  for(std::size_t i = 0; i < m_coords.size(); ++i)
155  line->setPoint(i, m_coords[i].x, m_coords[i].y);
156 
157  // Setup canvas style
158  canvas.setLineColor(m_pen.color().rgba());
159  canvas.setLineWidth(m_pen.width());
160 
161  // Let's draw!
162  canvas.draw(line);
163 
164  delete line;
165 }
166 
168 {
169  if(m_coords.size() < 3)
170  return drawLine(canvas);
171 
172  // Build the geometry
173  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
174  for(std::size_t i = 0; i < m_coords.size(); ++i)
175  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
176  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
177 
179  polygon->setRingN(0, ring);
180 
181  // Setup canvas style
182  canvas.setPolygonContourColor(m_pen.color().rgba());
183  canvas.setPolygonContourWidth(m_pen.width());
184  canvas.setPolygonFillColor(m_brush.color().rgba());
185 
186  // Let's draw!
187  canvas.draw(polygon);
188 
189  delete polygon;
190 }
191 
193 {
194  m_coords.clear();
195 
196  QPixmap* draft = m_display->getDraftPixmap();
197  draft->fill(Qt::transparent);
198 
199  m_display->repaint();
200 }
201 
203 {
204  if(m_coords.empty())
205  return;
206 
207  m_coords.push_back(m_lastPos);
208  drawGeometry();
209 }
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
Definition: Canvas.cpp:1862
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
Definition: Canvas.cpp:1620
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
void setLineWidth(int w)
It sets the line width.
Definition: Canvas.cpp:1725
PolygonAcquire(MapDisplay *display, QObject *parent=0)
It constructs a PolygonAcquire tool associated with the given map display.
bool mouseMoveEvent(QMouseEvent *e)
This class implements a concrete tool to acquire a polygon geometry.
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:62
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
Definition: Canvas.cpp:2104
void setPolygonFillColor(const te::color::RGBAColor &color)
It sets the color used to fill the draw of polygon geometries.
Definition: Canvas.cpp:1732
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
bool mouseReleaseEvent(QMouseEvent *e)
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
Definition: Canvas.cpp:296
QPen m_pen
The pen used to draw the path.
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
QBrush m_brush
The brush used to draw the path.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:160
A canvas built on top of Qt.
Definition: Canvas.h:54
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
A widget to control the display of a set of layers.
Definition: MapDisplay.h:65
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:313
void setPolygonContourWidth(int w)
It sets the polygon contour width.
Definition: Canvas.cpp:1919
bool mousePressEvent(QMouseEvent *e)
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
void drawPolygon(Canvas &canvas)
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
bool mouseDoubleClickEvent(QMouseEvent *e)