QGIS API Documentation  2.17.0-Master (dfeb663)
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  UseEmbeddedWidgets = 0x0010,
84 
85  // behavioral flags
86  AllowNodeReorder = 0x1000,
87  AllowNodeRename = 0x2000,
88  AllowNodeChangeVisibility = 0x4000,
89  AllowLegendChangeState = 0x8000,
90  AllowSymbologyChangeState = 0x8000,
91  };
92  Q_DECLARE_FLAGS( Flags, Flag )
93 
94 
95  void setFlags( const QgsLayerTreeModel::Flags& f );
97  void setFlag( Flag f, bool on = true );
99  Flags flags() const;
101  bool testFlag( Flag f ) const;
102 
105  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
107  QModelIndex node2index( QgsLayerTreeNode* node ) const;
111  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
112 
115  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
119  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
120 
124  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );
125 
129  QList<QgsLayerTreeModelLegendNode*> layerOriginalLegendNodes( QgsLayerTreeLayer* nodeLayer );
130 
138  QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;
139 
141  QgsLayerTreeGroup* rootGroup() const;
144  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
145 
148  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
149 
151  QModelIndex currentIndex() const;
153  void setCurrentIndex( const QModelIndex& currentIndex );
154 
156  void setLayerTreeNodeFont( int nodeType, const QFont& font );
158  QFont layerTreeNodeFont( int nodeType ) const;
159 
161  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
163  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
164 
168  void setLegendFilterByScale( double scaleDenominator );
169  double legendFilterByScale() const { return mLegendFilterByScale; }
170 
175  void setLegendFilterByMap( const QgsMapSettings* settings );
176 
183  void setLegendFilter( const QgsMapSettings* settings, bool useExtent = true, const QgsGeometry& polygon = QgsGeometry(), bool useExpressions = true );
184 
187  Q_DECL_DEPRECATED const QgsMapSettings* legendFilterByMap() const { return mLegendFilterMapSettings.data(); }
188 
191  const QgsMapSettings* legendFilterMapSettings() const { return mLegendFilterMapSettings.data(); }
192 
196  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
200  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale );
201 
204  QMap<QString, QString> layerStyleOverrides() const;
207  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
208 
211  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
214  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
216  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
218  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
220  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
221 
222  signals:
223 
224  protected slots:
225  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
226  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
227  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
228  void nodeRemovedChildren();
229 
230  void nodeVisibilityChanged( QgsLayerTreeNode* node );
231 
232  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
233 
234  void nodeLayerLoaded();
235  void nodeLayerWillBeUnloaded();
236  void layerLegendChanged();
237 
238  void layerNeedsUpdate();
239 
240  void legendNodeDataChanged();
241 
242  void invalidateLegendMapBasedData();
243 
244  protected:
245  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
246  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
247 
248  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
249  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
250 
251  void connectToLayers( QgsLayerTreeGroup* parentGroup );
252  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
253  void connectToRootNode();
254  void disconnectFromRootNode();
255 
257  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
258 
263  void refreshScaleBasedLayers( const QModelIndex& index = QModelIndex() );
264 
265  static const QIcon& iconGroup();
266 
269 
270  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
271 
272  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
273  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
274  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
275  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
276  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
277  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
278  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
279  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
281  QgsLayerTreeModelLegendNode* legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
282  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
283  void legendCleanup();
284  void legendInvalidateMapBasedData();
285 
286  protected:
290  Flags mFlags;
295 
303  {
308  };
309 
313  {
322  };
323 
325  void tryBuildLegendTree( LayerLegendData& data );
326 
330 
333 
336 
339 
342 
345 
350 };
351 
352 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
353 
354 #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
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const=0
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
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
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.
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Class that runs a hit test with given map settings.
Definition: qgsmaphittest.h:34
Q_DECL_DEPRECATED const QgsMapSettings * legendFilterByMap() const
Returns the current map settings used for legend filtering.
Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const
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) ...
virtual bool removeRows(int row, int count, const QModelIndex &parent)
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
typedef DropActions
double legendFilterByScale() const
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.
Structure that stores all data associated with one map layer.
typedef ItemFlags