28 #include <QContextMenuEvent> 38 setHeaderHidden(
true );
40 setDragEnabled(
true );
41 setAcceptDrops(
true );
42 setDropIndicatorShown(
true );
43 setEditTriggers( EditKeyPressed );
44 setExpandsOnDoubleClick(
false );
47 setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
49 setSelectionMode( ExtendedSelection );
50 setDefaultDropAction( Qt::MoveAction );
54 setStyle(
new QgsLayerTreeViewProxyStyle(
this ) );
67 if ( !qobject_cast<QgsLayerTreeModel *>( model ) )
73 QTreeView::setModel( model );
112 setCurrentIndex( QModelIndex() );
129 QModelIndex idx = indexAt( event->pos() );
130 if ( !idx.isValid() )
131 setCurrentIndex( QModelIndex() );
134 if ( menu && menu->actions().count() != 0 )
135 menu->exec( mapToGlobal( event->pos() ) );
152 int widgetsCount = layer->customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
154 for (
int i = 0; i < widgetsCount; ++i )
156 QString providerId = layer->customProperty( QStringLiteral(
"embeddedWidgets/%1/id" ).arg( i ) ).toString();
160 setIndexWidget( index, provider->createWidget( layer, i ) );
170 QStringList expandedNodeKeys = parentNode->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
171 if ( expandedNodeKeys.isEmpty() )
177 if ( expandedNodeKeys.contains( ruleKey ) )
183 QList<QgsLayerTreeNode *> children = parentNode->
children();
184 for (
int i = start; i <= end; ++i )
203 node->setExpanded( isExpanded( index ) );
208 QStringList lst = node->layerNode()->customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
209 bool expanded = isExpanded( index );
210 bool isInList = lst.contains( ruleKey );
211 if ( expanded && !isInList )
213 lst.append( ruleKey );
214 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
216 else if ( !expanded && isInList )
218 lst.removeAll( ruleKey );
219 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
227 QString layerCurrentID = layerCurrent ? layerCurrent->
id() : QString();
232 QModelIndex nodeLayerIndex;
248 if ( isExpanded( idx ) != expanded )
249 setExpanded( idx, expanded );
252 void QgsLayerTreeView::onCustomPropertyChanged(
QgsLayerTreeNode *node,
const QString &key )
257 QSet<QString> expandedLegendNodes = node->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList().toSet();
263 if ( !key.isEmpty() )
343 QList<QgsLayerTreeLayer *> layerNodes;
354 QList<QgsMapLayer *> list;
358 list << node->
layer();
367 return layersSet.toList();
387 QStringList QgsLayerTreeView::viewOnlyCustomProperties()
389 return QStringList() << QStringLiteral(
"expandedLegendNodes" );
411 if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
453 if ( event->modifiers() & Qt::ControlModifier )
457 QTreeView::mouseReleaseEvent( event );
464 if ( event->modifiers() & Qt::ControlModifier )
468 QTreeView::keyPressEvent( event );
474 if ( event->keyboardModifiers() & Qt::AltModifier )
478 QTreeView::dropEvent( event );
Layer tree group node serves as a container for layers and further groups.
QgsLayerTreeModelLegendNode * currentLegendNode() const
Gets current legend node.
QgsLayerTreeModel * layerTreeModel() const
Gets access to the model casted to QgsLayerTreeModel.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
QgsMapLayer * currentLayer() const
Gets currently selected layer. May be null.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
void setCurrentIndex(const QModelIndex ¤tIndex)
Sets index of the current item. May be used by view. Item marked as current is underlined.
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non-null pointer.
QgsLayerTreeViewMenuProvider * mMenuProvider
Context menu provider. Owned by the view.
void removeIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Removes a previously added indicator to a layer tree node.
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
QList< QgsMapLayer * > selectedLayersRecursive() const
Gets list of selected layers, including those that are not directly selected, but their ancestor grou...
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...
void refreshLayerSymbology(const QString &layerId)
Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitore...
QgsMapLayer * layer() const
Returns the map layer associated with this node.
void contextMenuEvent(QContextMenuEvent *event) override
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be null.
QgsLayerTreeNode * currentNode() const
Gets current node. May be null.
QList< QgsMapLayer * > selectedLayers() const
Gets list of selected layers.
void modelRowsInserted(const QModelIndex &index, int start, int end)
QgsLayerTreeViewDefaultActions * defaultActions()
Gets access to the default actions that may be used with the tree view.
QList< QgsLayerTreeNode * > selectedNodes(bool skipInternal=false) const
Returns list of selected nodes.
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
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...
QList< QgsLayerTreeViewIndicator * > indicators(QgsLayerTreeNode *node) const
Returns list of indicators associated with a particular layer tree node.
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)
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Returns index for a given legend node.
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...
QgsLayerTreeGroup * currentGroupNode() const
Gets current group node. If a layer is current node, the function will return parent group...
void addIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Adds an indicator to the given layer tree node.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is a null pointer, the node is a root node. ...
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked. ...
static QSet< QgsMapLayer * > collectMapLayersRecursive(const QList< QgsLayerTreeNode * > &nodes)
Returns map layers from the given list of layer tree nodes.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
friend class QgsLayerTreeViewItemDelegate
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
void collapseAllNodes()
Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes...
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
void keyPressEvent(QKeyEvent *event) override
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
QgsLayerTreeViewDefaultActions * mDefaultActions
helper class with default actions. Lazily initialized.
QList< QgsLayerTreeLayer * > selectedLayerNodes() const
Returns list of selected nodes filtered to just layer nodes.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
QModelIndex node2index(QgsLayerTreeNode *node) const
Returns index for a given node. If the node does not belong to the layer tree, the result is undefine...
Qt::ItemFlags flags(const QModelIndex &index) const override
void updateExpandedStateFromNode(QgsLayerTreeNode *node)
void onExpandedChanged(QgsLayerTreeNode *node, bool expanded)
Layer nodes may optionally include extra embedded widgets (if used in QgsLayerTreeView). Added in 2.16.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
void dropEvent(QDropEvent *event) override
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Check/uncheck action has consequences on children (or parents for leaf node)
void setModel(QAbstractItemModel *model) override
Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
void setCurrentLayer(QgsMapLayer *layer)
Sets currently selected layer. Null pointer will deselect any layer.
QgsLayerTreeView(QWidget *parent=nullptr)
Constructor for QgsLayerTreeView.
void setMenuProvider(QgsLayerTreeViewMenuProvider *menuProvider)
Sets provider for context menu. Takes ownership of the instance.
~QgsLayerTreeView() override
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes...
Indicator that can be used in a layer tree view to display icons next to items of the layer tree...
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
QgsMapLayer * layerForIndex(const QModelIndex &index) const
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file...
Layer tree node points to a map layer.