QGIS API Documentation  2.8.2-Wien
 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 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  Q_DECL_DEPRECATED bool isIndexSymbologyNode( const QModelIndex& index ) const;
172  Q_DECL_DEPRECATED QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;
174  Q_DECL_DEPRECATED void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) { refreshLayerLegend( nodeLayer ); }
176  Q_DECL_DEPRECATED void setAutoCollapseSymbologyNodes( int nodeCount ) { setAutoCollapseLegendNodes( nodeCount ); }
178  Q_DECL_DEPRECATED int autoCollapseSymbologyNodes() const { return autoCollapseLegendNodes(); }
179 
180  signals:
181 
182  protected slots:
183  void nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
184  void nodeAddedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
185  void nodeWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
186  void nodeRemovedChildren();
187 
188  void nodeVisibilityChanged( QgsLayerTreeNode* node );
189 
190  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
191 
192  void nodeLayerLoaded();
193  void nodeLayerWillBeUnloaded();
194  void layerLegendChanged();
195 
196  void layerNeedsUpdate();
197 
198  void legendNodeDataChanged();
199 
200  protected:
201  void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
202  void addLegendToLayer( QgsLayerTreeLayer* nodeL );
203 
204  void connectToLayer( QgsLayerTreeLayer* nodeLayer );
205  void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
206 
207  void connectToLayers( QgsLayerTreeGroup* parentGroup );
208  void disconnectFromLayers( QgsLayerTreeGroup* parentGroup );
209  void connectToRootNode();
210  void disconnectFromRootNode();
211 
213  void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
214 
215  static const QIcon& iconGroup();
216 
218  QList<QgsLayerTreeModelLegendNode*> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode*>& nodes );
219 
220  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode* parentNode ) const;
221 
222  int legendRootRowCount( QgsLayerTreeLayer* nL ) const;
223  int legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) const;
224  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer* nL ) const;
225  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode* node ) const;
226  QModelIndex legendParent( QgsLayerTreeModelLegendNode* legendNode ) const;
227  QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
228  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
229  bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
230  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
231  void legendCleanup();
232  void legendInvalidateMapBasedData();
233 
234  protected:
238  Flags mFlags;
240  QPersistentModelIndex mCurrentIndex;
243 
250  {
252  QMap<QgsLayerTreeModelLegendNode*, QgsLayerTreeModelLegendNode*> parents;
254  QMap<QgsLayerTreeModelLegendNode*, QList<QgsLayerTreeModelLegendNode*> > children;
255  };
256 
259  {
262  QList<QgsLayerTreeModelLegendNode*> activeNodes;
265  QList<QgsLayerTreeModelLegendNode*> originalNodes;
268  };
269 
270  void tryBuildLegendTree( LayerLegendData& data );
271 
273  QMap<QgsLayerTreeLayer*, LayerLegendData> mLegend;
274 
275  QFont mFontLayer;
276  QFont mFontGroup;
277 
280 
281  QScopedPointer<QgsMapSettings> mLegendFilterByMapSettings;
282  QScopedPointer<QgsMapHitTest> mLegendFilterByMapHitTest;
283 
287 };
288 
289 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
290 
291 #endif // QGSLAYERTREEMODEL_H