QGIS API Documentation  2.99.0-Master (c558d51)
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 <QAbstractItemModel>
20 #include <QFont>
21 #include <QIcon>
22 #include <QTimer>
23 
24 #include "qgsgeometry.h"
25 
26 class QgsLayerTreeNode;
27 class QgsLayerTreeGroup;
28 class QgsLayerTreeLayer;
30 class QgsMapHitTest;
31 class QgsMapLayer;
32 class QgsMapSettings;
33 class QgsExpression;
34 class QgsRenderContext;
35 
50 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
51 {
52  Q_OBJECT
53  public:
56  explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = nullptr );
58 
59  // Implementation of virtual functions from QAbstractItemModel
60 
61  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
62  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
63  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
64  QModelIndex parent( const QModelIndex &child ) const override;
65  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
66  Qt::ItemFlags flags( const QModelIndex &index ) const override;
67  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
68  Qt::DropActions supportedDropActions() const override;
69  QStringList mimeTypes() const override;
70  QMimeData* mimeData( const QModelIndexList& indexes ) const override;
71  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent ) override;
72  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() ) override;
73 
74  // New stuff
75 
76  enum Flag
77  {
78  // display flags
79  ShowLegend = 0x0001,
80  ShowRasterPreviewIcon = 0x0002,
81  ShowLegendAsTree = 0x0004,
82  DeferredLegendInvalidation = 0x0008,
83  UseEmbeddedWidgets = 0x0010,
84 
85  // behavioral flags
86  AllowNodeReorder = 0x1000,
87  AllowNodeRename = 0x2000,
88  AllowNodeChangeVisibility = 0x4000,
89  AllowLegendChangeState = 0x8000,
90  };
91  Q_DECLARE_FLAGS( Flags, Flag )
92 
93 
94  void setFlags( QgsLayerTreeModel::Flags f );
96  void setFlag( Flag f, bool on = true );
98  Flags flags() const;
100  bool testFlag( Flag f ) const;
101 
104  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
106  QModelIndex node2index( QgsLayerTreeNode* node ) const;
110  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
111 
114  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
118  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
119 
125  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer, bool skipNodeEmbeddedInParent = false );
126 
130  QList<QgsLayerTreeModelLegendNode*> layerOriginalLegendNodes( QgsLayerTreeLayer* nodeLayer );
131 
134  QgsLayerTreeModelLegendNode* legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
135 
143  QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;
144 
146  QgsLayerTreeGroup* rootGroup() const;
149  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
150 
153  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
154 
156  QModelIndex currentIndex() const;
158  void setCurrentIndex( const QModelIndex& currentIndex );
159 
161  void setLayerTreeNodeFont( int nodeType, const QFont& font );
163  QFont layerTreeNodeFont( int nodeType ) const;
164 
166  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
168  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
169 
173  void setLegendFilterByScale( double scaleDenominator );
174  double legendFilterByScale() const { return mLegendFilterByScale; }
175 
180  void setLegendFilterByMap( const QgsMapSettings* settings );
181 
188  void setLegendFilter( const QgsMapSettings* settings, bool useExtent = true, const QgsGeometry& polygon = QgsGeometry(), bool useExpressions = true );
189 
192  const QgsMapSettings* legendFilterMapSettings() const { return mLegendFilterMapSettings.data(); }
193 
197  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
201  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale ) const;
202 
205  QMap<QString, QString> layerStyleOverrides() const;
208  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
209 
210  signals:
211 
212  protected slots:
213  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
214  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
215  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
216  void nodeRemovedChildren();
217 
218  void nodeVisibilityChanged( QgsLayerTreeNode* node );
221  void nodeNameChanged( QgsLayerTreeNode* node, const QString& name );
222 
223  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
224 
225  void nodeLayerLoaded();
226  void nodeLayerWillBeUnloaded();
227  void layerLegendChanged();
228 
229  void layerNeedsUpdate();
230 
231  void legendNodeDataChanged();
232 
233  void invalidateLegendMapBasedData();
234 
235  protected:
236  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
237  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
238 
239  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
240  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
241 
242  void connectToLayers( QgsLayerTreeGroup* parentGroup );
243  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
244  void connectToRootNode();
245  void disconnectFromRootNode();
246 
248  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
249 
254  void refreshScaleBasedLayers( const QModelIndex& index = QModelIndex() );
255 
256  static const QIcon& iconGroup();
257 
259  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
260 
261  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
262 
263  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
264  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
265  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
266  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
267  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
268  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
269  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
270  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
271  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
272  void legendCleanup();
273  void legendInvalidateMapBasedData();
274 
275  protected:
279  Flags mFlags;
281  QPersistentModelIndex mCurrentIndex;
284 
292  {
294  QMap<QgsLayerTreeModelLegendNode*, QgsLayerTreeModelLegendNode*> parents;
296  QMap<QgsLayerTreeModelLegendNode*, QList<QgsLayerTreeModelLegendNode*> > children;
297  };
298 
302  {
304  : embeddedNodeInParent( nullptr )
305  , tree( nullptr )
306  {
307  }
308 
311  QList<QgsLayerTreeModelLegendNode*> activeNodes;
318  QList<QgsLayerTreeModelLegendNode*> originalNodes;
321  };
322 
324  LayerLegendTree* tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode*>& nodes );
325 
328  QMap<QString, QString> mLayerStyleOverrides;
329 
331  QMap<QgsLayerTreeLayer*, LayerLegendData> mLegend;
332 
333  QFont mFontLayer;
334  QFont mFontGroup;
335 
338 
339  QScopedPointer<QgsMapSettings> mLegendFilterMapSettings;
340  QScopedPointer<QgsMapHitTest> mLegendFilterHitTest;
341 
344 
349 
350  private:
351 
353  QgsRenderContext* createTemporaryRenderContext() const;
354 };
355 
356 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
357 
358 #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:49
double legendFilterByScale() const
LayerLegendTree * tree
Optional pointer to a tree structure - see LayerLegendTree for details.
Structure that stores tree representation of map layer&#39;s legend.
QgsLayerTreeModelLegendNode * embeddedNodeInParent
A legend node that is not displayed separately, its icon is instead shown within the layer node&#39;s ite...
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:78
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Set of flags for the model.
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
int autoCollapseLegendNodes() const
Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (...
This class is a base class for nodes in a layer tree.
QgsLayerTreeGroup * mRootNode
Pointer to the root node of the layer tree. Not owned by the model.
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:34
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) ...
Contains information about the context of a rendering operation.
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QScopedPointer< QgsMapHitTest > mLegendFilterHitTest
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers&#39; styles: key = layer ID, value = style XML.
QScopedPointer< QgsMapSettings > mLegendFilterMapSettings
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.