PointMove.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/PointMove.cpp
22 
23  \brief This class implements a concrete tool to geographic coordinate move a point on mouse click operation.
24 */
25 
26 // TerraLib
27 #include "../../../maptools/MarkRendererManager.h"
28 #include "../../../se/Mark.h"
29 #include "../../../se/Stroke.h"
30 #include "../../../se/Utils.h"
31 #include "../canvas/Canvas.h"
32 #include "../canvas/MapDisplay.h"
33 #include "PointMove.h"
34 
35 // Qt
36 #include <QMouseEvent>
37 
38 #define PATTERN_SIZE 12
39 
40 te::qt::widgets::PointMove::PointMove(te::qt::widgets::MapDisplay* display, std::map<unsigned int, QPointF>* points, const QCursor& cursor, QObject* parent)
41  : AbstractTool(display, parent),
42  m_moveStarted(false)
43 {
44  setCursor(cursor);
45  m_points = points;
46 }
47 
49 {
50 }
51 
53 {
54  if(e->button() != Qt::LeftButton)
55  return false;
56 
57 #if QT_VERSION >= 0x050000
58  QPointF p = e->localPos();
59 #else
60  QPointF p = e->posF();
61 #endif
62 
64 
65  m_moveStarted = false;
66 
67  if (m_points->size())
68  {
69  for (std::map<unsigned int, QPointF>::iterator it = m_points->begin(); it != m_points->end(); ++it)
70  {
71  te::gm::Envelope env_pt((*it).second.x(), (*it).second.y(), (*it).second.x(), (*it).second.y());
72  if (env.contains(env_pt))
73  {
74  m_ptMoving = (*it).first;
75  m_origin = (*it).second;
76  m_moveStarted = true;
77  break;
78  }
79  }
80  }
81 
82  if (m_moveStarted)
83  draw();
84 
85  return true;
86 }
87 
89 {
90 #if QT_VERSION >= 0x050000
91  QPointF p = e->localPos();
92 #else
93  QPointF p = e->posF();
94 #endif
95 
96  p = m_display->transform(p);
97  if (m_moveStarted)
98  {
99  m_points->at(m_ptMoving).setX(p.x());
100  m_points->at(m_ptMoving).setY(p.y());
101 
102  draw();
103  }
104  else
105  m_display->repaint();
106 
107  if (!p.isNull())
108  emit pointMoveMoving(p);
109 
110  return true;
111 }
112 
114 {
115  if (m_moveStarted)
116  {
117 #if QT_VERSION >= 0x050000
118  QPointF p = e->localPos();
119 #else
120  QPointF p = e->posF();
121 #endif
122 
123  p = m_display->transform(p);
124 
125  m_moveStarted = false;
126 
127  if (!p.isNull())
128  {
129  m_points->at(m_ptMoving) = p;
130  emit pointMoved(m_origin, p);
131  }
132  }
133 
134  return true;
135 }
136 
137 
139 {
140  QPointF pixelOffset(4.0, 4.0);
141 
142  QRectF rect(pos - pixelOffset, pos + pixelOffset);
143 
144  // Converts rect boundary to world coordinates
145  QPointF ll(rect.left(), rect.bottom());
146  QPointF ur(rect.right(), rect.top());
147  ll = m_display->transform(ll);
148  ur = m_display->transform(ur);
149 
150  te::gm::Envelope env(ll.x(), ll.y(), ur.x(), ur.y());
151 
152  return env;
153 }
154 
156 {
157  const te::gm::Envelope& env = m_display->getExtent();
158  if (!env.isValid())
159  return;
160 
161  te::se::Stroke* strokeSel = te::se::CreateStroke("#000000", "1");
162  te::se::Fill* fillSel = te::se::CreateFill("#FF0000", "1.0");
163  te::se::Mark* markSelected = te::se::CreateMark("cross", strokeSel, fillSel);
164  te::color::RGBAColor** rgbaMarkSelected = te::map::MarkRendererManager::getInstance().render(markSelected, PATTERN_SIZE);
165  //define mark unselected
166 
167  te::se::Stroke* strokeUnsel = te::se::CreateStroke("#000000", "1");
168  te::se::Fill* fillUnsel = te::se::CreateFill("#00FF00", "1.0");
169  te::se::Mark* markUnselected = te::se::CreateMark("cross", strokeUnsel, fillUnsel);
170  te::color::RGBAColor** rgbaMarkUnselected = te::map::MarkRendererManager::getInstance().render(markUnselected, PATTERN_SIZE);
171 
172  // Clear!
173  QPixmap* draft = m_display->getDraftPixmap();
174  draft->fill(Qt::transparent);
175 
176  std::unique_ptr<te::qt::widgets::Canvas> CanvasInstance(new te::qt::widgets::Canvas(draft));
177  CanvasInstance->setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
178  CanvasInstance->setTextContourEnabled(true);
179  CanvasInstance->setTextContourWidth(2);
180  CanvasInstance->setTextContourColor(te::color::RGBAColor(0, 0, 0, TE_OPAQUE));
181 
182  for (std::map<unsigned int, QPointF> :: iterator it = m_points->begin(); it != m_points->end(); ++it)
183  {
184  unsigned int i = (*it).first;
185  QPointF pt = (*it).second;
186  if (i == m_ptMoving)
187  {
188  CanvasInstance->setPointPattern(rgbaMarkSelected, PATTERN_SIZE, PATTERN_SIZE);
189  CanvasInstance->setTextColor(te::color::RGBAColor(255, 0, 0, TE_OPAQUE));
190  }
191  else
192  {
193  CanvasInstance->setPointPattern(rgbaMarkUnselected, PATTERN_SIZE, PATTERN_SIZE);
194  CanvasInstance->setTextColor(te::color::RGBAColor(0, 255, 0, TE_OPAQUE));
195  }
196 
197  te::gm::Point Point(pt.x(), pt.y());
198  CanvasInstance->draw(&Point);
199  //ref text
200  QMatrix matrix = CanvasInstance->getMatrix();
201  QPointF pointCanvas = matrix.map(pt);
202  pointCanvas.setY(pointCanvas.y() + 15);
203  QPointF pointGeo = matrix.inverted().map(pointCanvas);
204  Point.setX(pointGeo.x());
205  Point.setY(pointGeo.y());
206  CanvasInstance->drawText(&Point, QString::number(i).toUtf8().data());
207  }
208 
209  m_display->repaint();
210 
211 }
PointMove(MapDisplay *display, std::map< unsigned int, QPointF > *points, const QCursor &cursor, QObject *parent=0)
It constructs a coordinate tracking tool associated with the given map display.
Definition: PointMove.cpp:40
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
bool m_moveStarted
Flag that indicates if move operation was started.
Definition: PointMove.h:105
double m_urx
Upper right corner x-coordinate.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
A widget to control the display of a set of layers.
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
~PointMove()
Destructor.
Definition: PointMove.cpp:48
void pointMoved(QPointF &coordinate_original, QPointF &coordinate_new)
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.
static MarkRendererManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
#define PATTERN_SIZE
Definition: PointMove.cpp:38
bool contains(const Envelope &rhs) const
It returns true if this envelope "spatially contains" the rhs envelope.
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
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Definition: PointMove.cpp:52
double m_lly
Lower left corner y-coordinate.
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
QPointF m_origin
Origin point on mouse pressed.
Definition: PointMove.h:106
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
double m_ury
Upper right corner y-coordinate.
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
void pointMoveMoving(QPointF &coordinate)
This class implements a concrete tool to geographic coordinate move a point on mouse operation...
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 setY(const double &y)
It sets the Point y-coordinate value.
Definition: Point.h:159
te::gm::Envelope buildEnvelope(const QPointF &pos)
Definition: PointMove.cpp:138
std::map< unsigned int, QPointF > * m_points
Definition: PointMove.h:108
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
Definition: PointMove.cpp:113
bool isValid() const
It tells if the rectangle is valid or not.
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Definition: PointMove.cpp:88