QGIS API Documentation
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 
49 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
50 {
51  Q_OBJECT
52  public:
55  explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = nullptr );
57 
58  // Implementation of virtual functions from QAbstractItemModel
59 
60  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
61  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
62  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
63  QModelIndex parent( const QModelIndex &child ) const override;
64  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
65  Qt::ItemFlags flags( const QModelIndex &index ) const override;
66  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
67  Qt::DropActions supportedDropActions() const override;
68  QStringList mimeTypes() const override;
69  QMimeData* mimeData( const QModelIndexList& indexes ) const override;
70  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent ) override;
71  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() ) override;
72 
73  // New stuff
74 
75  enum Flag
76  {
77  // display flags
78  ShowLegend = 0x0001,
79  ShowSymbology = 0x0001,
80  ShowRasterPreviewIcon = 0x0002,
81  ShowLegendAsTree = 0x0004,
82  DeferredLegendInvalidation = 0x0008,
83 
84  // behavioral flags
85  AllowNodeReorder = 0x1000,
86  AllowNodeRename = 0x2000,
87  AllowNodeChangeVisibility = 0x4000,
88  AllowLegendChangeState = 0x8000,
89  AllowSymbologyChangeState = 0x8000,
90  };
91  Q_DECLARE_FLAGS( Flags, Flag )
92 
93 
94  void setFlags( const 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 
123  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );
124 
128  QList<QgsLayerTreeModelLegendNode*> layerOriginalLegendNodes( QgsLayerTreeLayer* nodeLayer );
129 
137  QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;
138 
140  QgsLayerTreeGroup* rootGroup() const;
143  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
144 
147  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
148 
150  QModelIndex currentIndex() const;
152  void setCurrentIndex( const QModelIndex& currentIndex );
153 
155  void setLayerTreeNodeFont( int nodeType, const QFont& font );
157  QFont layerTreeNodeFont( int nodeType ) const;
158 
160  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
162  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
163 
167  void setLegendFilterByScale( double scaleDenominator );
168  double legendFilterByScale() const { return mLegendFilterByScale; }
169 
174  void setLegendFilterByMap( const QgsMapSettings* settings );
175 
182  void setLegendFilter( const QgsMapSettings* settings, bool useExtent = true, const QgsGeometry& polygon = QgsGeometry(), bool useExpressions = true );
183 
186  Q_DECL_DEPRECATED const QgsMapSettings* legendFilterByMap() const { return mLegendFilterMapSettings.data(); }
187 
190  const QgsMapSettings* legendFilterMapSettings() const { return mLegendFilterMapSettings.data(); }
191 
195  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
199  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale );
200 
203  QMap<QString, QString> layerStyleOverrides() const;
206  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
207 
210  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
213  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
215  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
217  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
219  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
220 
221  signals:
222 
223  protected slots:
224  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
225  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
226  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
227  void nodeRemovedChildren();
228 
229  void nodeVisibilityChanged( QgsLayerTreeNode* node );
230 
231  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
232 
233  void nodeLayerLoaded();
234  void nodeLayerWillBeUnloaded();
235  void layerLegendChanged();
236 
237  void layerNeedsUpdate();
238 
239  void legendNodeDataChanged();
240 
241  void invalidateLegendMapBasedData();
242 
243  protected:
244  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
245  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
246 
247  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
248  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
249 
250  void connectToLayers( QgsLayerTreeGroup* parentGroup );
251  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
252  void connectToRootNode();
253  void disconnectFromRootNode();
254 
256  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
257 
262  void refreshScaleBasedLayers( const QModelIndex& index = QModelIndex() );
263 
264  static const QIcon& iconGroup();
265 
268 
269  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
270 
271  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
272  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
273  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
274  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
275  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
276  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
277  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
278  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
279  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
280  void legendCleanup();
281  void legendInvalidateMapBasedData();
282 
283  protected:
287  Flags mFlags;
292 
300  {
305  };
306 
310  {
319  };
320 
322  void tryBuildLegendTree( LayerLegendData& data );
323 
327 
330 
333 
336 
339 
342 
347 };
348 
349 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
350 
351 #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.
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
virtual int rowCount(const QModelIndex &parent) const =0
Base class for all map layer types.
Definition: qgsmaplayer.h:49
Q_DECL_DEPRECATED const QgsMapSettings * legendFilterByMap() const
Returns the current map settings used for legend filtering.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
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.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Set of flags for the model.
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
virtual QStringList mimeTypes() const
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 (...
virtual Qt::DropActions supportedDropActions() const
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.
virtual QVariant data(const QModelIndex &index, int role) const =0
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
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)
virtual bool removeRows(int row, int count, const QModelIndex &parent)
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or null if none is enabled) ...
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
typedef DropActions
virtual int columnCount(const QModelIndex &parent) const =0
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
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.
QObject * parent() const
Q_DECL_DEPRECATED void refreshLayerSymbology(QgsLayerTreeLayer *nodeLayer)
QScopedPointer< QgsMapSettings > mLegendFilterMapSettings
Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes(int nodeCount)
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer&#39;s legend nodes.
Layer tree node points to a map layer.
Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const
Structure that stores all data associated with one map layer.
typedef ItemFlags