All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ToolBar.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/qt/plugins/edit/ToolBar.cpp
22 
23  \brief The main toolbar of TerraLib Edit Qt plugin.
24 */
25 
26 // Terralib
27 #include "../../../common/Exception.h"
28 #include "../../../common/Translator.h"
29 #include "../../../dataaccess/dataset/ObjectId.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../edit/Feature.h"
32 #include "../../../edit/Repository.h"
33 #include "../../../edit/RepositoryManager.h"
34 #include "../../../edit/qt/tools/CreateLineTool.h"
35 #include "../../../edit/qt/tools/CreatePolygonTool.h"
36 #include "../../../edit/qt/tools/MoveGeometryTool.h"
37 #include "../../../edit/qt/tools/VertexTool.h"
38 #include "../../../edit/qt/SnapOptionsDialog.h"
39 #include "../../../maptools/DataSetLayer.h"
40 #include "../../../memory/DataSet.h"
41 #include "../../../memory/DataSetItem.h"
42 #include "../../af/events/LayerEvents.h"
43 #include "../../af/events/MapEvents.h"
44 #include "../../af/ApplicationController.h"
45 #include "../../af/Project.h"
46 #include "ToolBar.h"
47 
48 // Qt
49 #include <QActionGroup>
50 #include <QMessageBox>
51 
52 // Boost
53 #include <boost/ptr_container/ptr_vector.hpp>
54 
55 // STL
56 #include <cassert>
57 #include <list>
58 #include <vector>
59 
61  : m_toolBar(0),
62  m_editAction(0),
63  m_saveAction(0),
64  m_vertexToolAction(0),
65  m_createPolygonToolAction(0),
66  m_createLineToolAction(0),
67  m_moveGeometryToolAction(0),
68  m_snapOptionsAction(0)
69 {
70  initialize();
71 }
72 
74 {
75  delete m_toolBar;
76 }
77 
79 {
80  return m_toolBar;
81 }
82 
84 {
87 
89 
90  return layer;
91 }
92 
94 {
95  std::list<te::map::AbstractLayerPtr> layers = te::qt::af::ApplicationController::getInstance().getProject()->getSingleLayers();
96 
97  std::list<te::map::AbstractLayerPtr>::iterator it;
98  for(it = layers.begin(); it != layers.end(); ++it)
99  {
100  if((*it)->getId() == id)
101  return *it;
102  }
103 
104  throw te::common::Exception(TE_TR("Could not retrieve the layer."));
105 }
106 
108 {
109  // Create the main toolbar
110  m_toolBar = new QToolBar;
111 
112  initializeActions();
113 }
114 
116 {
117  // Enable Edition Mode
118  createAction(m_editAction, tr("Turn on/off edition mode"), "edit-enable", true, true, SLOT(onEditActivated(bool)));
119  m_toolBar->addAction(m_editAction);
120 
121  // Save
122  createAction(m_saveAction, tr("Save edition"), "edit-save", false, false, SLOT(onSaveActivated()));
123  m_toolBar->addAction(m_saveAction);
124 
125  m_toolBar->addSeparator();
126 
127  // Tools
128  createAction(m_vertexToolAction, tr("Vertex Tool - Move, add and remove"), "edit-vertex-tool", true, false, SLOT(onVertexToolActivated(bool)));
129  createAction(m_createPolygonToolAction, tr("Create Polygon"), "edit-create-polygon", true, false, SLOT(onCreatePolygonToolActivated(bool)));
130  createAction(m_createLineToolAction, tr("Create Line"), "edit-create-line", true, false, SLOT(onCreateLineToolActivated(bool)));
131  createAction(m_moveGeometryToolAction, tr("Move Geometry"), "edit-move-geometry", true, false, SLOT(onMoveGeometryToolActivated(bool)));
132 
133  // Get the action group of map tools.
134  QActionGroup* toolsGroup = te::qt::af::ApplicationController::getInstance().findActionGroup("Map.ToolsGroup");
135  assert(toolsGroup);
136 
137  // Adding the new tools
138  toolsGroup->addAction(m_vertexToolAction);
139  toolsGroup->addAction(m_createPolygonToolAction);
140  toolsGroup->addAction(m_createLineToolAction);
141  toolsGroup->addAction(m_moveGeometryToolAction);
142 
143  // Grouping...
144  m_tools.push_back(m_vertexToolAction);
145  m_tools.push_back(m_createPolygonToolAction);
146  m_tools.push_back(m_createLineToolAction);
147  m_tools.push_back(m_moveGeometryToolAction);
148 
149  // Adding tools to toolbar
150  for(int i = 0; i < m_tools.size(); ++i)
151  m_toolBar->addAction(m_tools[i]);
152 
153  // Snap
154  createAction(m_snapOptionsAction, tr("Snap Options"), "edit-snap", false, false, SLOT(onSnapOptionsActivated()));
155  m_toolBar->addSeparator();
156  m_toolBar->addAction(m_snapOptionsAction);
157 }
158 
159 void te::qt::plugins::edit::ToolBar::createAction(QAction*& action, const QString& tooltip, const QString& icon, bool checkable, bool enabled, const char* member)
160 {
161  action = new QAction(this);
162  action->setIcon(QIcon::fromTheme(icon));
163  action->setToolTip(tooltip);
164  action->setCheckable(checkable);
165  action->setEnabled(enabled);
166  connect(action, SIGNAL(triggered(bool)), this, member);
167 }
168 
170 {
171  m_saveAction->setEnabled(checked);
172 
173  for(int i = 0; i < m_tools.size(); ++i)
174  m_tools[i]->setEnabled(checked);
175 
176  m_snapOptionsAction->setEnabled(checked);
177 }
178 
180 {
181  try
182  {
183  std::map<std::string, te::edit::Repository*> repositories = te::edit::RepositoryManager::getInstance().getRepositories();
184 
185  std::map<std::string, te::edit::Repository*>::iterator it;
186  for(it = repositories.begin(); it != repositories.end(); ++it) // for each repository
187  {
188  // The current repository
189  te::edit::Repository* repo = it->second;
190  assert(repo);
191 
192  // Retrieve the layer associated with the current repository
193  te::map::AbstractLayerPtr layer = getLayer(it->first);
194  assert(layer.get());
195 
196  // Get the layer schema
197  std::auto_ptr<te::map::LayerSchema> schema(layer->getSchema());
198  assert(schema.get());
199 
200  // Get the property names that compose the object id
201  std::vector<std::string> oidPropertyNames;
202  te::da::GetOIDPropertyNames(schema.get(), oidPropertyNames);
203 
204  // Get the edited geometries
205  const std::vector<te::edit::Feature*>& features = repo->getAllFeatures();
206 
207  // Build the DataSet that will be used to update
208  std::auto_ptr<te::mem::DataSet> memds(new te::mem::DataSet(schema.get()));
209 
210  // Get the geometry property position
211  std::size_t gpos = te::da::GetFirstSpatialPropertyPos(memds.get());
212  assert(gpos != std::string::npos);
213 
214  for(std::size_t i = 0; i < features.size(); ++i) // for each edited feature
215  {
216  // Create the new item
217  te::mem::DataSetItem* item = new te::mem::DataSetItem(memds.get());
218 
219  // Get the object id
220  te::da::ObjectId* oid = features[i]->getId();
221  assert(oid);
222 
223  const boost::ptr_vector<te::dt::AbstractData>& values = oid->getValue();
224  assert(values.size() == oidPropertyNames.size());
225 
226  // Get the edited geometry
227  te::gm::Geometry* geom = features[i]->getGeometry();
228  assert(geom);
229 
230  // Fill the new item
231  for(std::size_t j = 0; j < values.size(); ++j)
232  item->setValue(oidPropertyNames[j], values[j].clone());
233 
234  item->setGeometry(gpos, static_cast<te::gm::Geometry*>(geom->clone()));
235 
236  memds->add(item);
237  }
238 
239  // For while, use DataSetLayer to get the DataSource
240  te::map::DataSetLayer* dslayer = dynamic_cast<te::map::DataSetLayer*>(layer.get());
241  assert(dslayer);
242 
244  assert(ds.get());
245 
246  std::set<int> gproperty;
247  gproperty.insert(gpos);
248 
249  std::vector<std::set<int> > properties;
250  for(std::size_t i = 0; i < memds->size(); ++i)
251  properties.push_back(gproperty);
252 
253  std::vector<std::size_t> oidPropertyPosition;
254  for(std::size_t i = 0; i < oidPropertyNames.size(); ++i)
255  oidPropertyPosition.push_back(te::da::GetPropertyPos(memds.get(), oidPropertyNames[i]));
256 
257  ds->update(dslayer->getDataSetName(), memds.get(), properties, oidPropertyPosition);
258 
259  repo->clear();
260  }
261  }
262  catch(te::common::Exception& e)
263  {
264  QMessageBox::critical(0, tr("TerraLib Edit Qt Plugin"), e.what());
265  return;
266  }
267 }
268 
270 {
271  te::map::AbstractLayerPtr layer = getSelectedLayer();
272  if(layer.get() == 0)
273  {
274  QMessageBox::information(0, tr("TerraLib Edit Qt Plugin"), tr("Select a layer first!"));
275  return;
276  }
277 
280 
281  assert(e.m_display);
282 
284  e.m_display->setCurrentTool(tool);
285 }
286 
288 {
289  te::map::AbstractLayerPtr layer = getSelectedLayer();
290  if(layer.get() == 0)
291  {
292  QMessageBox::information(0, tr("TerraLib Edit Qt Plugin"), tr("Select a layer first!"));
293  return;
294  }
295 
298 
299  assert(e.m_display);
300 
301  te::edit::CreatePolygonTool* tool = new te::edit::CreatePolygonTool(e.m_display->getDisplay(), layer, Qt::ArrowCursor, 0);
302  e.m_display->setCurrentTool(tool);
303 }
304 
306 {
307  te::map::AbstractLayerPtr layer = getSelectedLayer();
308  if(layer.get() == 0)
309  {
310  QMessageBox::information(0, tr("TerraLib Edit Qt Plugin"), tr("Select a layer first!"));
311  return;
312  }
313 
316 
317  assert(e.m_display);
318 
319  te::edit::CreateLineTool* tool = new te::edit::CreateLineTool(e.m_display->getDisplay(), layer, Qt::ArrowCursor, 0);
320  e.m_display->setCurrentTool(tool);
321 }
322 
324 {
325  te::map::AbstractLayerPtr layer = getSelectedLayer();
326  if(layer.get() == 0)
327  {
328  QMessageBox::information(0, tr("TerraLib Edit Qt Plugin"), tr("Select a layer first!"));
329  return;
330  }
331 
334 
335  assert(e.m_display);
336 
338  e.m_display->setCurrentTool(tool);
339 }
340 
342 {
343  te::edit::SnapOptionsDialog options(m_toolBar);
344  options.setLayers(te::qt::af::ApplicationController::getInstance().getProject()->getAllLayers(false));
345  options.exec();
346 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
const std::string & getDataSetName() const
te::map::AbstractLayerPtr m_layer
Layer selected.
Definition: LayerEvents.h:308
This class implements a concrete tool to move geometries.
This event is used to get a single layer selected in layer tree.
Definition: LayerEvents.h:297
const std::vector< Feature * > & getAllFeatures() const
Definition: Repository.cpp:161
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
void createAction(QAction *&action, const QString &tooltip, const QString &icon, bool checkable, bool enabled, const char *member)
Definition: ToolBar.cpp:159
A dialog used to configure geometry snap options.
te::qt::widgets::MapDisplay * getDisplay()
Definition: MapDisplay.cpp:116
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
void onMoveGeometryToolActivated(bool checked)
Definition: ToolBar.cpp:323
This class implements a concrete tool to create lines.
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:500
te::map::AbstractLayerPtr getSelectedLayer()
Definition: ToolBar.cpp:83
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void onCreateLineToolActivated(bool checked)
Definition: ToolBar.cpp:305
void setLayers(const std::list< te::map::AbstractLayerPtr > &layers)
This class implements a concrete tool to create polygons.
This class implements a concrete tool for vertex operations (move, add, etc.).
Definition: VertexTool.h:70
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
static ApplicationController & getInstance()
It returns a reference to the singleton instance.
void setCurrentTool(te::qt::widgets::AbstractTool *tool)
Updates the current tool being used on te::qt::widgets::MapDisplay.
Definition: MapDisplay.cpp:185
const std::string & getDataSourceId() const
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
This event can be used to retrieve the MapDisplat component.
Definition: MapEvents.h:96
The main toolbar of TerraLib Edit Qt plugin.
void onEditActivated(bool checked)
Definition: ToolBar.cpp:169
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
QToolBar * get() const
Definition: ToolBar.cpp:78
te::map::AbstractLayerPtr getLayer(const std::string &id)
Definition: ToolBar.cpp:93
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:309
This class represents a repository of geometries and features.
Definition: Repository.h:62
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
te::qt::af::MapDisplay * m_display
Definition: MapEvents.h:107
void onCreatePolygonToolActivated(bool checked)
Definition: ToolBar.cpp:287
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void onVertexToolActivated(bool checked)
Definition: ToolBar.cpp:269