QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 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;
58  int columnCount( const QModelIndex &parent = QModelIndex() ) const;
59  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
60  QModelIndex parent( const QModelIndex &child ) const;
61  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
62  Qt::ItemFlags flags( const QModelIndex &index ) const;
63  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
64  Qt::DropActions supportedDropActions() const;
65  QStringList mimeTypes() const;
66  QMimeData* mimeData( const QModelIndexList& indexes ) const;
67  bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent );
68  bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() );
69 
70  // New stuff
71 
72  enum Flag
73  {
74  // display flags
75  ShowLegend = 0x0001,
76  ShowSymbology = 0x0001,
77  ShowRasterPreviewIcon = 0x0002,
78 
79  // behavioral flags
80  AllowNodeReorder = 0x1000,
81  AllowNodeRename = 0x2000,
82  AllowNodeChangeVisibility = 0x4000,
83  AllowLegendChangeState = 0x8000,
84  AllowSymbologyChangeState = 0x8000,
85  };
86  Q_DECLARE_FLAGS( Flags, Flag )
87 
88 
89  void setFlags( Flags f );
91  void setFlag( Flag f, bool on = true );
93  Flags flags() const;
95  bool testFlag( Flag f ) const;
96 
99  QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
101  QModelIndex node2index( QgsLayerTreeNode* node ) const;
105  QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
106 
109  static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
113  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );
114 
117  QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );
118 
120  QgsLayerTreeGroup* rootGroup() const;
123  void setRootGroup( QgsLayerTreeGroup* newRootGroup );
124 
127  void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );
128 
130  QModelIndex currentIndex() const;
132  void setCurrentIndex( const QModelIndex& currentIndex );
133 
135  void setLayerTreeNodeFont( int nodeType, const QFont& font );
137  QFont layerTreeNodeFont( int nodeType ) const;
138 
140  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
142  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
143 
147  void setLegendFilterByScale( double scaleDenominator );
148  double legendFilterByScale() const { return mLegendFilterByScale; }
149 
154  void setLegendFilterByMap( const QgsMapSettings* settings );
155  const QgsMapSettings* legendFilterByMap() const { return mLegendFilterByMapSettings.data(); }
156 
160  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
164  void legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale );
165 
168  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
171  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
173  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
175  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
177  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
178 
179  signals:
180 
181  protected slots:
182  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
183  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
184  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
185  void nodeRemovedChildren();
186 
187  void nodeVisibilityChanged( QgsLayerTreeNode* node );
188 
189  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
190 
191  void nodeLayerLoaded();
192  void nodeLayerWillBeUnloaded();
193  void layerLegendChanged();
194 
195  void layerNeedsUpdate();
196 
197  void legendNodeDataChanged();
198 
199  protected:
200  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
201  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
202 
203  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
204  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
205 
206  void connectToLayers( QgsLayerTreeGroup* parentGroup );
207  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
208  void connectToRootNode();
209  void disconnectFromRootNode();
210 
212  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
213 
214  static const QIcon& iconGroup();
215 
217  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
218 
219  protected:
223  Flags mFlags;
226  QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelLegendNode*> > mLegendNodes;
229  QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelLegendNode*> > mOriginalLegendNodes;
231  QPersistentModelIndex mCurrentIndex;
234 
235  QFont mFontLayer;
236  QFont mFontGroup;
237 
240 
241  QScopedPointer<QgsMapSettings> mLegendFilterByMapSettings;
242  QScopedPointer<QgsMapHitTest> mLegendFilterByMapHitTest;
243 
247 };
248 
249 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
250 
251 #endif // QGSLAYERTREEMODEL_H