QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
126  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer, bool skipNodeEmbeddedInParent = false );
127 
131  QList<QgsLayerTreeModelLegendNode*> layerOriginalLegendNodes( QgsLayerTreeLayer* nodeLayer );
132 
135  QgsLayerTreeModelLegendNode* legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
136 
144  QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;
145 
147  QgsLayerTreeGroup* rootGroup() const;
150  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
151 
154  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
155 
157  QModelIndex currentIndex() const;
159  void setCurrentIndex( const QModelIndex& currentIndex );
160 
162  void setLayerTreeNodeFont( int nodeType, const QFont& font );
164  QFont layerTreeNodeFont( int nodeType ) const;
165 
167  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
169  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
170 
174  void setLegendFilterByScale( double scaleDenominator );
175  double legendFilterByScale() const { return mLegendFilterByScale; }
176 
181  void setLegendFilterByMap( const QgsMapSettings* settings );
182 
189  void setLegendFilter( const QgsMapSettings* settings, bool useExtent = true, const QgsGeometry& polygon = QgsGeometry(), bool useExpressions = true );
190 
193  Q_DECL_DEPRECATED const QgsMapSettings* legendFilterByMap() const { return mLegendFilterMapSettings.data(); }
194 
197  const QgsMapSettings* legendFilterMapSettings() const { return mLegendFilterMapSettings.data(); }
198 
202  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
206  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale );
207 
210  QMap<QString, QString> layerStyleOverrides() const;
213  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
214 
217  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
220  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
222  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
224  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
226  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
227 
228  signals:
229 
230  protected slots:
231  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
232  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
233  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
234  void nodeRemovedChildren();
235 
236  void nodeVisibilityChanged( QgsLayerTreeNode* node );
239  void nodeNameChanged( QgsLayerTreeNode* node, const QString& name );
240 
241  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
242 
243  void nodeLayerLoaded();
244  void nodeLayerWillBeUnloaded();
245  void layerLegendChanged();
246 
247  void layerNeedsUpdate();
248 
249  void legendNodeDataChanged();
250 
251  void invalidateLegendMapBasedData();
252 
253  protected:
254  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
255  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
256 
257  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
258  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
259 
260  void connectToLayers( QgsLayerTreeGroup* parentGroup );
261  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
262  void connectToRootNode();
263  void disconnectFromRootNode();
264 
266  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
267 
272  void refreshScaleBasedLayers( const QModelIndex& index = QModelIndex() );
273 
274  static const QIcon& iconGroup();
275 
278 
279  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
280 
281  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
282  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
283  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
284  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
285  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
286  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
287  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
288  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
289  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
290  void legendCleanup();
291  void legendInvalidateMapBasedData();
292 
293  protected:
297  Flags mFlags;
302 
310  {
315  };
316 
320  {
322  : embeddedNodeInParent( nullptr )
323  , tree( nullptr )
324  {
325  }
326 
339  };
340 
342  LayerLegendTree* tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode*>& nodes );
343 
347 
350 
353 
356 
359 
362 
367 };
368 
369 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
370 
371 #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.
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: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)
void setAutoCollapseLegendNodes(int nodeCount)
Set at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-c...
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