SelectionTool.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 SelectionTool.cpp
22 
23  \brief An example of MapDisplay Tool. The only purpose of this tool is to show how you can implement a new tool. Do not consider it as a final application.
24 */
25 
26 // Example
27 #include "SelectionTool.h"
28 
29 // TerraLib
31 #include <terralib/dataaccess.h>
32 #include <terralib/geometry.h>
36 
37 // Qt
38 #include <QMouseEvent>
39 #include <QToolTip>
40 
41 // STL
42 #include <cassert>
43 #include <memory>
44 
46  : te::qt::widgets::AbstractTool(display, parent)
47 {
48  assert(layer);
49 
50  m_layer = dynamic_cast<te::map::DataSetLayer*>(layer);
51  assert(m_layer);
52 
53  // Signals & slots
54  connect(m_display, SIGNAL(extentChanged()), SLOT(onExtentChanged()));
55 }
56 
58 {
60 
61  QPixmap* draft = m_display->getDraftPixmap();
62  draft->fill(Qt::transparent);
63 }
64 
66 {
67  if(e->button() != Qt::LeftButton)
68  return false;
69 
70  // Converts clicked point to world coordinates
71 #if QT_VERSION >= 0x050000
72  QPointF qpoint = m_display->transform(e->localPos());
73 #else
74  QPointF qpoint = m_display->transform(e->posF());
75 #endif
76 
77  // Get the datasource and the transactor
79  std::unique_ptr<te::da::DataSourceTransactor> transactor(dataSource->getTransactor());
80 
81  // Bulding restriction geometry
82  te::gm::Point point(qpoint.x(), qpoint.y());
83 
84  // Gets the dataset
85  std::unique_ptr<te::da::DataSetType> datasetType = transactor->getDataSetType(m_layer->getDataSetName());
86  te::dt::Property* p = datasetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
87  std::string propname = p->getName();
88 
89  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet(m_layer->getDataSetName(),propname, &point, te::gm::INTERSECTS);
90 
91  // Clear the geometries
93  m_geoms.clear();
94 
95  // For feature attributes
96  std::size_t nproperties = dataset->getNumProperties();
97  QString information("<h2>Attributes</h2><ul>");
98 
99  // Find the geometries that really intersect the clicked point
100  while(dataset->moveNext())
101  {
102  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
103  std::unique_ptr<te::gm::Geometry> g = dataset->getGeometry(pos);
104  if(g->intersects(&point))
105  {
106  // Stores the geometry
107  m_geoms.push_back(g.get());
108 
109  // Building the features attributes text
110  for(std::size_t i = 0; i < nproperties; ++i)
111  {
112  int propertyType = dataset->getPropertyDataType(i);
113  if(propertyType != te::dt::GEOMETRY_TYPE)
114  information += "<li><b>" + QString::fromUtf8(dataset->getPropertyName(i).c_str()) + ":</b> " + QString::fromUtf8(dataset->getAsString(i, 3).c_str()) + "</li>";
115  }
116  }
117  }
118  information += "</ul>";
119 
120  // Draws the selected geometries
121  drawGeometries();
122 
123  // Show attributes
124  if(!m_geoms.empty())
125  QToolTip::showText(QCursor::pos(), information, m_display, m_display->rect());
126 
127  return true;
128 }
129 
131 {
132  // Clear!
133  QPixmap* draft = m_display->getDraftPixmap();
134  draft->fill(Qt::transparent);
135 
136  // Prepares the canvas
138  te::qt::widgets::Canvas canvas(draft);
139  canvas.setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
140  canvas.setPolygonFillColor(te::color::RGBAColor(0, 200, 0, TE_OPAQUE));
141  canvas.setPolygonContourColor(te::color::RGBAColor(0, 120, 0, TE_OPAQUE));
142  canvas.setPolygonContourWidth(3);
143 
144  // Let's draw!
145  for(std::size_t i = 0; i < m_geoms.size(); ++i)
146  canvas.draw(m_geoms[i]);
147 
148  // Updates the display
149  m_display->repaint();
150 }
151 
153 {
154  drawGeometries();
155 }
A canvas built on top of Qt.
boost::shared_ptr< DataSource > DataSourcePtr
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
A widget to control the display of a set of layers.
A Qt4 widget to control the display of a set of layers.
A layer with reference to a dataset.
It models a property definition.
Definition: Property.h:59
std::vector< te::gm::Geometry * > m_geoms
Definition: SelectionTool.h:87
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
URI C++ Library.
Definition: Attributes.h:37
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
te::gm::Polygon * p
const std::string & getDataSetName() const
te::map::DataSetLayer * m_layer
Definition: SelectionTool.h:86
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
An example of MapDisplay Tool. The only purpose of this tool is to show how you can implement a new t...
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
This file contains include headers for the Vector Geometry model of TerraLib.
SelectionTool(te::qt::widgets::MapDisplay *display, te::map::DataSetLayer *layer, QObject *parent=0)
This file contains include headers for the Data Access module of TerraLib.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
virtual const std::string & getDataSourceId() const
This file contains several utility functions for dealing with STL containers.
void onExtentChanged()
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
const std::string & getName() const
It returns the property name.
Definition: Property.h:127