QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
57class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
58{
59
60#ifdef SIP_RUN
62 if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
63 sipType = sipType_QgsLayerTreeModel;
64 else
65 sipType = 0;
67#endif
68
69 Q_OBJECT
70 public:
71
76 explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
77
78 ~QgsLayerTreeModel() override;
79
80 // Implementation of virtual functions from QAbstractItemModel
81
82 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
83 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
84 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
85 QModelIndex parent( const QModelIndex &child ) const override;
86 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
87 Qt::ItemFlags flags( const QModelIndex &index ) const override;
88 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
89 Qt::DropActions supportedDropActions() const override;
90 QStringList mimeTypes() const override;
91 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
92 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
93 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
94
95 // New stuff
96
97 enum Flag
98 {
99 // display flags
100 ShowLegend = 0x0001,
101 ShowLegendAsTree = 0x0004,
102 DeferredLegendInvalidation = 0x0008,
103 UseEmbeddedWidgets = 0x0010,
104 UseTextFormatting = 0x0020,
105
106 // behavioral flags
107 AllowNodeReorder = 0x1000,
108 AllowNodeRename = 0x2000,
109 AllowNodeChangeVisibility = 0x4000,
110 AllowLegendChangeState = 0x8000,
111 ActionHierarchical = 0x10000,
112 UseThreadedHitTest = 0x20000,
113 };
114 Q_DECLARE_FLAGS( Flags, Flag )
115
116
117 void setFlags( QgsLayerTreeModel::Flags f );
119 void setFlag( Flag f, bool on = true );
121 Flags flags() const;
123 bool testFlag( Flag f ) const;
124
129 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
131 QModelIndex node2index( QgsLayerTreeNode *node ) const;
132
138 QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
139
144 static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
145
151 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
152
160 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
161
167 QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
168
173 QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
174
183 QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
184
186 QgsLayerTree *rootGroup() const;
187
192 void setRootGroup( QgsLayerTree *newRootGroup );
193
198 void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
199
201 QModelIndex currentIndex() const;
203 void setCurrentIndex( const QModelIndex &currentIndex );
204
206 void setLayerTreeNodeFont( int nodeType, const QFont &font );
208 QFont layerTreeNodeFont( int nodeType ) const;
209
211 void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
213 int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
214
222 void setLegendFilterByScale( double scale );
223
231 double legendFilterByScale() const { return mLegendFilterByScale; }
232
239 Q_DECL_DEPRECATED void setLegendFilterByMap( const QgsMapSettings *settings ) SIP_DEPRECATED;
240
249 Q_DECL_DEPRECATED void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true ) SIP_DEPRECATED;
250
255 const QgsMapSettings *legendFilterMapSettings() const;
256
266 void setFilterSettings( const QgsLayerTreeFilterSettings *settings = nullptr );
267
275 const QgsLayerTreeFilterSettings *filterSettings() const;
276
282 void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
283
289 void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
290
295 QMap<QString, QString> layerStyleOverrides() const;
296
301 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
302
311 void addTargetScreenProperties( const QgsScreenProperties &properties );
312
322 QSet< QgsScreenProperties > targetScreenProperties() const;
323
333 static int scaleIconSize( int standardSize );
334
341 void waitForHitTestBlocking();
342
351 bool hitTestInProgress() const;
352
353 signals:
354
359 void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = 5 );
360
369 void hitTestStarted();
370
379 void hitTestCompleted();
380
381 protected slots:
382 void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
383 void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
384 void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
385 void nodeRemovedChildren();
386
387 void nodeVisibilityChanged( QgsLayerTreeNode *node );
388
393 void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
394
395 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
396
397 void nodeLayerLoaded();
398 void nodeLayerWillBeUnloaded();
399 void layerLegendChanged();
400
405 void layerFlagsChanged();
406
407 void layerNeedsUpdate();
408
409 void legendNodeDataChanged();
410
411 void invalidateLegendMapBasedData();
412
413 protected:
414 void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
415 void addLegendToLayer( QgsLayerTreeLayer *nodeL );
416
417 void connectToLayer( QgsLayerTreeLayer *nodeLayer );
418 void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
419
420 void connectToLayers( QgsLayerTreeGroup *parentGroup );
421 void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
422 void connectToRootNode();
423 void disconnectFromRootNode();
424
426 void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
427
433 void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
434
435 static QIcon iconGroup();
436
438 QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
439
440 QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
441
442 int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
443 int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
444 QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
445 QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
446 QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
447 QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
448 Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
449 bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
450 QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
451 void legendCleanup();
452 void legendInvalidateMapBasedData();
453
454 protected:
455
461 QgsRenderContext *createTemporaryRenderContext() const SIP_SKIP;
462
464 QgsLayerTree *mRootNode = nullptr;
466 Flags mFlags;
468 QPersistentModelIndex mCurrentIndex;
470 int mAutoCollapseLegendNodesCount = -1;
471
482#ifndef SIP_RUN
484 {
486 QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
488 QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
489 };
490#endif
491
496#ifndef SIP_RUN
498 {
499 LayerLegendData() = default;
500
505 QList<QgsLayerTreeModelLegendNode *> activeNodes;
506
512 QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
513
518 QList<QgsLayerTreeModelLegendNode *> originalNodes;
520 LayerLegendTree *tree = nullptr;
521 };
522#endif
523
525 LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
526
531 QMap<QString, QString> mLayerStyleOverrides;
532
534 QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
535
540 QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
541
544
546 double mLegendFilterByScale = 0;
547
548 QPointer< QgsMapHitTestTask > mHitTestTask;
549
550 QMap<QString, QSet<QString>> mHitTestResults;
551
552 std::unique_ptr< QgsLayerTreeFilterSettings > mFilterSettings;
553
554 double mLegendMapViewMupp = 0;
555 int mLegendMapViewDpi = 0;
556 double mLegendMapViewScale = 0;
558
559 QSet< QgsScreenProperties > mTargetScreenProperties;
560
561 private slots:
562 void legendNodeSizeChanged();
563 void hitTestTaskCompleted();
564
565 private:
566 void handleHitTestResults();
567
568
569};
570
571Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
572
573
574#ifndef SIP_RUN
575
581class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
582{
583 Q_OBJECT
584
585 public:
586 EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
588 {
589 // we need a valid rule key to allow the model to build a tree out of legend nodes
590 // if that's possible (if there is a node without a rule key, building of tree is canceled)
591 mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
592 }
593
594 QVariant data( int role ) const override
595 {
596 if ( role == RuleKeyRole )
597 return mRuleKey;
600 return QVariant();
601 }
602
603 private:
604 QString mRuleKey;
605};
606#endif
607
609
610#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.
@ NodeTypeRole
Type of node. Added in 3.16.
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:186
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#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(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.