QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 // clang-format off
63
64#ifdef SIP_RUN
66 if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
67 sipType = sipType_QgsLayerTreeModel;
68 else
69 sipType = 0;
71#endif
72
73 Q_OBJECT
74
75 public:
76 // clang-format on
77
82 explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
83
84 ~QgsLayerTreeModel() override;
85
86 // Implementation of virtual functions from QAbstractItemModel
87
88 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
89 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
90 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
91 QModelIndex parent( const QModelIndex &child ) const override;
92 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
93 Qt::ItemFlags flags( const QModelIndex &index ) const override;
94 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
95 Qt::DropActions supportedDropActions() const override;
96 QStringList mimeTypes() const override;
97 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
98 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
99 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
100
101 // New stuff
102
103 enum Flag SIP_ENUM_BASETYPE( IntFlag )
104 {
105 // display flags
106 ShowLegend = 0x0001,
111
112 // behavioral flags
119 };
120 Q_DECLARE_FLAGS( Flags, Flag )
121
122
125 void setFlag( Flag f, bool on = true );
127 Flags flags() const;
129 bool testFlag( Flag f ) const;
130
135 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
137 QModelIndex node2index( QgsLayerTreeNode *node ) const;
138
144 QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
145
149 static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
150
155 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
156
163 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
164
169 QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
170
175
183 QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
184
186 QgsLayerTree *rootGroup() const;
187
191 void setRootGroup( QgsLayerTree *newRootGroup );
192
197 void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
198
200 QModelIndex currentIndex() const;
202 void setCurrentIndex( const QModelIndex &currentIndex );
203
205 void setLayerTreeNodeFont( int nodeType, const QFont &font );
207 QFont layerTreeNodeFont( int nodeType ) const;
208
210 void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
213
220 void setLegendFilterByScale( double scale );
221
228 double legendFilterByScale() const { return mLegendFilterByScale; }
229
236 Q_DECL_DEPRECATED void setLegendFilterByMap( const QgsMapSettings *settings ) SIP_DEPRECATED;
237
246 Q_DECL_DEPRECATED void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true ) SIP_DEPRECATED;
247
251 const QgsMapSettings *legendFilterMapSettings() const;
252
262 void setFilterSettings( const QgsLayerTreeFilterSettings *settings = nullptr );
263
271 const QgsLayerTreeFilterSettings *filterSettings() const;
272
277 void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
278
283 void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
284
288 QMap<QString, QString> layerStyleOverrides() const;
289
293 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
294
303 void addTargetScreenProperties( const QgsScreenProperties &properties );
304
314 QSet< QgsScreenProperties > targetScreenProperties() const;
315
325 static int scaleIconSize( int standardSize );
326
333 void waitForHitTestBlocking();
334
343 bool hitTestInProgress() const;
344
345 signals:
346
351 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
352
362
372
373 protected slots:
374 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
375 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
376 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
377 void nodeRemovedChildren();
378
380
384 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
385
386 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
387
388 void nodeLayerLoaded();
390 void layerLegendChanged();
391
396 void layerFlagsChanged();
397
398 void layerNeedsUpdate();
399
401
403
404 private slots:
405
410 void layerProfileGenerationPropertyChanged();
411
412 protected:
413 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
414 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
415
416 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
417 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
418
419 void connectToLayers( QgsLayerTreeGroup *parentGroup );
420 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
421 void connectToRootNode();
423
425 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
426
431 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
432
433 static QIcon iconGroup();
434
437
438 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
439
440 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
442 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
443 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
444 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
445 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
446 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
447 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
448 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
449 void legendCleanup();
451
452 protected:
459
465 QPersistentModelIndex mCurrentIndex;
468
479#ifndef SIP_RUN
481 {
483 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
485 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
486 };
487#endif
488
493#ifndef SIP_RUN
495 {
496 LayerLegendData() = default;
497
502 QList<QgsLayerTreeModelLegendNode *> activeNodes;
503
510
515 QList<QgsLayerTreeModelLegendNode *> originalNodes;
518 };
519#endif
520
522 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
523
528 QMap<QString, QString> mLayerStyleOverrides;
529
531 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
532
537 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
538
541
544
545 QPointer< QgsMapHitTestTask > mHitTestTask;
546
547 QMap<QString, QSet<QString>> mHitTestResults;
548
549 QMap<QString, QPair<double, double>> mHitTestResultsRendererUpdatedCanvas;
550
551 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
552
557
558 QSet< QgsScreenProperties > mTargetScreenProperties;
559
560 private slots:
561 void legendNodeSizeChanged();
562 void hitTestTaskCompleted();
563
564 private:
565 void handleHitTestResults();
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:62
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:198
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_END
Definition qgis_sip.h:215
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.