QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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"
22 #include "qgslayertreeutils.h"
24 #include "qgsmaplayer.h"
25 #include "qgsgui.h"
26 
27 #include <QMenu>
28 #include <QContextMenuEvent>
29 #include <QHeaderView>
30 
33 
34 
36  : QTreeView( parent )
37 
38 {
39  setHeaderHidden( true );
40 
41  setDragEnabled( true );
42  setAcceptDrops( true );
43  setDropIndicatorShown( true );
44  setEditTriggers( EditKeyPressed );
45  setExpandsOnDoubleClick( false ); // normally used for other actions
46 
47  // Ensure legend graphics are scrollable
48  header()->setStretchLastSection( false );
49  header()->setSectionResizeMode( QHeaderView::ResizeToContents );
50 
51  // If vertically scrolling by item, legend graphics can get clipped
52  setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
53 
54  setSelectionMode( ExtendedSelection );
55  setDefaultDropAction( Qt::MoveAction );
56 
57  // we need a custom item delegate in order to draw indicators
58  setItemDelegate( new QgsLayerTreeViewItemDelegate( this ) );
59  setStyle( new QgsLayerTreeViewProxyStyle( this ) );
60 
61  setLayerMarkWidth( static_cast< int >( QFontMetricsF( font() ).width( 'l' ) * Qgis::UI_SCALE_FACTOR ) );
62 
63  connect( this, &QTreeView::collapsed, this, &QgsLayerTreeView::updateExpandedStateToNode );
64  connect( this, &QTreeView::expanded, this, &QgsLayerTreeView::updateExpandedStateToNode );
65 }
66 
68 {
69  delete mMenuProvider;
70 }
71 
72 void QgsLayerTreeView::setModel( QAbstractItemModel *model )
73 {
74  if ( !qobject_cast<QgsLayerTreeModel *>( model ) )
75  return;
76 
77  connect( model, &QAbstractItemModel::rowsInserted, this, &QgsLayerTreeView::modelRowsInserted );
78  connect( model, &QAbstractItemModel::rowsRemoved, this, &QgsLayerTreeView::modelRowsRemoved );
79 
80  QTreeView::setModel( model );
81 
83  connect( layerTreeModel()->rootGroup(), &QgsLayerTreeNode::customPropertyChanged, this, &QgsLayerTreeView::onCustomPropertyChanged );
84 
85  connect( selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsLayerTreeView::onCurrentChanged );
86 
87  connect( layerTreeModel(), &QAbstractItemModel::modelReset, this, &QgsLayerTreeView::onModelReset );
88 
90 }
91 
93 {
94  return qobject_cast<QgsLayerTreeModel *>( model() );
95 }
96 
98 {
99  if ( !mDefaultActions )
101  return mDefaultActions;
102 }
103 
105 {
106  delete mMenuProvider;
108 }
109 
111 {
112  return layerForIndex( currentIndex() );
113 }
114 
116 {
117  if ( !layer )
118  {
119  setCurrentIndex( QModelIndex() );
120  return;
121  }
122 
123  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layer->id() );
124  if ( !nodeLayer )
125  return;
126 
127  setCurrentIndex( layerTreeModel()->node2index( nodeLayer ) );
128 }
129 
130 void QgsLayerTreeView::setLayerVisible( QgsMapLayer *layer, bool visible )
131 {
132  if ( !layer )
133  return;
134  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layer->id() );
135  if ( !nodeLayer )
136  return;
137  nodeLayer->setItemVisibilityChecked( visible );
138 }
139 
140 void QgsLayerTreeView::contextMenuEvent( QContextMenuEvent *event )
141 {
142  if ( !mMenuProvider )
143  return;
144 
145  QModelIndex idx = indexAt( event->pos() );
146  if ( !idx.isValid() )
147  setCurrentIndex( QModelIndex() );
148 
149  QMenu *menu = mMenuProvider->createContextMenu();
150  if ( menu && menu->actions().count() != 0 )
151  menu->exec( mapToGlobal( event->pos() ) );
152  delete menu;
153 }
154 
155 
156 void QgsLayerTreeView::modelRowsInserted( const QModelIndex &index, int start, int end )
157 {
158  QgsLayerTreeNode *parentNode = layerTreeModel()->index2node( index );
159  if ( !parentNode )
160  return;
161 
162  // Embedded widgets - replace placeholders in the model by actual widgets
163  if ( layerTreeModel()->testFlag( QgsLayerTreeModel::UseEmbeddedWidgets ) && QgsLayerTree::isLayer( parentNode ) )
164  {
165  QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( parentNode );
166  if ( QgsMapLayer *layer = nodeLayer->layer() )
167  {
168  int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
169  QList<QgsLayerTreeModelLegendNode *> legendNodes = layerTreeModel()->layerLegendNodes( nodeLayer, true );
170  for ( int i = 0; i < widgetsCount; ++i )
171  {
172  QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
173  if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
174  {
175  QModelIndex index = layerTreeModel()->legendNode2index( legendNodes[i] );
176  QWidget *wdgt = provider->createWidget( layer, i );
177  // Since column is resized to contents, limit the expanded width of embedded
178  // widgets, if they are not already limited, e.g. have the default MAX value.
179  // Else, embedded widget may grow very wide due to large legend graphics.
180  // NOTE: This approach DOES NOT work right. It causes horizontal scroll
181  // bar to disappear if the embedded widget is expanded and part
182  // of the last layer in the panel, even if much wider legend items
183  // are expanded above it. The correct width-limiting method should
184  // be setting fixed-width, hidpi-aware embedded widget items in a
185  // layout and appending an expanding QSpacerItem to end. This ensures
186  // full width is always created in the column by the embedded widget.
187  // See QgsLayerTreeOpacityWidget
188  //if ( wdgt->maximumWidth() == QWIDGETSIZE_MAX )
189  //{
190  // wdgt->setMaximumWidth( 250 );
191  //}
192 
193  setIndexWidget( index, wdgt );
194  }
195  }
196  }
197  }
198 
199 
200  if ( QgsLayerTree::isLayer( parentNode ) )
201  {
202  // if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes
203  QStringList expandedNodeKeys = parentNode->customProperty( QStringLiteral( "expandedLegendNodes" ) ).toStringList();
204  if ( expandedNodeKeys.isEmpty() )
205  return;
206 
207  const auto constLayerLegendNodes = layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ), true );
208  for ( QgsLayerTreeModelLegendNode *legendNode : constLayerLegendNodes )
209  {
210  QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
211  if ( expandedNodeKeys.contains( ruleKey ) )
212  setExpanded( layerTreeModel()->legendNode2index( legendNode ), true );
213  }
214  return;
215  }
216 
217  QList<QgsLayerTreeNode *> children = parentNode->children();
218  for ( int i = start; i <= end; ++i )
219  {
220  updateExpandedStateFromNode( children[i] );
221  }
222 
223  // make sure we still have correct current layer
225 }
226 
228 {
229  // make sure we still have correct current layer
231 }
232 
233 void QgsLayerTreeView::updateExpandedStateToNode( const QModelIndex &index )
234 {
235  if ( QgsLayerTreeNode *node = layerTreeModel()->index2node( index ) )
236  {
237  node->setExpanded( isExpanded( index ) );
238  }
239  else if ( QgsLayerTreeModelLegendNode *node = layerTreeModel()->index2legendNode( index ) )
240  {
241  QString ruleKey = node->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
242  QStringList lst = node->layerNode()->customProperty( QStringLiteral( "expandedLegendNodes" ) ).toStringList();
243  bool expanded = isExpanded( index );
244  bool isInList = lst.contains( ruleKey );
245  if ( expanded && !isInList )
246  {
247  lst.append( ruleKey );
248  node->layerNode()->setCustomProperty( QStringLiteral( "expandedLegendNodes" ), lst );
249  }
250  else if ( !expanded && isInList )
251  {
252  lst.removeAll( ruleKey );
253  node->layerNode()->setCustomProperty( QStringLiteral( "expandedLegendNodes" ), lst );
254  }
255  }
256 }
257 
259 {
260  QgsMapLayer *layerCurrent = layerForIndex( currentIndex() );
261  QString layerCurrentID = layerCurrent ? layerCurrent->id() : QString();
262  if ( mCurrentLayerID == layerCurrentID )
263  return;
264 
265  // update the current index in model (the item will be underlined)
266  QModelIndex nodeLayerIndex;
267  if ( layerCurrent )
268  {
269  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerCurrentID );
270  if ( nodeLayer )
271  nodeLayerIndex = layerTreeModel()->node2index( nodeLayer );
272  }
273  layerTreeModel()->setCurrentIndex( nodeLayerIndex );
274 
275  mCurrentLayerID = layerCurrentID;
276  emit currentLayerChanged( layerCurrent );
277 }
278 
280 {
281  QModelIndex idx = layerTreeModel()->node2index( node );
282  if ( isExpanded( idx ) != expanded )
283  setExpanded( idx, expanded );
284 }
285 
286 void QgsLayerTreeView::onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key )
287 {
288  if ( key != QStringLiteral( "expandedLegendNodes" ) || !QgsLayerTree::isLayer( node ) )
289  return;
290 
291  QSet<QString> expandedLegendNodes = node->customProperty( QStringLiteral( "expandedLegendNodes" ) ).toStringList().toSet();
292 
293  const QList<QgsLayerTreeModelLegendNode *> legendNodes = layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( node ), true );
294  for ( QgsLayerTreeModelLegendNode *legendNode : legendNodes )
295  {
296  QString key = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
297  if ( !key.isEmpty() )
298  setExpanded( layerTreeModel()->legendNode2index( legendNode ), expandedLegendNodes.contains( key ) );
299  }
300 }
301 
303 {
305 }
306 
308 {
309  QModelIndex idx = layerTreeModel()->node2index( node );
310  setExpanded( idx, node->isExpanded() );
311 
312  const auto constChildren = node->children();
313  for ( QgsLayerTreeNode *child : constChildren )
315 }
316 
317 QgsMapLayer *QgsLayerTreeView::layerForIndex( const QModelIndex &index ) const
318 {
319  // Check if model has been set and index is valid
320  if ( layerTreeModel() && index.isValid() )
321  {
322  QgsLayerTreeNode *node = layerTreeModel()->index2node( index );
323  if ( node )
324  {
325  if ( QgsLayerTree::isLayer( node ) )
326  return QgsLayerTree::toLayer( node )->layer();
327  }
328  else
329  {
330  // possibly a legend node
332  if ( legendNode )
333  return legendNode->layerNode()->layer();
334  }
335  }
336  return nullptr;
337 }
338 
340 {
341  return layerTreeModel()->index2node( selectionModel()->currentIndex() );
342 }
343 
345 {
346  QgsLayerTreeNode *node = currentNode();
347  if ( QgsLayerTree::isGroup( node ) )
348  return QgsLayerTree::toGroup( node );
349  else if ( QgsLayerTree::isLayer( node ) )
350  {
351  QgsLayerTreeNode *parent = node->parent();
352  if ( QgsLayerTree::isGroup( parent ) )
353  return QgsLayerTree::toGroup( parent );
354  }
355 
356  if ( QgsLayerTreeModelLegendNode *legendNode = layerTreeModel()->index2legendNode( selectionModel()->currentIndex() ) )
357  {
359  if ( QgsLayerTree::isGroup( parent->parent() ) )
360  return QgsLayerTree::toGroup( parent->parent() );
361  }
362 
363  return nullptr;
364 }
365 
367 {
368  return layerTreeModel()->index2legendNode( selectionModel()->currentIndex() );
369 }
370 
371 QList<QgsLayerTreeNode *> QgsLayerTreeView::selectedNodes( bool skipInternal ) const
372 {
373  return layerTreeModel()->indexes2nodes( selectionModel()->selectedIndexes(), skipInternal );
374 }
375 
376 QList<QgsLayerTreeLayer *> QgsLayerTreeView::selectedLayerNodes() const
377 {
378  QList<QgsLayerTreeLayer *> layerNodes;
379  const auto constSelectedNodes = selectedNodes();
380  for ( QgsLayerTreeNode *node : constSelectedNodes )
381  {
382  if ( QgsLayerTree::isLayer( node ) )
383  layerNodes << QgsLayerTree::toLayer( node );
384  }
385  return layerNodes;
386 }
387 
388 QList<QgsMapLayer *> QgsLayerTreeView::selectedLayers() const
389 {
390  QList<QgsMapLayer *> list;
391  const auto constSelectedLayerNodes = selectedLayerNodes();
392  for ( QgsLayerTreeLayer *node : constSelectedLayerNodes )
393  {
394  if ( node->layer() )
395  list << node->layer();
396  }
397  return list;
398 }
399 
400 QList<QgsMapLayer *> QgsLayerTreeView::selectedLayersRecursive() const
401 {
402  const QList<QgsLayerTreeNode *> nodes = layerTreeModel()->indexes2nodes( selectionModel()->selectedIndexes(), false );
403  QSet<QgsMapLayer *> layersSet = QgsLayerTreeUtils::collectMapLayersRecursive( nodes );
404  return layersSet.toList();
405 }
406 
408 {
409  if ( !mIndicators[node].contains( indicator ) )
410  {
411  mIndicators[node].append( indicator );
412  connect( indicator, &QgsLayerTreeViewIndicator::changed, this, [ = ]
413  {
414  update();
415  } );
416  update();
417  }
418 }
419 
421 {
422  mIndicators[node].removeOne( indicator );
423  update();
424 }
425 
426 QList<QgsLayerTreeViewIndicator *> QgsLayerTreeView::indicators( QgsLayerTreeNode *node ) const
427 {
428  return mIndicators.value( node );
429 }
430 
432 QStringList QgsLayerTreeView::viewOnlyCustomProperties()
433 {
434  return QStringList() << QStringLiteral( "expandedLegendNodes" );
435 }
437 
438 void QgsLayerTreeView::refreshLayerSymbology( const QString &layerId )
439 {
440  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerId );
441  if ( nodeLayer )
442  layerTreeModel()->refreshLayerLegend( nodeLayer );
443 }
444 
445 
446 static void _expandAllLegendNodes( QgsLayerTreeLayer *nodeLayer, bool expanded, QgsLayerTreeModel *model )
447 {
448  // for layers we also need to find out with legend nodes contain some children and make them expanded/collapsed
449  // if we are collapsing, we just write out an empty list
450  QStringList lst;
451  if ( expanded )
452  {
453  const auto constLayerLegendNodes = model->layerLegendNodes( nodeLayer, true );
454  for ( QgsLayerTreeModelLegendNode *legendNode : constLayerLegendNodes )
455  {
456  QString parentKey = legendNode->data( QgsLayerTreeModelLegendNode::ParentRuleKeyRole ).toString();
457  if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
458  lst << parentKey;
459  }
460  }
461  nodeLayer->setCustomProperty( QStringLiteral( "expandedLegendNodes" ), lst );
462 }
463 
464 
465 static void _expandAllNodes( QgsLayerTreeGroup *parent, bool expanded, QgsLayerTreeModel *model )
466 {
467  const auto constChildren = parent->children();
468  for ( QgsLayerTreeNode *node : constChildren )
469  {
470  node->setExpanded( expanded );
471  if ( QgsLayerTree::isGroup( node ) )
472  _expandAllNodes( QgsLayerTree::toGroup( node ), expanded, model );
473  else if ( QgsLayerTree::isLayer( node ) )
474  _expandAllLegendNodes( QgsLayerTree::toLayer( node ), expanded, model );
475  }
476 }
477 
478 
480 {
481  // unfortunately expandAll() does not emit expanded() signals
482  _expandAllNodes( layerTreeModel()->rootGroup(), true, layerTreeModel() );
483  expandAll();
484 }
485 
487 {
488  // unfortunately collapseAll() does not emit collapsed() signals
489  _expandAllNodes( layerTreeModel()->rootGroup(), false, layerTreeModel() );
490  collapseAll();
491 }
492 
493 void QgsLayerTreeView::mouseReleaseEvent( QMouseEvent *event )
494 {
495  // we need to keep last mouse position in order to know whether to emit an indicator's clicked() signal
496  // (the item delegate needs to know which indicator has been clicked)
497  mLastReleaseMousePos = event->pos();
498 
499  const QgsLayerTreeModel::Flags oldFlags = layerTreeModel()->flags();
500  if ( event->modifiers() & Qt::ControlModifier )
502  else
504  QTreeView::mouseReleaseEvent( event );
505  layerTreeModel()->setFlags( oldFlags );
506 }
507 
508 void QgsLayerTreeView::keyPressEvent( QKeyEvent *event )
509 {
510  const QgsLayerTreeModel::Flags oldFlags = layerTreeModel()->flags();
511  if ( event->modifiers() & Qt::ControlModifier )
513  else
515  QTreeView::keyPressEvent( event );
516  layerTreeModel()->setFlags( oldFlags );
517 }
518 
519 void QgsLayerTreeView::dropEvent( QDropEvent *event )
520 {
521  if ( event->keyboardModifiers() & Qt::AltModifier )
522  {
523  event->accept();
524  }
525  QTreeView::dropEvent( event );
526 }
527 
528 void QgsLayerTreeView::resizeEvent( QResizeEvent *event )
529 {
530  // Since last column is resized to content (instead of stretched), the active
531  // selection rectangle ends at width of widest visible item in tree,
532  // regardless of which item is selected. This causes layer indicators to
533  // become 'inactive' (not clickable and no tool tip) unless their rectangle
534  // enters the view item's selection (active) rectangle.
535  // Always resetting the minimum section size relative to the viewport ensures
536  // the view item's selection rectangle extends to the right edge of the
537  // viewport, which allows indicators to become active again.
538  header()->setMinimumSectionSize( viewport()->width() );
539  QTreeView::resizeEvent( event );
540 }
Layer tree group node serves as a container for layers and further groups.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
QList< QgsMapLayer * > selectedLayers() const
Gets list of selected layers.
Base class for all map layer types.
Definition: qgsmaplayer.h:79
Implementation of this interface can be implemented to allow QgsLayerTreeView instance to provide cus...
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43
void setCurrentIndex(const QModelIndex &currentIndex)
Sets 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 removeIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Removes a previously added indicator to a layer tree node.
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:154
void refreshLayerSymbology(const QString &layerId)
Force refresh of layer symbology. Normally not needed as the changes of layer&#39;s renderer are monitore...
void contextMenuEvent(QContextMenuEvent *event) override
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Definition: qgslayertree.h:64
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
void modelRowsInserted(const QModelIndex &index, int start, int end)
QgsMapLayer * currentLayer() const
Returns the currently selected layer, or nullptr if no layers is selected.
QgsLayerTreeViewDefaultActions * defaultActions()
Gets access to the default actions that may be used with the tree view.
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
QModelIndex node2index(QgsLayerTreeNode *node) const
Returns index for a given node. If the node does not belong to the layer tree, the result is undefine...
virtual QMenu * createContextMenu()=0
Returns a newly created menu instance (or nullptr on error)
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
QList< QgsLayerTreeLayer * > selectedLayerNodes() const
Returns list of selected nodes filtered to just layer nodes.
void mouseReleaseEvent(QMouseEvent *event) override
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer) ...
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...
void updateExpandedStateToNode(const QModelIndex &index)
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Returns index for a given legend node.
void changed()
Emitted when the indicator changes state (e.g.
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
Definition: qgsgui.cpp:87
void setLayerVisible(QgsMapLayer *layer, bool visible)
Convenience methods which sets the visible state of the specified map layer.
Provider interface to be implemented in order to introduce new kinds of embedded widgets for use in l...
void addIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Adds an indicator to the given layer tree node.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
QgsLayerTreeGroup * currentGroupNode() const
Gets current group node. If a layer is current node, the function will return parent group...
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked. ...
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file...
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
This class is a base class for nodes in a layer tree.
friend class QgsLayerTreeViewItemDelegate
void collapseAllNodes()
Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes...
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
void keyPressEvent(QKeyEvent *event) override
static QSet< QgsMapLayer * > collectMapLayersRecursive(const QList< QgsLayerTreeNode *> &nodes)
Returns map layers from the given list of layer tree nodes.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
QgsLayerTreeModel * layerTreeModel() const
Gets 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)
Sets whether the node should be shown as expanded or collapsed in GUI.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Qt::ItemFlags flags(const QModelIndex &index) const override
void updateExpandedStateFromNode(QgsLayerTreeNode *node)
QList< QgsLayerTreeNode * > selectedNodes(bool skipInternal=false) const
Returns list of selected nodes.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
void onExpandedChanged(QgsLayerTreeNode *node, bool expanded)
Layer nodes may optionally include extra embedded widgets (if used in QgsLayerTreeView). Added in 2.16.
QgsLayerTreeModelLegendNode * currentLegendNode() const
Gets current legend node.
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be nullptr.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
void dropEvent(QDropEvent *event) override
QgsMapLayer * layerForIndex(const QModelIndex &index) const
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Check/uncheck action has consequences on children (or parents for leaf node)
void setModel(QAbstractItemModel *model) override
Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
void setLayerMarkWidth(int width)
Set width of contextual menu mark, at right of layer node items.
void setCurrentLayer(QgsMapLayer *layer)
Sets the currently selected layer.
void resizeEvent(QResizeEvent *event) override
QgsLayerTreeView(QWidget *parent=nullptr)
Constructor for QgsLayerTreeView.
void setMenuProvider(QgsLayerTreeViewMenuProvider *menuProvider)
Sets provider for context menu. Takes ownership of the instance.
~QgsLayerTreeView() override
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes...
Indicator that can be used in a layer tree view to display icons next to items of the layer tree...
QList< QgsLayerTreeViewIndicator * > indicators(QgsLayerTreeNode *node) const
Returns list of indicators associated with a particular layer tree node.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file...
Layer tree node points to a map layer.
QgsLayerTreeNode * currentNode() const
Gets current node. May be nullptr.
QList< QgsMapLayer * > selectedLayersRecursive() const
Gets list of selected layers, including those that are not directly selected, but their ancestor grou...