QGIS API Documentation  2.99.0-Master (19b062c)
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  ShowLegendAsTree = 0x0004,
100  DeferredLegendInvalidation = 0x0008,
101  UseEmbeddedWidgets = 0x0010,
102 
103  // behavioral flags
104  AllowNodeReorder = 0x1000,
105  AllowNodeRename = 0x2000,
106  AllowNodeChangeVisibility = 0x4000,
107  AllowLegendChangeState = 0x8000,
108  ActionHierarchical = 0x10000,
109  };
110  Q_DECLARE_FLAGS( Flags, Flag )
111 
112 
113  void setFlags( QgsLayerTreeModel::Flags f );
115  void setFlag( Flag f, bool on = true );
117  Flags flags() const;
119  bool testFlag( Flag f ) const;
120 
125  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
127  QModelIndex node2index( QgsLayerTreeNode *node ) const;
128 
134  QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
135 
140  static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
141 
147  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
148 
156  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
157 
163  QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
164 
169  QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
170 
179  QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
180 
182  QgsLayerTree *rootGroup() const;
183 
188  void setRootGroup( QgsLayerTree *newRootGroup );
189 
194  void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
195 
197  QModelIndex currentIndex() const;
199  void setCurrentIndex( const QModelIndex &currentIndex );
200 
202  void setLayerTreeNodeFont( int nodeType, const QFont &font );
204  QFont layerTreeNodeFont( int nodeType ) const;
205 
207  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
209  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
210 
218  void setLegendFilterByScale( double scale );
219 
227  double legendFilterByScale() const { return mLegendFilterByScale; }
228 
235  void setLegendFilterByMap( const QgsMapSettings *settings );
236 
245  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
246 
251  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
252 
258  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
259 
265  void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
266 
271  QMap<QString, QString> layerStyleOverrides() const;
272 
277  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
278 
279  protected slots:
280  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
281  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
282  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
283  void nodeRemovedChildren();
284 
285  void nodeVisibilityChanged( QgsLayerTreeNode *node );
286 
291  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
292 
293  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
294 
295  void nodeLayerLoaded();
296  void nodeLayerWillBeUnloaded();
297  void layerLegendChanged();
298 
299  void layerNeedsUpdate();
300 
301  void legendNodeDataChanged();
302 
303  void invalidateLegendMapBasedData();
304 
305  protected:
306  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
307  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
308 
309  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
310  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
311 
312  void connectToLayers( QgsLayerTreeGroup *parentGroup );
313  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
314  void connectToRootNode();
315  void disconnectFromRootNode();
316 
318  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
319 
325  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
326 
327  static QIcon iconGroup();
328 
330  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
331 
332  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
333 
334  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
335  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
336  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
337  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
338  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
339  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
340  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
341  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
342  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
343  void legendCleanup();
344  void legendInvalidateMapBasedData();
345 
346  protected:
348  QgsLayerTree *mRootNode = nullptr;
350  Flags mFlags;
352  QPersistentModelIndex mCurrentIndex;
355 
364 #ifndef SIP_RUN
366  {
368  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
370  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
371  };
372 #endif
373 
378 #ifndef SIP_RUN
380  {
381  LayerLegendData() = default;
382 
387  QList<QgsLayerTreeModelLegendNode *> activeNodes;
388 
394  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
395 
400  QList<QgsLayerTreeModelLegendNode *> originalNodes;
402  LayerLegendTree *tree = nullptr;
403  };
404 #endif
405 
407  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
408 
413  QMap<QString, QString> mLayerStyleOverrides;
414 
416  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
417 
418  QFont mFontLayer;
419  QFont mFontGroup;
420 
423 
424  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
425  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
426 
429 
434 
435  private:
436 
438  QgsRenderContext *createTemporaryRenderContext() const;
439 };
440 
441 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
442 
443 #ifndef SIP_RUN
445 
451 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
452 {
453  Q_OBJECT
454 
455  public:
456  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
457  : QgsLayerTreeModelLegendNode( nodeL )
458  {
459  // we need a valid rule key to allow the model to build a tree out of legend nodes
460  // if that's possible (if there is a node without a rule key, building of tree is canceled)
461  mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
462  }
463 
464  QVariant data( int role ) const override
465  {
466  if ( role == RuleKeyRole )
467  return mRuleKey;
468  return QVariant();
469  }
470 
471  private:
472  QString mRuleKey;
473 };
474 #endif
475 
477 
478 #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:111
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.