src/terralib/qt/plugins/edit/Plugin.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/Plugin.cpp
22 
23  \brief Plugin implementation for the TerraLib Edit Qt Plugin.
24 */
25 
26 // TerraLib
27 #include "../../../common/Config.h"
28 #include "../../../core/translator/Translator.h"
29 #include "../../../core/logger/Logger.h"
30 #include "../../../edit/SnapManager.h"
31 #include "../../../edit/qt/Renderer.h"
32 #include "../../../maptools/AbstractLayer.h"
33 #include "../../af/ApplicationController.h"
34 #include "../../af/events/ApplicationEvents.h"
35 #include "../../af/events/LayerEvents.h"
36 #include "../../af/events/MapEvents.h"
37 #include "../../widgets/canvas/MapDisplay.h"
38 #include "../../widgets/canvas/Canvas.h"
39 #include "../../widgets/layer/explorer/LayerItem.h"
40 #include "../../widgets/layer/explorer/LayerItemView.h"
41 #include "Plugin.h"
42 #include "ToolBar.h"
43 #include "Stasher.h"
44 #include "EditedDelegate.h"
45 
46 // QT
47 #include <QAction>
48 #include <QObject>
49 #include <QMenu>
50 #include <QMenuBar>
51 
52 std::list<te::map::AbstractLayer*> GetVisibleLayers(const std::list<te::map::AbstractLayerPtr>& ls)
53 {
54  std::list<te::map::AbstractLayer*> res;
55 
56  for(std::list<te::map::AbstractLayerPtr>::const_iterator it = ls.begin(); it != ls.end(); ++it)
57  if((*it)->getVisibility() == te::map::VISIBLE)
58  res.push_back((*it).get());
59 
60  return res;
61 }
62 
64 {
65  QModelIndex idx = *view->selectionModel()->selectedIndexes().begin();
66 
67  if(view != nullptr && idx.isValid())
68  {
69  QVector<int> roles;
70  roles << Qt::DecorationRole;
71 
72 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
73  view->dataChanged(idx, idx, roles);
74 #else
75  view->dataChanged(idx, idx);
76 #endif
77  }
78 }
79 
81 {
82  QModelIndexList idxs = view->selectionModel()->selectedIndexes();
83 
84  if(idxs.isEmpty() || idxs.size() > 1)
85  return nullptr;
86 
87  te::qt::widgets::TreeItem* item = static_cast<te::qt::widgets::TreeItem*>((*idxs.begin()).internalPointer());
88 
89  if(item->getType() != "LAYER")
90  return nullptr;
91 
92  return ((te::qt::widgets::LayerItem*)item)->getLayer().get();
93 }
94 
96  : te::core::CppPlugin(pluginInfo), m_toolbar(nullptr)
97 {
98 }
99 
101 {
102  delete m_toolbar;
103 }
104 
106 {
107  if(m_initialized)
108  return;
109 
110  // Create the main toolbar
111  m_toolbar = new ToolBar(this);
112 
113  // Register the application framework listener
114  te::qt::af::AppCtrlSingleton::getInstance().addListener(this);
115 
116  onUpdateDelegate(true);
117 
119  connect(m_toolbar, SIGNAL(stashed(te::map::AbstractLayer*)), SLOT(onStashedLayer(te::map::AbstractLayer*)));
120  connect(m_toolbar, SIGNAL(geometriesEdited()), SLOT(onGeometriesChanged()));
121  connect(m_toolbar, SIGNAL(updateDelegate(bool)), SLOT(onUpdateDelegate(bool)));
122 
123  // Get plugins menu
124  QMenu* pluginsMenu = te::qt::af::AppCtrlSingleton::getInstance().getMenu("Plugins");
125 
126  // Insert menu before plugins last action
127  m_action = new QAction(this);
128  m_action->setText(tr("&Edition Tools"));
129  m_action->setIcon(QIcon::fromTheme("layer-edit"));
130 
131  pluginsMenu->addAction(m_action);
132 
133  connect(m_action, SIGNAL(triggered(bool)), this, SLOT(onActionActivated(bool)));
134 
135  TE_LOG_TRACE(TE_TR("TerraLib Edit Qt Plugin startup!"));
136 
137  m_initialized = true;
138 
139  std::set<std::string> sts = GetStashedLayers();
140 
141  for(std::set<std::string>::iterator it = sts.begin(); it != sts.end(); ++it)
142  m_delegate->addStashed(*it);
143 }
144 
146 {
147  if(!m_initialized)
148  return;
149 
150  onUpdateDelegate(false);
151 
152  TE_LOG_TRACE(TE_TR("TerraLib Edit Qt Plugin shutdown!"));
153 
154  m_initialized = false;
155 
156  te::qt::af::AppCtrlSingleton::getInstance().removeListener(this);
157 }
158 
160 {
162  e.m_toolbar = m_toolbar->get();
163  e.m_category = "Edition";
164 
165  emit triggered(&e);
166 }
167 
169 {
170  if (!m_toolbar->isEnabled())
171  return;
172 
173  switch(e->m_id)
174  {
176  {
177  drawStashed();
178  }
179  break;
180 
182  {
184 
186 
187  if(l == nullptr)
188  {
189  m_toolbar->updateLayer(nullptr, false);
190  return;
191  }
192 
194  emit triggered(&de);
195 
196  m_toolbar->updateLayer(evt->m_layer.get(), m_delegate->isStached(evt->m_layer->getTitle()));
197  }
198  break;
199 
200  default:
201  break;
202  }
203 }
204 
206 {
207  if(m_delegate->isStached(layer->getTitle()))
208  {
209  m_delegate->removeStashed(layer->getTitle());
210  RemoveStash(layer);
211 
213  emit triggered(&e);
214 
215  e.m_display->getDisplay()->refresh();
216  }
217  else
218  m_delegate->addStashed(layer->getTitle());
219 
221 }
222 
224 {
225  if (m_toolbar->getSelectedLayer() == nullptr)
226  return;
227 
229 
231 
232  drawStashed();
233 }
234 
236 {
237  if (!m_toolbar->isEnabled())
238  return;
239 
241  emit triggered(&e);
242 
243  const te::gm::Envelope& env = e.m_display->getDisplay()->getExtent();
245 
247 
248  if(l != nullptr && l->getVisibility() == te::map::VISIBLE)
249  {
250  // Initialize the renderer
251  QPixmap* draft = e.m_display->getDisplay()->getDraftPixmap();
252  draft->fill(Qt::transparent);
253 
255  renderer.begin(draft, env, e.m_display->getDisplay()->getSRID());
256 
257  // Draw the layer edited geometries
258  renderer.drawRepository(l->getId(), env, e.m_display->getDisplay()->getSRID());
259 
260  renderer.end();
261  }
262 
263  e.m_display->getDisplay()->repaint();
264 
265 }
266 
268 {
270 
271  if(view == nullptr)
272  return;
273 
274  if(add)
275  {
276  m_delegate = new EditDelegate((QStyledItemDelegate*)view->itemDelegate(), this);
277  view->setItemDelegate(m_delegate);
278  }
279  else
280  {
281  view->removeDelegate(m_delegate);
282  delete m_delegate;
283  m_delegate = nullptr;
284 
285  view->repaint();
286  }
287 }
288 
290 {
292 
293  emit triggered(&e);
294 
295  return e.m_layerExplorer;
296 }
297 
298 
virtual const std::string & getId() const
It returns the layer id.
virtual void refresh(bool redraw=false)
It updates the contents in the map display.
The main toolbar of TerraLib Edit Qt plugin.
Definition: ToolBar.h:74
unsigned int getWidth() const
It returns the MapDisplay current width in pixels.
This is the base class for layers.
Definition: AbstractLayer.h:77
An item that contains a te::map::AbstractLayerPtr.
Definition: LayerItem.h:51
static te::dt::Date de(2010, 12, 31)
virtual const std::string & getTitle() const
It returns the layer title.
A base class for application events.
void UpdateTreeView(QTreeView *view)
te::qt::widgets::LayerItemView * m_layerExplorer
te::map::AbstractLayerPtr m_layer
Pointer to the selected layer.
Definition: LayerEvents.h:196
te::map::AbstractLayerPtr getSelectedLayer()
Definition: ToolBar.cpp:204
double m_urx
Upper right corner x-coordinate.
Basic information about a plugin.
virtual Visibility getVisibility() const
It returns the layer visibility.
ToolBar * m_toolbar
Main toolbar of TerraLib Edit Qt Plugin.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
Defines a hierarchical structure.
This event indicates that the layer has been selected.
Definition: LayerEvents.h:183
double m_llx
Lower left corner x-coordinate.
static ApplicationController & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
void RemoveStash(const te::map::AbstractLayer *layer)
Definition: Stasher.cpp:167
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
This event can be used to retrieve the MapDisplat component.
Definition: MapEvents.h:96
te::qt::widgets::LayerItemView * getLayerExplorer()
URI C++ Library.
Definition: Attributes.h:37
void startup()
This method will be called by applications to startup some plugin&#39;s functionality.
The main toolbar of TerraLib Edit Qt plugin.
void addEdited(const std::string &lName)
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
A specialization of QTreeView for manipulate layers.
Definition: LayerItemView.h:78
void shutdown()
This method will be called by applicatons to shutdown plugin&#39;s functionality.
void onApplicationTriggered(te::qt::af::evt::Event *e)
QAction * m_action
Action used to call the process.
#define TERRALIB_PLUGIN_CALL_BACK_IMPL(PLUGIN_CLASS_NAME)
This macro should be used by C++ plugins in order to declare the exportable/callable DLL function...
void updateLayer(te::map::AbstractLayer *layer, const bool &stashed)
Definition: ToolBar.cpp:145
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
double m_lly
Lower left corner y-coordinate.
unsigned int getHeight() const
It returns the MapDisplay current height in pixels.
QToolBar * get() const
Definition: ToolBar.cpp:140
void removeDelegate(QStyledItemDelegate *d)
Removes the delegate from the tree.
Plugin implementation for the TerraLib Edit Qt Plugin.
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
double m_ury
Upper right corner y-coordinate.
std::list< te::map::AbstractLayer * > GetVisibleLayers(const std::list< te::map::AbstractLayerPtr > &ls)
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:59
void addStashed(const std::string &lName)
#define TE_LOG_TRACE(message)
Use this tag in order to log a message to the TerraLib default logger with the TRACE level...
Definition: Logger.h:293
void drawRepository(const std::string &source, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:78
te::map::AbstractLayer * GetSelectedLayer(QTreeView *view)
void removeStashed(const std::string &lName)
te::qt::af::MapDisplay * m_display
Definition: MapEvents.h:107
void triggered(te::qt::af::evt::Event *e)
bool isStached(const std::string &lName)
std::set< std::string > GetStashedLayers()
Definition: Stasher.cpp:145
void onStashedLayer(te::map::AbstractLayer *layer)
std::string getType() const
Returns the type of the item.
Plugin(const te::core::PluginInfo &pluginInfo)
void onActionActivated(bool)
Slot function used when a action was selected.