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