Measure.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/Measure.cpp
22 
23  \brief This class implements a concrete tool to measure operation (distance, area, and angle).
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../common/UnitOfMeasure.h"
29 #include "../../../common/UnitsOfMeasureManager.h"
30 #include "../../../geometry/Envelope.h"
31 #include "../../../geometry/Geometry.h"
32 #include "../../../geometry/LinearRing.h"
33 #include "../../../geometry/LineString.h"
34 #include "../../../geometry/Point.h"
35 #include "../../../geometry/Polygon.h"
36 #include "../canvas/Canvas.h"
37 #include "../canvas/MapDisplay.h"
38 #include "Measure.h"
39 
40 // Qt
41 #include <QMouseEvent>
42 #include <QPainter>
43 #include <QPixmap>
44 
45 // STL
46 #include <cassert>
47 
48 te::qt::widgets::Measure::Measure(te::qt::widgets::MapDisplay* display, const MeasureType& measureType, const QCursor& cursor, QObject* parent)
49  : AbstractTool(display, parent),
50  m_measureType(measureType),
51  m_isFinished(false),
52  m_unit("")
53 {
54  setCursor(cursor);
55 
56  // Setups the path style
57  m_pen.setColor(QColor(100, 177, 216));
58  m_pen.setWidth(3);
59  m_brush = QColor(100, 177, 216, 80);
60 
62  {
64  if (unit.get())
65  m_unit = unit->getSymbol();
66  }
67  // Signals & slots
68  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
69 }
70 
72 {
73  QPixmap* draft = m_display->getDraftPixmap();
74  draft->fill(Qt::transparent);
75 }
76 
78 {
79  if(e->button() != Qt::LeftButton)
80  return false;
81 
82  if(m_isFinished) // Is Finished?! So, start again...
83  {
84  clear();
85  m_isFinished = false;
86  }
87 
88  // Angle case: the maximum is 2 points collected...
89  if(m_coords.size() == 2 && m_measureType == Angle)
90  return false; // No collect more!
91 
92  QPointF pw = m_display->transform(e->pos());
93  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
94 
95  return true;
96 }
97 
99 {
100  if(m_coords.size() < 1 || m_isFinished)
101  return false;
102 
103 #if QT_VERSION >= 0x050000
104  QPointF pw = m_display->transform(e->localPos());
105 #else
106  QPointF pw = m_display->transform(e->posF());
107 #endif
108  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
109 
110 #if QT_VERSION >= 0x050000
111  QPointF pos = e->localPos() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
112 #else
113  QPointF pos = e->posF() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
114 #endif
115  pw = m_display->transform(pos);
116  m_lastPos = te::gm::Coord2D(pw.x(), pw.y());
117 
118  drawGeometry();
119 
120  return false;
121 }
122 
124 {
125  return false;
126 }
127 
129 {
130  if(e->button() != Qt::LeftButton)
131  return false;
132 
133  if(m_measureType == Area && m_coords.size() < 3) // Can not stop yet...
134  return false;
135 
136  m_isFinished = true;
137 
138  return true;
139 }
140 
142 {
143  if (e->key() == Qt::Key_Escape)
144  {
145  clear();
146  m_isFinished = false;
147  }
148 
149  return true;
150 }
151 
153 {
154  const te::gm::Envelope& env = m_display->getExtent();
155  if(!env.isValid())
156  return;
157 
158  // Clear!
159  QPixmap* draft = m_display->getDraftPixmap();
160  draft->fill(Qt::transparent);
161 
162  // Prepares the canvas
163  Canvas canvas(m_display->width(), m_display->height());
164  canvas.setDevice(draft, false);
165  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
166  canvas.setRenderHint(QPainter::Antialiasing, true);
167 
168  // Let's draw!
169  switch(m_measureType)
170  {
171  case Distance:
172  case Angle:
173  drawLine(canvas);
174  break;
175 
176  case Area:
177  drawPolygon(canvas);
178  break;
179  }
180 
181  m_coords.pop_back();
182 
183  m_display->repaint();
184 }
185 
187 {
188  // Build the geometry
190  for(std::size_t i = 0; i < m_coords.size(); ++i)
191  line->setPoint(i, m_coords[i].x, m_coords[i].y);
192 
193  // Setup canvas style
194  canvas.setLineColor(m_pen.color().rgba());
195  canvas.setLineWidth(m_pen.width());
196 
197  // Let's draw!
198  canvas.draw(line);
199 
200  // Measuring and feedback...
201  if(m_measureType == Distance)
202  {
203  std::unique_ptr<te::gm::Point> endPoint = line->getEndPoint();
204  drawText(canvas, (tr("Distance: ") + QString::number(calculateLength(line)) + " " + m_unit.c_str()).toUtf8().data(), endPoint.get());
205  }
206  else if(m_measureType == Angle)
207  {
208  if (line->getNPoints() >= 3)
209  {
210  std::unique_ptr<te::gm::Point> point1 = line->getPointN(1);
211  drawText(canvas, (tr("Angle: ") + QString::number(calculateAngle(line)) + " deg").toUtf8().data(), point1.get());
212  }
213  }
214 
215  delete line;
216 }
217 
219 {
220  if(m_coords.size() < 3)
221  return drawLine(canvas);
222 
223  // Build the geometry
225  for(std::size_t i = 0; i < m_coords.size(); ++i)
226  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
227  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
228 
230  polygon->setRingN(0, ring);
231 
232  // Setup canvas style
233  canvas.setPolygonContourColor(m_pen.color().rgba());
234  canvas.setPolygonContourWidth(m_pen.width());
235  canvas.setPolygonFillColor(m_brush.color().rgba());
236 
237  // Let's draw!
238  canvas.draw(polygon);
239 
240  // Measuring and feedback...
241  const te::gm::Envelope* env = polygon->getMBR();
243  drawText(canvas, (tr("Area: ") + QString::number(polygon->getArea()) + (m_unit.empty() ? "" : (" " + m_unit + "^2").c_str())).toUtf8().data(), &p);
244 
245  delete polygon;
246 }
247 
248 void te::qt::widgets::Measure::drawText(Canvas& canvas, const std::string& text, te::gm::Point* p)
249 {
250  // Testing...
251  canvas.setTextColor(te::color::RGBAColor(0, 0, 0, TE_OPAQUE));
252  canvas.setFontFamily("Arial");
254  canvas.setTextPointSize(9);
255  canvas.setTextContourEnabled(true);
256  canvas.setTextContourWidth(3);
257  canvas.setTextContourColor(te::color::RGBAColor(255, 255, 255, TE_OPAQUE));
258  canvas.drawText(p, text);
259 }
260 
262 {
263  m_coords.clear();
264 
265  QPixmap* draft = m_display->getDraftPixmap();
266  draft->fill(Qt::transparent);
267 
268  m_display->repaint();
269 }
270 
272 {
273  double length = 0.0;
274  std::size_t n = line->getNPoints();
275  for (std::size_t i = 0; i < n - 1; ++i)
276  {
277  std::unique_ptr<te::gm::Point> pA = line->getPointN(i);
278  std::unique_ptr<te::gm::Point> pB = line->getPointN(i + 1);
279  length += pA->distance(pB.get());
280  }
281 
282  return length;
283 }
284 
286 {
287  std::unique_ptr<te::gm::Point> p1 = line->getPointN(0);
288  std::unique_ptr<te::gm::Point> p2 = line->getPointN(1);
289  std::unique_ptr<te::gm::Point> p3 = line->getPointN(2);
290 
291  QLineF line1(p2->getX(), p2->getY(), p1->getX(), p1->getY());
292  QLineF line2(p2->getX(), p2->getY(), p3->getX(), p3->getY());
293 
294  return line1.angle(line2);
295 }
296 
298 {
300  {
302  if (unit.get())
303  m_unit = unit->getSymbol();
304  else
305  m_unit = "";
306  }
307  else
308  m_unit = "";
309 
310  if(m_coords.empty())
311  return;
312 
313  m_coords.push_back(m_lastPos);
314  drawGeometry();
315 }
std::unique_ptr< Point > getEndPoint() const
It returns the curve end point.
void drawText(Canvas &canvas, const std::string &text, te::gm::Point *p)
Definition: Measure.cpp:248
bool m_isFinished
A flag that indicates if the operations was finished.
Definition: Measure.h:148
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Definition: Measure.cpp:77
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
double y
y-coordinate.
Definition: Coord2D.h:114
void drawText(int x, int y, const std::string &txt, float angle=0.0, double anchorX=0.5, double anchorY=0.5, int displacementX=0, int displacementY=0)
It draws a text.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
te::gm::Coord2D m_lastPos
The last position captured on mouse move event.
Definition: Measure.h:144
double x
x-coordinate.
Definition: Coord2D.h:113
double m_urx
Upper right corner x-coordinate.
QBrush m_brush
The brush used to draw the path.
Definition: Measure.h:147
std::unique_ptr< Point > getPointN(std::size_t i) const
It returns the specified point in this LineString.
void setPolygonFillColor(const te::color::RGBAColor &color)
It sets the color used to fill the draw of polygon geometries.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
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
void setTextContourWidth(int width)
It sets the text contour width.
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Definition: Measure.cpp:98
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
MeasureType
Defines the measure type measured by this tool.
Definition: Measure.h:77
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
Definition: Measure.cpp:128
double calculateAngle(te::gm::LineString *line) const
Definition: Measure.cpp:285
This class implements a concrete tool to measure operation (distance, area, and angle).
void drawPolygon(Canvas &canvas)
Definition: Measure.cpp:218
unsigned int line
~Measure()
Destructor.
Definition: Measure.cpp:71
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
void setFontFamily(const std::string &family)
It sets the text font family.
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
QPen m_pen
The pen used to draw the path.
Definition: Measure.h:146
std::vector< te::gm::Coord2D > m_coords
The coord list managed by the measure tool.
Definition: Measure.h:143
double calculateLength(te::gm::LineString *line) const
Definition: Measure.cpp:271
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
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 setTextContourColor(const te::color::RGBAColor &color)
It sets the text contour (outline) drawing color.
void setPolygonContourWidth(int w)
It sets the polygon contour width.
virtual void setCursor(const QCursor &cursor)
It sets the tool cursor.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
te::gm::Polygon * p
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
Definition: LineString.h:193
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
Definition: Measure.cpp:123
MeasureType m_measureType
The measure type.
Definition: Measure.h:145
void setTextWeight(te::se::Font::FontWeightType weight)
It sets the text weight.
Measure(MapDisplay *display, const MeasureType &measureType, const QCursor &cursor, QObject *parent=0)
It constructs a measure tool associated with the given map display.
Definition: Measure.cpp:48
bool keyPressEvent(QKeyEvent *e)
This event handler can be reimplemented in a concrete tool class to receive key press events for the ...
Definition: Measure.cpp:141
double m_lly
Lower left corner y-coordinate.
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
double m_ury
Upper right corner y-coordinate.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
void setTextContourEnabled(bool b)
It controls the display of the text outline.
void drawLine(Canvas &canvas)
Definition: Measure.cpp:186
void setTextColor(const te::color::RGBAColor &color)
It sets the text drawing color.
std::string m_unit
The unit symbol for the measure.
Definition: Measure.h:149
void setTextPointSize(double psize)
It sets the text point Size.
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.