CreatePointTool.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/CreatePointTool.cpp
22 
23  \brief This class implements a concrete tool to create points.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Envelope.h"
28 #include "../../../geometry/Geometry.h"
29 #include "../../../geometry/Point.h"
30 #include "../../../qt/widgets/canvas/MapDisplay.h"
31 #include "../../../qt/widgets/Utils.h"
32 #include "../../Feature.h"
33 #include "../../RepositoryManager.h"
34 #include "../../Utils.h"
35 #include "../Renderer.h"
36 #include "../Utils.h"
37 #include "CreatePointTool.h"
38 
39 // Qt
40 #include <QMessageBox>
41 #include <QMouseEvent>
42 #include <QPainter>
43 #include <QPixmap>
44 
45 // STL
46 #include <cassert>
47 #include <memory>
48 
50  : GeometriesUpdateTool(display, layer.get(), parent),
51  m_isFinished(false)
52 {
53  setCursor(cursor);
54 }
55 
57 
59 {
60  if (e->button() != Qt::LeftButton)
61  return false;
62 
63  if(m_isFinished) // Is Finished?! So, start again...
64  {
65  clear();
66  m_isFinished = false;
67  }
68 
69  QPointF pw = m_display->transform(GetPosition(e));
70 
71  te::gm::Coord2D coord = te::gm::Coord2D(pw.x(), pw.y());
72 
73  TrySnap(coord, m_display->getSRID());
74 
75  clear();
76 
77  m_coords.push_back(coord);
78 
79  draw();
80 
81  return true;
82 }
83 
85 {
86  if(e->button() != Qt::LeftButton)
87  return false;
88 
89  m_isFinished = true;
90 
91  storeFeature();
92 
93  emit geometriesEdited();
94 
95  return true;
96 }
97 
99 {
100  const te::gm::Envelope& env = m_display->getExtent();
101  if(!env.isValid())
102  return;
103 
104  // Clear!
105  QPixmap* draft = m_display->getDraftPixmap();
106  draft->fill(Qt::transparent);
107 
108  // Initialize the renderer
109  Renderer& renderer = Renderer::getInstance();
110  renderer.begin(draft, env, m_display->getSRID());
111 
112  // Draw the layer edited geometries
113  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
114 
115  if(!m_coords.empty())
116  {
117  // Draw the geometry being created
118  te::gm::Geometry* point = buildPoint();
119  renderer.draw(point);
120  }
121 
122  renderer.end();
123 
124  m_display->repaint();
125 }
126 
128 {
129  m_coords.clear();
130 }
131 
133 {
134  te::gm::Point* point = new te::gm::Point(m_coords[0].x, m_coords[0].y, m_display->getSRID());
135  point->setSRID(m_display->getSRID());
136 
137  return point;
138 }
139 
141 {
142  m_feature = new Feature();
145 
147 
148  delete m_feature;
149  m_feature = nullptr;
150 }
151 
153 {
154  clear();
155 }
virtual const std::string & getId() const
It returns the layer id.
void setSRID(int srid) _NOEXCEPT_OP(true)
It sets the Spatial Reference System ID of the Point.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
Feature * clone() const
Definition: Feature.cpp:182
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
std::vector< te::gm::Coord2D > m_coords
The coord list managed by this tool.
CreatePointTool(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.
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
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
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
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
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
bool m_isFinished
A flag that indicates if the operations was finished.
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
This class implements a concrete tool to create points.
te::map::AbstractLayer * m_layer
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
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 setCursor(const QCursor &cursor)
It sets the tool cursor.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
void setGeometry(te::gm::Geometry *geom)
Definition: Feature.cpp:95
~CreatePointTool()
Destructor.
te::gm::Geometry * buildPoint()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool isValid() const
It tells if the rectangle is valid or not.