All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  const te::gm::Envelope& env = m_display->getExtent();
144  if(!env.isValid())
145  return;
146 
147  // Clear!
148  QPixmap* draft = m_display->getDraftPixmap();
149  draft->fill(Qt::transparent);
150 
151  // Prepares the canvas
152  Canvas canvas(m_display->width(), m_display->height());
153  canvas.setDevice(draft, false);
154  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
155  canvas.setRenderHint(QPainter::Antialiasing, true);
156 
157  // Let's draw!
158  switch(m_measureType)
159  {
160  case Distance:
161  case Angle:
162  drawLine(canvas);
163  break;
164 
165  case Area:
166  drawPolygon(canvas);
167  break;
168  }
169 
170  m_coords.pop_back();
171 
172  m_display->repaint();
173 }
174 
176 {
177  // Build the geometry
178  te::gm::LineString* line = new te::gm::LineString(m_coords.size(), te::gm::LineStringType);
179  for(std::size_t i = 0; i < m_coords.size(); ++i)
180  line->setPoint(i, m_coords[i].x, m_coords[i].y);
181 
182  // Setup canvas style
183  canvas.setLineColor(m_pen.color().rgba());
184  canvas.setLineWidth(m_pen.width());
185 
186  // Let's draw!
187  canvas.draw(line);
188 
189  // Measuring and feedback...
190  if(m_measureType == Distance)
191  {
192  drawText(canvas, (tr("Distance: ") + QString::number(calculateLength(line)) + " " + m_unit.c_str()).toStdString(), line->getEndPoint());
193  }
194  else if(m_measureType == Angle)
195  {
196  if(line->getNPoints() >= 3)
197  drawText(canvas, (tr("Angle: ") + QString::number(calculateAngle(line)) + " deg").toStdString(), line->getPointN(1));
198  }
199 
200  delete line;
201 }
202 
204 {
205  if(m_coords.size() < 3)
206  return drawLine(canvas);
207 
208  // Build the geometry
209  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
210  for(std::size_t i = 0; i < m_coords.size(); ++i)
211  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
212  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
213 
215  polygon->setRingN(0, ring);
216 
217  // Setup canvas style
218  canvas.setPolygonContourColor(m_pen.color().rgba());
219  canvas.setPolygonContourWidth(m_pen.width());
220  canvas.setPolygonFillColor(m_brush.color().rgba());
221 
222  // Let's draw!
223  canvas.draw(polygon);
224 
225  // Measuring and feedback...
226  const te::gm::Envelope* env = polygon->getMBR();
227  te::gm::Point p(env->getCenter().x, env->getCenter().y);
228  drawText(canvas, (tr("Area: ") + QString::number(polygon->getArea()) + (m_unit.empty() ? "" : (" " + m_unit + "^2").c_str())).toStdString(), &p);
229 
230  delete polygon;
231 }
232 
233 void te::qt::widgets::Measure::drawText(Canvas& canvas, const std::string& text, te::gm::Point* p)
234 {
235  // Testing...
236  canvas.setTextColor(te::color::RGBAColor(0, 0, 0, TE_OPAQUE));
237  canvas.setFontFamily("Arial");
238  canvas.setTextWeight(te::at::Bold);
239  canvas.setTextPointSize(9);
240  canvas.setTextContourEnabled(true);
241  canvas.setTextContourWidth(3);
242  canvas.setTextContourColor(te::color::RGBAColor(255, 255, 255, TE_OPAQUE));
243  canvas.drawText(p, text);
244 }
245 
247 {
248  m_coords.clear();
249 
250  QPixmap* draft = m_display->getDraftPixmap();
251  draft->fill(Qt::transparent);
252 
253  m_display->repaint();
254 }
255 
257 {
258  double length = 0.0;
259  std::size_t n = line->getNPoints();
260  for(std::size_t i = 0; i < n - 1; ++i)
261  length += line->getPointN(i)->distance( line->getPointN(i + 1));
262 
263  return length;
264 }
265 
267 {
268  std::size_t n = line->getNPoints();
269  assert(n >= 3);
270 
271  te::gm::Point* p1 = line->getPointN(0);
272  te::gm::Point* p2 = line->getPointN(1);
273  te::gm::Point* p3 = line->getPointN(2);
274 
275  QLineF line1(p2->getX(), p2->getY(), p1->getX(), p1->getY());
276  QLineF line2(p2->getX(), p2->getY(), p3->getX(), p3->getY());
277 
278  return line1.angle(line2);
279 }
280 
282 {
283  if (m_display->getSRID()!= TE_UNKNOWN_SRS)
284  {
286  if (unit.get())
287  m_unit = unit->getSymbol();
288  else
289  m_unit = "";
290  }
291  else
292  m_unit = "";
293 
294  if(m_coords.empty())
295  return;
296 
297  m_coords.push_back(m_lastPos);
298  drawGeometry();
299 }
void drawText(Canvas &canvas, const std::string &text, te::gm::Point *p)
Definition: Measure.cpp:233
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.
Definition: Canvas.cpp:2104
double y
y-coordinate.
Definition: Coord2D.h:114
Point * getEndPoint() const
It returns the curve end point.
Definition: LineString.cpp:253
double x
x-coordinate.
Definition: Coord2D.h:113
void setTextWeight(te::at::FontWeight weight)
It sets the text weight.
Definition: Canvas.cpp:1460
Point * getPointN(std::size_t i) const
It returns the specified point in this LineString.
Definition: LineString.cpp:323
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
QBrush m_brush
The brush used to draw the path.
Definition: Measure.h:145
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
void setTextContourWidth(int width)
It sets the text contour width.
Definition: Canvas.cpp:1516
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:62
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.
Definition: Canvas.cpp:296
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:266
This class implements a concrete tool to measure operation (distance, area, and angle).
void drawPolygon(Canvas &canvas)
Definition: Measure.cpp:203
~Measure()
Destructor.
Definition: Measure.cpp:71
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
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.
Definition: Canvas.cpp:1436
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
QPen m_pen
The pen used to draw the path.
Definition: Measure.h:144
double calculateLength(te::gm::LineString *line) const
Definition: Measure.cpp:256
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
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
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
void setLineWidth(int w)
It sets the line width.
Definition: Canvas.cpp:1725
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
Definition: MapDisplay.cpp:73
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
Definition: Canvas.cpp:1620
void setTextContourColor(const te::color::RGBAColor &color)
It sets the text contour (outline) drawing color.
Definition: Canvas.cpp:1498
void setPolygonContourWidth(int w)
It sets the polygon contour width.
Definition: Canvas.cpp:1919
virtual void setCursor(const QCursor &cursor)
It sets the tool cursor.
void drawText(int x, int y, const std::string &txt, float angle=0.0, te::at::HorizontalAlignment hAlign=te::at::Start, te::at::VerticalAlignment vAlign=te::at::Baseline)
It draws a text.
Definition: Canvas.cpp:1224
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
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
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
A canvas built on top of Qt.
Definition: Canvas.h:54
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
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
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:160
void setTextContourEnabled(bool b)
It controls the display of the text outline.
Definition: Canvas.cpp:1505
void drawLine(Canvas &canvas)
Definition: Measure.cpp:175
void setTextColor(const te::color::RGBAColor &color)
It sets the text drawing color.
Definition: Canvas.cpp:1423
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:104
virtual double distance(const Geometry *const rhs) const
It returns the shortest distance between any two points in the two geometry objects.
Definition: Geometry.cpp:453
std::string m_unit
The unit symbol for the measure.
Definition: Measure.h:147
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
void setTextPointSize(double psize)
It sets the text point Size.
Definition: Canvas.cpp:1441
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.