LayerItemView.cpp
Go to the documentation of this file.
1 #include "LayerItemView.h"
2 
3 #include "LayerItemModel.h"
4 #include "LayerViewDelegate.h"
5 #include "LayerViewMenuManager.h"
6 #include "LayerItem.h"
7 #include "RuleItem.h"
8 #include "StyleItem.h"
9 
10 // Qt
11 #include <QMouseEvent>
12 
13 bool Expand(const QModelIndex& idx, te::qt::widgets::LayerItemView* /*view*/)
14 {
15  te::qt::widgets::TreeItem* item = static_cast<te::qt::widgets::TreeItem*>(idx.internalPointer());
16 
17  return (item->getType() == "FOLDER");
18 }
19 
20 std::list<te::map::AbstractLayerPtr> GetSelectedLayersOnly(te::qt::widgets::LayerItemView* view)
21 {
22  std::list<te::map::AbstractLayerPtr> res;
23  std::list<te::qt::widgets::TreeItem*> items = view->getSelectedItems();
24 
25  for(std::list<te::qt::widgets::TreeItem*>::iterator it = items.begin(); it != items.end(); ++it)
26  if((*it)->getType() == "LAYER")
27  res.push_back(((te::qt::widgets::LayerItem*)(*it))->getLayer());
28 
29  return res;
30 }
31 
33  QTreeView(parent),
34  m_outterFilter(nullptr)
35 {
36  setAcceptDrops(true);
37  setDragEnabled(true);
38 
39  setDefaultDropAction(Qt::MoveAction);
40 
41  setRootIsDecorated(true);
42  setSelectionMode(QAbstractItemView::ExtendedSelection);
43 
44  viewport()->setAutoFillBackground(true);
45 
46  m_model = new LayerItemModel(this);
47 
48  m_mnuMger = new LayerViewMenuManager(this);
49 
50  installEventFilter(m_mnuMger);
51 
52  setModel(m_model);
53 
54  connect(m_model, SIGNAL(visibilityChanged()), SIGNAL(visibilityChanged()));
56 }
57 
59 
60 void te::qt::widgets::LayerItemView::addLayers(const std::list<te::map::AbstractLayerPtr>& layers, const QModelIndex& idx, const std::string& idxPath)
61 {
62  m_model->addLayers(layers, idx, idxPath);
63 
64  if(idx.isValid() && Expand(idx, this))
65  expand(idx);
66 
67  selectionModel()->clearSelection();
68 }
69 
70 void te::qt::widgets::LayerItemView::setLayers(const std::list<te::map::AbstractLayerPtr>& layers)
71 {
72  m_model->setLayers(layers);
73 }
74 
75 std::list<te::map::AbstractLayerPtr> te::qt::widgets::LayerItemView::getAllLayers() const
76 {
77  return m_model->getAllLayers();
78 }
79 
80 std::list<te::map::AbstractLayerPtr> te::qt::widgets::LayerItemView::getVisibleLayers() const
81 {
82  return m_model->getVisibleLayers();
83 }
84 
85 std::list<te::qt::widgets::TreeItem*> te::qt::widgets::LayerItemView::getSelectedItems() const
86 {
87  std::list<te::qt::widgets::TreeItem*> res;
88 
89  QModelIndexList lst = QTreeView::selectedIndexes();
90 
91  qSort(lst);
92 
93  for(QModelIndexList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
94  res.push_back(static_cast<te::qt::widgets::TreeItem*>((*it).internalPointer()));
95 
96  return res;
97 }
98 
99 void te::qt::widgets::LayerItemView::addFolder(const std::string& name, const QModelIndex& idx)
100 {
101  m_model->addFolder(name, idx);
102 
103  if(idx.isValid() && Expand(idx, this))
104  expand(idx);
105 
106  selectionModel()->clearSelection();
107 }
108 
110 {
111  std::vector<TreeItem*> items;
112  TreeItem* item = static_cast<TreeItem*>(idx.internalPointer());
113 
114  item->getChildren(items, "CHART");
115 
116  if(!items.empty())
117  {
118  TreeItem* chart = *items.begin();
119  int pos = chart->getPosition();
120 
121  QModelIndex cIdx = idx.child(pos, 0);
122 
123  QModelIndexList ls;
124 
125  ls << cIdx;
126 
127  removeItems(ls);
128  }
129 
130  ((LayerItem*)item)->updateChart();
131 
132  m_model->insertRows(1, 1, idx);
133 
134  QTreeView::expand(idx);
135  QTreeView::expand(idx.child(1, 0));
136 }
137 
139 {
140  QModelIndex lIdx = findLayerIndex(model(), QModelIndex(), l);
141 
142  if(lIdx.isValid())
143  {
144  LayerItem* li = static_cast<LayerItem*>(lIdx.internalPointer());
145 
146  std::vector<TreeItem*> items;
147 
148  li->getChildren(items, "STYLE");
149 
150  QModelIndexList ls;
151 
152  if (!items.empty())
153  {
154  TreeItem* g = *items.begin();
155  int pos = g->getPosition();
156 
157  QModelIndex cIdx = lIdx.child(pos, 0);
158  ls << cIdx;
159  }
160 
161  if (!ls.isEmpty())
162  removeItems(ls);
163 
164  int pos = li->updateLegend();
165 
166  m_model->insertRows(pos, 1, lIdx);
167 
168  QTreeView::expand(lIdx);
169  QTreeView::expand(lIdx.child(pos, 0));
170  }
171 }
172 
174 {
176 }
177 
179 {
181 }
182 
184 {
186 }
187 
189 {
191 }
192 
194 {
196 }
197 
199 {
201 }
202 
204 {
206 }
207 
209 {
211 }
212 
213 void te::qt::widgets::LayerItemView::removeItems(const QModelIndexList& idxs)
214 {
215  m_model->removeItems(idxs);
216  selectionModel()->clearSelection();
217 }
218 
220 {
221  removeItems(selectionModel()->selectedIndexes());
222 }
223 
225 {
227  dynamic_cast< te::common::Decorator<QStyledItemDelegate>* >(itemDelegate());
228 
229  if(delegate != nullptr)
230  {
231  QStyledItemDelegate* newDelegate = delegate->removeDecorator(d);
232 
233  if(newDelegate != nullptr)
234  setItemDelegate(newDelegate);
235  }
236 }
237 
239 {
240  if(m_outterFilter != nullptr)
241  removeEventFilter(m_outterFilter);
242 
243  if(obj != m_mnuMger)
244  {
245  removeEventFilter(m_mnuMger);
246  m_outterFilter = obj;
247  }
248  else
249  m_outterFilter = nullptr;
250 
251  installEventFilter(obj);
252 }
253 
255 {
256  return (m_outterFilter != nullptr) ? m_outterFilter : m_mnuMger;
257 }
258 
260 {
261  if(m_outterFilter == nullptr)
262  return;
263 
265 
266  if(h != nullptr)
267  {
268  QObject* newH = h->removeDecorator(handler);
269 
270  if(newH != nullptr)
271  setMenuEventHandler(newH);
272  }
273 }
274 
275 void te::qt::widgets::LayerItemView::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
276 {
277  QTreeView::selectionChanged(selected, deselected);
278 
280 }
281 
283 {
284  QTreeView::dropEvent(event);
285 
286  emit layerOrderChanged();
287 }
288 
290 {
291  QPoint pos = event->globalPos();
292  QModelIndex idx = indexAt(viewport()->mapFromGlobal(pos));
293 
294  if(idx.isValid())
295  {
296  TreeItem* item = static_cast<TreeItem*>(idx.internalPointer());
297 
298  if(item->getType() == "LAYER")
299  emit doubleClicked(((LayerItem*)item)->getLayer());
300 
301  if (item->getType() == "STYLE" && item->getParent()->getType() == "LAYER")
302  {
303  LayerItem* layerItem = (LayerItem*)item->getParent();
304  emit styleDoubleClicked(layerItem->getLayer());
305  }
306 
307  if (item->getType() == "RULE" && item->getParent()->getType() == "STYLE")
308  {
309  StyleItem* styleItem = (StyleItem*)item->getParent();
310  LayerItem* layerItem = (LayerItem*)styleItem->getParent();
311  emit ruleDoubleClicked(layerItem->getLayer(), ((RuleItem*)item)->getRule());
312  }
313  }
314 }
315 
317 {
318  int cs = model->rowCount(parent);
319 
320  for (int i = 0; i < cs; i++)
321  {
322  QModelIndex cIdx = model->index(i, 0, parent);
323  te::qt::widgets::TreeItem* item = static_cast<te::qt::widgets::TreeItem*>(cIdx.internalPointer());
324 
325  if (item->getType() == "LAYER")
326  {
327  if (((te::qt::widgets::LayerItem*)item)->getLayer().get() == l)
328  return cIdx;
329  }
330  else if (item->getType() == "FOLDER")
331  {
332  QModelIndex fIdx = findLayerIndex(model, cIdx, l);
333 
334  if (fIdx.isValid())
335  return fIdx;
336  }
337  }
338 
339  return QModelIndex();
340 }
void setMenuEventHandler(QObject *obj)
Updates the popup event handler.
void doubleClicked(te::map::AbstractLayerPtr layer)
Emited when user double clicks over an AbstractLayer.
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This is the base class for layers.
Definition: AbstractLayer.h:77
void addMultipleSelectionAction(QAction *act)
Adds the action to the popup menu presented when there is multiple items selected.
void addNoLayerAction(QAction *act)
Adds the action to the popup menu presented when there is no layers selected.
A class that represents a style of a layer in a LayerTreeModel.
void ruleDoubleClicked(te::map::AbstractLayerPtr layer, te::se::Rule *rule)
Emited when user double clicks over an Rule.
An item that contains a te::map::AbstractLayerPtr.
Definition: LayerItem.h:51
std::list< te::map::AbstractLayerPtr > getVisibleLayers() const
Returns just the visible layers. No folder layers are returned.
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) Q_DECL_OVERRIDE
void updateLegend(te::map::AbstractLayer *l)
updateLegend
te::map::AbstractLayerPtr getLayer() const
Returns the layer contained in the item.
Definition: LayerItem.cpp:140
T * removeDecorator(T *decorated)
Removes The decorator of the decorated.
Definition: Decorator.h:154
LayerItemModel * m_model
Model to be used.
int updateLegend()
updateLegend
Definition: LayerItem.cpp:155
void addFolder(const std::string &name, const QModelIndex &idx)
Adds a folder layer to the model.
void styleVisibilityChanged(te::map::AbstractLayerPtr layer)
Emited when some style item changes its visibility.
Defines a hierarchical structure.
QModelIndex findLayerIndex(QAbstractItemModel *model, const QModelIndex &parent, te::map::AbstractLayer *l)
Return the QModelIndex of the Layer in the Tree.
void addLayers(const std::list< te::map::AbstractLayerPtr > &layers, const QModelIndex &idx, const std::string &idxPath="./")
Add the layers to the model.
QObject * getMenuEventHandler() const
Rerturns the current popup handler being used.
LayerItemView(QWidget *parent=0)
Constructor.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
Defines an abstract model based on TreeItem objects.
void dropEvent(QDropEvent *event)
void removeItems(const QModelIndexList &idxs)
Removes the items in the list.
A specialization of QTreeView for manipulate layers.
Definition: LayerItemView.h:78
A class for rendering icons for layer and folder itens.
void addTabularLayerAction(QAction *act)
Adds the action to the popup menu presented when the selected layer has tabular representation.
void addAllLayerAction(QAction *act)
Adds the action to the popup menu presented for all kinds of selected items.
QObject * m_outterFilter
Popup handler defined outside the tree.
Represents a lengend in the tree structure.
Definition: RuleItem.h:53
void addFolder(const std::string &name, TreeItem *parent=0)
Adds a new folder item to the model.
Defines a layer item model for Qt5.
void setLayers(const std::list< te::map::AbstractLayerPtr > &layers)
Updates the list of layers in the model.
void setLayers(const std::list< te::map::AbstractLayerPtr > &layers)
Sets the list of layers. Old layers in the tree are then removed.
void updateChart(const QModelIndex &idx)
Updates the chart item of the element.
Defines a layer item view for Qt5.
Class for manage the menus of the LayerViewItem.
std::list< te::qt::widgets::TreeItem * > getSelectedItems() const
Returns a list of TreeItem that are selected.
std::list< te::map::AbstractLayerPtr > getVisibleLayers()
Returns a list with layers that are visible.
void removeItems(const QModelIndexList &lst)
Removes the items in lst from the model.
void removeDelegate(QStyledItemDelegate *d)
Removes the delegate from the tree.
void selectedLayersChanged(const std::list< te::map::AbstractLayerPtr > &layers)
Emited when the selection changes.
TEQTWIDGETSEXPORT std::list< te::map::AbstractLayerPtr > GetSelectedLayersOnly(te::qt::widgets::LayerItemView *view, const bool getFolder=false)
Defines a rule item.
void visibilityChanged()
Emited when some item changes its visibility.
int getPosition()
Returns the position of item in its parent&#39;s list of children.
void mouseDoubleClickEvent(QMouseEvent *event)
void styleDoubleClicked(te::map::AbstractLayerPtr layer)
Emited when user double clicks over an Style.
TreeItem * getParent() const
Returns the item parent.
Defines a layer item.
std::list< te::map::AbstractLayerPtr > getAllLayers() const
Returs all layers in the tree including folders.
void addLayers(const std::list< te::map::AbstractLayerPtr > &layers, const std::string &idxPath="./")
Adds a list of layers to the model at the end of children list.
void addRasterLayerAction(QAction *act)
Adds the action to the popup menu presented when the selected layer has raster representation.
void addAction(LMENUACTIONS mnu, QAction *act, int pos=-1)
Adds an action to some menu.
LayerViewMenuManager * m_mnuMger
Default popup handler.
void addVectorLayerAction(QAction *act)
Adds the action to the popup menu presented when the selected layer has vector representation.
std::list< te::map::AbstractLayerPtr > GetSelectedLayersOnly(te::qt::widgets::LayerItemView *view)
bool Expand(const QModelIndex &idx, te::qt::widgets::LayerItemView *)
void addFolderLayerAction(QAction *act)
Adds the action to the popup menu presented when the selected item is a folder.
A class that represents a style of a layer in a LayerTreeModel.
Definition: StyleItem.h:50
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void layerOrderChanged()
Emited when the order of the layers has changed.
void addInvalidLayerAction(QAction *act)
Adds the action to the popup menu presented when the selected layer is invalid.
void getChildren(std::vector< TreeItem * > &items, const std::string &type) const
Returns all children of a certain type.
void removeSelectedItems()
Removes the items that are selected.
std::string getType() const
Returns the type of the item.
std::list< te::map::AbstractLayerPtr > getAllLayers()
Get all layers contained in the model. Commonly used for persistent pourposes.
Class for manage the menus of the LayerItemView.
void removeMenuEventHandler(QObject *handler)
Removes the menu handler.