QGIS API Documentation  2.99.0-Master (40f86b2)
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 <memory>
25 
26 #include "qgsgeometry.h"
27 #include "qgsmaplayer.h"
28 
29 class QgsLayerTreeNode;
30 class QgsLayerTreeGroup;
31 class QgsLayerTreeLayer;
33 class QgsMapHitTest;
34 class QgsMapLayer;
35 class QgsMapSettings;
36 class QgsExpression;
37 class QgsRenderContext;
38 class QgsLayerTree;
39 
54 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
55 {
56  Q_OBJECT
57  public:
60  explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent = nullptr );
61 
63 
64  // Implementation of virtual functions from QAbstractItemModel
65 
66  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
67  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
68  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
69  QModelIndex parent( const QModelIndex &child ) const override;
70  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
71  Qt::ItemFlags flags( const QModelIndex &index ) const override;
72  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
73  Qt::DropActions supportedDropActions() const override;
74  QStringList mimeTypes() const override;
75  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
76  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
77  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
78 
79  // New stuff
80 
81  enum Flag
82  {
83  // display flags
84  ShowLegend = 0x0001,
85  ShowRasterPreviewIcon = 0x0002,
86  ShowLegendAsTree = 0x0004,
87  DeferredLegendInvalidation = 0x0008,
88  UseEmbeddedWidgets = 0x0010,
89 
90  // behavioral flags
91  AllowNodeReorder = 0x1000,
92  AllowNodeRename = 0x2000,
93  AllowNodeChangeVisibility = 0x4000,
94  AllowLegendChangeState = 0x8000,
95  ActionHierarchical = 0x10000,
96  };
97  Q_DECLARE_FLAGS( Flags, Flag )
98 
99 
100  void setFlags( QgsLayerTreeModel::Flags f );
102  void setFlag( Flag f, bool on = true );
104  Flags flags() const;
106  bool testFlag( Flag f ) const;
107 
110  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
112  QModelIndex node2index( QgsLayerTreeNode *node ) const;
116  QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
117 
120  static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
124  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
125 
131  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
132 
136  QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
137 
140  QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
141 
149  QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
150 
152  QgsLayerTree *rootGroup() const;
155  void setRootGroup( QgsLayerTree *newRootGroup );
156 
159  void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
160 
162  QModelIndex currentIndex() const;
164  void setCurrentIndex( const QModelIndex &currentIndex );
165 
167  void setLayerTreeNodeFont( int nodeType, const QFont &font );
169  QFont layerTreeNodeFont( int nodeType ) const;
170 
172  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
174  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
175 
179  void setLegendFilterByScale( double scaleDenominator );
180  double legendFilterByScale() const { return mLegendFilterByScale; }
181 
186  void setLegendFilterByMap( const QgsMapSettings *settings );
187 
194  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
195 
198  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
199 
203  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
207  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale ) const;
208 
211  QMap<QString, QString> layerStyleOverrides() const;
214  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
215 
216  protected slots:
217  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
218  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
219  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
220  void nodeRemovedChildren();
221 
222  void nodeVisibilityChanged( QgsLayerTreeNode *node );
225  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
226 
227  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
228 
229  void nodeLayerLoaded();
230  void nodeLayerWillBeUnloaded();
231  void layerLegendChanged();
232 
233  void layerNeedsUpdate();
234 
235  void legendNodeDataChanged();
236 
237  void invalidateLegendMapBasedData();
238 
239  protected:
240  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
241  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
242 
243  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
244  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
245 
246  void connectToLayers( QgsLayerTreeGroup *parentGroup );
247  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
248  void connectToRootNode();
249  void disconnectFromRootNode();
250 
252  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
253 
258  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
259 
260  static QIcon iconGroup();
261 
263  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
264 
265  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
266 
267  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
268  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
269  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
270  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
271  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
272  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
273  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
274  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
275  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
276  void legendCleanup();
277  void legendInvalidateMapBasedData();
278 
279  protected:
281  QgsLayerTree *mRootNode = nullptr;
283  Flags mFlags;
285  QPersistentModelIndex mCurrentIndex;
288 
296  {
298  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
300  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
301  };
302 
306  {
308  : embeddedNodeInParent( nullptr )
309  , tree( nullptr )
310  {
311  }
312 
315  QList<QgsLayerTreeModelLegendNode *> activeNodes;
319  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
322  QList<QgsLayerTreeModelLegendNode *> originalNodes;
324  LayerLegendTree *tree = nullptr;
325  };
326 
328  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes );
329 
332  QMap<QString, QString> mLayerStyleOverrides;
333 
335  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
336 
337  QFont mFontLayer;
338  QFont mFontGroup;
339 
342 
343  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
344  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
345 
348 
353 
354  private:
355 
357  QgsRenderContext *createTemporaryRenderContext() const;
358 };
359 
360 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
361 
362 #endif // QGSLAYERTREEMODEL_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Layer tree group node serves as a container for layers and further groups.
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer&#39;s legend nodes.
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:52
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
Structure that stores tree representation of map layer&#39;s legend.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Set of flags for the model.
std::unique_ptr< QgsMapSettings > mLegendFilterMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QTimer mDeferLegendInvalidationTimer
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:31
This class is a base class for nodes in a layer tree.
int autoCollapseLegendNodes() const
Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (...
Class that runs a hit test with given map settings.
Definition: qgsmaphittest.h:35
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have null parent. Pointers are not owned...
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or null if none is enabled) ...
void setAutoCollapseLegendNodes(int nodeCount)
Set at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-c...
Contains information about the context of a rendering operation.
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
double legendFilterByScale() const
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers&#39; styles: key = layer ID, value = style XML.
Layer tree node points to a map layer.
Structure that stores all data associated with one map layer.