QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  signals:
290 
295  void messageEmitted( const QString &message, Qgis::MessageLevel level = Qgis::Info, int duration = 5 );
296 
297  protected slots:
298  void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
299  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
300  void nodeWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
301  void nodeRemovedChildren();
302 
303  void nodeVisibilityChanged( QgsLayerTreeNode *node );
304 
309  void nodeNameChanged( QgsLayerTreeNode *node, const QString &name );
310 
311  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
312 
313  void nodeLayerLoaded();
314  void nodeLayerWillBeUnloaded();
315  void layerLegendChanged();
316 
317  void layerNeedsUpdate();
318 
319  void legendNodeDataChanged();
320 
321  void invalidateLegendMapBasedData();
322 
323  protected:
324  void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer );
325  void addLegendToLayer( QgsLayerTreeLayer *nodeL );
326 
327  void connectToLayer( QgsLayerTreeLayer *nodeLayer );
328  void disconnectFromLayer( QgsLayerTreeLayer *nodeLayer );
329 
330  void connectToLayers( QgsLayerTreeGroup *parentGroup );
331  void disconnectFromLayers( QgsLayerTreeGroup *parentGroup );
332  void connectToRootNode();
333  void disconnectFromRootNode();
334 
336  void recursivelyEmitDataChanged( const QModelIndex &index = QModelIndex() );
337 
343  void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
344 
345  static QIcon iconGroup();
346 
348  QList<QgsLayerTreeModelLegendNode *> filterLegendNodes( const QList<QgsLayerTreeModelLegendNode *> &nodes );
349 
350  QModelIndex indexOfParentLayerTreeNode( QgsLayerTreeNode *parentNode ) const;
351 
352  int legendRootRowCount( QgsLayerTreeLayer *nL ) const;
353  int legendNodeRowCount( QgsLayerTreeModelLegendNode *node ) const;
354  QModelIndex legendRootIndex( int row, int column, QgsLayerTreeLayer *nL ) const;
355  QModelIndex legendNodeIndex( int row, int column, QgsLayerTreeModelLegendNode *node ) const;
356  QModelIndex legendParent( QgsLayerTreeModelLegendNode *legendNode ) const;
357  QVariant legendNodeData( QgsLayerTreeModelLegendNode *node, int role ) const;
358  Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode *node ) const;
359  bool legendEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
360  QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer *nodeLayer ) const;
361  void legendCleanup();
362  void legendInvalidateMapBasedData();
363 
364  protected:
366  QgsLayerTree *mRootNode = nullptr;
368  Flags mFlags;
370  QPersistentModelIndex mCurrentIndex;
373 
384 #ifndef SIP_RUN
386  {
388  QMap<QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode *> parents;
390  QMap<QgsLayerTreeModelLegendNode *, QList<QgsLayerTreeModelLegendNode *> > children;
391  };
392 #endif
393 
398 #ifndef SIP_RUN
400  {
401  LayerLegendData() = default;
402 
407  QList<QgsLayerTreeModelLegendNode *> activeNodes;
408 
414  QgsLayerTreeModelLegendNode *embeddedNodeInParent = nullptr;
415 
420  QList<QgsLayerTreeModelLegendNode *> originalNodes;
422  LayerLegendTree *tree = nullptr;
423  };
424 #endif
425 
427  LayerLegendTree *tryBuildLegendTree( const QList<QgsLayerTreeModelLegendNode *> &nodes ) SIP_SKIP;
428 
433  QMap<QString, QString> mLayerStyleOverrides;
434 
436  QHash<QgsLayerTreeLayer *, LayerLegendData> mLegend;
437 
438  QFont mFontLayer;
439  QFont mFontGroup;
440 
443 
444  std::unique_ptr<QgsMapSettings> mLegendFilterMapSettings;
445  std::unique_ptr<QgsMapHitTest> mLegendFilterHitTest;
446 
449 
454 
455  private:
456 
458  QgsRenderContext *createTemporaryRenderContext() const;
459 };
460 
461 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
462 
463 #ifndef SIP_RUN
465 
471 class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
472 {
473  Q_OBJECT
474 
475  public:
476  EmbeddedWidgetLegendNode( QgsLayerTreeLayer *nodeL )
477  : QgsLayerTreeModelLegendNode( nodeL )
478  {
479  // we need a valid rule key to allow the model to build a tree out of legend nodes
480  // if that's possible (if there is a node without a rule key, building of tree is canceled)
481  mRuleKey = QStringLiteral( "embedded-widget-" ) + QUuid::createUuid().toString();
482  }
483 
484  QVariant data( int role ) const override
485  {
486  if ( role == RuleKeyRole )
487  return mRuleKey;
488  return QVariant();
489  }
490 
491  private:
492  QString mRuleKey;
493 };
494 #endif
495 
497 
498 #endif // QGSLAYERTREEMODEL_H
QgsLayerTreeModel::mFontGroup
QFont mFontGroup
Definition: qgslayertreemodel.h:439
QgsLayerTreeNode
Definition: qgslayertreenode.h:74
qgslayertreemodellegendnode.h
QgsLayerTreeModel::LayerLegendTree::children
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under nullptr key. Pointers are not owned.
Definition: qgslayertreemodel.h:390
QgsLayerTreeModelLegendNode::data
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
QgsLayerTreeModel::LayerLegendData::activeNodes
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
Definition: qgslayertreemodel.h:407
QgsLayerTreeModel::mLegendMapViewMupp
double mLegendMapViewMupp
Definition: qgslayertreemodel.h:450
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsLayerTreeModel::mLegend
QHash< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
Definition: qgslayertreemodel.h:436
QgsLayerTreeModel::mFlags
Flags mFlags
Sets of flags for the model.
Definition: qgslayertreemodel.h:368
QgsLayerTreeModel::mLegendFilterHitTest
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
Definition: qgslayertreemodel.h:445
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsLayerTreeModel::mLegendFilterMapSettings
std::unique_ptr< QgsMapSettings > mLegendFilterMapSettings
Definition: qgslayertreemodel.h:444
QgsLayerTreeModel::mFontLayer
QFont mFontLayer
Definition: qgslayertreemodel.h:438
QgsLayerTreeModel::mLegendFilterUsesExtent
bool mLegendFilterUsesExtent
whether to use map filtering
Definition: qgslayertreemodel.h:448
QgsLayerTreeModel
Definition: qgslayertreemodel.h:53
QgsMapHitTest
Definition: qgsmaphittest.h:37
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsLayerTreeModel::LayerLegendData::originalNodes
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
Definition: qgslayertreemodel.h:420
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTree
Definition: qgslayertree.h:32
Qgis::Info
@ Info
Definition: qgis.h:103
QgsLayerTreeModel::Flag
Flag
Definition: qgslayertreemodel.h:93
QgsLayerTreeModel::mCurrentIndex
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
Definition: qgslayertreemodel.h:370
QgsLayerTreeModel::mDeferLegendInvalidationTimer
QTimer mDeferLegendInvalidationTimer
Definition: qgslayertreemodel.h:453
QgsLayerTreeLayer
Definition: qgslayertreelayer.h:43
QgsLayerTreeModel::LayerLegendData
Structure that stores all data associated with one map layer.
Definition: qgslayertreemodel.h:399
QgsLayerTreeModel::legendFilterMapSettings
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled)
Definition: qgslayertreemodel.h:250
QgsLayerTreeModel::mLegendFilterByScale
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
Definition: qgslayertreemodel.h:442
QgsLayerTreeGroup
Definition: qgslayertreegroup.h:34
QgsLayerTreeModel::LayerLegendTree
Structure that stores tree representation of map layer's legend.
Definition: qgslayertreemodel.h:385
QgsLayerTreeModel::setAutoCollapseLegendNodes
void setAutoCollapseLegendNodes(int nodeCount)
Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-...
Definition: qgslayertreemodel.h:206
QgsLayerTreeModel::LayerLegendTree::parents
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have nullptr parent. Pointers are not owned.
Definition: qgslayertreemodel.h:388
QgsLayerTreeModel::autoCollapseLegendNodes
int autoCollapseLegendNodes() const
Returns at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse ...
Definition: qgslayertreemodel.h:208
QgsWms::legendNode
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
Definition: qgswmsgetlegendgraphics.cpp:358
qgsgeometry.h
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:101
QgsLayerTreeModel::mLegendMapViewScale
double mLegendMapViewScale
Definition: qgslayertreemodel.h:452
QgsGeometry
Definition: qgsgeometry.h:122
QgsLayerTreeModel::mLegendMapViewDpi
int mLegendMapViewDpi
Definition: qgslayertreemodel.h:451
QgsLayerTreeModel::mLayerStyleOverrides
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
Definition: qgslayertreemodel.h:433
QgsLayerTreeModel::mAutoCollapseLegendNodesCount
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
Definition: qgslayertreemodel.h:372
QgsLayerTreeModel::legendFilterByScale
double legendFilterByScale() const
Returns the scale which restricts the legend nodes which are visible.
Definition: qgslayertreemodel.h:226
QgsGuiUtils::scaleIconSize
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
Definition: qgsguiutils.cpp:257
QgsExpression
Definition: qgsexpression.h:113
QgsMapSettings
Definition: qgsmapsettings.h:86
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayerTreeModelLegendNode
Definition: qgslayertreemodellegendnode.h:50