QGIS API Documentation  2.99.0-Master (ae4d26a)
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"
29 
30 class QgsLayerTreeNode;
31 class QgsLayerTreeGroup;
32 class QgsLayerTreeLayer;
33 class QgsMapHitTest;
34 class QgsMapLayer;
35 class QgsMapSettings;
36 class QgsExpression;
37 class QgsRenderContext;
38 class QgsLayerTree;
39 
55 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
56 {
57 
58 #ifdef SIP_RUN
60  if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
61  sipType = sipType_QgsLayerTreeModel;
62  else
63  sipType = 0;
64  SIP_END
65 #endif
66 
67  Q_OBJECT
68  public:
69 
74  explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
75 
77 
78  // Implementation of virtual functions from QAbstractItemModel
79 
80  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
81  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
82  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
83  QModelIndex parent( const QModelIndex &child ) const override;
84  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
85  Qt::ItemFlags flags( const QModelIndex &index ) const override;
86  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
87  Qt::DropActions supportedDropActions() const override;
88  QStringList mimeTypes() const override;
89  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
90  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
91  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
92 
93  // New stuff
94 
95  enum Flag
96  {
97  // display flags
98  ShowLegend = 0x0001,
99  ShowRasterPreviewIcon = 0x0002,
100  ShowLegendAsTree = 0x0004,
101  DeferredLegendInvalidation = 0x0008,
102  UseEmbeddedWidgets = 0x0010,
103 
104  // behavioral flags
105  AllowNodeReorder = 0x1000,
106  AllowNodeRename = 0x2000,
107  AllowNodeChangeVisibility = 0x4000,
108  AllowLegendChangeState = 0x8000,
109  ActionHierarchical = 0x10000,
110  };
111  Q_DECLARE_FLAGS( Flags, Flag )
112 
113 
114  void setFlags( QgsLayerTreeModel::Flags f );
116  void setFlag( Flag f, bool on = true );
118  Flags flags() const;
120  bool testFlag( Flag f ) const;
121 
126  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
128  QModelIndex node2index( QgsLayerTreeNode *node ) const;
129 
135  QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
136 
141  static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
142 
148  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
149 
157  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
158 
164  QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
165 
170  QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
171 
180  QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
181 
183  QgsLayerTree *rootGroup() const;
184 
189  void setRootGroup( QgsLayerTree *newRootGroup );
190 
195  void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
196 
198  QModelIndex currentIndex() const;
200  void setCurrentIndex( const QModelIndex &currentIndex );
201 
203  void setLayerTreeNodeFont( int nodeType, const QFont &font );
205  QFont layerTreeNodeFont( int nodeType ) const;
206 
208  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
210  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
211 
219  void setLegendFilterByScale( double scale );
220 
228  double legendFilterByScale() const { return mLegendFilterByScale; }
229 
236  void setLegendFilterByMap( const QgsMapSettings *settings );
237 
246  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
247 
252  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
253 
259  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
260 
266  void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
267 
272  QMap<QString, QString> layerStyleOverrides() const;
273 
278  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
279 
280  protected slots:
281  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
282  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
283  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
284  void nodeRemovedChildren();
285 
286  void nodeVisibilityChanged( QgsLayerTreeNode *node );
287 
292  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
293 
294  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
295 
296  void nodeLayerLoaded();
297  void nodeLayerWillBeUnloaded();
298  void layerLegendChanged();
299 
300  void layerNeedsUpdate();
301 
302  void legendNodeDataChanged();
303 
304  void invalidateLegendMapBasedData();
305 
306  protected:
307  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
308  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
309 
310  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
311  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
312 
313  void connectToLayers( QgsLayerTreeGroup *parentGroup );
314  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
315  void connectToRootNode();
316  void disconnectFromRootNode();
317 
319  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
320 
326  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
327 
328  static QIcon iconGroup();
329 
331  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
332 
333  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
334 
335  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
336  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
337  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
338  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
339  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
340  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
341  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
342  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
343  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
344  void legendCleanup();
345  void legendInvalidateMapBasedData();
346 
347  protected:
349  QgsLayerTree *mRootNode = nullptr;
351  Flags mFlags;
353  QPersistentModelIndex mCurrentIndex;
356 
365 #ifndef SIP_RUN
367  {
369  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
371  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
372  };
373 #endif
374 
379 #ifndef SIP_RUN
381  {
382  LayerLegendData() = default;
383 
388  QList<QgsLayerTreeModelLegendNode *> activeNodes;
389 
395  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
396 
401  QList<QgsLayerTreeModelLegendNode *> originalNodes;
403  LayerLegendTree *tree = nullptr;
404  };
405 #endif
406 
408  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
409 
414  QMap<QString, QString> mLayerStyleOverrides;
415 
417  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
418 
419  QFont mFontLayer;
420  QFont mFontGroup;
421 
424 
425  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
426  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
427 
430 
435 
436  private:
437 
439  QgsRenderContext *createTemporaryRenderContext() const;
440 };
441 
442 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
443 
444 #ifndef SIP_RUN
446 
452 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
453 {
454  Q_OBJECT
455 
456  public:
457  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
458  : QgsLayerTreeModelLegendNode( nodeL )
459  {
460  // we need a valid rule key to allow the model to build a tree out of legend nodes
461  // if that's possible (if there is a node without a rule key, building of tree is canceled)
462  mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
463  }
464 
465  QVariant data( int role ) const override
466  {
467  if ( role == RuleKeyRole )
468  return mRuleKey;
469  return QVariant();
470  }
471 
472  private:
473  QString mRuleKey;
474 };
475 #endif
476 
478 
479 #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:56
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:94
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:32
#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:37
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.