All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MoveGeometryTool.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/MoveGeometryTool.cpp
22 
23  \brief This class implements a concrete tool to move geometries.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/ObjectId.h"
28 #include "../../../qt/widgets/canvas/MapDisplay.h"
29 #include "../../../qt/widgets/Utils.h"
30 #include "../../Feature.h"
31 #include "../../RepositoryManager.h"
32 #include "../../Utils.h"
33 #include "../Renderer.h"
34 #include "../Utils.h"
35 #include "MoveGeometryTool.h"
36 
37 // Qt
38 #include <QMessageBox>
39 #include <QMouseEvent>
40 #include <QPainter>
41 #include <QPixmap>
42 
43 // STL
44 #include <cassert>
45 #include <memory>
46 
48  : AbstractTool(display, parent),
49  m_layer(layer),
50  m_feature(0),
51  m_moveStarted(false)
52 {
53  // Signals & slots
54  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
55 
56  updateCursor();
57 
58  draw();
59 }
60 
62 {
63  delete m_feature;
64 }
65 
67 {
68  if(e->button() != Qt::LeftButton)
69  return false;
70 
71  pickFeature(m_layer, GetPosition(e));
72 
73  if(m_feature)
74  m_moveStarted = true;
75 
76  m_origin = m_display->transform(GetPosition(e));
77 
78  m_delta *= 0;
79 
80  return true;
81 }
82 
84 {
85  if(!m_moveStarted)
86  return false;
87 
88  QPointF currentPosition = m_display->transform(GetPosition(e));
89 
90  // Calculates the delta value
91  m_delta = currentPosition - m_origin;
92 
93  // Move geometry using the current delta
94  MoveGeometry(m_feature->getGeometry(), m_delta.x(), m_delta.y());
95 
96  storeEditedFeature();
97 
98  draw();
99 
100  m_origin = currentPosition;
101 
102  return false;
103 }
104 
106 {
107  m_moveStarted = false;
108 
109  return false;
110 }
111 
113 {
114  return false;
115 }
116 
118 {
119  delete m_feature;
120  m_feature = 0;
121 
122  m_moveStarted = false;
123 
124  m_origin *= 0;
125  m_delta *= 0;
126 }
127 
129 {
130  reset();
131 
132  te::gm::Envelope env = buildEnvelope(pos);
133 
134  try
135  {
136  m_feature = PickFeature(m_layer, env, m_display->getSRID());
137 
138  draw();
139  }
140  catch(std::exception& e)
141  {
142  QMessageBox::critical(m_display, tr("Error"), QString(tr("The geometry cannot be selected from the layer. Details:") + " %1.").arg(e.what()));
143  }
144 }
145 
147 {
148  QPointF pixelOffset(4.0, 4.0);
149 
150  QRectF rect(pos - pixelOffset, pos + pixelOffset);
151 
152  // Converts rect boundary to world coordinates
153  QPointF ll(rect.left(), rect.bottom());
154  QPointF ur(rect.right(), rect.top());
155  ll = m_display->transform(ll);
156  ur = m_display->transform(ur);
157 
158  te::gm::Envelope env(ll.x(), ll.y(), ur.x(), ur.y());
159 
160  return env;
161 }
162 
164 {
165  const te::gm::Envelope& env = m_display->getExtent();
166  if(!env.isValid())
167  return;
168 
169  // Clear!
170  QPixmap* draft = m_display->getDraftPixmap();
171  draft->fill(Qt::transparent);
172 
173  // Initialize the renderer
174  Renderer& renderer = Renderer::getInstance();
175  renderer.begin(draft, env, m_display->getSRID());
176 
177  // Draw the layer edited geometries
178  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
179 
180  if(m_feature == 0)
181  {
182  renderer.end();
183  m_display->repaint();
184  return;
185  }
186 
187  // Draw the vertexes
188  if(RepositoryManager::getInstance().hasIdentify(m_layer->getId(), m_feature->getId()) == false)
189  renderer.draw(m_feature->getGeometry(), true);
190  else
191  renderer.drawVertexes(m_feature->getGeometry());
192 
193  renderer.end();
194 
195  m_display->repaint();
196 }
197 
199 {
200  m_display->setCursor(Qt::ArrowCursor);
201 }
202 
204 {
205  draw();
206 }
207 
209 {
210  RepositoryManager::getInstance().addGeometry(m_layer->getId(), m_feature->getId()->clone(), dynamic_cast<te::gm::Geometry*>(m_feature->getGeometry()->clone()));
211 }
void addGeometry(const std::string &source, te::gm::Geometry *geom)
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
This class implements a concrete tool to move geometries.
static Renderer & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
TEEDITEXPORT Feature * PickFeature(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &env, int srid)
Definition: Utils.cpp:55
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
Definition: Utils.cpp:42
void draw(te::gm::Geometry *geom, bool showVertexes=false)
Definition: Renderer.cpp:141
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
TEEDITEXPORT void MoveGeometry(te::gm::Geometry *geom, const double &deltax, const double &deltay)
Definition: Utils.cpp:273
This is a singleton for rendering geometries and features.
Definition: Renderer.h:67
te::gm::Envelope buildEnvelope(const QPointF &pos)
void drawVertexes(te::gm::Geometry *geom)
Definition: Renderer.cpp:157
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:55
void drawRepository(const std::string &source, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:73
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:160
MoveGeometryTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, QObject *parent=0)
It constructs a move geometry tool associated with the given map display.
void pickFeature(const te::map::AbstractLayerPtr &layer, const QPointF &pos)
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438