QGIS API Documentation  2.99.0-Master (3450a9f)
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 "qgis_core.h"
20 #include <QAbstractItemModel>
21 #include <QFont>
22 #include <QIcon>
23 #include <QTimer>
24 #include <memory>
25 
26 #include "qgsgeometry.h"
27 #include "qgsmaplayer.h"
28 
29 class QgsLayerTreeNode;
30 class QgsLayerTreeGroup;
31 class QgsLayerTreeLayer;
33 class QgsMapHitTest;
34 class QgsMapLayer;
35 class QgsMapSettings;
36 class QgsExpression;
37 class QgsRenderContext;
38 class QgsLayerTree;
39 
54 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
55 {
56 
57 #ifdef SIP_RUN
59  if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
60  sipType = sipType_QgsLayerTreeModel;
61  else
62  sipType = 0;
63  SIP_END
64 #endif
65 
66  Q_OBJECT
67  public:
70  explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
71 
73 
74  // Implementation of virtual functions from QAbstractItemModel
75 
76  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
77  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
78  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
79  QModelIndex parent( const QModelIndex &child ) const override;
80  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
81  Qt::ItemFlags flags( const QModelIndex &index ) const override;
82  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
83  Qt::DropActions supportedDropActions() const override;
84  QStringList mimeTypes() const override;
85  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
86  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
87  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
88 
89  // New stuff
90 
91  enum Flag
92  {
93  // display flags
94  ShowLegend = 0x0001,
95  ShowRasterPreviewIcon = 0x0002,
96  ShowLegendAsTree = 0x0004,
97  DeferredLegendInvalidation = 0x0008,
98  UseEmbeddedWidgets = 0x0010,
99 
100  // behavioral flags
101  AllowNodeReorder = 0x1000,
102  AllowNodeRename = 0x2000,
103  AllowNodeChangeVisibility = 0x4000,
104  AllowLegendChangeState = 0x8000,
105  ActionHierarchical = 0x10000,
106  };
107  Q_DECLARE_FLAGS( Flags, Flag )
108 
109 
110  void setFlags( QgsLayerTreeModel::Flags f );
112  void setFlag( Flag f, bool on = true );
114  Flags flags() const;
116  bool testFlag( Flag f ) const;
117 
120  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
122  QModelIndex node2index( QgsLayerTreeNode *node ) const;
126  QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
127 
130  static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
134  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
135 
141  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
142 
146  QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
147 
150  QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
151 
159  QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
160 
162  QgsLayerTree *rootGroup() const;
165  void setRootGroup( QgsLayerTree *newRootGroup );
166 
169  void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
170 
172  QModelIndex currentIndex() const;
174  void setCurrentIndex( const QModelIndex &currentIndex );
175 
177  void setLayerTreeNodeFont( int nodeType, const QFont &font );
179  QFont layerTreeNodeFont( int nodeType ) const;
180 
182  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
184  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
185 
189  void setLegendFilterByScale( double scaleDenominator );
190  double legendFilterByScale() const { return mLegendFilterByScale; }
191 
196  void setLegendFilterByMap( const QgsMapSettings *settings );
197 
204  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
205 
208  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
209 
213  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
217  void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
218 
221  QMap<QString, QString> layerStyleOverrides() const;
224  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
225 
226  protected slots:
227  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
228  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
229  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
230  void nodeRemovedChildren();
231 
232  void nodeVisibilityChanged( QgsLayerTreeNode *node );
235  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
236 
237  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
238 
239  void nodeLayerLoaded();
240  void nodeLayerWillBeUnloaded();
241  void layerLegendChanged();
242 
243  void layerNeedsUpdate();
244 
245  void legendNodeDataChanged();
246 
247  void invalidateLegendMapBasedData();
248 
249  protected:
250  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
251  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
252 
253  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
254  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
255 
256  void connectToLayers( QgsLayerTreeGroup *parentGroup );
257  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
258  void connectToRootNode();
259  void disconnectFromRootNode();
260 
262  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
263 
268  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
269 
270  static QIcon iconGroup();
271 
273  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
274 
275  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
276 
277  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
278  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
279  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
280  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
281  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
282  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
283  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
284  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
285  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
286  void legendCleanup();
287  void legendInvalidateMapBasedData();
288 
289  protected:
291  QgsLayerTree *mRootNode = nullptr;
293  Flags mFlags;
295  QPersistentModelIndex mCurrentIndex;
298 
305 #ifndef SIP_RUN
307  {
309  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
311  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
312  };
313 #endif
314 
317 #ifndef SIP_RUN
319  {
321  : embeddedNodeInParent( nullptr )
322  , tree( nullptr )
323  {
324  }
325 
328  QList<QgsLayerTreeModelLegendNode *> activeNodes;
332  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
335  QList<QgsLayerTreeModelLegendNode *> originalNodes;
337  LayerLegendTree *tree = nullptr;
338  };
339 #endif
340 
342  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
343 
346  QMap<QString, QString> mLayerStyleOverrides;
347 
349  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
350 
351  QFont mFontLayer;
352  QFont mFontGroup;
353 
356 
357  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
358  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
359 
362 
367 
368  private:
369 
371  QgsRenderContext *createTemporaryRenderContext() const;
372 };
373 
374 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
375 
376 #endif // QGSLAYERTREEMODEL_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Layer tree group node serves as a container for layers and further groups.
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer&#39;s legend nodes.
Base class for all map layer types.
Definition: qgsmaplayer.h:53
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
Structure that stores tree representation of map layer&#39;s legend.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Set of flags for the model.
std::unique_ptr< QgsMapSettings > mLegendFilterMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis.h:498
The QgsLayerTreeModel class is model implementation for Qt item views framework.
#define SIP_OUT
Definition: qgis.h:414
QTimer mDeferLegendInvalidationTimer
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:31
This class is a base class for nodes in a layer tree.
int autoCollapseLegendNodes() const
Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (...
Class that runs a hit test with given map settings.
Definition: qgsmaphittest.h:35
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have null parent. Pointers are not owned...
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or null if none is enabled) ...
void setAutoCollapseLegendNodes(int nodeCount)
Set at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-c...
Contains information about the context of a rendering operation.
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
#define SIP_SKIP
Definition: qgis.h:457
double legendFilterByScale() const
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
#define SIP_TRANSFERTHIS
Definition: qgis.h:409
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers&#39; styles: key = layer ID, value = style XML.
#define SIP_END
Definition: qgis.h:503
Layer tree node points to a map layer.
Structure that stores all data associated with one map layer.