QGIS API Documentation 4.1.0-Master (0cdd3ae6384)
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
313 void setTargetScreenProperties( const QSet< QgsScreenProperties > &properties );
314
324 QSet< QgsScreenProperties > targetScreenProperties() const;
325
335 static int scaleIconSize( int standardSize );
336
343 void waitForHitTestBlocking();
344
353 bool hitTestInProgress() const;
354
360 void invalidateDisplayData();
361
362 signals:
363
368 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
369
379
389
390 protected slots:
391 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
392 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
393 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
394 void nodeRemovedChildren();
395
397
401 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
402
403 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
404
405 void nodeLayerLoaded();
407 void layerLegendChanged();
408
413 void layerFlagsChanged();
414
415 void layerNeedsUpdate();
416
418
420
421 private slots:
422
427 void layerProfileGenerationPropertyChanged();
428
429 protected:
430 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
431 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
432
433 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
434 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
435
436 void connectToLayers( QgsLayerTreeGroup *parentGroup );
437 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
438 void connectToRootNode();
440
442 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
443
448 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
449
450 static QIcon iconGroup();
451
454
455 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
456
457 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
459 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
460 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
461 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
462 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
463 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
464 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
465 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
466 void legendCleanup();
468
469 protected:
476
482 QPersistentModelIndex mCurrentIndex;
485
496#ifndef SIP_RUN
498 {
500 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
502 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
503 };
504#endif
505
510#ifndef SIP_RUN
512 {
513 LayerLegendData() = default;
514
519 QList<QgsLayerTreeModelLegendNode *> activeNodes;
520
527
532 QList<QgsLayerTreeModelLegendNode *> originalNodes;
535 };
536#endif
537
539 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
540
545 QMap<QString, QString> mLayerStyleOverrides;
546
548 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
549
554 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
555
558
561
562 QPointer< QgsMapHitTestTask > mHitTestTask;
563
564 QMap<QString, QSet<QString>> mHitTestResults;
565
566 QMap<QString, QPair<double, double>> mHitTestResultsRendererUpdatedCanvas;
567
568 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
569
574
575 QSet< QgsScreenProperties > mTargetScreenProperties;
576
577 private slots:
578 void legendNodeSizeChanged();
579 void hitTestTaskCompleted();
580
581 private:
582 void handleHitTestResults();
583};
584
586
587
588#ifndef SIP_RUN
589
595class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
596{
597 Q_OBJECT
598
599 public:
600 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
602 {
603 // we need a valid rule key to allow the model to build a tree out of legend nodes
604 // if that's possible (if there is a node without a rule key, building of tree is canceled)
605 mRuleKey = u"embedded-widget-"_s + QUuid::createUuid().toString();
606 }
607
608 QVariant data( int role ) const override
609 {
610 if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
611 return mRuleKey;
612 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
614 return QVariant();
615 }
616
617 private:
618 QString mRuleKey;
619};
620#endif
621
623
624#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:272
#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.