QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  setLayerMarkWidth( static_cast< int >( QFontMetricsF( font() ).width( 'l' ) * Qgis::UI_SCALE_FACTOR ) );
65 
66  connect( this, &QTreeView::collapsed, this, &QgsLayerTreeView::updateExpandedStateToNode );
67  connect( this, &QTreeView::expanded, this, &QgsLayerTreeView::updateExpandedStateToNode );
68 
69  connect( horizontalScrollBar(), &QScrollBar::valueChanged, this, &QgsLayerTreeView::onHorizontalScroll );
70 }
71 
73 {
74  delete mMenuProvider;
75 }
76 
77 void QgsLayerTreeView::setModel( QAbstractItemModel *model )
78 {
79  if ( !qobject_cast<QgsLayerTreeModel *>( model ) )
80  return;
81 
82  connect( model, &QAbstractItemModel::rowsInserted, this, &QgsLayerTreeView::modelRowsInserted );
83  connect( model, &QAbstractItemModel::rowsRemoved, this, &QgsLayerTreeView::modelRowsRemoved );
84 
85  if ( mMessageBar )
87  [ = ]( const QString & message, Qgis::MessageLevel level = Qgis::Info, int duration = 5 )
88  {mMessageBar->pushMessage( message, level, duration );}
89  );
90 
91  QTreeView::setModel( model );
92 
94  connect( layerTreeModel()->rootGroup(), &QgsLayerTreeNode::customPropertyChanged, this, &QgsLayerTreeView::onCustomPropertyChanged );
95 
96  connect( selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsLayerTreeView::onCurrentChanged );
97 
98  connect( layerTreeModel(), &QAbstractItemModel::modelReset, this, &QgsLayerTreeView::onModelReset );
99 
101 }
102 
104 {
105  return qobject_cast<QgsLayerTreeModel *>( model() );
106 }
107 
109 {
110  if ( !mDefaultActions )
112  return mDefaultActions;
113 }
114 
116 {
117  delete mMenuProvider;
119 }
120 
122 {
123  return layerForIndex( currentIndex() );
124 }
125 
127 {
128  if ( !layer )
129  {
130  setCurrentIndex( QModelIndex() );
131  return;
132  }
133 
134  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layer->id() );
135  if ( !nodeLayer )
136  return;
137 
138  setCurrentIndex( layerTreeModel()->node2index( nodeLayer ) );
139 }
140 
141 void QgsLayerTreeView::setLayerVisible( QgsMapLayer *layer, bool visible )
142 {
143  if ( !layer )
144  return;
145  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layer->id() );
146  if ( !nodeLayer )
147  return;
148  nodeLayer->setItemVisibilityChecked( visible );
149 }
150 
151 void QgsLayerTreeView::contextMenuEvent( QContextMenuEvent *event )
152 {
153  if ( !mMenuProvider )
154  return;
155 
156  QModelIndex idx = indexAt( event->pos() );
157  if ( !idx.isValid() )
158  setCurrentIndex( QModelIndex() );
159 
160  QMenu *menu = mMenuProvider->createContextMenu();
161  if ( menu && menu->actions().count() != 0 )
162  menu->exec( mapToGlobal( event->pos() ) );
163  delete menu;
164 }
165 
166 
167 void QgsLayerTreeView::modelRowsInserted( const QModelIndex &index, int start, int end )
168 {
169  QgsLayerTreeNode *parentNode = layerTreeModel()->index2node( index );
170  if ( !parentNode )
171  return;
172 
173  // Embedded widgets - replace placeholders in the model by actual widgets
174  if ( layerTreeModel()->testFlag( QgsLayerTreeModel::UseEmbeddedWidgets ) && QgsLayerTree::isLayer( parentNode ) )
175  {
176  QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( parentNode );
177  if ( QgsMapLayer *layer = nodeLayer->layer() )
178  {
179  int widgetsCount = layer->customProperty( QStringLiteral( "embeddedWidgets/count" ), 0 ).toInt();
180  QList<QgsLayerTreeModelLegendNode *> legendNodes = layerTreeModel()->layerLegendNodes( nodeLayer, true );
181  for ( int i = 0; i < widgetsCount; ++i )
182  {
183  QString providerId = layer->customProperty( QStringLiteral( "embeddedWidgets/%1/id" ).arg( i ) ).toString();
184  if ( QgsLayerTreeEmbeddedWidgetProvider *provider = QgsGui::layerTreeEmbeddedWidgetRegistry()->provider( providerId ) )
185  {
186  QModelIndex index = layerTreeModel()->legendNode2index( legendNodes[i] );
187  QWidget *wdgt = provider->createWidget( layer, i );
188  // Since column is resized to contents, limit the expanded width of embedded
189  // widgets, if they are not already limited, e.g. have the default MAX value.
190  // Else, embedded widget may grow very wide due to large legend graphics.
191  // NOTE: This approach DOES NOT work right. It causes horizontal scroll
192  // bar to disappear if the embedded widget is expanded and part
193  // of the last layer in the panel, even if much wider legend items
194  // are expanded above it. The correct width-limiting method should
195  // be setting fixed-width, hidpi-aware embedded widget items in a
196  // layout and appending an expanding QSpacerItem to end. This ensures
197  // full width is always created in the column by the embedded widget.
198  // See QgsLayerTreeOpacityWidget
199  //if ( wdgt->maximumWidth() == QWIDGETSIZE_MAX )
200  //{
201  // wdgt->setMaximumWidth( 250 );
202  //}
203 
204  setIndexWidget( index, wdgt );
205  }
206  }
207  }
208  }
209 
210 
211  if ( QgsLayerTree::isLayer( parentNode ) )
212  {
213  // if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes
214  QStringList expandedNodeKeys = parentNode->customProperty( QStringLiteral( "expandedLegendNodes" ) ).toStringList();
215  if ( expandedNodeKeys.isEmpty() )
216  return;
217 
218  const auto constLayerLegendNodes = layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ), true );
219  for ( QgsLayerTreeModelLegendNode *legendNode : constLayerLegendNodes )
220  {
221  QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
222  if ( expandedNodeKeys.contains( ruleKey ) )
223  setExpanded( layerTreeModel()->legendNode2index( legendNode ), true );
224  }
225  return;
226  }
227 
228  QList<QgsLayerTreeNode *> children = parentNode->children();
229  for ( int i = start; i <= end; ++i )
230  {
231  updateExpandedStateFromNode( children[i] );
232  }
233 
234  // make sure we still have correct current layer
236 }
237 
239 {
240  // make sure we still have correct current layer
242 }
243 
244 void QgsLayerTreeView::updateExpandedStateToNode( const QModelIndex &index )
245 {
246  if ( QgsLayerTreeNode *node = layerTreeModel()->index2node( index ) )
247  {
248  node->setExpanded( isExpanded( index ) );
249  }
250  else if ( QgsLayerTreeModelLegendNode *node = layerTreeModel()->index2legendNode( index ) )
251  {
252  QString ruleKey = node->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
253  QStringList lst = node->layerNode()->customProperty( QStringLiteral( "expandedLegendNodes" ) ).toStringList();
254  bool expanded = isExpanded( index );
255  bool isInList = lst.contains( ruleKey );
256  if ( expanded && !isInList )
257  {
258  lst.append( ruleKey );
259  node->layerNode()->setCustomProperty( QStringLiteral( "expandedLegendNodes" ), lst );
260  }
261  else if ( !expanded && isInList )
262  {
263  lst.removeAll( ruleKey );
264  node->layerNode()->setCustomProperty( QStringLiteral( "expandedLegendNodes" ), lst );
265  }
266  }
267 }
268 
270 {
271  QgsMapLayer *layerCurrent = layerForIndex( currentIndex() );
272  QString layerCurrentID = layerCurrent ? layerCurrent->id() : QString();
273  if ( mCurrentLayerID == layerCurrentID )
274  return;
275 
276  // update the current index in model (the item will be underlined)
277  QModelIndex nodeLayerIndex;
278  if ( layerCurrent )
279  {
280  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerCurrentID );
281  if ( nodeLayer )
282  nodeLayerIndex = layerTreeModel()->node2index( nodeLayer );
283  }
284  layerTreeModel()->setCurrentIndex( nodeLayerIndex );
285 
286  mCurrentLayerID = layerCurrentID;
287  emit currentLayerChanged( layerCurrent );
288 }
289 
291 {
292  QModelIndex idx = layerTreeModel()->node2index( node );
293  if ( isExpanded( idx ) != expanded )
294  setExpanded( idx, expanded );
295 }
296 
297 void QgsLayerTreeView::onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key )
298 {
299  if ( key != QStringLiteral( "expandedLegendNodes" ) || !QgsLayerTree::isLayer( node ) )
300  return;
301 
302  QSet<QString> expandedLegendNodes = qgis::listToSet( node->customProperty( QStringLiteral( "expandedLegendNodes" ) ).toStringList() );
303 
304  const QList<QgsLayerTreeModelLegendNode *> legendNodes = layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( node ), true );
305  for ( QgsLayerTreeModelLegendNode *legendNode : legendNodes )
306  {
307  QString key = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
308  if ( !key.isEmpty() )
309  setExpanded( layerTreeModel()->legendNode2index( legendNode ), expandedLegendNodes.contains( key ) );
310  }
311 }
312 
314 {
316 }
317 
319 {
320  QModelIndex idx = layerTreeModel()->node2index( node );
321  setExpanded( idx, node->isExpanded() );
322 
323  const auto constChildren = node->children();
324  for ( QgsLayerTreeNode *child : constChildren )
326 }
327 
328 QgsMapLayer *QgsLayerTreeView::layerForIndex( const QModelIndex &index ) const
329 {
330  // Check if model has been set and index is valid
331  if ( layerTreeModel() && index.isValid() )
332  {
333  QgsLayerTreeNode *node = layerTreeModel()->index2node( index );
334  if ( node )
335  {
336  if ( QgsLayerTree::isLayer( node ) )
337  return QgsLayerTree::toLayer( node )->layer();
338  }
339  else
340  {
341  // possibly a legend node
343  if ( legendNode )
344  return legendNode->layerNode()->layer();
345  }
346  }
347  return nullptr;
348 }
349 
351 {
352  return layerTreeModel()->index2node( selectionModel()->currentIndex() );
353 }
354 
356 {
357  QgsLayerTreeNode *node = currentNode();
358  if ( QgsLayerTree::isGroup( node ) )
359  return QgsLayerTree::toGroup( node );
360  else if ( QgsLayerTree::isLayer( node ) )
361  {
362  QgsLayerTreeNode *parent = node->parent();
363  if ( QgsLayerTree::isGroup( parent ) )
364  return QgsLayerTree::toGroup( parent );
365  }
366 
367  if ( QgsLayerTreeModelLegendNode *legendNode = layerTreeModel()->index2legendNode( selectionModel()->currentIndex() ) )
368  {
370  if ( QgsLayerTree::isGroup( parent->parent() ) )
371  return QgsLayerTree::toGroup( parent->parent() );
372  }
373 
374  return nullptr;
375 }
376 
378 {
379  return layerTreeModel()->index2legendNode( selectionModel()->currentIndex() );
380 }
381 
382 QList<QgsLayerTreeNode *> QgsLayerTreeView::selectedNodes( bool skipInternal ) const
383 {
384  return layerTreeModel()->indexes2nodes( selectionModel()->selectedIndexes(), skipInternal );
385 }
386 
387 QList<QgsLayerTreeLayer *> QgsLayerTreeView::selectedLayerNodes() const
388 {
389  QList<QgsLayerTreeLayer *> layerNodes;
390  const auto constSelectedNodes = selectedNodes();
391  for ( QgsLayerTreeNode *node : constSelectedNodes )
392  {
393  if ( QgsLayerTree::isLayer( node ) )
394  layerNodes << QgsLayerTree::toLayer( node );
395  }
396  return layerNodes;
397 }
398 
399 QList<QgsMapLayer *> QgsLayerTreeView::selectedLayers() const
400 {
401  QList<QgsMapLayer *> list;
402  const auto constSelectedLayerNodes = selectedLayerNodes();
403  for ( QgsLayerTreeLayer *node : constSelectedLayerNodes )
404  {
405  if ( node->layer() )
406  list << node->layer();
407  }
408  return list;
409 }
410 
411 QList<QgsMapLayer *> QgsLayerTreeView::selectedLayersRecursive() const
412 {
413  const QList<QgsLayerTreeNode *> nodes = layerTreeModel()->indexes2nodes( selectionModel()->selectedIndexes(), false );
414  QSet<QgsMapLayer *> layersSet = QgsLayerTreeUtils::collectMapLayersRecursive( nodes );
415  return qgis::setToList( layersSet );
416 }
417 
419 {
420  if ( !mIndicators[node].contains( indicator ) )
421  {
422  mIndicators[node].append( indicator );
423  connect( indicator, &QgsLayerTreeViewIndicator::changed, this, [ = ]
424  {
425  update();
426  } );
427  update();
428  }
429 }
430 
432 {
433  mIndicators[node].removeOne( indicator );
434  update();
435 }
436 
437 QList<QgsLayerTreeViewIndicator *> QgsLayerTreeView::indicators( QgsLayerTreeNode *node ) const
438 {
439  return mIndicators.value( node );
440 }
441 
443 QStringList QgsLayerTreeView::viewOnlyCustomProperties()
444 {
445  return QStringList() << QStringLiteral( "expandedLegendNodes" );
446 }
448 
449 void QgsLayerTreeView::refreshLayerSymbology( const QString &layerId )
450 {
451  QgsLayerTreeLayer *nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerId );
452  if ( nodeLayer )
453  layerTreeModel()->refreshLayerLegend( nodeLayer );
454 }
455 
456 
457 static void _expandAllLegendNodes( QgsLayerTreeLayer *nodeLayer, bool expanded, QgsLayerTreeModel *model )
458 {
459  // for layers we also need to find out with legend nodes contain some children and make them expanded/collapsed
460  // if we are collapsing, we just write out an empty list
461  QStringList lst;
462  if ( expanded )
463  {
464  const auto constLayerLegendNodes = model->layerLegendNodes( nodeLayer, true );
465  for ( QgsLayerTreeModelLegendNode *legendNode : constLayerLegendNodes )
466  {
467  QString parentKey = legendNode->data( QgsLayerTreeModelLegendNode::ParentRuleKeyRole ).toString();
468  if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
469  lst << parentKey;
470  }
471  }
472  nodeLayer->setCustomProperty( QStringLiteral( "expandedLegendNodes" ), lst );
473 }
474 
475 
476 static void _expandAllNodes( QgsLayerTreeGroup *parent, bool expanded, QgsLayerTreeModel *model )
477 {
478  const auto constChildren = parent->children();
479  for ( QgsLayerTreeNode *node : constChildren )
480  {
481  node->setExpanded( expanded );
482  if ( QgsLayerTree::isGroup( node ) )
483  _expandAllNodes( QgsLayerTree::toGroup( node ), expanded, model );
484  else if ( QgsLayerTree::isLayer( node ) )
485  _expandAllLegendNodes( QgsLayerTree::toLayer( node ), expanded, model );
486  }
487 }
488 
489 
491 {
492  // unfortunately expandAll() does not emit expanded() signals
493  _expandAllNodes( layerTreeModel()->rootGroup(), true, layerTreeModel() );
494  expandAll();
495 }
496 
498 {
499  // unfortunately collapseAll() does not emit collapsed() signals
500  _expandAllNodes( layerTreeModel()->rootGroup(), false, layerTreeModel() );
501  collapseAll();
502 }
503 
505 {
506  if ( mMessageBar == messageBar )
507  return;
508 
509  mMessageBar = messageBar;
510 
511  if ( mMessageBar )
513  [ = ]( const QString & message, Qgis::MessageLevel level = Qgis::Info, int duration = 5 )
514  {mMessageBar->pushMessage( message, level, duration );}
515  );
516 }
517 
518 void QgsLayerTreeView::mouseReleaseEvent( QMouseEvent *event )
519 {
520  // we need to keep last mouse position in order to know whether to emit an indicator's clicked() signal
521  // (the item delegate needs to know which indicator has been clicked)
522  mLastReleaseMousePos = event->pos();
523 
524  const QgsLayerTreeModel::Flags oldFlags = layerTreeModel()->flags();
525  if ( event->modifiers() & Qt::ControlModifier )
527  else
529  QTreeView::mouseReleaseEvent( event );
530  layerTreeModel()->setFlags( oldFlags );
531 }
532 
533 void QgsLayerTreeView::keyPressEvent( QKeyEvent *event )
534 {
535  if ( event->key() == Qt::Key_Space )
536  {
537  const auto constSelectedNodes = selectedNodes();
538 
539  if ( ! constSelectedNodes.isEmpty() )
540  {
541  bool isFirstNodeChecked = constSelectedNodes[0]->itemVisibilityChecked();
542  for ( QgsLayerTreeNode *node : constSelectedNodes )
543  {
544  node->setItemVisibilityChecked( ! isFirstNodeChecked );
545  }
546 
547  // if we call the original keyPress handler, the current item will be checked to the original state yet again
548  return;
549  }
550  }
551 
552  const QgsLayerTreeModel::Flags oldFlags = layerTreeModel()->flags();
553  if ( event->modifiers() & Qt::ControlModifier )
555  else
557  QTreeView::keyPressEvent( event );
558  layerTreeModel()->setFlags( oldFlags );
559 }
560 
561 void QgsLayerTreeView::dropEvent( QDropEvent *event )
562 {
563  if ( event->keyboardModifiers() & Qt::AltModifier )
564  {
565  event->accept();
566  }
567  QTreeView::dropEvent( event );
568 }
569 
570 void QgsLayerTreeView::resizeEvent( QResizeEvent *event )
571 {
572  // Since last column is resized to content (instead of stretched), the active
573  // selection rectangle ends at width of widest visible item in tree,
574  // regardless of which item is selected. This causes layer indicators to
575  // become 'inactive' (not clickable and no tool tip) unless their rectangle
576  // enters the view item's selection (active) rectangle.
577  // Always resetting the minimum section size relative to the viewport ensures
578  // the view item's selection rectangle extends to the right edge of the
579  // viewport, which allows indicators to become active again.
580  header()->setMinimumSectionSize( viewport()->width() );
581  QTreeView::resizeEvent( event );
582 }
583 
584 void QgsLayerTreeView::onHorizontalScroll( int value )
585 {
586  Q_UNUSED( value )
587  viewport()->update();
588 }
QgsLayerTreeView::currentGroupNode
QgsLayerTreeGroup * currentGroupNode() const
Gets current group node. If a layer is current node, the function will return parent group....
Definition: qgslayertreeview.cpp:355
QgsLayerTreeView::addIndicator
void addIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Adds an indicator to the given layer tree node.
Definition: qgslayertreeview.cpp:418
QgsLayerTreeGroup::findLayer
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Definition: qgslayertreegroup.cpp:195
QgsLayerTreeNode
Definition: qgslayertreenode.h:74
QgsLayerTreeView::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Definition: qgslayertreeview.cpp:533
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:313
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:103
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:253
qgslayertreeviewindicator.h
QgsLayerTreeViewMenuProvider
Definition: qgslayertreeview.h:282
QgsLayerTreeModel::setFlags
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
Definition: qgslayertreemodel.cpp:1157
QgsLayerTreeView::updateExpandedStateFromNode
void updateExpandedStateFromNode(QgsLayerTreeNode *node)
Definition: qgslayertreeview.cpp:318
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:88
QgsLayerTreeModel
Definition: qgslayertreemodel.h:53
QgsLayerTreeView::dropEvent
void dropEvent(QDropEvent *event) override
Definition: qgslayertreeview.cpp:561
QgsLayerTreeView::selectedNodes
QList< QgsLayerTreeNode * > selectedNodes(bool skipInternal=false) const
Returns list of selected nodes.
Definition: qgslayertreeview.cpp:382
QgsLayerTreeView::onExpandedChanged
void onExpandedChanged(QgsLayerTreeNode *node, bool expanded)
Definition: qgslayertreeview.cpp:290
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:377
QgsLayerTree::toGroup
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Definition: qgslayertree.h:77
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:115
QgsLayerTreeView::~QgsLayerTreeView
~QgsLayerTreeView() override
Definition: qgslayertreeview.cpp:72
QgsLayerTreeView::layerForIndex
QgsMapLayer * layerForIndex(const QModelIndex &index) const
Definition: qgslayertreeview.cpp:328
QgsLayerTreeView::setModel
void setModel(QAbstractItemModel *model) override
Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
Definition: qgslayertreeview.cpp:77
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:1402
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:455
QgsLayerTreeModel::index2legendNode
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
Definition: qgslayertreemodel.cpp:1396
QgsLayerTreeView::setCurrentLayer
void setCurrentLayer(QgsMapLayer *layer)
Sets the currently selected layer.
Definition: qgslayertreeview.cpp:126
QgsLayerTreeViewIndicator
Definition: qgslayertreeviewindicator.h:35
QgsLayerTreeView::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: qgslayertreeview.cpp:570
QgsLayerTreeEmbeddedWidgetProvider
Definition: qgslayertreeembeddedwidgetregistry.h:35
Qgis::Info
@ Info
Definition: qgis.h:103
QgsLayerTreeView::expandAllNodes
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.
Definition: qgslayertreeview.cpp:490
qgslayertreeviewitemdelegate.h
QgsLayerTreeModel::refreshLayerLegend
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
Definition: qgslayertreemodel.cpp:538
QgsLayerTreeLayer
Definition: qgslayertreelayer.h:43
qgsmaplayer.h
QgsLayerTreeView::indicators
QList< QgsLayerTreeViewIndicator * > indicators(QgsLayerTreeNode *node) const
Returns list of indicators associated with a particular layer tree node.
Definition: qgslayertreeview.cpp:437
QgsLayerTreeView::modelRowsRemoved
void modelRowsRemoved()
Definition: qgslayertreeview.cpp:238
QgsLayerTreeGroup
Definition: qgslayertreegroup.h:34
QgsMessageBar
Definition: qgsmessagebar.h:60
QgsLayerTreeUtils::collectMapLayersRecursive
static QSet< QgsMapLayer * > collectMapLayersRecursive(const QList< QgsLayerTreeNode * > &nodes)
Returns map layers from the given list of layer tree nodes.
Definition: qgslayertreeutils.cpp:496
QgsLayerTreeView::mMenuProvider
QgsLayerTreeViewMenuProvider * mMenuProvider
Context menu provider. Owned by the view.
Definition: qgslayertreeview.h:255
QgsLayerTreeView::currentNode
QgsLayerTreeNode * currentNode() const
Gets current node. May be nullptr.
Definition: qgslayertreeview.cpp:350
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:411
Qgis::UI_SCALE_FACTOR
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:195
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:88
QgsLayerTreeView::selectedLayers
QList< QgsMapLayer * > selectedLayers() const
Gets list of selected layers.
Definition: qgslayertreeview.cpp:399
QgsLayerTreeLayer::layer
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Definition: qgslayertreelayer.h:74
qgslayertree.h
QgsLayerTreeViewDefaultActions
Definition: qgslayertreeviewdefaultactions.h:39
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:504
QgsLayerTreeModel::rootGroup
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
Definition: qgslayertreemodel.cpp:518
QgsWms::legendNode
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
Definition: qgswmsgetlegendgraphics.cpp:358
QgsLayerTreeView::removeIndicator
void removeIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Removes a previously added indicator to a layer tree node.
Definition: qgslayertreeview.cpp:431
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:66
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:449
qgslayertreeview.h
QgsLayerTreeView::modelRowsInserted
void modelRowsInserted(const QModelIndex &index, int start, int end)
Definition: qgslayertreeview.cpp:167
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:151
QgsLayerTreeView::currentLayer
QgsMapLayer * currentLayer() const
Returns the currently selected layer, or nullptr if no layers is selected.
Definition: qgslayertreeview.cpp:121
QgsLayerTreeModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgslayertreemodel.cpp:354
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:101
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:269
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
Definition: qgsmaplayer.h:81
QgsLayerTreeView::selectedLayerNodes
QList< QgsLayerTreeLayer * > selectedLayerNodes() const
Returns list of selected nodes filtered to just layer nodes.
Definition: qgslayertreeview.cpp:387
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:108
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:518
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:1533
QgsLayerTreeView::setLayerVisible
void setLayerVisible(QgsMapLayer *layer, bool visible)
Convenience methods which sets the visible state of the specified map layer.
Definition: qgslayertreeview.cpp:141
QgsLayerTreeView::mCurrentLayerID
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer)
Definition: qgslayertreeview.h:257
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:56
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:244
QgsLayerTreeView::mLastReleaseMousePos
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked.
Definition: qgslayertreeview.h:261
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:566
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:259
QgsLayerTreeView::QgsLayerTreeViewItemDelegate
friend class QgsLayerTreeViewItemDelegate
Definition: qgslayertreeview.h:270
QgsLayerTreeView::collapseAllNodes
void collapseAllNodes()
Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes.
Definition: qgslayertreeview.cpp:497
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:491
QgsLayerTreeModelLegendNode
Definition: qgslayertreemodellegendnode.h:50