All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Measure.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/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 <QtGui/QMouseEvent>
42 #include <QtGui/QPainter>
43 #include <QtGui/QPixmap>
44 
45 // STL
46 #include <cassert>
47 
48 te::qt::widgets::Measure::Measure(te::qt::widgets::MapDisplay* display, const MeasureType& measureType, QObject* parent)
49  : AbstractTool(display, parent),
50  m_measureType(measureType),
51  m_isFinished(false),
52  m_unit("")
53 {
54  // Setups the path style
55  m_pen.setColor(QColor(100, 177, 216));
56  m_pen.setWidth(3);
57  m_brush = QColor(100, 177, 216, 80);
58 
60  {
62  if (unit.get())
63  m_unit = unit->getSymbol();
64  }
65  // Signals & slots
66  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
67 }
68 
70 {
71  QPixmap* draft = m_display->getDraftPixmap();
72  draft->fill(Qt::transparent);
73 }
74 
76 {
77  if(e->button() != Qt::LeftButton)
78  return false;
79 
80  if(m_isFinished) // Is Finished?! So, start again...
81  {
82  clear();
83  m_isFinished = false;
84  }
85 
86  // Angle case: the maximum is 2 points collected...
87  if(m_coords.size() == 2 && m_measureType == Angle)
88  return false; // No collect more!
89 
90  QPointF pw = m_display->transform(e->pos());
91  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
92 
93  return true;
94 }
95 
97 {
98  if(m_coords.size() < 1 || m_isFinished)
99  return false;
100 
101  QPointF pw = m_display->transform(e->posF());
102  m_coords.push_back(te::gm::Coord2D(pw.x(), pw.y()));
103 
104  QPointF pos = e->posF() + QPointF(0.0001, 0.0001); // To avoid collinear points on polygon
105  pw = m_display->transform(pos);
106  m_lastPos = te::gm::Coord2D(pw.x(), pw.y());
107 
108  drawGeometry();
109 
110  return false;
111 }
112 
114 {
115  return false;
116 }
117 
119 {
120  if(e->button() != Qt::LeftButton)
121  return false;
122 
123  if(m_measureType == Area && m_coords.size() < 3) // Can not stop yet...
124  return false;
125 
126  m_isFinished = true;
127 
128  return true;
129 }
130 
132 {
133  const te::gm::Envelope& env = m_display->getExtent();
134  if(!env.isValid())
135  return;
136 
137  // Clear!
138  QPixmap* draft = m_display->getDraftPixmap();
139  draft->fill(Qt::transparent);
140 
141  // Prepares the canvas
142  Canvas canvas(m_display->width(), m_display->height());
143  canvas.setDevice(draft, false);
144  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
145  canvas.setRenderHint(QPainter::Antialiasing, true);
146 
147  // Let's draw!
148  switch(m_measureType)
149  {
150  case Distance:
151  case Angle:
152  drawLine(canvas);
153  break;
154 
155  case Area:
156  drawPolygon(canvas);
157  break;
158  }
159 
160  m_coords.pop_back();
161 
162  m_display->repaint();
163 }
164 
166 {
167  // Build the geometry
168  te::gm::LineString* line = new te::gm::LineString(m_coords.size(), te::gm::LineStringType);
169  for(std::size_t i = 0; i < m_coords.size(); ++i)
170  line->setPoint(i, m_coords[i].x, m_coords[i].y);
171 
172  // Setup canvas style
173  canvas.setLineColor(m_pen.color().rgba());
174  canvas.setLineWidth(m_pen.width());
175 
176  // Let's draw!
177  canvas.draw(line);
178 
179  // Measuring and feedback...
180  if(m_measureType == Distance)
181  {
182  drawText(canvas, (tr("Distance: ") + QString::number(calculateLength(line)) + " " + m_unit.c_str()).toStdString(), line->getEndPoint());
183  }
184  else if(m_measureType == Angle)
185  {
186  if(line->getNPoints() >= 3)
187  drawText(canvas, (tr("Angle: ") + QString::number(calculateAngle(line)) + " deg").toStdString(), line->getPointN(1));
188  }
189 
190  delete line;
191 }
192 
194 {
195  if(m_coords.size() < 3)
196  return drawLine(canvas);
197 
198  // Build the geometry
199  te::gm::LinearRing* ring = new te::gm::LinearRing(m_coords.size() + 1, te::gm::LineStringType);
200  for(std::size_t i = 0; i < m_coords.size(); ++i)
201  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
202  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
203 
205  polygon->setRingN(0, ring);
206 
207  // Setup canvas style
208  canvas.setPolygonContourColor(m_pen.color().rgba());
209  canvas.setPolygonContourWidth(m_pen.width());
210  canvas.setPolygonFillColor(m_brush.color().rgba());
211 
212  // Let's draw!
213  canvas.draw(polygon);
214 
215  // Measuring and feedback...
216  const te::gm::Envelope* env = polygon->getMBR();
217  te::gm::Point p(env->getCenter().x, env->getCenter().y);
218  drawText(canvas, (tr("Area: ") + QString::number(polygon->getArea()) + (m_unit.empty() ? "" : (" " + m_unit + "^2").c_str())).toStdString(), &p);
219 
220  delete polygon;
221 }
222 
223 void te::qt::widgets::Measure::drawText(Canvas& canvas, const std::string& text, te::gm::Point* p)
224 {
225  // Testing...
226  canvas.setTextColor(te::color::RGBAColor(0, 0, 0, TE_OPAQUE));
227  canvas.setFontFamily("Arial");
228  canvas.setTextWeight(te::at::Bold);
229  canvas.setTextPointSize(9);
230  canvas.setTextContourEnabled(true);
231  canvas.setTextContourWidth(3);
232  canvas.setTextContourColor(te::color::RGBAColor(255, 255, 255, TE_OPAQUE));
233  canvas.drawText(p, text);
234 }
235 
237 {
238  m_coords.clear();
239 
240  QPixmap* draft = m_display->getDraftPixmap();
241  draft->fill(Qt::transparent);
242 
243  m_display->repaint();
244 }
245 
247 {
248  double length = 0.0;
249  std::size_t n = line->getNPoints();
250  for(std::size_t i = 0; i < n - 1; ++i)
251  length += line->getPointN(i)->distance( line->getPointN(i + 1));
252 
253  return length;
254 }
255 
257 {
258  std::size_t n = line->getNPoints();
259  assert(n >= 3);
260 
261  te::gm::Point* p1 = line->getPointN(0);
262  te::gm::Point* p2 = line->getPointN(1);
263  te::gm::Point* p3 = line->getPointN(2);
264 
265  QLineF line1(p2->getX(), p2->getY(), p1->getX(), p1->getY());
266  QLineF line2(p2->getX(), p2->getY(), p3->getX(), p3->getY());
267 
268  return line1.angle(line2);
269 }
270 
272 {
273  if (m_display->getSRID()!= TE_UNKNOWN_SRS)
274  {
276  if (unit.get())
277  m_unit = unit->getSymbol();
278  else
279  m_unit = "";
280  }
281  else
282  m_unit = "";
283 
284  if(m_coords.empty())
285  return;
286 
287  m_coords.push_back(m_lastPos);
288  drawGeometry();
289 }
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 mouseReleaseEvent(QMouseEvent *e)
Definition: Measure.cpp:113
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
bool mouseMoveEvent(QMouseEvent *e)
Definition: Measure.cpp:96
void setTextContourWidth(int width)
It sets the text contour width.
Definition: Canvas.cpp:1516
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
Definition: Canvas.cpp:1620
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
Definition: LineString.h:193
MeasureType
Defines the measure type measured by this tool.
Definition: Measure.h:77
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
This class implements a concrete tool to measure operation (distance, area, and angle).
~Measure()
Destructor.
Definition: Measure.cpp:69
bool mouseDoubleClickEvent(QMouseEvent *e)
Definition: Measure.cpp:118
void setLineWidth(int w)
It sets the line width.
Definition: Canvas.cpp:1725
double y
y-coordinate.
Definition: Coord2D.h:87
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:392
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:62
void drawText(Canvas &canvas, const std::string &text, te::gm::Point *p)
Definition: Measure.cpp:223
std::string m_unit
The unit symbol for the measure.
Definition: Measure.h:147
Measure(MapDisplay *display, const MeasureType &measureType, QObject *parent=0)
It constructs a measure tool associated with the given map display.
Definition: Measure.cpp:48
void drawLine(Canvas &canvas)
Definition: Measure.cpp:165
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:72
double calculateLength(te::gm::LineString *line) const
Definition: Measure.cpp:246
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
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
bool mousePressEvent(QMouseEvent *e)
Definition: Measure.cpp:75
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
Definition: Canvas.cpp:296
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
Definition: MapDisplay.cpp:73
void setTextPointSize(double psize)
It sets the text point Size.
Definition: Canvas.cpp:1441
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
void setTextContourColor(const te::color::RGBAColor &color)
It sets the text contour (outline) drawing color.
Definition: Canvas.cpp:1498
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
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
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:160
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A canvas built on top of Qt.
Definition: Canvas.h:54
A point with x and y coordinate values.
Definition: Point.h:50
void setTextWeight(te::at::FontWeight weight)
It sets the text weight.
Definition: Canvas.cpp:1460
void setTextColor(const te::color::RGBAColor &color)
It sets the text drawing color.
Definition: Canvas.cpp:1423
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 setFontFamily(const std::string &family)
It sets the text font family.
Definition: Canvas.cpp:1436
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
Definition: Envelope.cpp:48
Point * getPointN(std::size_t i) const
It returns the specified point in this LineString.
Definition: LineString.cpp:283
Point * getEndPoint() const
It returns the curve end point.
Definition: LineString.cpp:213
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:313
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void setPolygonContourWidth(int w)
It sets the polygon contour width.
Definition: Canvas.cpp:1919
double calculateAngle(te::gm::LineString *line) const
Definition: Measure.cpp:256
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:103
QBrush m_brush
The brush used to draw the path.
Definition: Measure.h:145
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
QPen m_pen
The pen used to draw the path.
Definition: Measure.h:144
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
double x
x-coordinate.
Definition: Coord2D.h:86
void setTextContourEnabled(bool b)
It controls the display of the text outline.
Definition: Canvas.cpp:1505
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void drawPolygon(Canvas &canvas)
Definition: Measure.cpp:193