QGIS API Documentation  2.12.0-Lyon
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 class QgsLayerTreeNode;
25 class QgsLayerTreeGroup;
26 class QgsLayerTreeLayer;
28 class QgsMapHitTest;
29 class QgsMapLayer;
30 class QgsMapSettings;
31 
46 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
47 {
48  Q_OBJECT
49  public:
52  explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = 0 );
54 
55  // Implementation of virtual functions from QAbstractItemModel
56 
57  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
58  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
59  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
60  QModelIndex parent( const QModelIndex &child ) const override;
61  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
62  Qt::ItemFlags flags( const QModelIndex &index ) const override;
63  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
64  Qt::DropActions supportedDropActions() const override;
65  QStringList mimeTypes() const override;
66  QMimeData* mimeData( const QModelIndexList& indexes ) const override;
67  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent ) override;
68  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() ) override;
69 
70  // New stuff
71 
72  enum Flag
73  {
74  // display flags
75  ShowLegend = 0x0001,
76  ShowSymbology = 0x0001,
77  ShowRasterPreviewIcon = 0x0002,
78  ShowLegendAsTree = 0x0004,
79  DeferredLegendInvalidation = 0x0008,
80 
81  // behavioral flags
82  AllowNodeReorder = 0x1000,
83  AllowNodeRename = 0x2000,
84  AllowNodeChangeVisibility = 0x4000,
85  AllowLegendChangeState = 0x8000,
86  AllowSymbologyChangeState = 0x8000,
87  };
88  Q_DECLARE_FLAGS( Flags, Flag )
89 
90 
91  void setFlags( const QgsLayerTreeModel::Flags& f );
93  void setFlag( Flag f, bool on = true );
95  Flags flags() const;
97  bool testFlag( Flag f ) const;
98 
101  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
103  QModelIndex node2index( QgsLayerTreeNode* node ) const;
107  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
108 
111  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
115  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
116 
119  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );
120 
122  QgsLayerTreeGroup* rootGroup() const;
125  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
126 
129  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
130 
132  QModelIndex currentIndex() const;
134  void setCurrentIndex( const QModelIndex& currentIndex );
135 
137  void setLayerTreeNodeFont( int nodeType, const QFont& font );
139  QFont layerTreeNodeFont( int nodeType ) const;
140 
142  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
144  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
145 
149  void setLegendFilterByScale( double scaleDenominator );
150  double legendFilterByScale() const { return mLegendFilterByScale; }
151 
156  void setLegendFilterByMap( const QgsMapSettings* settings );
157  const QgsMapSettings* legendFilterByMap() const { return mLegendFilterByMapSettings.data(); }
158 
162  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
166  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale );
167 
170  QMap<QString, QString> layerStyleOverrides() const;
173  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
174 
177  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
180  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
182  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
184  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
186  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
187 
188  signals:
189 
190  protected slots:
191  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
192  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
193  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
194  void nodeRemovedChildren();
195 
196  void nodeVisibilityChanged( QgsLayerTreeNode* node );
197 
198  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
199 
200  void nodeLayerLoaded();
201  void nodeLayerWillBeUnloaded();
202  void layerLegendChanged();
203 
204  void layerNeedsUpdate();
205 
206  void legendNodeDataChanged();
207 
208  void invalidateLegendMapBasedData();
209 
210  protected:
211  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
212  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
213 
214  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
215  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
216 
217  void connectToLayers( QgsLayerTreeGroup* parentGroup );
218  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
219  void connectToRootNode();
220  void disconnectFromRootNode();
221 
223  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
224 
225  static const QIcon& iconGroup();
226 
229 
230  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
231 
232  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
233  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
234  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
235  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
236  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
237  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
238  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
239  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
240  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
241  void legendCleanup();
242  void legendInvalidateMapBasedData();
243 
244  protected:
248  Flags mFlags;
253 
260  {
265  };
266 
269  {
278  };
279 
280  void tryBuildLegendTree( LayerLegendData& data );
281 
285 
288 
291 
294 
297 
302 };
303 
304 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
305 
306 #endif // QGSLAYERTREEMODEL_H
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
double legendFilterByScale() const
LayerLegendTree * tree
Optional pointer to a tree structure - see LayerLegendTree for details.
Structure that stores tree representation of map layer's legend.
QScopedPointer< QgsMapSettings > mLegendFilterByMapSettings
QScopedPointer< QgsMapHitTest > mLegendFilterByMapHitTest
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:18
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 * legendFilterByMap() const
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
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
QObject * parent() const
Q_DECL_DEPRECATED void refreshLayerSymbology(QgsLayerTreeLayer *nodeLayer)
Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes(int nodeCount)
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer'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