QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
30 #include <QContextMenuEvent>
31 #include <QHeaderView>
42 setHeaderHidden(
true );
44 setDragEnabled(
true );
45 setAcceptDrops(
true );
46 setDropIndicatorShown(
true );
47 setEditTriggers( EditKeyPressed );
48 setExpandsOnDoubleClick(
false );
51 header()->setStretchLastSection(
false );
52 header()->setSectionResizeMode( QHeaderView::ResizeToContents );
55 setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
57 setSelectionMode( ExtendedSelection );
58 setDefaultDropAction( Qt::MoveAction );
62 setStyle(
new QgsLayerTreeViewProxyStyle(
this ) );
69 connect( horizontalScrollBar(), &QScrollBar::valueChanged,
this, &QgsLayerTreeView::onHorizontalScroll );
79 if ( !qobject_cast<QgsLayerTreeModel *>( model ) )
88 {mMessageBar->
pushMessage( message, level, duration );}
91 QTreeView::setModel( model );
105 return qobject_cast<QgsLayerTreeModel *>( model() );
130 setCurrentIndex( QModelIndex() );
156 QModelIndex idx = indexAt( event->pos() );
157 if ( !idx.isValid() )
158 setCurrentIndex( QModelIndex() );
161 if ( menu && menu->actions().count() != 0 )
162 menu->exec( mapToGlobal( event->pos() ) );
179 int widgetsCount = layer->customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
181 for (
int i = 0; i < widgetsCount; ++i )
183 QString providerId = layer->customProperty( QStringLiteral(
"embeddedWidgets/%1/id" ).arg( i ) ).toString();
187 QWidget *wdgt = provider->createWidget( layer, i );
204 setIndexWidget( index, wdgt );
214 QStringList expandedNodeKeys = parentNode->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
215 if ( expandedNodeKeys.isEmpty() )
222 if ( expandedNodeKeys.contains( ruleKey ) )
228 QList<QgsLayerTreeNode *> children = parentNode->
children();
229 for (
int i = start; i <= end; ++i )
248 node->setExpanded( isExpanded( index ) );
253 QStringList lst = node->layerNode()->customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList();
254 bool expanded = isExpanded( index );
255 bool isInList = lst.contains( ruleKey );
256 if ( expanded && !isInList )
258 lst.append( ruleKey );
259 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
261 else if ( !expanded && isInList )
263 lst.removeAll( ruleKey );
264 node->layerNode()->setCustomProperty( QStringLiteral(
"expandedLegendNodes" ), lst );
272 QString layerCurrentID = layerCurrent ? layerCurrent->
id() : QString();
277 QModelIndex nodeLayerIndex;
293 if ( isExpanded( idx ) != expanded )
294 setExpanded( idx, expanded );
297 void QgsLayerTreeView::onCustomPropertyChanged(
QgsLayerTreeNode *node,
const QString &key )
302 QSet<QString> expandedLegendNodes = qgis::listToSet( node->
customProperty( QStringLiteral(
"expandedLegendNodes" ) ).toStringList() );
308 if ( !key.isEmpty() )
323 const auto constChildren = node->
children();
389 QList<QgsLayerTreeLayer *> layerNodes;
401 QList<QgsMapLayer *> list;
406 list << node->layer();
415 return qgis::setToList( layersSet );
443 QStringList QgsLayerTreeView::viewOnlyCustomProperties()
445 return QStringList() << QStringLiteral(
"expandedLegendNodes" );
464 const auto constLayerLegendNodes = model->
layerLegendNodes( nodeLayer,
true );
468 if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
478 const auto constChildren = parent->
children();
506 if ( mMessageBar == messageBar )
509 mMessageBar = messageBar;
514 {mMessageBar->
pushMessage( message, level, duration );}
525 if ( event->modifiers() & Qt::ControlModifier )
529 QTreeView::mouseReleaseEvent( event );
535 if ( event->key() == Qt::Key_Space )
539 if ( ! constSelectedNodes.isEmpty() )
541 bool isFirstNodeChecked = constSelectedNodes[0]->itemVisibilityChecked();
553 if ( event->modifiers() & Qt::ControlModifier )
557 QTreeView::keyPressEvent( event );
563 if ( event->keyboardModifiers() & Qt::AltModifier )
567 QTreeView::dropEvent( event );
580 header()->setMinimumSectionSize( viewport()->width() );
581 QTreeView::resizeEvent( event );
584 void QgsLayerTreeView::onHorizontalScroll(
int value )
587 viewport()->update();
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.
void keyPressEvent(QKeyEvent *event) override
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
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.
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.
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.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
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.
~QgsLayerTreeView() override
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.
void setCurrentLayer(QgsMapLayer *layer)
Sets the currently selected layer.
void resizeEvent(QResizeEvent *event) override
void expandAllNodes()
Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
QList< QgsLayerTreeViewIndicator * > indicators(QgsLayerTreeNode *node) const
Returns list of indicators associated with a particular layer tree node.
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.
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....
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.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
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 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.
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.
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.
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 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::Info, int duration=5)
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.