QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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"
28 
29 class QgsLayerTreeNode;
30 class QgsLayerTreeGroup;
31 class QgsLayerTreeLayer;
32 class QgsMapHitTest;
33 class QgsMapSettings;
34 class QgsExpression;
35 class QgsRenderContext;
36 class QgsLayerTree;
37 
53 class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
54 {
55 
56 #ifdef SIP_RUN
58  if ( sipCpp->inherits( "QgsLayerTreeModel" ) )
59  sipType = sipType_QgsLayerTreeModel;
60  else
61  sipType = 0;
62  SIP_END
63 #endif
64 
65  Q_OBJECT
66  public:
67 
72  explicit QgsLayerTreeModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
73 
74  ~QgsLayerTreeModel() override;
75 
76  // Implementation of virtual functions from QAbstractItemModel
77 
78  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
79  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
80  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
81  QModelIndex parent( const QModelIndex &child ) const override;
82  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
83  Qt::ItemFlags flags( const QModelIndex &index ) const override;
84  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
85  Qt::DropActions supportedDropActions() const override;
86  QStringList mimeTypes() const override;
87  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
88  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
89  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
90 
91  // New stuff
92 
93  enum Flag
94  {
95  // display flags
96  ShowLegend = 0x0001,
97  ShowLegendAsTree = 0x0004,
98  DeferredLegendInvalidation = 0x0008,
99  UseEmbeddedWidgets = 0x0010,
100 
101  // behavioral flags
102  AllowNodeReorder = 0x1000,
103  AllowNodeRename = 0x2000,
104  AllowNodeChangeVisibility = 0x4000,
105  AllowLegendChangeState = 0x8000,
106  ActionHierarchical = 0x10000,
107  };
108  Q_DECLARE_FLAGS( Flags, Flag )
109 
110 
111  void setFlags( QgsLayerTreeModel::Flags f );
113  void setFlag( Flag f, bool on = true );
115  Flags flags() const;
117  bool testFlag( Flag f ) const;
118 
123  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
125  QModelIndex node2index( QgsLayerTreeNode *node ) const;
126 
132  QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
133 
138  static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
139 
145  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
146 
154  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
155 
161  QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
162 
167  QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
168 
177  QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
178 
180  QgsLayerTree *rootGroup() const;
181 
186  void setRootGroup( QgsLayerTree *newRootGroup );
187 
192  void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
193 
195  QModelIndex currentIndex() const;
197  void setCurrentIndex( const QModelIndex &currentIndex );
198 
200  void setLayerTreeNodeFont( int nodeType, const QFont &font );
202  QFont layerTreeNodeFont( int nodeType ) const;
203 
205  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
207  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
208 
216  void setLegendFilterByScale( double scale );
217 
225  double legendFilterByScale() const { return mLegendFilterByScale; }
226 
233  void setLegendFilterByMap( const QgsMapSettings *settings );
234 
243  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
244 
249  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
250 
256  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
257 
263  void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
264 
269  QMap<QString, QString> layerStyleOverrides() const;
270 
275  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
276 
286  static int scaleIconSize( int standardSize );
287 
288  protected slots:
289  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
290  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
291  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
292  void nodeRemovedChildren();
293 
294  void nodeVisibilityChanged( QgsLayerTreeNode *node );
295 
300  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
301 
302  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
303 
304  void nodeLayerLoaded();
305  void nodeLayerWillBeUnloaded();
306  void layerLegendChanged();
307 
308  void layerNeedsUpdate();
309 
310  void legendNodeDataChanged();
311 
312  void invalidateLegendMapBasedData();
313 
314  protected:
315  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
316  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
317 
318  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
319  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
320 
321  void connectToLayers( QgsLayerTreeGroup *parentGroup );
322  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
323  void connectToRootNode();
324  void disconnectFromRootNode();
325 
327  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
328 
334  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
335 
336  static QIcon iconGroup();
337 
339  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
340 
341  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
342 
343  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
344  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
345  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
346  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
347  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
348  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
349  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
350  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
351  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
352  void legendCleanup();
353  void legendInvalidateMapBasedData();
354 
355  protected:
357  QgsLayerTree *mRootNode = nullptr;
359  Flags mFlags;
361  QPersistentModelIndex mCurrentIndex;
364 
373 #ifndef SIP_RUN
375  {
377  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
379  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
380  };
381 #endif
382 
387 #ifndef SIP_RUN
389  {
390  LayerLegendData() = default;
391 
396  QList<QgsLayerTreeModelLegendNode *> activeNodes;
397 
403  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
404 
409  QList<QgsLayerTreeModelLegendNode *> originalNodes;
411  LayerLegendTree *tree = nullptr;
412  };
413 #endif
414 
416  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
417 
422  QMap<QString, QString> mLayerStyleOverrides;
423 
425  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
426 
427  QFont mFontLayer;
428  QFont mFontGroup;
429 
432 
433  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
434  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
435 
438 
443 
444  private:
445 
447  QgsRenderContext *createTemporaryRenderContext() const;
448 };
449 
450 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
451 
452 #ifndef SIP_RUN
454 
460 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
461 {
462  Q_OBJECT
463 
464  public:
465  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
466  : QgsLayerTreeModelLegendNode( nodeL )
467  {
468  // we need a valid rule key to allow the model to build a tree out of legend nodes
469  // if that's possible (if there is a node without a rule key, building of tree is canceled)
470  mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
471  }
472 
473  QVariant data( int role ) const override
474  {
475  if ( role == RuleKeyRole )
476  return mRuleKey;
477  return QVariant();
478  }
479 
480  private:
481  QString mRuleKey;
482 };
483 #endif
484 
486 
487 #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.
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
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 nullptr key. Pointers are not owned...
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Sets 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:182
This class is a base class for nodes in a layer tree.
int autoCollapseLegendNodes() const
Returns 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
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
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 nullptr parent. Pointers are not owned...
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled) ...
void setAutoCollapseLegendNodes(int nodeCount)
Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-...
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.