All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../RepositoryManager.h"
35 #include "../../Utils.h"
36 #include "../Renderer.h"
37 #include "../Utils.h"
38 #include "CreateLineTool.h"
39 
40 // Qt
41 #include <QMouseEvent>
42 #include <QPainter>
43 #include <QPixmap>
44 
45 // STL
46 #include <cassert>
47 #include <memory>
48 
49 te::edit::CreateLineTool::CreateLineTool(te::qt::widgets::MapDisplay* display, const te::map::AbstractLayerPtr& layer, const QCursor& cursor, QObject* parent)
50  : AbstractTool(display, parent),
51  m_layer(layer),
52  m_continuousMode(false),
53  m_isFinished(false)
54 {
55  setCursor(cursor);
56 
57  // Signals & slots
58  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
59 
60  draw();
61 }
62 
64 {
65  QPixmap* draft = m_display->getDraftPixmap();
66  draft->fill(Qt::transparent);
67 }
68 
70 {
71  if(e->button() != Qt::LeftButton)
72  return false;
73 
74  if(m_isFinished) // Is Finished?! So, start again...
75  {
76  clear();
77  m_isFinished = false;
78  }
79 
80  QPointF pw = m_display->transform(GetPosition(e));
81 
82  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
83 
84  TrySnap(coord, m_display->getSRID());
85 
86  m_coords.push_back(coord);
87 
88  return true;
89 }
90 
92 {
93  if(m_coords.size() < 1 || m_isFinished)
94  return false;
95 
96  QPointF pos = GetPosition(e);
97 
98  QPointF pw = m_display->transform(pos);
99 
100  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
101 
102  TrySnap(coord, m_display->getSRID());
103 
104  m_coords.push_back(coord);
105 
106  m_lastPos = te::gm::Coord2D(coord.x, coord.y);
107 
108  Qt::KeyboardModifiers keys = e->modifiers();
109 
110  if(keys == Qt::NoModifier)
111  m_continuousMode = false;
112  else if(keys == Qt::ShiftModifier)
113  m_continuousMode = true;
114 
115  draw();
116 
117  return false;
118 }
119 
121 {
122  return false;
123 }
124 
126 {
127  if(e->button() != Qt::LeftButton)
128  return false;
129 
130  m_isFinished = true;
131 
132  storeNewGeometry();
133 
134  return true;
135 }
136 
138 {
139  const te::gm::Envelope& env = m_display->getExtent();
140  if(!env.isValid())
141  return;
142 
143  // Clear!
144  QPixmap* draft = m_display->getDraftPixmap();
145  draft->fill(Qt::transparent);
146 
147  // Initialize the renderer
148  Renderer& renderer = Renderer::getInstance();
149  renderer.begin(draft, env, m_display->getSRID());
150 
151  // Draw the layer edited geometries
152  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
153 
154  if(!m_coords.empty())
155  {
156  // Draw the geometry being created
157  te::gm::Geometry* line = buildLine();
158  renderer.draw(line, true);
159 
160  if(m_continuousMode == false)
161  m_coords.pop_back();
162  }
163 
164  renderer.end();
165 
166  m_display->repaint();
167 }
168 
170 {
171  m_coords.clear();
172 }
173 
175 {
176  te::gm::LineString* line = new te::gm::LineString(m_coords.size(), te::gm::LineStringType);
177  for(std::size_t i = 0; i < m_coords.size(); ++i)
178  line->setPoint(i, m_coords[i].x, m_coords[i].y);
179 
180  line->setSRID(m_display->getSRID());
181 
182  if(line->getSRID() == m_layer->getSRID())
183  return line;
184 
185  // else, need conversion...
186  line->transform(m_layer->getSRID());
187 
188  return line;
189 }
190 
192 {
193  RepositoryManager::getInstance().addGeometry(m_layer->getId(), buildLine());
194 }
195 
197 {
198  if(m_coords.empty())
199  return;
200 
201  m_coords.push_back(m_lastPos);
202 
203  draw();
204 }
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
void addGeometry(const std::string &source, te::gm::Geometry *geom)
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.
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
static Renderer & getInstance()
It returns a reference to the singleton instance.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:353
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
Definition: Utils.cpp:42
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
virtual void setCursor(const QCursor &cursor)
It sets the tool cursor.
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
This is a singleton for rendering geometries and features.
Definition: Renderer.h:67
TEEDITEXPORT void TrySnap(te::gm::Coord2D &coord, int srid)
Definition: Utils.cpp:343
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
void setSRID(int srid)
It sets the Spatial Reference System ID of the linestring.
Definition: LineString.cpp:176
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:160
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.
Definition: Envelope.h:438
void transform(int srid)
It converts the coordinate values of the linestring to the new spatial reference system.
Definition: LineString.cpp:181