QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
30 class QgsLayerTreeNode;
31 class QgsLayerTreeGroup;
32 class QgsLayerTreeLayer;
33 class QgsMapHitTest;
34 class QgsMapSettings;
35 class QgsExpression;
36 class QgsRenderContext;
37 class QgsLayerTree;
38 
55 class 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;
64  SIP_END
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 
460  QFont mFontLayer;
461  QFont mFontGroup;
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 
485 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
486 
487 #ifndef SIP_RUN
489 
495 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
496 {
497  Q_OBJECT
498 
499  public:
500  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
501  : QgsLayerTreeModelLegendNode( 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
QgsLayerTreeModel::mFontGroup
QFont mFontGroup
Definition: qgslayertreemodel.h:461
QgsLayerTreeNode
This class is a base class for nodes in a layer tree.
Definition: qgslayertreenode.h:75
qgslayertreemodellegendnode.h
QgsLayerTreeModel::LayerLegendTree::children
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under nullptr key. Pointers are not owned.
Definition: qgslayertreemodel.h:406
QgsLayerTreeModelLegendNode::data
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
QgsLayerTreeModel::LayerLegendData::activeNodes
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
Definition: qgslayertreemodel.h:423
QgsLayerTreeModel::mLegendMapViewMupp
double mLegendMapViewMupp
Definition: qgslayertreemodel.h:472
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsLayerTreeModel::mLegend
QHash< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
Definition: qgslayertreemodel.h:452
QgsLayerTreeModel::mLegendFilterHitTest
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
Definition: qgslayertreemodel.h:467
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsLayerTreeModel::mLegendFilterMapSettings
std::unique_ptr< QgsMapSettings > mLegendFilterMapSettings
Definition: qgslayertreemodel.h:466
QgsLayerTreeModel::mFontLayer
QFont mFontLayer
Definition: qgslayertreemodel.h:460
QgsLayerTreeModel::mLegendFilterUsesExtent
bool mLegendFilterUsesExtent
whether to use map filtering
Definition: qgslayertreemodel.h:470
QgsLayerTreeModel
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Definition: qgslayertreemodel.h:55
QgsMapHitTest
Class that runs a hit test with given map settings. Based on the hit test it returns which symbols wi...
Definition: qgsmaphittest.h:37
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
QgsLayerTreeModel::LayerLegendData::originalNodes
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
Definition: qgslayertreemodel.h:436
QgsLayerTreeModelLegendNode::EmbeddedWidget
@ EmbeddedWidget
Embedded widget placeholder node type.
Definition: qgslayertreemodellegendnode.h:97
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTree
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
QgsLayerTreeModelLegendNode::NodeTypeRole
@ NodeTypeRole
Type of node. Added in 3.16.
Definition: qgslayertreemodellegendnode.h:85
QgsLayerTreeModel::Flag
Flag
Definition: qgslayertreemodel.h:95
QgsLayerTreeModel::mDeferLegendInvalidationTimer
QTimer mDeferLegendInvalidationTimer
Definition: qgslayertreemodel.h:475
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:43
QgsLayerTreeModel::LayerLegendData
Structure that stores all data associated with one map layer.
Definition: qgslayertreemodel.h:415
QgsLayerTreeModel::legendFilterMapSettings
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled)
Definition: qgslayertreemodel.h:252
QgsLayerTreeModel::mLegendFilterByScale
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
Definition: qgslayertreemodel.h:464
QgsLayerTreeGroup
Layer tree group node serves as a container for layers and further groups.
Definition: qgslayertreegroup.h:40
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
QgsLayerTreeModel::LayerLegendTree
Structure that stores tree representation of map layer's legend.
Definition: qgslayertreemodel.h:401
QgsLayerTreeModel::setAutoCollapseLegendNodes
void setAutoCollapseLegendNodes(int nodeCount)
Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-...
Definition: qgslayertreemodel.h:208
QgsLayerTreeModel::LayerLegendTree::parents
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have nullptr parent. Pointers are not owned.
Definition: qgslayertreemodel.h:404
QgsLayerTreeModel::autoCollapseLegendNodes
int autoCollapseLegendNodes() const
Returns at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse ...
Definition: qgslayertreemodel.h:210
QgsWms::legendNode
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
Definition: qgswmsgetlegendgraphics.cpp:363
qgsgeometry.h
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:114
QgsLayerTreeModel::mLegendMapViewScale
double mLegendMapViewScale
Definition: qgslayertreemodel.h:474
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsLayerTreeModel::mLegendMapViewDpi
int mLegendMapViewDpi
Definition: qgslayertreemodel.h:473
QgsLayerTreeModel::mLayerStyleOverrides
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
Definition: qgslayertreemodel.h:449
QgsLayerTreeModel::legendFilterByScale
double legendFilterByScale() const
Returns the scale which restricts the legend nodes which are visible.
Definition: qgslayertreemodel.h:228
QgsGuiUtils::scaleIconSize
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
Definition: qgsguiutils.cpp:259
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayerTreeModel::mInvalidatedNodes
QSet< QgsLayerTreeLayer * > mInvalidatedNodes
Keep track of layer nodes for which the legend size needs to be recalculated.
Definition: qgslayertreemodel.h:458
QgsLayerTreeModelLegendNode
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Definition: qgslayertreemodellegendnode.h:49