QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "qgis_core.h"
20#include <QAbstractItemModel>
21#include <QFont>
22#include <QIcon>
23#include <QTimer>
24#include <QUuid>
25#include <memory>
26
27#include "qgsgeometry.h"
29
33class QgsMapHitTest;
34class QgsMapSettings;
35class QgsExpression;
37class QgsLayerTree;
38
55class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
56{
57
58#ifdef SIP_RUN
60 if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
61 sipType = sipType_QgsLayerTreeModel;
62 else
63 sipType = 0;
65#endif
66
67 Q_OBJECT
68 public:
69
74 explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
75
76 ~QgsLayerTreeModel() override;
77
78 // Implementation of virtual functions from QAbstractItemModel
79
80 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
81 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
82 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
83 QModelIndex parent( const QModelIndex &child ) const override;
84 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
85 Qt::ItemFlags flags( const QModelIndex &index ) const override;
86 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
87 Qt::DropActions supportedDropActions() const override;
88 QStringList mimeTypes() const override;
89 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
90 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
91 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
92
93 // New stuff
94
95 enum Flag
96 {
97 // display flags
98 ShowLegend = 0x0001,
99 ShowLegendAsTree = 0x0004,
100 DeferredLegendInvalidation = 0x0008,
101 UseEmbeddedWidgets = 0x0010,
102 UseTextFormatting = 0x0020,
103
104 // behavioral flags
105 AllowNodeReorder = 0x1000,
106 AllowNodeRename = 0x2000,
107 AllowNodeChangeVisibility = 0x4000,
108 AllowLegendChangeState = 0x8000,
109 ActionHierarchical = 0x10000,
110 };
111 Q_DECLARE_FLAGS( Flags, Flag )
112
113
114 void setFlags( QgsLayerTreeModel::Flags f );
116 void setFlag( Flag f, bool on = true );
118 Flags flags() const;
120 bool testFlag( Flag f ) const;
121
126 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
128 QModelIndex node2index( QgsLayerTreeNode *node ) const;
129
135 QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
136
141 static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
142
148 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
149
157 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
158
164 QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
165
170 QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
171
180 QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
181
183 QgsLayerTree *rootGroup() const;
184
189 void setRootGroup( QgsLayerTree *newRootGroup );
190
195 void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
196
198 QModelIndex currentIndex() const;
200 void setCurrentIndex( const QModelIndex &currentIndex );
201
203 void setLayerTreeNodeFont( int nodeType, const QFont &font );
205 QFont layerTreeNodeFont( int nodeType ) const;
206
208 void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
210 int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
211
219 void setLegendFilterByScale( double scale );
220
228 double legendFilterByScale() const { return mLegendFilterByScale; }
229
236 void setLegendFilterByMap( const QgsMapSettings *settings );
237
246 void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
247
252 const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
253
259 void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
260
266 void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
267
272 QMap<QString, QString> layerStyleOverrides() const;
273
278 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
279
289 static int scaleIconSize( int standardSize );
290
291 signals:
292
297 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
298
299 protected slots:
300 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
301 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
302 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
303 void nodeRemovedChildren();
304
305 void nodeVisibilityChanged( QgsLayerTreeNode *node );
306
311 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
312
313 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
314
315 void nodeLayerLoaded();
316 void nodeLayerWillBeUnloaded();
317 void layerLegendChanged();
318
323 void layerFlagsChanged();
324
325 void layerNeedsUpdate();
326
327 void legendNodeDataChanged();
328
329 void invalidateLegendMapBasedData();
330
331 protected:
332 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
333 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
334
335 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
336 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
337
338 void connectToLayers( QgsLayerTreeGroup *parentGroup );
339 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
340 void connectToRootNode();
341 void disconnectFromRootNode();
342
344 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
345
351 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
352
353 static QIcon iconGroup();
354
356 QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
357
358 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
359
360 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
361 int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
362 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
363 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
364 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
365 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
366 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
367 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
368 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
369 void legendCleanup();
370 void legendInvalidateMapBasedData();
371
372 protected:
373
379 QgsRenderContext *createTemporaryRenderContext() const SIP_SKIP;
380
382 QgsLayerTree *mRootNode = nullptr;
384 Flags mFlags;
386 QPersistentModelIndex mCurrentIndex;
388 int mAutoCollapseLegendNodesCount;
389
400#ifndef SIP_RUN
402 {
404 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
406 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
407 };
408#endif
409
414#ifndef SIP_RUN
416 {
417 LayerLegendData() = default;
418
423 QList<QgsLayerTreeModelLegendNode *> activeNodes;
424
430 QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
431
436 QList<QgsLayerTreeModelLegendNode *> originalNodes;
438 LayerLegendTree *tree = nullptr;
439 };
440#endif
441
443 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
444
449 QMap<QString, QString> mLayerStyleOverrides;
450
452 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
453
458 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
459
462
465
466 std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
467 std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
468
471
476
477 private slots:
478 void legendNodeSizeChanged();
479
480 private:
481
482
483};
484
485Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
486
487
488#ifndef SIP_RUN
489
495class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
496{
497 Q_OBJECT
498
499 public:
500 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
502 {
503 // we need a valid rule key to allow the model to build a tree out of legend nodes
504 // if that's possible (if there is a node without a rule key, building of tree is canceled)
505 mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
506 }
507
508 QVariant data( int role ) const override
509 {
510 if ( role == RuleKeyRole )
511 return mRuleKey;
514 return QVariant();
515 }
516
517 private:
518 QString mRuleKey;
519};
520#endif
521
523
524#endif // QGSLAYERTREEMODEL_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:100
Class for parsing and evaluation of expressions (formerly called "search strings").
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
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.
@ NodeTypeRole
Type of node. Added in 3.16.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QTimer mDeferLegendInvalidationTimer
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
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.
std::unique_ptr< QgsMapSettings > mLegendFilterMapSettings
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
bool mLegendFilterUsesExtent
whether to use map filtering
void setAutoCollapseLegendNodes(int nodeCount)
Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-...
QHash< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
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.
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled)
QSet< QgsLayerTreeLayer * > mInvalidatedNodes
Keep track of layer nodes for which the legend size needs to be recalculated.
This class is a base class for nodes in a layer tree.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:33
Class that runs a hit test with given map settings.
Definition: qgsmaphittest.h:38
The QgsMapSettings class contains configuration for rendering of the map.
Contains information about the context of a rendering operation.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_END
Definition: qgis_sip.h:203
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Structure that stores all data associated with one map layer.
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
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.