QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 "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;
35class QgsMapSettings;
36class QgsExpression;
38class QgsLayerTree;
40
56class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
57{
58
59#ifdef SIP_RUN
61 if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
62 sipType = sipType_QgsLayerTreeModel;
63 else
64 sipType = 0;
66#endif
67
68 Q_OBJECT
69 public:
70
75 explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
76
77 ~QgsLayerTreeModel() override;
78
79 // Implementation of virtual functions from QAbstractItemModel
80
81 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
82 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
83 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
84 QModelIndex parent( const QModelIndex &child ) const override;
85 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
86 Qt::ItemFlags flags( const QModelIndex &index ) const override;
87 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
88 Qt::DropActions supportedDropActions() const override;
89 QStringList mimeTypes() const override;
90 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
91 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
92 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
93
94 // New stuff
95
96 enum Flag SIP_ENUM_BASETYPE( IntFlag )
97 {
98 // display flags
99 ShowLegend = 0x0001,
100 ShowLegendAsTree = 0x0004,
101 DeferredLegendInvalidation = 0x0008,
102 UseEmbeddedWidgets = 0x0010,
103 UseTextFormatting = 0x0020,
104
105 // behavioral flags
106 AllowNodeReorder = 0x1000,
107 AllowNodeRename = 0x2000,
108 AllowNodeChangeVisibility = 0x4000,
109 AllowLegendChangeState = 0x8000,
110 ActionHierarchical = 0x10000,
111 UseThreadedHitTest = 0x20000,
112 };
113 Q_DECLARE_FLAGS( Flags, Flag )
114
115
116 void setFlags( QgsLayerTreeModel::Flags f );
118 void setFlag( Flag f, bool on = true );
120 Flags flags() const;
122 bool testFlag( Flag f ) const;
123
128 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
130 QModelIndex node2index( QgsLayerTreeNode *node ) const;
131
137 QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
138
142 static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
143
148 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
149
156 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
157
162 QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
163
167 QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
168
176 QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
177
179 QgsLayerTree *rootGroup() const;
180
184 void setRootGroup( QgsLayerTree *newRootGroup );
185
190 void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
191
193 QModelIndex currentIndex() const;
195 void setCurrentIndex( const QModelIndex &currentIndex );
196
198 void setLayerTreeNodeFont( int nodeType, const QFont &font );
200 QFont layerTreeNodeFont( int nodeType ) const;
201
203 void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
205 int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
206
213 void setLegendFilterByScale( double scale );
214
221 double legendFilterByScale() const { return mLegendFilterByScale; }
222
229 Q_DECL_DEPRECATED void setLegendFilterByMap( const QgsMapSettings *settings ) SIP_DEPRECATED;
230
239 Q_DECL_DEPRECATED void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true ) SIP_DEPRECATED;
240
244 const QgsMapSettings *legendFilterMapSettings() const;
245
255 void setFilterSettings( const QgsLayerTreeFilterSettings *settings = nullptr );
256
264 const QgsLayerTreeFilterSettings *filterSettings() const;
265
270 void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
271
276 void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
277
281 QMap<QString, QString> layerStyleOverrides() const;
282
286 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
287
296 void addTargetScreenProperties( const QgsScreenProperties &properties );
297
307 QSet< QgsScreenProperties > targetScreenProperties() const;
308
318 static int scaleIconSize( int standardSize );
319
326 void waitForHitTestBlocking();
327
336 bool hitTestInProgress() const;
337
338 signals:
339
344 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
345
354 void hitTestStarted();
355
364 void hitTestCompleted();
365
366 protected slots:
367 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
368 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
369 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
370 void nodeRemovedChildren();
371
372 void nodeVisibilityChanged( QgsLayerTreeNode *node );
373
377 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
378
379 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
380
381 void nodeLayerLoaded();
382 void nodeLayerWillBeUnloaded();
383 void layerLegendChanged();
384
389 void layerFlagsChanged();
390
391 void layerNeedsUpdate();
392
393 void legendNodeDataChanged();
394
395 void invalidateLegendMapBasedData();
396
397 protected:
398 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
399 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
400
401 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
402 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
403
404 void connectToLayers( QgsLayerTreeGroup *parentGroup );
405 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
406 void connectToRootNode();
407 void disconnectFromRootNode();
408
410 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
411
416 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
417
418 static QIcon iconGroup();
419
421 QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
422
423 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
424
425 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
426 int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
427 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
428 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
429 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
430 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
431 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
432 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
433 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
434 void legendCleanup();
435 void legendInvalidateMapBasedData();
436
437 protected:
438
444 QgsRenderContext *createTemporaryRenderContext() const SIP_SKIP;
445
447 QgsLayerTree *mRootNode = nullptr;
449 Flags mFlags;
451 QPersistentModelIndex mCurrentIndex;
453 int mAutoCollapseLegendNodesCount = -1;
454
465#ifndef SIP_RUN
467 {
469 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
471 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
472 };
473#endif
474
479#ifndef SIP_RUN
481 {
482 LayerLegendData() = default;
483
488 QList<QgsLayerTreeModelLegendNode *> activeNodes;
489
495 QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
496
501 QList<QgsLayerTreeModelLegendNode *> originalNodes;
503 LayerLegendTree *tree = nullptr;
504 };
505#endif
506
508 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
509
514 QMap<QString, QString> mLayerStyleOverrides;
515
517 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
518
523 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
524
527
529 double mLegendFilterByScale = 0;
530
531 QPointer< QgsMapHitTestTask > mHitTestTask;
532
533 QMap<QString, QSet<QString>> mHitTestResults;
534
535 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
536
537 double mLegendMapViewMupp = 0;
538 int mLegendMapViewDpi = 0;
539 double mLegendMapViewScale = 0;
541
542 QSet< QgsScreenProperties > mTargetScreenProperties;
543
544 private slots:
545 void legendNodeSizeChanged();
546 void hitTestTaskCompleted();
547
548 private:
549 void handleHitTestResults();
550
551
552};
553
555
556
557#ifndef SIP_RUN
558
564class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
565{
566 Q_OBJECT
567
568 public:
569 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
571 {
572 // we need a valid rule key to allow the model to build a tree out of legend nodes
573 // if that's possible (if there is a node without a rule key, building of tree is canceled)
574 mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
575 }
576
577 QVariant data( int role ) const override
578 {
579 if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
580 return mRuleKey;
581 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
583 return QVariant();
584 }
585
586 private:
587 QString mRuleKey;
588};
589#endif
590
592
593#endif // QGSLAYERTREEMODEL_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
Class for 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.
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.
@ NodeType
Type of node. Added in 3.16.
@ RuleKey
Rule key of the node (QString)
The QgsLayerTreeModel class is model implementation for Qt item views framework.
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< QgsLayerTreeFilterSettings > mFilterSettings
QMap< QString, QSet< QString > > mHitTestResults
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.
QSet< QgsScreenProperties > mTargetScreenProperties
QPointer< QgsMapHitTestTask > mHitTestTask
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
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.
Executes a QgsMapHitTest in a background thread.
Class that runs a hit test with given map settings.
The QgsMapSettings class contains configuration for rendering of the map.
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:191
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_END
Definition qgis_sip.h:208
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
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.