QGIS API Documentation  2.99.0-Master (90ae728)
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 
28 class QgsLayerTreeNode;
29 class QgsLayerTreeGroup;
30 class QgsLayerTreeLayer;
32 class QgsMapHitTest;
33 class QgsMapLayer;
34 class QgsMapSettings;
35 class QgsExpression;
36 class QgsRenderContext;
37 
52 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
53 {
54  Q_OBJECT
55  public:
58  explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = nullptr );
60 
61  // Implementation of virtual functions from QAbstractItemModel
62 
63  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
64  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
65  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
66  QModelIndex parent( const QModelIndex &child ) const override;
67  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
68  Qt::ItemFlags flags( const QModelIndex &index ) const override;
69  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
70  Qt::DropActions supportedDropActions() const override;
71  QStringList mimeTypes() const override;
72  QMimeData* mimeData( const QModelIndexList& indexes ) const override;
73  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent ) override;
74  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() ) override;
75 
76  // New stuff
77 
78  enum Flag
79  {
80  // display flags
81  ShowLegend = 0x0001,
82  ShowRasterPreviewIcon = 0x0002,
83  ShowLegendAsTree = 0x0004,
84  DeferredLegendInvalidation = 0x0008,
85  UseEmbeddedWidgets = 0x0010,
86 
87  // behavioral flags
88  AllowNodeReorder = 0x1000,
89  AllowNodeRename = 0x2000,
90  AllowNodeChangeVisibility = 0x4000,
91  AllowLegendChangeState = 0x8000,
92  ActionHierarchical = 0x10000,
93  };
94  Q_DECLARE_FLAGS( Flags, Flag )
95 
96 
97  void setFlags( QgsLayerTreeModel::Flags f );
99  void setFlag( Flag f, bool on = true );
101  Flags flags() const;
103  bool testFlag( Flag f ) const;
104 
107  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
109  QModelIndex node2index( QgsLayerTreeNode* node ) const;
113  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
114 
117  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
121  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
122 
128  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer, bool skipNodeEmbeddedInParent = false );
129 
133  QList<QgsLayerTreeModelLegendNode*> layerOriginalLegendNodes( QgsLayerTreeLayer* nodeLayer );
134 
137  QgsLayerTreeModelLegendNode* legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
138 
146  QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;
147 
149  QgsLayerTreeGroup* rootGroup() const;
152  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
153 
156  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
157 
159  QModelIndex currentIndex() const;
161  void setCurrentIndex( const QModelIndex& currentIndex );
162 
164  void setLayerTreeNodeFont( int nodeType, const QFont& font );
166  QFont layerTreeNodeFont( int nodeType ) const;
167 
169  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
171  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
172 
176  void setLegendFilterByScale( double scaleDenominator );
177  double legendFilterByScale() const { return mLegendFilterByScale; }
178 
183  void setLegendFilterByMap( const QgsMapSettings* settings );
184 
191  void setLegendFilter( const QgsMapSettings* settings, bool useExtent = true, const QgsGeometry& polygon = QgsGeometry(), bool useExpressions = true );
192 
195  const QgsMapSettings* legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
196 
200  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
204  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale ) const;
205 
208  QMap<QString, QString> layerStyleOverrides() const;
211  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
212 
213  signals:
214 
215  protected slots:
216  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
217  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
218  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
219  void nodeRemovedChildren();
220 
221  void nodeVisibilityChanged( QgsLayerTreeNode* node );
224  void nodeNameChanged( QgsLayerTreeNode* node, const QString& name );
225 
226  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
227 
228  void nodeLayerLoaded();
229  void nodeLayerWillBeUnloaded();
230  void layerLegendChanged();
231 
232  void layerNeedsUpdate();
233 
234  void legendNodeDataChanged();
235 
236  void invalidateLegendMapBasedData();
237 
238  protected:
239  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
240  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
241 
242  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
243  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
244 
245  void connectToLayers( QgsLayerTreeGroup* parentGroup );
246  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
247  void connectToRootNode();
248  void disconnectFromRootNode();
249 
251  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
252 
257  void refreshScaleBasedLayers( const QModelIndex& index = QModelIndex() );
258 
259  static QIcon iconGroup();
260 
262  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
263 
264  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
265 
266  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
267  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
268  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
269  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
270  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
271  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
272  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
273  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
274  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
275  void legendCleanup();
276  void legendInvalidateMapBasedData();
277 
278  protected:
280  QgsLayerTreeGroup* mRootNode = nullptr;
282  Flags mFlags;
284  QPersistentModelIndex mCurrentIndex;
287 
295  {
297  QMap<QgsLayerTreeModelLegendNode*, QgsLayerTreeModelLegendNode*> parents;
299  QMap<QgsLayerTreeModelLegendNode*, QList<QgsLayerTreeModelLegendNode*> > children;
300  };
301 
305  {
307  : embeddedNodeInParent( nullptr )
308  , tree( nullptr )
309  {
310  }
311 
314  QList<QgsLayerTreeModelLegendNode*> activeNodes;
318  QgsLayerTreeModelLegendNode* embeddedNodeInParent = nullptr;
321  QList<QgsLayerTreeModelLegendNode*> originalNodes;
323  LayerLegendTree* tree = nullptr;
324  };
325 
327  LayerLegendTree* tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode*>& nodes );
328 
331  QMap<QString, QString> mLayerStyleOverrides;
332 
334  QMap<QgsLayerTreeLayer*, LayerLegendData> mLegend;
335 
336  QFont mFontLayer;
337  QFont mFontGroup;
338 
341 
342  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
343  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
344 
347 
352 
353  private:
354 
356  QgsRenderContext* createTemporaryRenderContext() const;
357 };
358 
359 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
360 
361 #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.
static unsigned index
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
Base class for all map layer types.
Definition: qgsmaplayer.h:52
Structure that stores tree representation of map layer&#39;s legend.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
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.
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have null parent. Pointers are not owned...
QTimer mDeferLegendInvalidationTimer
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 (...
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
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)
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.
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer&#39;s legend nodes.
Layer tree node points to a map layer.
Structure that stores all data associated with one map layer.