QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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:
374  QgsLayerTree *mRootNode = nullptr;
376  Flags mFlags;
378  QPersistentModelIndex mCurrentIndex;
381 
392 #ifndef SIP_RUN
394  {
396  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
398  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
399  };
400 #endif
401 
406 #ifndef SIP_RUN
408  {
409  LayerLegendData() = default;
410 
415  QList<QgsLayerTreeModelLegendNode *> activeNodes;
416 
422  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
423 
428  QList<QgsLayerTreeModelLegendNode *> originalNodes;
430  LayerLegendTree *tree = nullptr;
431  };
432 #endif
433 
435  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
436 
441  QMap<QString, QString> mLayerStyleOverrides;
442 
444  QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
445 
450  QSet<QgsLayerTreeLayer *> mInvalidatedNodes;
451 
452  QFont mFontLayer;
453  QFont mFontGroup;
454 
457 
458  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
459  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
460 
463 
468 
469  private slots:
470  void legendNodeSizeChanged();
471 
472  private:
473 
475  QgsRenderContext *createTemporaryRenderContext() const;
476 
477 };
478 
479 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
480 
481 #ifndef SIP_RUN
483 
489 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
490 {
491  Q_OBJECT
492 
493  public:
494  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
495  : QgsLayerTreeModelLegendNode( nodeL )
496  {
497  // we need a valid rule key to allow the model to build a tree out of legend nodes
498  // if that's possible (if there is a node without a rule key, building of tree is canceled)
499  mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
500  }
501 
502  QVariant data( int role ) const override
503  {
504  if ( role == RuleKeyRole )
505  return mRuleKey;
508  return QVariant();
509  }
510 
511  private:
512  QString mRuleKey;
513 };
514 #endif
515 
517 
518 #endif // QGSLAYERTREEMODEL_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:106
Class for parsing and evaluation of expressions (formerly called "search strings").
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
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.
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled)
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
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.
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
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.
Flags mFlags
Sets of flags for the model.
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:177
#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:194
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.