QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgslayertreeview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertreeview.cpp
3  --------------------------------------
4  Date : May 2014
5  Copyright : (C) 2014 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgslayertreeview.h"
17 
18 #include "qgslayertree.h"
20 #include "qgslayertreemodel.h"
23 #include "qgsmaplayer.h"
24 
25 #include <QMenu>
26 #include <QContextMenuEvent>
27 
28 
30  : QTreeView( parent )
31  , mDefaultActions( nullptr )
32  , mMenuProvider( nullptr )
33 {
34  setHeaderHidden( true );
35 
36  setDragEnabled( true );
37  setAcceptDrops( true );
38  setDropIndicatorShown( true );
39  setEditTriggers( EditKeyPressed );
40  setExpandsOnDoubleClick( false ); // normally used for other actions
41 
42  setSelectionMode( ExtendedSelection );
43  setDefaultDropAction( Qt::MoveAction );
44 
45  connect( this, SIGNAL( collapsed( QModelIndex ) ), this, SLOT( updateExpandedStateToNode( QModelIndex ) ) );
46  connect( this, SIGNAL( expanded( QModelIndex ) ), this, SLOT( updateExpandedStateToNode( QModelIndex ) ) );
47 }
48 
50 {
51  delete mMenuProvider;
52 }
53 
55 {
56  if ( !qobject_cast<QgsLayerTreeModel*>( model ) )
57  return;
58 
59  connect( model, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( modelRowsInserted( QModelIndex, int, int ) ) );
60  connect( model, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( modelRowsRemoved() ) );
61 
62  QTreeView::setModel( model );
63 
64  connect( layerTreeModel()->rootGroup(), SIGNAL( expandedChanged( QgsLayerTreeNode*, bool ) ), this, SLOT( onExpandedChanged( QgsLayerTreeNode*, bool ) ) );
65 
66  connect( selectionModel(), SIGNAL( currentChanged( QModelIndex, QModelIndex ) ), this, SLOT( onCurrentChanged() ) );
67 
68  connect( layerTreeModel(), SIGNAL( modelReset() ), this, SLOT( onModelReset() ) );
69 
71 }
72 
74 {
75  return qobject_cast<QgsLayerTreeModel*>( model() );
76 }
77 
79 {
80  if ( !mDefaultActions )
82  return mDefaultActions;
83 }
84 
86 {
87  delete mMenuProvider;
89 }
90 
92 {
93  return layerForIndex( currentIndex() );
94 }
95 
97 {
98  if ( !layer )
99  {
101  return;
102  }
103 
104  QgsLayerTreeLayer* nodeLayer = layerTreeModel()->rootGroup()->findLayer( layer->id() );
105  if ( !nodeLayer )
106  return;
107 
108  setCurrentIndex( layerTreeModel()->node2index( nodeLayer ) );
109 }
110 
111 
113 {
114  if ( !mMenuProvider )
115  return;
116 
117  QModelIndex idx = indexAt( event->pos() );
118  if ( !idx.isValid() )
120 
122  if ( menu && menu->actions().count() != 0 )
123  menu->exec( mapToGlobal( event->pos() ) );
124  delete menu;
125 }
126 
127 
128 void QgsLayerTreeView::modelRowsInserted( const QModelIndex& index, int start, int end )
129 {
130  QgsLayerTreeNode* parentNode = layerTreeModel()->index2node( index );
131  if ( !parentNode )
132  return;
133 
134  // Embedded widgets - replace placeholders in the model by actual widgets
135  if ( layerTreeModel()->testFlag( QgsLayerTreeModel::UseEmbeddedWidgets ) && QgsLayerTree::isLayer( parentNode ) )
136  {
137  QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( parentNode );
138  if ( QgsMapLayer* layer = nodeLayer->layer() )
139  {
140  int widgetsCount = layer->customProperty( "embeddedWidgets/count", 0 ).toInt();
141  QList<QgsLayerTreeModelLegendNode*> legendNodes = layerTreeModel()->layerLegendNodes( nodeLayer, true );
142  for ( int i = 0; i < widgetsCount; ++i )
143  {
144  QString providerId = layer->customProperty( QString( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
145  if ( QgsLayerTreeEmbeddedWidgetProvider* provider = QgsLayerTreeEmbeddedWidgetRegistry::instance()->provider( providerId ) )
146  {
147  QModelIndex index = layerTreeModel()->legendNode2index( legendNodes[i] );
148  setIndexWidget( index, provider->createWidget( layer, i ) );
149  }
150  }
151  }
152  }
153 
154 
155  if ( QgsLayerTree::isLayer( parentNode ) )
156  {
157  // if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes
158  QStringList expandedNodeKeys = parentNode->customProperty( "expandedLegendNodes" ).toStringList();
159  if ( expandedNodeKeys.isEmpty() )
160  return;
161 
162  Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ), true ) )
163  {
165  if ( expandedNodeKeys.contains( ruleKey ) )
166  setExpanded( layerTreeModel()->legendNode2index( legendNode ), true );
167  }
168  return;
169  }
170 
172  for ( int i = start; i <= end; ++i )
173  {
174  updateExpandedStateFromNode( children[i] );
175  }
176 
177  // make sure we still have correct current layer
179 }
180 
182 {
183  // make sure we still have correct current layer
185 }
186 
188 {
189  if ( QgsLayerTreeNode* node = layerTreeModel()->index2node( index ) )
190  {
191  node->setExpanded( isExpanded( index ) );
192  }
193  else if ( QgsLayerTreeModelLegendNode* node = layerTreeModel()->index2legendNode( index ) )
194  {
195  QString ruleKey = node->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
196  QStringList lst = node->layerNode()->customProperty( "expandedLegendNodes" ).toStringList();
197  bool expanded = isExpanded( index );
198  bool isInList = lst.contains( ruleKey );
199  if ( expanded && !isInList )
200  {
201  lst.append( ruleKey );
202  node->layerNode()->setCustomProperty( "expandedLegendNodes", lst );
203  }
204  else if ( !expanded && isInList )
205  {
206  lst.removeAll( ruleKey );
207  node->layerNode()->setCustomProperty( "expandedLegendNodes", lst );
208  }
209  }
210 }
211 
213 {
214  QgsMapLayer* layerCurrent = layerForIndex( currentIndex() );
215  QString layerCurrentID = layerCurrent ? layerCurrent->id() : QString();
216  if ( mCurrentLayerID == layerCurrentID )
217  return;
218 
219  // update the current index in model (the item will be underlined)
220  QModelIndex nodeLayerIndex;
221  if ( layerCurrent )
222  {
223  QgsLayerTreeLayer* nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerCurrentID );
224  if ( nodeLayer )
225  nodeLayerIndex = layerTreeModel()->node2index( nodeLayer );
226  }
227  layerTreeModel()->setCurrentIndex( nodeLayerIndex );
228 
229  mCurrentLayerID = layerCurrentID;
230  emit currentLayerChanged( layerCurrent );
231 }
232 
234 {
235  QModelIndex idx = layerTreeModel()->node2index( node );
236  if ( isExpanded( idx ) != expanded )
237  setExpanded( idx, expanded );
238 }
239 
241 {
243 }
244 
246 {
247  QModelIndex idx = layerTreeModel()->node2index( node );
248  setExpanded( idx, node->isExpanded() );
249 
250  Q_FOREACH ( QgsLayerTreeNode* child, node->children() )
252 }
253 
255 {
256  // Check if model has been set and index is valid
257  if ( layerTreeModel() && index.isValid( ) )
258  {
259  QgsLayerTreeNode* node = layerTreeModel()->index2node( index );
260  if ( node )
261  {
262  if ( QgsLayerTree::isLayer( node ) )
263  return QgsLayerTree::toLayer( node )->layer();
264  }
265  else
266  {
267  // possibly a legend node
269  if ( legendNode )
270  return legendNode->layerNode()->layer();
271  }
272  }
273 
274  return nullptr;
275 }
276 
278 {
280 }
281 
283 {
284  QgsLayerTreeNode* node = currentNode();
285  if ( QgsLayerTree::isGroup( node ) )
286  return QgsLayerTree::toGroup( node );
287  else if ( QgsLayerTree::isLayer( node ) )
288  {
289  QgsLayerTreeNode* parent = node->parent();
290  if ( QgsLayerTree::isGroup( parent ) )
291  return QgsLayerTree::toGroup( parent );
292  }
293 
294  if ( QgsLayerTreeModelLegendNode* legendNode = layerTreeModel()->index2legendNode( selectionModel()->currentIndex() ) )
295  {
296  QgsLayerTreeLayer* parent = legendNode->layerNode();
297  if ( QgsLayerTree::isGroup( parent->parent() ) )
298  return QgsLayerTree::toGroup( parent->parent() );
299  }
300 
301  return nullptr;
302 }
303 
305 {
307 }
308 
310 {
311  return layerTreeModel()->indexes2nodes( selectionModel()->selectedIndexes(), skipInternal );
312 }
313 
315 {
316  QList<QgsLayerTreeLayer*> layerNodes;
317  Q_FOREACH ( QgsLayerTreeNode* node, selectedNodes() )
318  {
319  if ( QgsLayerTree::isLayer( node ) )
320  layerNodes << QgsLayerTree::toLayer( node );
321  }
322  return layerNodes;
323 }
324 
326 {
327  QList<QgsMapLayer*> list;
328  Q_FOREACH ( QgsLayerTreeLayer* node, selectedLayerNodes() )
329  {
330  if ( node->layer() )
331  list << node->layer();
332  }
333  return list;
334 }
335 
336 
338 {
339  QgsLayerTreeLayer* nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerId );
340  if ( nodeLayer )
341  layerTreeModel()->refreshLayerLegend( nodeLayer );
342 }
343 
344 
346 {
347  // for layers we also need to find out with legend nodes contain some children and make them expanded/collapsed
348  // if we are collapsing, we just write out an empty list
349  QStringList lst;
350  if ( expanded )
351  {
352  Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, model->layerLegendNodes( nodeLayer, true ) )
353  {
355  if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
356  lst << parentKey;
357  }
358  }
359  nodeLayer->setCustomProperty( "expandedLegendNodes", lst );
360 }
361 
362 
364 {
365  Q_FOREACH ( QgsLayerTreeNode* node, parent->children() )
366  {
367  node->setExpanded( expanded );
368  if ( QgsLayerTree::isGroup( node ) )
369  _expandAllNodes( QgsLayerTree::toGroup( node ), expanded, model );
370  else if ( QgsLayerTree::isLayer( node ) )
371  _expandAllLegendNodes( QgsLayerTree::toLayer( node ), expanded, model );
372  }
373 }
374 
375 
377 {
378  // unfortunately expandAll() does not emit expanded() signals
379  _expandAllNodes( layerTreeModel()->rootGroup(), true, layerTreeModel() );
380  expandAll();
381 }
382 
384 {
385  // unfortunately collapseAll() does not emit collapsed() signals
386  _expandAllNodes( layerTreeModel()->rootGroup(), false, layerTreeModel() );
387  collapseAll();
388 }
389 
391 {
392  if ( event->keyboardModifiers() & Qt::AltModifier )
393  {
394  event->accept();
395  }
396  QTreeView::dropEvent( event );
397 }
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
Layer tree group node serves as a container for layers and further groups.
static unsigned index
QList< QgsMapLayer * > selectedLayers() const
Get list of selected layers.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsLayerTreeGroup * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
Implementation of this interface can be implemented to allow QgsLayerTreeView instance to provide cus...
void setCurrentIndex(const QModelIndex &currentIndex)
Set index of the current item. May be used by view. Item marked as current is underlined.
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
QgsLayerTreeViewMenuProvider * mMenuProvider
Context menu provider. Owned by the view.
void setCurrentIndex(const QModelIndex &index)
void setSelectionMode(QAbstractItemView::SelectionMode mode)
static void _expandAllLegendNodes(QgsLayerTreeLayer *nodeLayer, bool expanded, QgsLayerTreeModel *model)
QItemSelectionModel * selectionModel() const
void refreshLayerSymbology(const QString &layerId)
Force refresh of layer symbology. Normally not needed as the changes of layer&#39;s renderer are monitore...
virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous)
void contextMenuEvent(QContextMenuEvent *event) override
void collapseAll()
void collapsed(const QModelIndex &index)
const QObjectList & children() const
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QPoint mapToGlobal(const QPoint &pos) const
QgsLayerTreeLayer * layerNode() const
Return pointer to the parent layer node.
void modelRowsInserted(const QModelIndex &index, int start, int end)
QgsMapLayer * currentLayer() const
Get currently selected layer. May be null.
void rowsRemoved(const QModelIndex &parent, int start, int end)
QgsLayerTreeViewDefaultActions * defaultActions()
Get access to the default actions that may be used with the tree view.
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
QModelIndex node2index(QgsLayerTreeNode *node) const
Return index for a given node. If the node does not belong to the layer tree, the result is undefined...
virtual QMenu * createContextMenu()=0
Return a newly created menu instance (or null pointer on error)
QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is ...
Definition: qgslayertree.h:46
QList< QgsLayerTreeLayer * > selectedLayerNodes() const
Return list of selected nodes filtered to just layer nodes.
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer) ...
void setExpanded(const QModelIndex &index, bool expanded)
The QgsLayerTreeViewDefaultActions class serves as a factory of actions that can be used together wit...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2...
bool isValid() const
void updateExpandedStateToNode(const QModelIndex &index)
void append(const T &value)
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Return index for a given legend node.
Provider interface to be implemented in order to introduce new kinds of embedded widgets for use in l...
virtual void rowsInserted(const QModelIndex &parent, int start, int end)
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
bool isEmpty() const
QgsLayerTreeNode * parent()
Get pointer to the parent. If parent is a null pointer, the node is a root node.
QgsLayerTreeGroup * currentGroupNode() const
Get current group node. If a layer is current node, the function will return parent group...
bool isEmpty() const
int removeAll(const T &value)
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void setEditTriggers(QFlags< QAbstractItemView::EditTrigger > triggers)
This class is a base class for nodes in a layer tree.
void collapseAllNodes()
Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes...
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:40
bool isExpanded(const QModelIndex &index) const
QAction * exec()
void setAcceptDrops(bool on)
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
QgsLayerTreeModel * layerTreeModel() const
Get access to the model casted to QgsLayerTreeModel.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
QgsLayerTreeViewDefaultActions * mDefaultActions
helper class with default actions. Lazily initialized.
void setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
void expandAll()
QgsLayerTreeLayer * findLayer(const QString &layerId) const
Find layer node representing the map layer specified by its ID. Searches recursively the whole sub-tr...
QgsMapLayer * layer() const
void updateExpandedStateFromNode(QgsLayerTreeNode *node)
void setExpandsOnDoubleClick(bool enable)
virtual bool event(QEvent *event)
const QPoint & pos() const
QList< QgsLayerTreeNode * > selectedNodes(bool skipInternal=false) const
Return list of selected nodes.
QStringList toStringList() const
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
void onExpandedChanged(QgsLayerTreeNode *node, bool expanded)
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
Definition: qgslayertree.h:52
void setHeaderHidden(bool hide)
Layer nodes may optionally include extra embedded widgets (if used in QgsLayerTreeView). Added in 2.16.
QgsLayerTreeModelLegendNode * currentLegendNode() const
Get current legend node.
virtual void setModel(QAbstractItemModel *model)
virtual void dropEvent(QDropEvent *event)
void setIndexWidget(const QModelIndex &index, QWidget *widget)
QgsLayerTreeViewMenuProvider * menuProvider() const
Return pointer to the context menu provider. May be null.
virtual QVariant data(int role) const =0
Return data associated with the item.
void dropEvent(QDropEvent *event) override
QgsMapLayer * layerForIndex(const QModelIndex &index) const
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual QModelIndexList selectedIndexes() const
static QgsLayerTreeEmbeddedWidgetRegistry * instance()
Means of accessing canonical single instance.
virtual void setModel(QAbstractItemModel *model) override
Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Return filtered list of active legend nodes attached to a particular layer node (by default it return...
void setCurrentLayer(QgsMapLayer *layer)
Set currently selected layer. Null pointer will deselect any layer.
QgsLayerTreeView(QWidget *parent=nullptr)
void expanded(const QModelIndex &index)
void setDefaultDropAction(Qt::DropAction dropAction)
void setMenuProvider(QgsLayerTreeViewMenuProvider *menuProvider)
Set provider for context menu. Takes ownership of the instance.
QAbstractItemModel * model() const
QModelIndex currentIndex() const
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes...
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QList< QAction * > actions() const
QObject * parent() const
QChar * data()
bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:34
QString toString() const
virtual QModelIndex indexAt(const QPoint &point) const
void setDropIndicatorShown(bool enable)
void setDragEnabled(bool enable)
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node.
Layer tree node points to a map layer.
Qt::KeyboardModifiers keyboardModifiers() const
static void _expandAllNodes(QgsLayerTreeGroup *parent, bool expanded, QgsLayerTreeModel *model)
QgsLayerTreeNode * currentNode() const
Get current node. May be null.