QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
QgsLayerTreeViewBase Class Reference

Base class for QTreeView widgets which display a layer tree. More...

#include <qgslayertreeview.h>

Inheritance diagram for QgsLayerTreeViewBase:

Public Slots

void collapseAllNodes ()
 Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes.
void expandAllNodes ()
 Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.

Public Member Functions

 QgsLayerTreeViewBase (QWidget *parent=nullptr)
 Constructor for QgsLayerTreeViewBase.
 ~QgsLayerTreeViewBase () override
QgsLayerTreeGroupcurrentGroupNode () const
 Returns the current group node.
QgsMapLayercurrentLayer () const
 Returns the currently selected layer, or nullptr if no layers is selected.
QgsLayerTreeModelLegendNodecurrentLegendNode () const
 Gets current legend node.
QgsLayerTreeNodecurrentNode () const
 Returns the current node.
QgsLayerTreeViewDefaultActionsdefaultActions ()
 Gets access to the default actions that may be used with the tree view.
QgsLayerTreeModelLegendNodeindex2legendNode (const QModelIndex &index) const
 Returns legend node for given view index.
QgsLayerTreeNodeindex2node (const QModelIndex &index) const
 Returns the layer tree node for given view index.
QgsMapLayerlayerForIndex (const QModelIndex &index) const
 Returns the map layer corresponding to a view index.
QgsLayerTreeModellayerTreeModel () const
 Returns the associated layer tree model.
QModelIndex legendNode2index (QgsLayerTreeModelLegendNode *legendNode)
 Returns the view index for a given legend node.
QModelIndex legendNode2sourceIndex (QgsLayerTreeModelLegendNode *legendNode)
 Returns the layer tree source model index for a given legend node.
void mouseDoubleClickEvent (QMouseEvent *event) override
QModelIndex node2index (QgsLayerTreeNode *node) const
 Returns the view model index for a given node.
QModelIndex node2sourceIndex (QgsLayerTreeNode *node) const
 Returns the layer tree source model index for a given node.
QList< QgsLayerTreeLayer * > selectedLayerNodes () const
 Returns the list of selected nodes filtered to just layer nodes (QgsLayerTreeLayer).
QList< QgsMapLayer * > selectedLayers () const
 Returns the list of selected layers.
QList< QgsMapLayer * > selectedLayersRecursive () const
 Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected.
QList< QgsLayerTreeModelLegendNode * > selectedLegendNodes () const
 Returns the list of selected legend nodes.
QList< QgsLayerTreeNode * > selectedNodes (bool skipInternal=false) const
 Returns the list of selected layer tree nodes.
void setCurrentLayer (QgsMapLayer *layer)
 Sets the currently selected layer.
void setCurrentNode (QgsLayerTreeNode *node)
 Sets the currently selected node.
void setLayerTreeModel (QgsLayerTreeModel *model)
 Associates a layer tree model with the view.

Protected Slots

void onExpandedChanged (QgsLayerTreeNode *node, bool expanded)
 Called when the expanded state changes for a node.
void onModelReset ()
 Called when the model is reset.
void updateExpandedStateToNode (const QModelIndex &index)
 Stores the expanded state to a node with matching index.

Protected Member Functions

QModelIndex layerTreeModelIndexToViewIndex (const QModelIndex &index) const
 Returns the layer tree model index corresponding with a view index.
void updateExpandedStateFromNode (QgsLayerTreeNode *node)
 Updates the expanded state from a node.
QModelIndex viewIndexToLayerTreeModelIndex (const QModelIndex &index) const
 Returns the view index corresponding with a layer tree model index.

Protected Attributes

QgsLayerTreeViewDefaultActionsmDefaultActions = nullptr
 helper class with default actions. Lazily initialized.

Detailed Description

Base class for QTreeView widgets which display a layer tree.

The view updates expanded state of layer tree nodes and also listens to changes to expanded states in the layer tree.

Warning
Subclasses must take care to call both setLayerTreeModel() and QTreeView::setModel() in order to have a fully functional tree view. This is by design, as it permits use of a custom proxy model in the view.
See also
QgsLayerTreeView
Since
QGIS 4.0

Definition at line 130 of file qgslayertreeview.h.

Constructor & Destructor Documentation

◆ QgsLayerTreeViewBase()

QgsLayerTreeViewBase::QgsLayerTreeViewBase ( QWidget * parent = nullptr)
explicit

Constructor for QgsLayerTreeViewBase.

Definition at line 45 of file qgslayertreeview.cpp.

◆ ~QgsLayerTreeViewBase()

QgsLayerTreeViewBase::~QgsLayerTreeViewBase ( )
override

Definition at line 74 of file qgslayertreeview.cpp.

Member Function Documentation

◆ collapseAllNodes

void QgsLayerTreeViewBase::collapseAllNodes ( )
slot

Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes.

Definition at line 445 of file qgslayertreeview.cpp.

◆ currentGroupNode()

QgsLayerTreeGroup * QgsLayerTreeViewBase::currentGroupNode ( ) const

Returns the current group node.

If a layer is the current node, the function will return the layer's parent group.

May be nullptr.

Definition at line 267 of file qgslayertreeview.cpp.

◆ currentLayer()

QgsMapLayer * QgsLayerTreeViewBase::currentLayer ( ) const

Returns the currently selected layer, or nullptr if no layers is selected.

See also
setCurrentLayer()

Definition at line 241 of file qgslayertreeview.cpp.

◆ currentLegendNode()

QgsLayerTreeModelLegendNode * QgsLayerTreeViewBase::currentLegendNode ( ) const

Gets current legend node.

May be nullptr if current node is not a legend node.

Definition at line 356 of file qgslayertreeview.cpp.

◆ currentNode()

QgsLayerTreeNode * QgsLayerTreeViewBase::currentNode ( ) const

Returns the current node.

May be nullptr.

Definition at line 215 of file qgslayertreeview.cpp.

◆ defaultActions()

QgsLayerTreeViewDefaultActions * QgsLayerTreeViewBase::defaultActions ( )

Gets access to the default actions that may be used with the tree view.

Definition at line 455 of file qgslayertreeview.cpp.

◆ expandAllNodes

void QgsLayerTreeViewBase::expandAllNodes ( )
slot

Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.

Definition at line 435 of file qgslayertreeview.cpp.

◆ index2legendNode()

QgsLayerTreeModelLegendNode * QgsLayerTreeViewBase::index2legendNode ( const QModelIndex & index) const

Returns legend node for given view index.

Returns nullptr for invalid index.

Unlike QgsLayerTreeModel::index2legendNode(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

Since
QGIS 3.18

Definition at line 210 of file qgslayertreeview.cpp.

◆ index2node()

QgsLayerTreeNode * QgsLayerTreeViewBase::index2node ( const QModelIndex & index) const

Returns the layer tree node for given view index.

Returns root node for an invalid index.

Returns nullptr if index does not refer to a layer tree node (e.g. it is a legend node).

Unlike QgsLayerTreeViewBase::index2Node(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

See also
node2index()
Since
QGIS 3.18

Definition at line 189 of file qgslayertreeview.cpp.

◆ layerForIndex()

QgsMapLayer * QgsLayerTreeViewBase::layerForIndex ( const QModelIndex & index) const

Returns the map layer corresponding to a view index.

This method correctly accounts for proxy models set on the tree view.

Returns nullptr if the index does not correspond to a map layer.

Definition at line 246 of file qgslayertreeview.cpp.

◆ layerTreeModel()

QgsLayerTreeModel * QgsLayerTreeViewBase::layerTreeModel ( ) const

Returns the associated layer tree model.

See also
setLayerTreeModel()

Definition at line 101 of file qgslayertreeview.cpp.

◆ layerTreeModelIndexToViewIndex()

QModelIndex QgsLayerTreeViewBase::layerTreeModelIndexToViewIndex ( const QModelIndex & index) const
protected

Returns the layer tree model index corresponding with a view index.

This method correctly accounts for proxy models set on the tree view.

See also
viewIndexToLayerTreeModelIndex()

Definition at line 130 of file qgslayertreeview.cpp.

◆ legendNode2index()

QModelIndex QgsLayerTreeViewBase::legendNode2index ( QgsLayerTreeModelLegendNode * legendNode)

Returns the view index for a given legend node.

If the legend node does not belong to the layer tree, the result is undefined.

If the legend node belongs to the tree but it is filtered out, an invalid model index is returned.

Unlike QgsLayerTreeModel::legendNode2index(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

Since
QGIS 3.18

Definition at line 318 of file qgslayertreeview.cpp.

◆ legendNode2sourceIndex()

QModelIndex QgsLayerTreeViewBase::legendNode2sourceIndex ( QgsLayerTreeModelLegendNode * legendNode)

Returns the layer tree source model index for a given legend node.

If the legend node does not belong to the layer tree, the result is undefined.

If the legend node belongs to the tree but it is filtered out, an invalid model index is returned.

Warning
The returned index belongs the underlying layer tree model, and care should be taken to correctly map to a proxy index if a proxy model is in use.
See also
legendNode2index()
Since
QGIS 3.18

Definition at line 323 of file qgslayertreeview.cpp.

◆ mouseDoubleClickEvent()

void QgsLayerTreeViewBase::mouseDoubleClickEvent ( QMouseEvent * event)
override

Definition at line 79 of file qgslayertreeview.cpp.

◆ node2index()

QModelIndex QgsLayerTreeViewBase::node2index ( QgsLayerTreeNode * node) const

Returns the view model index for a given node.

If the node does not belong to the layer tree, the result is undefined.

Unlike QgsLayerTreeModel::node2index(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

See also
index2node()
Since
QGIS 3.18

Definition at line 197 of file qgslayertreeview.cpp.

◆ node2sourceIndex()

QModelIndex QgsLayerTreeViewBase::node2sourceIndex ( QgsLayerTreeNode * node) const

Returns the layer tree source model index for a given node.

If the node does not belong to the layer tree, the result is undefined.

Warning
The returned index belongs the underlying layer tree model, and care should be taken to correctly map to a proxy index if a proxy model is in use.
See also
node2index()
Since
QGIS 3.18

Definition at line 202 of file qgslayertreeview.cpp.

◆ onExpandedChanged

void QgsLayerTreeViewBase::onExpandedChanged ( QgsLayerTreeNode * node,
bool expanded )
protectedslot

Called when the expanded state changes for a node.

Definition at line 169 of file qgslayertreeview.cpp.

◆ onModelReset

void QgsLayerTreeViewBase::onModelReset ( )
protectedslot

Called when the model is reset.

Definition at line 176 of file qgslayertreeview.cpp.

◆ selectedLayerNodes()

QList< QgsLayerTreeLayer * > QgsLayerTreeViewBase::selectedLayerNodes ( ) const

Returns the list of selected nodes filtered to just layer nodes (QgsLayerTreeLayer).

See also
selectedNodes()
selectedLayers()
selectedLegendNodes()

Definition at line 292 of file qgslayertreeview.cpp.

◆ selectedLayers()

QList< QgsMapLayer * > QgsLayerTreeViewBase::selectedLayers ( ) const

Returns the list of selected layers.

See also
selectedNodes()
selectedLayerNodes()
selectedLegendNodes()

Definition at line 305 of file qgslayertreeview.cpp.

◆ selectedLayersRecursive()

QList< QgsMapLayer * > QgsLayerTreeViewBase::selectedLayersRecursive ( ) const

Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected.

If we have a group with two layers L1, L2 and just the group node is selected, this method returns L1 and L2, while selectedLayers() returns an empty list.

Since
QGIS 3.4

Definition at line 385 of file qgslayertreeview.cpp.

◆ selectedLegendNodes()

QList< QgsLayerTreeModelLegendNode * > QgsLayerTreeViewBase::selectedLegendNodes ( ) const

Returns the list of selected legend nodes.

See also
selectedNodes()
selectedLayerNodes()
Since
QGIS 3.32

Definition at line 364 of file qgslayertreeview.cpp.

◆ selectedNodes()

QList< QgsLayerTreeNode * > QgsLayerTreeViewBase::selectedNodes ( bool skipInternal = false) const

Returns the list of selected layer tree nodes.

Parameters
skipInternalIf true, will ignore nodes which have an ancestor in the selection
See also
selectedLayerNodes()
selectedLegendNodes()
selectedLayers()

Definition at line 223 of file qgslayertreeview.cpp.

◆ setCurrentLayer()

void QgsLayerTreeViewBase::setCurrentLayer ( QgsMapLayer * layer)

Sets the currently selected layer.

If layer is nullptr then all layers will be deselected.

See also
currentLayer()

Definition at line 341 of file qgslayertreeview.cpp.

◆ setCurrentNode()

void QgsLayerTreeViewBase::setCurrentNode ( QgsLayerTreeNode * node)

Sets the currently selected node.

If node is nullptr then all nodes will be deselected.

See also
currentNode()
Since
QGIS 3.40

Definition at line 330 of file qgslayertreeview.cpp.

◆ setLayerTreeModel()

void QgsLayerTreeViewBase::setLayerTreeModel ( QgsLayerTreeModel * model)

Associates a layer tree model with the view.

Warning
This does NOT explicitly set the view's model, and a subsequent call to QTreeView::setModel() must be made. This is by design, as it permits use of a custom proxy model in the view.
See also
layerTreeModel()

Definition at line 87 of file qgslayertreeview.cpp.

◆ updateExpandedStateFromNode()

void QgsLayerTreeViewBase::updateExpandedStateFromNode ( QgsLayerTreeNode * node)
protected

Updates the expanded state from a node.

Definition at line 106 of file qgslayertreeview.cpp.

◆ updateExpandedStateToNode

void QgsLayerTreeViewBase::updateExpandedStateToNode ( const QModelIndex & index)
protectedslot

Stores the expanded state to a node with matching index.

Definition at line 144 of file qgslayertreeview.cpp.

◆ viewIndexToLayerTreeModelIndex()

QModelIndex QgsLayerTreeViewBase::viewIndexToLayerTreeModelIndex ( const QModelIndex & index) const
protected

Returns the view index corresponding with a layer tree model index.

This method correctly accounts for proxy models set on the tree view.

See also
layerTreeModelIndexToViewIndex()

Definition at line 116 of file qgslayertreeview.cpp.

Member Data Documentation

◆ mDefaultActions

QgsLayerTreeViewDefaultActions* QgsLayerTreeViewBase::mDefaultActions = nullptr
protected

helper class with default actions. Lazily initialized.

Definition at line 385 of file qgslayertreeview.h.


The documentation for this class was generated from the following files: