27 #include <QContextMenuEvent> 34 setHeaderHidden(
true );
36 setDragEnabled(
true );
37 setAcceptDrops(
true );
38 setDropIndicatorShown(
true );
39 setEditTriggers( EditKeyPressed );
40 setExpandsOnDoubleClick(
false );
42 setSelectionMode( ExtendedSelection );
43 setDefaultDropAction( Qt::MoveAction );
56 if ( !qobject_cast<QgsLayerTreeModel *>( model ) )
62 QTreeView::setModel( model );
100 setCurrentIndex( QModelIndex() );
117 QModelIndex idx = indexAt( event->pos() );
118 if ( !idx.isValid() )
119 setCurrentIndex( QModelIndex() );
122 if ( menu && menu->actions().count() != 0 )
123 menu->exec( mapToGlobal( event->pos() ) );
140 int widgetsCount = layer->customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
142 for (
int i = 0; i < widgetsCount; ++i )
144 QString providerId = layer->customProperty( QStringLiteral(
"embeddedWidgets/%1/id" ).arg( i ) ).toString();
148 setIndexWidget( index, provider->createWidget( layer, i ) );
158 QStringList expandedNodeKeys = parentNode->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
159 if ( expandedNodeKeys.isEmpty() )
165 if ( expandedNodeKeys.contains( ruleKey ) )
171 QList<QgsLayerTreeNode *> children = parentNode->
children();
172 for (
int i = start; i <=
end; ++i )
191 node->setExpanded( isExpanded( index ) );
196 QStringList lst = node->layerNode()->customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
197 bool expanded = isExpanded( index );
198 bool isInList = lst.contains( ruleKey );
199 if ( expanded && !isInList )
201 lst.append( ruleKey );
202 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
204 else if ( !expanded && isInList )
206 lst.removeAll( ruleKey );
207 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
215 QString layerCurrentID = layerCurrent ? layerCurrent->
id() : QString();
220 QModelIndex nodeLayerIndex;
236 if ( isExpanded( idx ) != expanded )
237 setExpanded( idx, expanded );
315 QList<QgsLayerTreeLayer *> layerNodes;
326 QList<QgsMapLayer *> list;
330 list << node->
layer();
354 if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
392 if ( event->modifiers() & Qt::ControlModifier )
396 QTreeView::mouseReleaseEvent( event );
403 if ( event->modifiers() & Qt::ControlModifier )
407 QTreeView::keyPressEvent( event );
413 if ( event->keyboardModifiers() & Qt::AltModifier )
417 QTreeView::dropEvent( event );
Layer tree group node serves as a container for layers and further groups.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
QList< QgsMapLayer * > selectedLayers() const
Get list of selected layers.
Base class for all map layer types.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
void setCurrentIndex(const QModelIndex ¤tIndex)
Set index of the current item. May be used by view. Item marked as current is underlined.
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
QgsLayerTreeViewMenuProvider * mMenuProvider
Context menu provider. Owned by the view.
void refreshLayerSymbology(const QString &layerId)
Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitore...
void contextMenuEvent(QContextMenuEvent *event) override
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
QgsLayerTreeLayer * layerNode() const
Return pointer to the parent layer node.
void modelRowsInserted(const QModelIndex &index, int start, int end)
QgsMapLayer * currentLayer() const
Get currently selected layer. May be null.
QgsLayerTreeViewDefaultActions * defaultActions()
Get access to the default actions that may be used with the tree view.
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
QModelIndex node2index(QgsLayerTreeNode *node) const
Return index for a given node. If the node does not belong to the layer tree, the result is undefined...
void setFlags(QgsLayerTreeModel::Flags f)
Set OR-ed combination of model flags.
QList< QgsLayerTreeLayer * > selectedLayerNodes() const
Return 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's unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Get 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)
Return index for a given legend node.
Rule key of the node (QString)
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
QgsLayerTreeNode * parent()
Get pointer to the parent. If parent is a null pointer, the node is a root node.
QgsLayerTreeGroup * currentGroupNode() const
Get current group node. If a layer is current node, the function will return parent group...
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...
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
void collapseAllNodes()
Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes...
void keyPressEvent(QKeyEvent *event) override
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
QgsLayerTreeModel * layerTreeModel() const
Get access to the model casted to QgsLayerTreeModel.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
QgsLayerTreeViewDefaultActions * mDefaultActions
helper class with default actions. Lazily initialized.
void setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
QgsMapLayer * layer() const
Qt::ItemFlags flags(const QModelIndex &index) const override
void updateExpandedStateFromNode(QgsLayerTreeNode *node)
QList< QgsLayerTreeNode * > selectedNodes(bool skipInternal=false) const
Return list of selected nodes.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
QgsLayerTree * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
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
Get current legend node.
QgsLayerTreeViewMenuProvider * menuProvider() const
Return pointer to the context menu provider. May be null.
virtual QVariant data(int role) const =0
Return data associated with the item. 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)
Return filtered list of active legend nodes attached to a particular layer node (by default it return...
void setCurrentLayer(QgsMapLayer *layer)
Set currently selected layer. Null pointer will deselect any layer.
QgsLayerTreeView(QWidget *parent=nullptr)
Constructor for QgsLayerTreeView.
void setMenuProvider(QgsLayerTreeViewMenuProvider *menuProvider)
Set provider for context menu. Takes ownership of the instance.
~QgsLayerTreeView() override
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes...
void setCustomProperty(const QString &key, const QVariant &value)
Set 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
Get current node. May be null.