RotateGeometryTool.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/edit/qt/tools/RotateGeometryTool.cpp
22 
23  \brief This class implements a concrete tool to rotate geometries.
24 */
25 
26 // TerraLib
27 #include "../../../qt/widgets/canvas/MapDisplay.h"
28 #include "../../Feature.h"
29 #include "../../RepositoryManager.h"
30 #include "../Renderer.h"
31 #include "../Utils.h"
32 #include "RotateGeometryTool.h"
33 
34 // Qt
35 #include <QMessageBox>
36 
38  : GeometriesUpdateTool(display, layer.get(), parent),
39  m_rotateStarted(false),
40  m_angle(0)
41 {
42  setCursor(Qt::ArrowCursor);
43 
44  draw();
45 }
46 
48 
50 {
51  if (e->button() != Qt::LeftButton)
52  return false;
53 
55 
56  if (m_feature)
57  m_rotateStarted = true;
58 
59  m_origin = GetPosition(e);
60 
61  m_axisX = 0.;
62 
63  return true;
64 }
65 
67 {
68  if(!m_rotateStarted)
69  return false;
70 
71  QPointF currentPosition = GetPosition(e);
72 
73  // Calculates the axis X value
74  m_axisX = currentPosition.x() - m_origin.x();
75 
76  // Rotate geometry using the current axis X
77  if (m_feature)
78  {
79  if (m_axisX > 0 )
80  m_angle += 1;
81  else
82  m_angle -= 1;
83 
84  if (std::abs(m_angle) > 180)
85  m_angle = 0;
86 
87  Rotate(m_feature->getGeometry(), (m_axisX > 0 ? -1 : 1));
88  }
89 
90  draw();
91 
92  m_origin = currentPosition;
93 
94  return true;
95 }
96 
98 {
99  if (e->button() != Qt::LeftButton)
100  return false;
101 
102  m_rotateStarted = false;
103 
104  storeFeature();
105 
106  draw();
107 
108  return true;
109 }
110 
112 {
113  delete m_feature;
114 
115  m_rotateStarted = false;
116 
117  m_origin *= 0;
118  m_axisX = 0.;
119 
120  m_angle = 0;
121 }
122 
124 {
125  reset();
126 
127  te::gm::Envelope env = buildEnvelope(pos);
128 
129  try
130  {
132 
133  draw();
134  }
135  catch(std::exception& e)
136  {
137  QMessageBox::critical(m_display, tr("Error"), QString(tr("The geometry cannot be selected from the layer. Details:") + " %1.").arg(e.what()));
138  }
139 }
140 
142 {
143  QPointF pixelOffset(4.0, 4.0);
144 
145  QRectF rect(pos - pixelOffset, pos + pixelOffset);
146 
147  // Converts rect boundary to world coordinates
148  QPointF ll(rect.left(), rect.bottom());
149  QPointF ur(rect.right(), rect.top());
150  ll = m_display->transform(ll);
151  ur = m_display->transform(ur);
152 
153  te::gm::Envelope env(ll.x(), ll.y(), ur.x(), ur.y());
154 
155  return env;
156 }
157 
159 {
160  const te::gm::Envelope& env = m_display->getExtent();
161  if(!env.isValid())
162  return;
163 
164  // Clear!
165  QPixmap* draft = m_display->getDraftPixmap();
166  draft->fill(Qt::transparent);
167 
168  // Initialize the renderer
169  Renderer& renderer = Renderer::getInstance();
170  renderer.begin(draft, env, m_display->getSRID());
171 
172  // Draw the layer edited geometries
173  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
174 
175  if (m_feature == nullptr)
176  {
177  renderer.end();
178  m_display->repaint();
179  return;
180  }
181 
182  // Draw the vertexes
183  if (RepositoryManager::getInstance().hasIdentify(m_layer->getId(), m_feature->getId()) == false)
184  renderer.draw(m_feature, true);
185  else
186  renderer.drawVertexes(m_feature->getGeometry());
187 
188  // Draw the angle
190  std::unique_ptr<te::gm::Point> point(new te::gm::Point(centroid.x, centroid.y, m_display->getSRID()));
191 
192  renderer.drawText((QString::number(m_angle) + tr(" deg")).toUtf8().data(), point.get());
193 
194  renderer.end();
195 
196  m_display->repaint();
197 }
198 
200 {
201  if (m_feature == nullptr)
202  return;
203 
205 }
virtual const std::string & getId() const
It returns the layer id.
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
This class implements a concrete tool to rotate geometries.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
te::da::ObjectId * getId() const
Definition: Feature.cpp:134
te::gm::Envelope buildEnvelope(const QPointF &pos)
Feature * clone() const
Definition: Feature.cpp:182
TEEDITEXPORT void Rotate(te::gm::Geometry *geom, double angle)
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:139
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
te::gm::Coord2D getCentroid() const _NOEXCEPT_OP(false)
It will get the centroid of the input geometries.
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
TEEDITEXPORT Feature * PickFeature(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &env, int srid)
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
void draw(te::gm::Geometry *geom, bool showVertexes=false)
Definition: Renderer.cpp:260
QPointF m_origin
Origin point on mouse pressed.
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
~RotateGeometryTool()
Destructor.
void drawText(const std::string &text, te::gm::Point *p)
Definition: Renderer.cpp:325
RotateGeometryTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, QObject *parent=0)
It constructs a rotate geometry tool associated with the given map display.
void pickFeature(const QPointF &pos)
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
te::map::AbstractLayer * m_layer
bool m_rotateStarted
Flag that indicates if move operation was started.
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
void drawVertexes(te::gm::Geometry *geom)
Definition: Renderer.cpp:292
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:59
void drawRepository(const std::string &source, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:78
void setCursor(const QCursor &cursor)
It sets the tool cursor.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool isValid() const
It tells if the rectangle is valid or not.