QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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 <QTimer>
29#include <QUuid>
30
34class QgsMapHitTest;
36class QgsMapSettings;
37class QgsExpression;
39class QgsLayerTree;
41
57class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
58{
59
60#ifdef SIP_RUN
62 if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
63 sipType = sipType_QgsLayerTreeModel;
64 else
65 sipType = 0;
67#endif
68
69 Q_OBJECT
70 public:
71
76 explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
77
78 ~QgsLayerTreeModel() override;
79
80 // Implementation of virtual functions from QAbstractItemModel
81
82 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
83 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
84 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
85 QModelIndex parent( const QModelIndex &child ) const override;
86 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
87 Qt::ItemFlags flags( const QModelIndex &index ) const override;
88 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
89 Qt::DropActions supportedDropActions() const override;
90 QStringList mimeTypes() const override;
91 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
92 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
93 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
94
95 // New stuff
96
97 enum Flag SIP_ENUM_BASETYPE( IntFlag )
98 {
99 // display flags
100 ShowLegend = 0x0001,
105
106 // behavioral flags
113 };
114 Q_DECLARE_FLAGS( Flags, Flag )
115
116
119 void setFlag( Flag f, bool on = true );
121 Flags flags() const;
123 bool testFlag( Flag f ) const;
124
129 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
131 QModelIndex node2index( QgsLayerTreeNode *node ) const;
132
138 QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
139
143 static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
144
149 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
150
157 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
158
163 QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
164
169
177 QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
178
180 QgsLayerTree *rootGroup() const;
181
185 void setRootGroup( QgsLayerTree *newRootGroup );
186
191 void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
192
194 QModelIndex currentIndex() const;
196 void setCurrentIndex( const QModelIndex &currentIndex );
197
199 void setLayerTreeNodeFont( int nodeType, const QFont &font );
201 QFont layerTreeNodeFont( int nodeType ) const;
202
204 void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
207
214 void setLegendFilterByScale( double scale );
215
222 double legendFilterByScale() const { return mLegendFilterByScale; }
223
230 Q_DECL_DEPRECATED void setLegendFilterByMap( const QgsMapSettings *settings ) SIP_DEPRECATED;
231
240 Q_DECL_DEPRECATED void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true ) SIP_DEPRECATED;
241
245 const QgsMapSettings *legendFilterMapSettings() const;
246
256 void setFilterSettings( const QgsLayerTreeFilterSettings *settings = nullptr );
257
265 const QgsLayerTreeFilterSettings *filterSettings() const;
266
271 void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
272
277 void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
278
282 QMap<QString, QString> layerStyleOverrides() const;
283
287 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
288
297 void addTargetScreenProperties( const QgsScreenProperties &properties );
298
308 QSet< QgsScreenProperties > targetScreenProperties() const;
309
319 static int scaleIconSize( int standardSize );
320
327 void waitForHitTestBlocking();
328
337 bool hitTestInProgress() const;
338
339 signals:
340
345 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
346
356
366
367 protected slots:
368 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
369 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
370 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
371 void nodeRemovedChildren();
372
374
378 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
379
380 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
381
382 void nodeLayerLoaded();
384 void layerLegendChanged();
385
390 void layerFlagsChanged();
391
392 void layerNeedsUpdate();
393
395
397
398 private slots:
399
404 void layerProfileGenerationPropertyChanged();
405
406 protected:
407 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
408 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
409
410 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
411 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
412
413 void connectToLayers( QgsLayerTreeGroup *parentGroup );
414 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
415 void connectToRootNode();
417
419 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
420
425 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
426
427 static QIcon iconGroup();
428
431
432 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
433
434 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
436 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
437 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
438 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
439 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
440 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
441 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
442 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
443 void legendCleanup();
445
446 protected:
447
454
460 QPersistentModelIndex mCurrentIndex;
463
474#ifndef SIP_RUN
476 {
478 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
480 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
481 };
482#endif
483
488#ifndef SIP_RUN
490 {
491 LayerLegendData() = default;
492
497 QList<QgsLayerTreeModelLegendNode *> activeNodes;
498
505
510 QList<QgsLayerTreeModelLegendNode *> originalNodes;
513 };
514#endif
515
517 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
518
523 QMap<QString, QString> mLayerStyleOverrides;
524
526 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
527
532 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
533
536
539
540 QPointer< QgsMapHitTestTask > mHitTestTask;
541
542 QMap<QString, QSet<QString>> mHitTestResults;
543
544 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
545
550
551 QSet< QgsScreenProperties > mTargetScreenProperties;
552
553 private slots:
554 void legendNodeSizeChanged();
555 void hitTestTaskCompleted();
556
557 private:
558 void handleHitTestResults();
559
560
561};
562
564
565
566#ifndef SIP_RUN
567
573class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
574{
575 Q_OBJECT
576
577 public:
578 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
580 {
581 // we need a valid rule key to allow the model to build a tree out of legend nodes
582 // if that's possible (if there is a node without a rule key, building of tree is canceled)
583 mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
584 }
585
586 QVariant data( int role ) const override
587 {
588 if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
589 return mRuleKey;
590 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
592 return QVariant();
593 }
594
595 private:
596 QString mRuleKey;
597};
598#endif
599
601
602#endif // QGSLAYERTREEMODEL_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
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
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.