QGIS API Documentation  2.99.0-Master (283d45a)
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 
25 #include "qgsgeometry.h"
26 
27 class QgsLayerTreeNode;
28 class QgsLayerTreeGroup;
29 class QgsLayerTreeLayer;
31 class QgsMapHitTest;
32 class QgsMapLayer;
33 class QgsMapSettings;
34 class QgsExpression;
35 class QgsRenderContext;
36 
51 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
52 {
53  Q_OBJECT
54  public:
57  explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = nullptr );
59 
60  // Implementation of virtual functions from QAbstractItemModel
61 
62  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
63  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
64  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
65  QModelIndex parent( const QModelIndex &child ) const override;
66  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
67  Qt::ItemFlags flags( const QModelIndex &index ) const override;
68  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
69  Qt::DropActions supportedDropActions() const override;
70  QStringList mimeTypes() const override;
71  QMimeData* mimeData( const QModelIndexList& indexes ) const override;
72  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent ) override;
73  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() ) override;
74 
75  // New stuff
76 
77  enum Flag
78  {
79  // display flags
80  ShowLegend = 0x0001,
81  ShowRasterPreviewIcon = 0x0002,
82  ShowLegendAsTree = 0x0004,
83  DeferredLegendInvalidation = 0x0008,
84  UseEmbeddedWidgets = 0x0010,
85 
86  // behavioral flags
87  AllowNodeReorder = 0x1000,
88  AllowNodeRename = 0x2000,
89  AllowNodeChangeVisibility = 0x4000,
90  AllowLegendChangeState = 0x8000,
91  ActionHierarchical = 0x10000,
92  };
93  Q_DECLARE_FLAGS( Flags, Flag )
94 
95 
96  void setFlags( QgsLayerTreeModel::Flags f );
98  void setFlag( Flag f, bool on = true );
100  Flags flags() const;
102  bool testFlag( Flag f ) const;
103 
106  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
108  QModelIndex node2index( QgsLayerTreeNode* node ) const;
112  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
113 
116  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
120  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
121 
127  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer, bool skipNodeEmbeddedInParent = false );
128 
132  QList<QgsLayerTreeModelLegendNode*> layerOriginalLegendNodes( QgsLayerTreeLayer* nodeLayer );
133 
136  QgsLayerTreeModelLegendNode* legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
137 
145  QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;
146 
148  QgsLayerTreeGroup* rootGroup() const;
151  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
152 
155  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
156 
158  QModelIndex currentIndex() const;
160  void setCurrentIndex( const QModelIndex& currentIndex );
161 
163  void setLayerTreeNodeFont( int nodeType, const QFont& font );
165  QFont layerTreeNodeFont( int nodeType ) const;
166 
168  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
170  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
171 
175  void setLegendFilterByScale( double scaleDenominator );
176  double legendFilterByScale() const { return mLegendFilterByScale; }
177 
182  void setLegendFilterByMap( const QgsMapSettings* settings );
183 
190  void setLegendFilter( const QgsMapSettings* settings, bool useExtent = true, const QgsGeometry& polygon = QgsGeometry(), bool useExpressions = true );
191 
194  const QgsMapSettings* legendFilterMapSettings() const { return mLegendFilterMapSettings.data(); }
195 
199  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
203  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale ) const;
204 
207  QMap<QString, QString> layerStyleOverrides() const;
210  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
211 
212  signals:
213 
214  protected slots:
215  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
216  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
217  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
218  void nodeRemovedChildren();
219 
220  void nodeVisibilityChanged( QgsLayerTreeNode* node );
223  void nodeNameChanged( QgsLayerTreeNode* node, const QString& name );
224 
225  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
226 
227  void nodeLayerLoaded();
228  void nodeLayerWillBeUnloaded();
229  void layerLegendChanged();
230 
231  void layerNeedsUpdate();
232 
233  void legendNodeDataChanged();
234 
235  void invalidateLegendMapBasedData();
236 
237  protected:
238  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
239  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
240 
241  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
242  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
243 
244  void connectToLayers( QgsLayerTreeGroup* parentGroup );
245  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
246  void connectToRootNode();
247  void disconnectFromRootNode();
248 
250  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
251 
256  void refreshScaleBasedLayers( const QModelIndex& index = QModelIndex() );
257 
258  static QIcon iconGroup();
259 
261  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
262 
263  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
264 
265  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
266  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
267  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
268  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
269  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
270  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
271  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
272  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
273  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
274  void legendCleanup();
275  void legendInvalidateMapBasedData();
276 
277  protected:
281  Flags mFlags;
283  QPersistentModelIndex mCurrentIndex;
286 
294  {
296  QMap<QgsLayerTreeModelLegendNode*, QgsLayerTreeModelLegendNode*> parents;
298  QMap<QgsLayerTreeModelLegendNode*, QList<QgsLayerTreeModelLegendNode*> > children;
299  };
300 
304  {
306  : embeddedNodeInParent( nullptr )
307  , tree( nullptr )
308  {
309  }
310 
313  QList<QgsLayerTreeModelLegendNode*> activeNodes;
320  QList<QgsLayerTreeModelLegendNode*> originalNodes;
323  };
324 
326  LayerLegendTree* tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode*>& nodes );
327 
330  QMap<QString, QString> mLayerStyleOverrides;
331 
333  QMap<QgsLayerTreeLayer*, LayerLegendData> mLegend;
334 
335  QFont mFontLayer;
336  QFont mFontGroup;
337 
340 
341  QScopedPointer<QgsMapSettings> mLegendFilterMapSettings;
342  QScopedPointer<QgsMapHitTest> mLegendFilterHitTest;
343 
346 
351 
352  private:
353 
355  QgsRenderContext* createTemporaryRenderContext() const;
356 };
357 
358 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
359 
360 #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:50
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:79
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
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.
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...
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.