QGIS API Documentation  2.9.0-Master
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 
23 class QgsLayerTreeNode;
24 class QgsLayerTreeGroup;
25 class QgsLayerTreeLayer;
27 class QgsMapHitTest;
28 class QgsMapLayer;
29 class QgsMapSettings;
30 
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 
80  // behavioral flags
81  AllowNodeReorder = 0x1000,
82  AllowNodeRename = 0x2000,
83  AllowNodeChangeVisibility = 0x4000,
84  AllowLegendChangeState = 0x8000,
85  AllowSymbologyChangeState = 0x8000,
86  };
87  Q_DECLARE_FLAGS( Flags, Flag )
88 
89 
90  void setFlags( Flags f );
92  void setFlag( Flag f, bool on = true );
94  Flags flags() const;
96  bool testFlag( Flag f ) const;
97 
100  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
102  QModelIndex node2index( QgsLayerTreeNode* node ) const;
106  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
107 
110  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
114  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
115 
118  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );
119 
121  QgsLayerTreeGroup* rootGroup() const;
124  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
125 
128  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
129 
131  QModelIndex currentIndex() const;
133  void setCurrentIndex( const QModelIndex& currentIndex );
134 
136  void setLayerTreeNodeFont( int nodeType, const QFont& font );
138  QFont layerTreeNodeFont( int nodeType ) const;
139 
141  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
143  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
144 
148  void setLegendFilterByScale( double scaleDenominator );
149  double legendFilterByScale() const { return mLegendFilterByScale; }
150 
155  void setLegendFilterByMap( const QgsMapSettings* settings );
156  const QgsMapSettings* legendFilterByMap() const { return mLegendFilterByMapSettings.data(); }
157 
161  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
165  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale );
166 
169  QMap<QString, QString> layerStyleOverrides() const;
172  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
173 
176  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
179  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
181  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
183  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
185  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
186 
187  signals:
188 
189  protected slots:
190  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
191  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
192  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
193  void nodeRemovedChildren();
194 
195  void nodeVisibilityChanged( QgsLayerTreeNode* node );
196 
197  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
198 
199  void nodeLayerLoaded();
200  void nodeLayerWillBeUnloaded();
201  void layerLegendChanged();
202 
203  void layerNeedsUpdate();
204 
205  void legendNodeDataChanged();
206 
207  protected:
208  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
209  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
210 
211  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
212  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
213 
214  void connectToLayers( QgsLayerTreeGroup* parentGroup );
215  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
216  void connectToRootNode();
217  void disconnectFromRootNode();
218 
220  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
221 
222  static const QIcon& iconGroup();
223 
225  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
226 
227  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
228 
229  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
230  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
231  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
232  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
233  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
234  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
235  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
236  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
237  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
238  void legendCleanup();
239  void legendInvalidateMapBasedData();
240 
241  protected:
245  Flags mFlags;
247  QPersistentModelIndex mCurrentIndex;
250 
257  {
259  QMap<QgsLayerTreeModelLegendNode*, QgsLayerTreeModelLegendNode*> parents;
261  QMap<QgsLayerTreeModelLegendNode*, QList<QgsLayerTreeModelLegendNode*> > children;
262  };
263 
266  {
269  QList<QgsLayerTreeModelLegendNode*> activeNodes;
272  QList<QgsLayerTreeModelLegendNode*> originalNodes;
275  };
276 
277  void tryBuildLegendTree( LayerLegendData& data );
278 
281  QMap<QString, QString> mLayerStyleOverrides;
282 
284  QMap<QgsLayerTreeLayer*, LayerLegendData> mLegend;
285 
286  QFont mFontLayer;
287  QFont mFontGroup;
288 
291 
292  QScopedPointer<QgsMapSettings> mLegendFilterByMapSettings;
293  QScopedPointer<QgsMapHitTest> mLegendFilterByMapHitTest;
294 
298 };
299 
300 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
301 
302 #endif // QGSLAYERTREEMODEL_H
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:49
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.
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...
int autoCollapseLegendNodes() const
Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (...
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.
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:18
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
const QgsMapSettings * legendFilterByMap() const
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
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.