QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  UseTextFormatting = 0x0020,
101 
102  // behavioral flags
103  AllowNodeReorder = 0x1000,
104  AllowNodeRename = 0x2000,
105  AllowNodeChangeVisibility = 0x4000,
106  AllowLegendChangeState = 0x8000,
107  ActionHierarchical = 0x10000,
108  };
109  Q_DECLARE_FLAGS( Flags, Flag )
110 
111 
112  void setFlags( QgsLayerTreeModel::Flags f );
114  void setFlag( Flag f, bool on = true );
116  Flags flags() const;
118  bool testFlag( Flag f ) const;
119 
124  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
126  QModelIndex node2index( QgsLayerTreeNode *node ) const;
127 
133  QList<QgsLayerTreeNode *> indexes2nodes( const QModelIndexList &list, bool skipInternal = false ) const;
134 
139  static QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index );
140 
146  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
147 
155  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false );
156 
162  QList<QgsLayerTreeModelLegendNode *> layerOriginalLegendNodes( QgsLayerTreeLayer *nodeLayer );
163 
168  QgsLayerTreeModelLegendNode *legendNodeEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
169 
178  QgsLayerTreeModelLegendNode *findLegendNode( const QString &layerId, const QString &ruleKey ) const;
179 
181  QgsLayerTree *rootGroup() const;
182 
187  void setRootGroup( QgsLayerTree *newRootGroup );
188 
193  void refreshLayerLegend( QgsLayerTreeLayer *nodeLayer );
194 
196  QModelIndex currentIndex() const;
198  void setCurrentIndex( const QModelIndex &currentIndex );
199 
201  void setLayerTreeNodeFont( int nodeType, const QFont &font );
203  QFont layerTreeNodeFont( int nodeType ) const;
204 
206  void setAutoCollapseLegendNodes( int nodeCount ) { mAutoCollapseLegendNodesCount = nodeCount; }
208  int autoCollapseLegendNodes() const { return mAutoCollapseLegendNodesCount; }
209 
217  void setLegendFilterByScale( double scale );
218 
226  double legendFilterByScale() const { return mLegendFilterByScale; }
227 
234  void setLegendFilterByMap( const QgsMapSettings *settings );
235 
244  void setLegendFilter( const QgsMapSettings *settings, bool useExtent = true, const QgsGeometry &polygon = QgsGeometry(), bool useExpressions = true );
245 
250  const QgsMapSettings *legendFilterMapSettings() const { return mLegendFilterMapSettings.get(); }
251 
257  void setLegendMapViewData( double mapUnitsPerPixel, int dpi, double scale );
258 
264  void legendMapViewData( double *mapUnitsPerPixel SIP_OUT, int *dpi SIP_OUT, double *scale SIP_OUT ) const;
265 
270  QMap<QString, QString> layerStyleOverrides() const;
271 
276  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
277 
287  static int scaleIconSize( int standardSize );
288 
289  protected slots:
290  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
291  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
292  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
293  void nodeRemovedChildren();
294 
295  void nodeVisibilityChanged( QgsLayerTreeNode *node );
296 
301  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
302 
303  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
304 
305  void nodeLayerLoaded();
306  void nodeLayerWillBeUnloaded();
307  void layerLegendChanged();
308 
309  void layerNeedsUpdate();
310 
311  void legendNodeDataChanged();
312 
313  void invalidateLegendMapBasedData();
314 
315  protected:
316  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
317  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
318 
319  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
320  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
321 
322  void connectToLayers( QgsLayerTreeGroup *parentGroup );
323  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
324  void connectToRootNode();
325  void disconnectFromRootNode();
326 
328  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
329 
335  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
336 
337  static QIcon iconGroup();
338 
340  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
341 
342  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
343 
344  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
345  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
346  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
347  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
348  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
349  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
350  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
351  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
352  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
353  void legendCleanup();
354  void legendInvalidateMapBasedData();
355 
356  protected:
358  QgsLayerTree *mRootNode = nullptr;
360  Flags mFlags;
362  QPersistentModelIndex mCurrentIndex;
365 
374 #ifndef SIP_RUN
376  {
378  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
380  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
381  };
382 #endif
383 
388 #ifndef SIP_RUN
390  {
391  LayerLegendData() = default;
392 
397  QList<QgsLayerTreeModelLegendNode *> activeNodes;
398 
404  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
405 
410  QList<QgsLayerTreeModelLegendNode *> originalNodes;
412  LayerLegendTree *tree = nullptr;
413  };
414 #endif
415 
417  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
418 
423  QMap<QString, QString> mLayerStyleOverrides;
424 
426  QMap<QgsLayerTreeLayer *, LayerLegendData> mLegend;
427 
428  QFont mFontLayer;
429  QFont mFontGroup;
430 
433 
434  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
435  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
436 
439 
444 
445  private:
446 
448  QgsRenderContext *createTemporaryRenderContext() const;
449 };
450 
451 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
452 
453 #ifndef SIP_RUN
455 
461 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
462 {
463  Q_OBJECT
464 
465  public:
466  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
467  : QgsLayerTreeModelLegendNode( nodeL )
468  {
469  // we need a valid rule key to allow the model to build a tree out of legend nodes
470  // if that's possible (if there is a node without a rule key, building of tree is canceled)
471  mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
472  }
473 
474  QVariant data( int role ) const override
475  {
476  if ( role == RuleKeyRole )
477  return mRuleKey;
478  return QVariant();
479  }
480 
481  private:
482  QString mRuleKey;
483 };
484 #endif
485 
487 
488 #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:53
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:122
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:126
#define SIP_END
Definition: qgis_sip.h:189
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:58
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
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.