CreateLineTool.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/CreateLineTool.cpp
22 
23  \brief This class implements a concrete tool to create lines.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Envelope.h"
28 #include "../../../geometry/Geometry.h"
29 #include "../../../geometry/LinearRing.h"
30 #include "../../../geometry/LineString.h"
31 #include "../../../geometry/Point.h"
32 #include "../../../qt/widgets/canvas/MapDisplay.h"
33 #include "../../../qt/widgets/Utils.h"
34 #include "../../Feature.h"
35 #include "../../RepositoryManager.h"
36 #include "../../Utils.h"
37 #include "../Renderer.h"
38 #include "../Utils.h"
39 #include "CreateLineTool.h"
40 
41 // Qt
42 #include <QMessageBox>
43 #include <QMouseEvent>
44 #include <QPainter>
45 #include <QPixmap>
46 
47 // STL
48 #include <cassert>
49 #include <memory>
50 
52  : GeometriesUpdateTool(display, layer.get(), parent),
53  m_continuousMode(false),
54  m_isFinished(false)
55 {
56  setCursor(cursor);
57 
58  // Signals & slots
59  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
60 
61  draw();
62 }
63 
65 
67 {
68  if (e->button() != Qt::LeftButton)
69  return false;
70 
71  if(m_isFinished) // Is Finished?! So, start again...
72  {
73  clear();
74  m_isFinished = false;
75  }
76 
77  QPointF pw = m_display->transform(GetPosition(e));
78 
79  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
80 
81  TrySnap(coord, m_display->getSRID());
82 
83  m_coords.push_back(coord);
84 
85  return true;
86 }
87 
89 {
90  if(m_coords.size() < 1 || m_isFinished)
91  return false;
92 
93  QPointF pos = GetPosition(e);
94 
95  QPointF pw = m_display->transform(pos);
96 
97  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
98 
99  TrySnap(coord, m_display->getSRID());
100 
101  m_coords.push_back(coord);
102 
103  m_lastPos = te::gm::Coord2D(coord.x, coord.y);
104 
105  if (e->buttons() & Qt::LeftButton)
106  m_continuousMode = true;
107  else
108  m_continuousMode = false;
109 
110  draw();
111 
112  return false;
113 }
114 
116 {
117  if(e->button() != Qt::LeftButton)
118  return false;
119 
120  m_isFinished = true;
121 
122  storeFeature();
123 
124  emit geometriesEdited();
125 
126  return true;
127 }
128 
130 {
131  const te::gm::Envelope& env = m_display->getExtent();
132  if(!env.isValid())
133  return;
134 
135  // Clear!
136  QPixmap* draft = m_display->getDraftPixmap();
137  draft->fill(Qt::transparent);
138 
139  // Initialize the renderer
140  Renderer& renderer = Renderer::getInstance();
141  renderer.begin(draft, env, m_display->getSRID());
142 
143  // Draw the layer edited geometries
144  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
145 
146  if(!m_coords.empty())
147  {
148  // Draw the geometry being created
150  renderer.draw(line, true);
151 
152  if(m_continuousMode == false)
153  m_coords.pop_back();
154 
155  }
156 
157  renderer.end();
158 
159  m_display->repaint();
160 
161 }
162 
164 {
165  m_feature = nullptr;
166 
167  m_coords.clear();
168 }
169 
171 {
173  for(std::size_t i = 0; i < m_coords.size(); ++i)
174  line->setPoint(i, m_coords[i].x, m_coords[i].y);
175 
176  line->setSRID(m_display->getSRID());
177 
178  return line;
179 }
180 
182 {
183  m_feature = new Feature();
186 
188 
189  delete m_feature;
190  m_feature = nullptr;
191 }
192 
194 {
195  if(m_coords.empty())
196  return;
197 
198  m_coords.push_back(m_lastPos);
199 
200  draw();
201 }
202 
204 {
205  clear();
206 }
virtual const std::string & getId() const
It returns the layer id.
bool m_isFinished
A flag that indicates if the operations was finished.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
This class implements a concrete tool to create lines.
Feature * clone() const
Definition: Feature.cpp:182
~CreateLineTool()
Destructor.
te::gm::Coord2D m_lastPos
The last position captured on mouse move event.
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
unsigned int line
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
void setOperationTypeId(const te::edit::OperationType &currentOperationType)
Definition: Feature.cpp:124
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
std::vector< te::gm::Coord2D > m_coords
The coord list managed by this tool.
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
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
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.
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
te::map::AbstractLayer * m_layer
TEEDITEXPORT void TrySnap(te::gm::Coord2D &coord, int srid)
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
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 setSRID(int srid)
It sets the Spatial Reference System ID of the linestring.
void setCursor(const QCursor &cursor)
It sets the tool cursor.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
bool m_continuousMode
A flag that indicates if the tool is working in &#39;continuous mode&#39;. i.e. the coordinates will be acqui...
void setGeometry(te::gm::Geometry *geom)
Definition: Feature.cpp:95
CreateLineTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, const QCursor &cursor, QObject *parent=0)
It constructs a create line tool associated with the given map display.
te::gm::Geometry * buildLine()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool isValid() const
It tells if the rectangle is valid or not.