QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
193  void setLegendFilterByScale( double scale );
194 
202  double legendFilterByScale() const { return mLegendFilterByScale; }
203 
208  void setLegendFilterByMap( const QgsMapSettings *settings );
209 
216  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
217 
220  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
221 
225  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
229  void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
230 
233  QMap<QString, QString> layerStyleOverrides() const;
236  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
237 
238  protected slots:
239  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
240  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
241  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
242  void nodeRemovedChildren();
243 
244  void nodeVisibilityChanged( QgsLayerTreeNode *node );
247  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
248 
249  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
250 
251  void nodeLayerLoaded();
252  void nodeLayerWillBeUnloaded();
253  void layerLegendChanged();
254 
255  void layerNeedsUpdate();
256 
257  void legendNodeDataChanged();
258 
259  void invalidateLegendMapBasedData();
260 
261  protected:
262  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
263  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
264 
265  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
266  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
267 
268  void connectToLayers( QgsLayerTreeGroup *parentGroup );
269  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
270  void connectToRootNode();
271  void disconnectFromRootNode();
272 
274  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
275 
280  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
281 
282  static QIcon iconGroup();
283 
285  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
286 
287  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
288 
289  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
290  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
291  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
292  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
293  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
294  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
295  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
296  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
297  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
298  void legendCleanup();
299  void legendInvalidateMapBasedData();
300 
301  protected:
303  QgsLayerTree *mRootNode = nullptr;
305  Flags mFlags;
307  QPersistentModelIndex mCurrentIndex;
310 
317 #ifndef SIP_RUN
319  {
321  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
323  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
324  };
325 #endif
326 
329 #ifndef SIP_RUN
331  {
333  : embeddedNodeInParent( nullptr )
334  , tree( nullptr )
335  {
336  }
337 
340  QList<QgsLayerTreeModelLegendNode *> activeNodes;
344  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
347  QList<QgsLayerTreeModelLegendNode *> originalNodes;
349  LayerLegendTree *tree = nullptr;
350  };
351 #endif
352 
354  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
355 
358  QMap<QString, QString> mLayerStyleOverrides;
359 
361  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
362 
363  QFont mFontLayer;
364  QFont mFontGroup;
365 
368 
369  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
370  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
371 
374 
379 
380  private:
381 
383  QgsRenderContext *createTemporaryRenderContext() const;
384 };
385 
386 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
387 
388 #endif // QGSLAYERTREEMODEL_H
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:54
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Structure that stores tree representation of map layer&#39;s legend.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:96
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.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QTimer mDeferLegendInvalidationTimer
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:31
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_END
Definition: qgis_sip.h:175
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:36
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.
double legendFilterByScale() const
Returns the scale which restricts the legend nodes which are visible.
#define SIP_OUT
Definition: qgis_sip.h:51
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
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&#39; styles: key = layer ID, value = style XML.
Layer tree node points to a map layer.
Structure that stores all data associated with one map layer.