QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgslayertreemodel.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreemodel.h
3 --------------------------------------
4 Date : May 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSLAYERTREEMODEL_H
17#define QGSLAYERTREEMODEL_H
18
19#include <memory>
20
21#include "qgis_core.h"
22#include "qgsgeometry.h"
24
25#include <QAbstractItemModel>
26#include <QFont>
27#include <QIcon>
28#include <QString>
29#include <QTimer>
30#include <QUuid>
31
32using namespace Qt::StringLiterals;
33
37class QgsMapHitTest;
39class QgsMapSettings;
40class QgsExpression;
42class QgsLayerTree;
44
60class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
61{
62
63#ifdef SIP_RUN
65 if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
66 sipType = sipType_QgsLayerTreeModel;
67 else
68 sipType = 0;
70#endif
71
72 Q_OBJECT
73 public:
74
79 explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
80
81 ~QgsLayerTreeModel() override;
82
83 // Implementation of virtual functions from QAbstractItemModel
84
85 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
86 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
87 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
88 QModelIndex parent( const QModelIndex &child ) const override;
89 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
90 Qt::ItemFlags flags( const QModelIndex &index ) const override;
91 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
92 Qt::DropActions supportedDropActions() const override;
93 QStringList mimeTypes() const override;
94 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
95 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
96 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
97
98 // New stuff
99
100 enum Flag SIP_ENUM_BASETYPE( IntFlag )
101 {
102 // display flags
103 ShowLegend = 0x0001,
108
109 // behavioral flags
116 };
117 Q_DECLARE_FLAGS( Flags, Flag )
118
119
122 void setFlag( Flag f, bool on = true );
124 Flags flags() const;
126 bool testFlag( Flag f ) const;
127
132 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
134 QModelIndex node2index( QgsLayerTreeNode *node ) const;
135
141 QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
142
146 static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
147
152 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
153
160 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
161
166 QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
167
172
180 QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
181
183 QgsLayerTree *rootGroup() const;
184
188 void setRootGroup( QgsLayerTree *newRootGroup );
189
194 void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
195
197 QModelIndex currentIndex() const;
199 void setCurrentIndex( const QModelIndex &currentIndex );
200
202 void setLayerTreeNodeFont( int nodeType, const QFont &font );
204 QFont layerTreeNodeFont( int nodeType ) const;
205
207 void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
210
217 void setLegendFilterByScale( double scale );
218
225 double legendFilterByScale() const { return mLegendFilterByScale; }
226
233 Q_DECL_DEPRECATED void setLegendFilterByMap( const QgsMapSettings *settings ) SIP_DEPRECATED;
234
243 Q_DECL_DEPRECATED void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true ) SIP_DEPRECATED;
244
248 const QgsMapSettings *legendFilterMapSettings() const;
249
259 void setFilterSettings( const QgsLayerTreeFilterSettings *settings = nullptr );
260
268 const QgsLayerTreeFilterSettings *filterSettings() const;
269
274 void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
275
280 void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
281
285 QMap<QString, QString> layerStyleOverrides() const;
286
290 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
291
300 void addTargetScreenProperties( const QgsScreenProperties &properties );
301
311 QSet< QgsScreenProperties > targetScreenProperties() const;
312
322 static int scaleIconSize( int standardSize );
323
330 void waitForHitTestBlocking();
331
340 bool hitTestInProgress() const;
341
342 signals:
343
348 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
349
359
369
370 protected slots:
371 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
372 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
373 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
374 void nodeRemovedChildren();
375
377
381 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
382
383 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
384
385 void nodeLayerLoaded();
387 void layerLegendChanged();
388
393 void layerFlagsChanged();
394
395 void layerNeedsUpdate();
396
398
400
401 private slots:
402
407 void layerProfileGenerationPropertyChanged();
408
409 protected:
410 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
411 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
412
413 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
414 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
415
416 void connectToLayers( QgsLayerTreeGroup *parentGroup );
417 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
418 void connectToRootNode();
420
422 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
423
428 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
429
430 static QIcon iconGroup();
431
434
435 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
436
437 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
439 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
440 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
441 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
442 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
443 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
444 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
445 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
446 void legendCleanup();
448
449 protected:
450
457
463 QPersistentModelIndex mCurrentIndex;
466
477#ifndef SIP_RUN
479 {
481 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
483 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
484 };
485#endif
486
491#ifndef SIP_RUN
493 {
494 LayerLegendData() = default;
495
500 QList<QgsLayerTreeModelLegendNode *> activeNodes;
501
508
513 QList<QgsLayerTreeModelLegendNode *> originalNodes;
516 };
517#endif
518
520 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
521
526 QMap<QString, QString> mLayerStyleOverrides;
527
529 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
530
535 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
536
539
542
543 QPointer< QgsMapHitTestTask > mHitTestTask;
544
545 QMap<QString, QSet<QString>> mHitTestResults;
546
547 QMap<QString, QPair<double, double>> mHitTestResultsRendererUpdatedCanvas;
548
549 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
550
555
556 QSet< QgsScreenProperties > mTargetScreenProperties;
557
558 private slots:
559 void legendNodeSizeChanged();
560 void hitTestTaskCompleted();
561
562 private:
563 void handleHitTestResults();
564
565
566};
567
569
570
571#ifndef SIP_RUN
572
578class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
579{
580 Q_OBJECT
581
582 public:
583 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
585 {
586 // we need a valid rule key to allow the model to build a tree out of legend nodes
587 // if that's possible (if there is a node without a rule key, building of tree is canceled)
588 mRuleKey = u"embedded-widget-"_s + QUuid::createUuid().toString();
589 }
590
591 QVariant data( int role ) const override
592 {
593 if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
594 return mRuleKey;
595 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
597 return QVariant();
598 }
599
600 private:
601 QString mRuleKey;
602};
603#endif
604
606
607#endif // QGSLAYERTREEMODEL_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
Handles parsing and evaluation of expressions (formerly called "search strings").
A geometry is the spatial representation of a feature.
Contains settings relating to filtering the contents of QgsLayerTreeModel and views.
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
An abstract interface for legend items returned from QgsMapLayerLegend implementation.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
@ EmbeddedWidget
Embedded widget placeholder node type.
void connectToLayer(QgsLayerTreeLayer *nodeLayer)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
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...
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
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...
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int autoCollapseLegendNodes() const
Returns at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse ...
double legendFilterByScale() const
Returns the scale which restricts the legend nodes which are visible.
QVariant legendNodeData(QgsLayerTreeModelLegendNode *node, int role) const
void hitTestStarted()
Emitted when a hit test for visible legend items starts.
void setRootGroup(QgsLayerTree *newRootGroup)
Reset the model and use a new root group node.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Returns index for a given legend node.
void hitTestCompleted()
Emitted when a hit test for visible legend items completes.
std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void nodeCustomPropertyChanged(QgsLayerTreeNode *node, const QString &key)
QgsLayerTreeModel(QgsLayerTree *rootNode, QObject *parent=nullptr)
Construct a new tree model with given layer tree (root node must not be nullptr).
void connectToLayers(QgsLayerTreeGroup *parentGroup)
QMap< QString, QSet< QString > > mHitTestResults
QModelIndex parent(const QModelIndex &child) const override
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
QModelIndex legendParent(QgsLayerTreeModelLegendNode *legendNode) const
void setLayerTreeNodeFont(int nodeType, const QFont &font)
Sets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeT...
int legendNodeRowCount(QgsLayerTreeModelLegendNode *node) const
Qt::ItemFlags flags(const QModelIndex &index) const override
void nodeAddedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
void setAutoCollapseLegendNodes(int nodeCount)
Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-...
QModelIndex currentIndex() const
Gets index of the item marked as current. Item marked as current is underlined.
void recursivelyEmitDataChanged(const QModelIndex &index=QModelIndex())
emit dataChanged() for layer tree node items
bool legendEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
QHash< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
void disconnectFromLayer(QgsLayerTreeLayer *nodeLayer)
void setCurrentIndex(const QModelIndex &currentIndex)
Sets index of the current item. May be used by view. Item marked as current is underlined.
QIcon legendIconEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
void layerFlagsChanged()
Triggered when layer flags have changed.
void nodeNameChanged(QgsLayerTreeNode *node, const QString &name)
Updates model when node's name has changed.
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
LayerLegendTree * tryBuildLegendTree(const QList< QgsLayerTreeModelLegendNode * > &nodes)
Qt::DropActions supportedDropActions() const override
QModelIndex legendNodeIndex(int row, int column, QgsLayerTreeModelLegendNode *node) const
QgsRenderContext * createTemporaryRenderContext() const
Returns a temporary render context.
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
void nodeWillRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
QgsLayerTree * mRootNode
Pointer to the root node of the layer tree. Not owned by the model.
QStringList mimeTypes() const override
QMimeData * mimeData(const QModelIndexList &indexes) const override
QSet< QgsScreenProperties > mTargetScreenProperties
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QPointer< QgsMapHitTestTask > mHitTestTask
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
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.
QgsLayerTreeModelLegendNode * legendNodeEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
Returns legend node that may be embedded in parent (i.e.
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QMap< QString, QPair< double, double > > mHitTestResultsRendererUpdatedCanvas
QList< QgsLayerTreeModelLegendNode * > filterLegendNodes(const QList< QgsLayerTreeModelLegendNode * > &nodes)
Filter nodes from QgsMapLayerLegend according to the current filtering rules.
QList< QgsLayerTreeModelLegendNode * > layerOriginalLegendNodes(QgsLayerTreeLayer *nodeLayer)
Returns original (unfiltered) list of legend nodes attached to a particular layer node.
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
Qt::ItemFlags legendNodeFlags(QgsLayerTreeModelLegendNode *node) const
void nodeVisibilityChanged(QgsLayerTreeNode *node)
void refreshScaleBasedLayers(const QModelIndex &index=QModelIndex(), double previousScale=0.0)
Updates layer data for scale dependent layers, should be called when map scale changes.
void removeLegendFromLayer(QgsLayerTreeLayer *nodeLayer)
QModelIndex legendRootIndex(int row, int column, QgsLayerTreeLayer *nL) const
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
void nodeWillAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
QFont layerTreeNodeFont(int nodeType) const
Gets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeT...
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
void addLegendToLayer(QgsLayerTreeLayer *nodeL)
bool testFlag(Flag f) const
Check whether a flag is enabled.
void disconnectFromLayers(QgsLayerTreeGroup *parentGroup)
QModelIndex indexOfParentLayerTreeNode(QgsLayerTreeNode *parentNode) const
QSet< QgsLayerTreeLayer * > mInvalidatedNodes
Keep track of layer nodes for which the legend size needs to be recalculated.
@ ActionHierarchical
Check/uncheck action has consequences on children (or parents for leaf node).
@ AllowNodeChangeVisibility
Allow user to set node visibility with a checkbox.
@ ShowLegendAsTree
For legends that support it, will show them in a tree instead of a list (needs also ShowLegend)....
@ UseTextFormatting
Layer nodes will alter text appearance based on layer properties, such as scale based visibility.
@ AllowNodeReorder
Allow reordering with drag'n'drop.
@ ShowLegend
Add legend nodes for layer nodes.
@ DeferredLegendInvalidation
Defer legend model invalidation.
@ AllowNodeRename
Allow renaming of groups and layers.
@ AllowLegendChangeState
Allow check boxes for legend nodes (if supported by layer's legend).
@ UseEmbeddedWidgets
Layer nodes may optionally include extra embedded widgets (if used in QgsLayerTreeView)....
@ UseThreadedHitTest
Run legend hit tests in a background thread.
Flags mFlags
Sets of flags for the model.
int legendRootRowCount(QgsLayerTreeLayer *nL) const
QgsLayerTreeModelLegendNode * findLegendNode(const QString &layerId, const QString &ruleKey) const
Searches through the layer tree to find a legend node with a matching layer ID and rule key.
Base class for nodes in a layer tree.
Namespace with helper functions for layer tree operations.
Executes a QgsMapHitTest in a background thread.
Runs a hit test with given map settings.
Contains configuration for rendering maps.
Contains information about the context of a rendering operation.
Stores properties relating to a screen.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_END
Definition qgis_sip.h:216
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)
LayerLegendTree * tree
Optional pointer to a tree structure - see LayerLegendTree for details.
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
QgsLayerTreeModelLegendNode * embeddedNodeInParent
A legend node that is not displayed separately, its icon is instead shown within the layer node's ite...
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
Structure that stores tree representation of map layer's legend.
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have nullptr parent. Pointers are not owned.
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under nullptr key. Pointers are not owned.