QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
31 #include <QContextMenuEvent>
32 #include <QHeaderView>
35 #ifdef ENABLE_MODELTEST
36 #include "modeltest.h"
47 setHeaderHidden(
true );
49 setDragEnabled(
true );
50 setAcceptDrops(
true );
51 setDropIndicatorShown(
true );
52 setEditTriggers( EditKeyPressed );
53 setExpandsOnDoubleClick(
false );
56 header()->setStretchLastSection(
false );
57 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
60 setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
62 setSelectionMode( ExtendedSelection );
63 setDefaultDropAction( Qt::MoveAction );
67 setStyle(
new QgsLayerTreeViewProxyStyle(
this ) );
74 connect( horizontalScrollBar(), &QScrollBar::valueChanged,
this, &QgsLayerTreeView::onHorizontalScroll );
90 [ = ](
const QString & message,
Qgis::MessageLevel level = Qgis::MessageLevel::Info,
int duration = 5 )
102 #ifdef ENABLE_MODELTEST
103 new ModelTest( mProxyModel,
this );
106 mProxyModel->setShowPrivateLayers( mShowPrivateLayers );
107 QTreeView::setModel( mProxyModel );
116 connect( treeModel, &QAbstractItemModel::dataChanged,
this, &QgsLayerTreeView::onDataChanged );
125 return mProxyModel ? qobject_cast<QgsLayerTreeModel *>( mProxyModel->sourceModel() ) :
nullptr;
150 setCurrentIndex( QModelIndex() );
176 const QModelIndex idx = indexAt( event->pos() );
177 if ( !idx.isValid() )
178 setCurrentIndex( QModelIndex() );
181 if ( menu && menu->actions().count() != 0 )
182 menu->exec( mapToGlobal( event->pos() ) );
199 const int widgetsCount = layer->customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
201 for (
int i = 0; i < widgetsCount; ++i )
203 const QString providerId = layer->customProperty( QStringLiteral(
"embeddedWidgets/%1/id" ).arg( i ) ).toString();
207 QWidget *wdgt = provider->createWidget( layer, i );
224 setIndexWidget( index, wdgt );
234 const QStringList expandedNodeKeys = parentNode->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
235 if ( expandedNodeKeys.isEmpty() )
242 if ( expandedNodeKeys.contains( ruleKey ) )
248 QList<QgsLayerTreeNode *> children = parentNode->
children();
249 for (
int i = start; i <= end; ++i )
268 node->setExpanded( isExpanded( index ) );
273 QStringList lst = node->layerNode()->customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
274 const bool expanded = isExpanded( index );
275 const bool isInList = lst.contains( ruleKey );
276 if ( expanded && !isInList )
278 lst.append( ruleKey );
279 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
281 else if ( !expanded && isInList )
283 lst.removeAll( ruleKey );
284 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
292 const QString layerCurrentID = layerCurrent ? layerCurrent->
id() : QString();
297 QModelIndex proxyModelNodeLayerIndex;
302 proxyModelNodeLayerIndex =
node2index( nodeLayer );
305 if ( ! proxyModelNodeLayerIndex.isValid() )
324 if ( isExpanded( idx ) != expanded )
325 setExpanded( idx, expanded );
328 void QgsLayerTreeView::onCustomPropertyChanged(
QgsLayerTreeNode *node,
const QString &key )
333 const QSet<QString> expandedLegendNodes = qgis::listToSet( node->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList() );
339 if ( !key.isEmpty() )
355 const auto constChildren = node->
children();
384 return index2node( selectionModel()->currentIndex() );
416 QModelIndexList mapped;
417 const QModelIndexList selected = selectionModel()->selectedIndexes();
418 mapped.reserve( selected.size() );
419 for (
const QModelIndex &index : selected )
420 mapped << mProxyModel->mapToSource( index );
427 QList<QgsLayerTreeLayer *> layerNodes;
439 QList<QgsMapLayer *> list;
444 list << node->layer();
451 QModelIndexList mapped;
452 const QModelIndexList selected = selectionModel()->selectedIndexes();
453 mapped.reserve( selected.size() );
454 for (
const QModelIndex &index : selected )
455 mapped << mProxyModel->mapToSource( index );
459 return qgis::setToList( layersSet );
470 viewport()->repaint();
473 viewport()->repaint();
489 QStringList QgsLayerTreeView::viewOnlyCustomProperties()
491 return QStringList() << QStringLiteral(
"expandedLegendNodes" );
510 const auto constLayerLegendNodes = model->
layerLegendNodes( nodeLayer,
true );
514 if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
524 const auto constChildren = parent->
children();
552 if ( mMessageBar == messageBar )
555 mMessageBar = messageBar;
559 [ = ](
const QString & message,
Qgis::MessageLevel level = Qgis::MessageLevel::Info,
int duration = 5 )
569 mShowPrivateLayers = showPrivate;
575 return mShowPrivateLayers;
585 if ( event->modifiers() & Qt::ControlModifier )
589 QTreeView::mouseReleaseEvent( event );
595 if ( event->key() == Qt::Key_Space )
599 if ( ! constSelectedNodes.isEmpty() )
601 const bool isFirstNodeChecked = constSelectedNodes[0]->itemVisibilityChecked();
613 if ( event->modifiers() & Qt::ControlModifier )
617 QTreeView::keyPressEvent( event );
623 if ( event->keyboardModifiers() & Qt::AltModifier )
627 QTreeView::dropEvent( event );
640 header()->setMinimumSectionSize( viewport()->width() );
641 QTreeView::resizeEvent( event );
644 void QgsLayerTreeView::onHorizontalScroll(
int value )
647 viewport()->update();
650 void QgsLayerTreeView::onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles )
653 Q_UNUSED( bottomRight )
659 if ( roles.contains( Qt::SizeHintRole ) )
660 viewport()->update();
667 void QgsLayerTreeView::checkModel()
673 qDebug() <<
"----------------------------------------------";
680 for (
int i = 0; i < mProxyModel->rowCount(
node2index( node ) ); i++ )
684 debug( childNode, depth + 1 );
686 qDebug() <<
"Warning no child node!";
729 : QSortFilterProxyModel( parent )
730 , mLayerTreeModel( treeModel )
732 setSourceModel( treeModel );
737 if ( filterText == mFilterText )
740 mFilterText = filterText;
747 return nodeShown( node );
764 if ( !mFilterText.isEmpty() && !layer->
name().contains( mFilterText, Qt::CaseInsensitive ) )
766 if ( ! mShowPrivateLayers && layer->
flags().testFlag( QgsMapLayer::LayerFlag::Private ) )
776 return mShowPrivateLayers;
781 mShowPrivateLayers = showPrivate;
@ NodeGroup
Container of other groups and layers.
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.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
This class is a base class for nodes in a layer tree.
void keyPressEvent(QKeyEvent *event) override
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Returns proxy model index for a given legend node.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
QgsLayerTreeModel * layerTreeModel() const
Gets access to the model casted to QgsLayerTreeModel.
QgsLayerTreeViewDefaultActions * mDefaultActions
helper class with default actions. Lazily initialized.
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
void updateExpandedStateFromNode(QgsLayerTreeNode *node)
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be nullptr.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
bool showPrivateLayers()
Returns the show private layers status.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
void messageEmitted(const QString &message, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=5)
Emits a message than can be displayed to the user in a GUI class.
void dropEvent(QDropEvent *event) override
QList< QgsLayerTreeNode * > selectedNodes(bool skipInternal=false) const
Returns list of selected nodes.
void onExpandedChanged(QgsLayerTreeNode *node, bool expanded)
@ ActionHierarchical
Check/uncheck action has consequences on children (or parents for leaf node)
QgsLayerTreeModelLegendNode * currentLegendNode() const
Gets current legend node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
QgsLayerTreeView(QWidget *parent=nullptr)
Constructor for QgsLayerTreeView.
void setMenuProvider(QgsLayerTreeViewMenuProvider *menuProvider)
Sets provider for context menu. Takes ownership of the instance.
bool showPrivateLayers() const
Returns if private layers are shown.
QModelIndex legendNode2sourceIndex(QgsLayerTreeModelLegendNode *legendNode)
Returns index for a given legend node.
~QgsLayerTreeView() override
The QgsLayerTreeProxyModel class is a proxy model for QgsLayerTreeModel, supports private layers and ...
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
QgsMapLayer * layerForIndex(const QModelIndex &index) const
void setModel(QAbstractItemModel *model) override
Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
void setLayerMarkWidth(int width)
Set width of contextual menu mark, at right of layer node items.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Returns index for a given legend node.
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...
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
void setCurrentLayer(QgsMapLayer *layer)
Sets the currently selected layer.
QModelIndex node2sourceIndex(QgsLayerTreeNode *node) const
Returns source model index for a given node.
Indicator that can be used in a layer tree view to display icons next to items of the layer tree....
void resizeEvent(QResizeEvent *event) override
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
Layer tree node points to a map layer.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QList< QgsLayerTreeViewIndicator * > indicators(QgsLayerTreeNode *node) const
Returns list of indicators associated with a particular layer tree node.
Layer tree group node serves as a container for layers and further groups.
A bar for displaying non-blocking messages to the user.
static QSet< QgsMapLayer * > collectMapLayersRecursive(const QList< QgsLayerTreeNode * > &nodes)
Returns map layers from the given list of layer tree nodes.
QgsLayerTreeViewMenuProvider * mMenuProvider
Context menu provider. Owned by the view.
QgsLayerTreeNode * currentNode() const
Gets current node. May be nullptr.
QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index) const
Returns legend node for given proxy model tree index.
QList< QgsMapLayer * > selectedLayersRecursive() const
Gets list of selected layers, including those that are not directly selected, but their ancestor grou...
static const double UI_SCALE_FACTOR
UI scaling factor.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
@ ParentRuleKeyRole
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given proxy model tree index.
QModelIndex node2index(QgsLayerTreeNode *node) const
Returns proxy model index for a given node.
static QgsLayerTreeEmbeddedWidgetRegistry * layerTreeEmbeddedWidgetRegistry()
Returns the global layer tree embedded widget registry, used for registering widgets that may be embe...
QList< QgsMapLayer * > selectedLayers() const
Gets list of selected layers.
QgsLayerTreeProxyModel(QgsLayerTreeModel *treeModel, QObject *parent)
Constructs QgsLayerTreeProxyModel with source model treeModel and a parent.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
The QgsLayerTreeViewDefaultActions class serves as a factory of actions that can be used together wit...
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
void setMessageBar(QgsMessageBar *messageBar)
Set the message bar to display messages from the layer tree.
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
void removeIndicator(QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
Removes a previously added indicator to a layer tree node.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void refreshLayerSymbology(const QString &layerId)
Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitore...
void modelRowsInserted(const QModelIndex &index, int start, int end)
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
void contextMenuEvent(QContextMenuEvent *event) override
QgsMapLayer * currentLayer() const
Returns the currently selected layer, or nullptr if no layers is selected.
Qt::ItemFlags flags(const QModelIndex &index) const override
MessageLevel
Level for messages This will be used both for message log and message bar in application.
void setShowPrivateLayers(bool showPrivate)
Set the show private layers to showPrivate.
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.
QgsLayerTreeProxyModel * proxyModel() const
Returns the proxy model used by the view.
void changed()
Emitted when the indicator changes state (e.g.
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.
Base class for all map layer types. This is the base class for all map layer types (vector,...
QList< QgsLayerTreeLayer * > selectedLayerNodes() const
Returns list of selected nodes filtered to just layer nodes.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
virtual QString name() const =0
Returns name of the node.
QgsLayerTreeViewDefaultActions * defaultActions()
Gets access to the default actions that may be used with the tree view.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
void setFilterText(const QString &filterText=QString())
Sets filter to filterText.
void setShowPrivateLayers(bool showPrivate)
Determines if private layers are shown.
void mouseReleaseEvent(QMouseEvent *event) override
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 setLayerVisible(QgsMapLayer *layer, bool visible)
Convenience methods which sets the visible state of the specified map layer.
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer)
@ RuleKeyRole
Rule key of the node (QString)
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
@ UseEmbeddedWidgets
Layer nodes may optionally include extra embedded widgets (if used in QgsLayerTreeView)....
void updateExpandedStateToNode(const QModelIndex &index)
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked.
void setCurrentIndex(const QModelIndex ¤tIndex)
Sets index of the current item. May be used by view. Item marked as current is underlined.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
friend class QgsLayerTreeViewItemDelegate
void collapseAllNodes()
Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes.
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...