QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 private slots:
398
403 void layerProfileGenerationPropertyChanged();
404
405 protected:
406 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
407 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
408
409 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
410 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
411
412 void connectToLayers( QgsLayerTreeGroup *parentGroup );
413 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
414 void connectToRootNode();
415 void disconnectFromRootNode();
416
418 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
419
424 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
425
426 static QIcon iconGroup();
427
429 QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
430
431 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
432
433 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
434 int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
435 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
436 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
437 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
438 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
439 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
440 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
441 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
442 void legendCleanup();
443 void legendInvalidateMapBasedData();
444
445 protected:
446
452 QgsRenderContext *createTemporaryRenderContext() const SIP_SKIP;
453
455 QgsLayerTree *mRootNode = nullptr;
457 Flags mFlags;
459 QPersistentModelIndex mCurrentIndex;
461 int mAutoCollapseLegendNodesCount = -1;
462
473#ifndef SIP_RUN
475 {
477 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
479 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
480 };
481#endif
482
487#ifndef SIP_RUN
489 {
490 LayerLegendData() = default;
491
496 QList<QgsLayerTreeModelLegendNode *> activeNodes;
497
503 QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
504
509 QList<QgsLayerTreeModelLegendNode *> originalNodes;
511 LayerLegendTree *tree = nullptr;
512 };
513#endif
514
516 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
517
522 QMap<QString, QString> mLayerStyleOverrides;
523
525 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
526
531 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
532
535
537 double mLegendFilterByScale = 0;
538
539 QPointer< QgsMapHitTestTask > mHitTestTask;
540
541 QMap<QString, QSet<QString>> mHitTestResults;
542
543 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
544
545 double mLegendMapViewMupp = 0;
546 int mLegendMapViewDpi = 0;
547 double mLegendMapViewScale = 0;
549
550 QSet< QgsScreenProperties > mTargetScreenProperties;
551
552 private slots:
553 void legendNodeSizeChanged();
554 void hitTestTaskCompleted();
555
556 private:
557 void handleHitTestResults();
558
559
560};
561
563
564
565#ifndef SIP_RUN
566
572class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
573{
574 Q_OBJECT
575
576 public:
577 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
579 {
580 // we need a valid rule key to allow the model to build a tree out of legend nodes
581 // if that's possible (if there is a node without a rule key, building of tree is canceled)
582 mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
583 }
584
585 QVariant data( int role ) const override
586 {
587 if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
588 return mRuleKey;
589 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
591 return QVariant();
592 }
593
594 private:
595 QString mRuleKey;
596};
597#endif
598
600
601#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.