QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 QgsMapLayer;
28 
29 
44 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
45 {
46  Q_OBJECT
47  public:
50  explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = 0 );
52 
53  // Implementation of virtual functions from QAbstractItemModel
54 
55  int rowCount( const QModelIndex &parent = QModelIndex() ) const;
56  int columnCount( const QModelIndex &parent = QModelIndex() ) const;
57  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
58  QModelIndex parent( const QModelIndex &child ) const;
59  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
60  Qt::ItemFlags flags( const QModelIndex &index ) const;
61  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
62  Qt::DropActions supportedDropActions() const;
63  QStringList mimeTypes() const;
64  QMimeData* mimeData( const QModelIndexList& indexes ) const;
65  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent );
66  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() );
67 
68  // New stuff
69 
70  enum Flag
71  {
72  // display flags
73  ShowLegend = 0x0001,
74  ShowSymbology = 0x0001,
75  ShowRasterPreviewIcon = 0x0002,
76 
77  // behavioral flags
78  AllowNodeReorder = 0x1000,
79  AllowNodeRename = 0x2000,
80  AllowNodeChangeVisibility = 0x4000,
81  AllowLegendChangeState = 0x8000,
82  AllowSymbologyChangeState = 0x8000,
83  };
84  Q_DECLARE_FLAGS( Flags, Flag )
85 
86 
87  void setFlags( Flags f );
89  void setFlag( Flag f, bool on = true );
91  Flags flags() const;
93  bool testFlag( Flag f ) const;
94 
97  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
99  QModelIndex node2index( QgsLayerTreeNode* node ) const;
103  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
104 
107  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
110  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
111 
114  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );
115 
117  QgsLayerTreeGroup* rootGroup();
120  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
121 
124  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
125 
127  QModelIndex currentIndex() const;
129  void setCurrentIndex( const QModelIndex& currentIndex );
130 
132  void setLayerTreeNodeFont( int nodeType, const QFont& font );
134  QFont layerTreeNodeFont( int nodeType ) const;
135 
137  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
139  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
140 
144  void setLegendFilterByScale( double scaleDenominator );
145  double legendFilterByScale() const { return mLegendFilterByScale; }
146 
149  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
152  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
154  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
156  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
158  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
159 
160  signals:
161 
162  protected slots:
163  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
164  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
165  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
166  void nodeRemovedChildren();
167 
168  void nodeVisibilityChanged( QgsLayerTreeNode* node );
169 
170  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
171 
172  void nodeLayerLoaded();
173  void nodeLayerWillBeUnloaded();
174  void layerLegendChanged();
175 
176  void layerNeedsUpdate();
177 
178  void legendNodeDataChanged();
179 
180  protected:
181  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
182  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
183 
184  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
185  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
186 
187  void connectToLayers( QgsLayerTreeGroup* parentGroup );
188  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
189  void connectToRootNode();
190  void disconnectFromRootNode();
191 
193  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
194 
195  static const QIcon& iconGroup();
196 
198  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
199 
200  protected:
204  Flags mFlags;
207  QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelLegendNode*> > mLegendNodes;
210  QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelLegendNode*> > mOriginalLegendNodes;
212  QPersistentModelIndex mCurrentIndex;
215 
216  QFont mFontLayer;
217  QFont mFontGroup;
218 
221 };
222 
223 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
224 
225 #endif // QGSLAYERTREEMODEL_H
Layer tree group node serves as a container for layers and further groups.
Definition: qgslayertreegroup.h:31
static unsigned index
Definition: mersenne-twister.cpp:36
Base class for all map layer types.
Definition: qgsmaplayer.h:48
double legendFilterByScale() const
Definition: qgslayertreemodel.h:145
QFont mFontGroup
Definition: qgslayertreemodel.h:217
Flags mFlags
Set of flags for the model.
Definition: qgslayertreemodel.h:204
QFont mFontLayer
Definition: qgslayertreemodel.h:216
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Definition: qgslayertreemodel.h:44
int autoCollapseLegendNodes() const
Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (...
Definition: qgslayertreemodel.h:139
This class is a base class for nodes in a layer tree.
Definition: qgslayertreenode.h:63
QgsLayerTreeGroup * mRootNode
Pointer to the root node of the layer tree. Not owned by the model.
Definition: qgslayertreemodel.h:202
Flag
Definition: qgslayertreemodel.h:70
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
Definition: qgslayertreemodel.h:212
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
Definition: qgslayertreemodel.h:220
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
Definition: qgslayertreemodel.h:214
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Definition: qgslayertreemodellegendnode.h:36
QMap< QgsLayerTreeLayer *, QList< QgsLayerTreeModelLegendNode * > > mLegendNodes
Active legend nodes for each layer node.
Definition: qgslayertreemodel.h:207
Q_DECL_DEPRECATED void refreshLayerSymbology(QgsLayerTreeLayer *nodeLayer)
Definition: qgslayertreemodel.h:154
Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes(int nodeCount)
Definition: qgslayertreemodel.h:156
QMap< QgsLayerTreeLayer *, QList< QgsLayerTreeModelLegendNode * > > mOriginalLegendNodes
Data structure for storage of legend nodes for each layer.
Definition: qgslayertreemodel.h:210
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:41
Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const
Definition: qgslayertreemodel.h:158