QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgslayertreenode.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertreenode.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 QGSLAYERTREENODE_H
17 #define QGSLAYERTREENODE_H
18 
19 #include "qgis_core.h"
20 #include <QObject>
21 
23 #include "qgsreadwritecontext.h"
24 #include "qgis_sip.h"
25 
26 class QDomElement;
27 
28 class QgsProject;
29 class QgsMapLayer;
30 
72 class CORE_EXPORT QgsLayerTreeNode : public QObject
73 {
74  Q_OBJECT
75 
76 #ifdef SIP_RUN
78  if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
79  {
80  sipType = sipType_QgsLayerTreeNode;
81  QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
82  if ( QgsLayerTree::isLayer( node ) )
83  sipType = sipType_QgsLayerTreeLayer;
84  else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
85  sipType = sipType_QgsLayerTree;
86  else if ( QgsLayerTree::isGroup( node ) )
87  sipType = sipType_QgsLayerTreeGroup;
88  }
89  else
90  sipType = 0;
91  SIP_END
92 #endif
93 
94  public:
95 
97  enum NodeType
98  {
100  NodeLayer
101  };
102 
103  ~QgsLayerTreeNode() override;
104 
106  NodeType nodeType() const { return mNodeType; }
108  QgsLayerTreeNode *parent() { return mParent; }
110  QList<QgsLayerTreeNode *> children() { return mChildren; }
112  QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
113 
118  virtual QString name() const = 0;
119 
124  virtual void setName( const QString &name ) = 0;
125 
130  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
131 
137  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
138 
140  virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
141 
143  virtual QString dump() const = 0;
144 
146  virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
147 
157  virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
158 
163  bool isVisible() const;
164 
169  bool itemVisibilityChecked() const { return mChecked; }
170 
178  void setItemVisibilityChecked( bool checked );
179 
184  virtual void setItemVisibilityCheckedRecursive( bool checked );
185 
190  void setItemVisibilityCheckedParentRecursive( bool checked );
191 
196  bool isItemVisibilityCheckedRecursive() const;
197 
202  bool isItemVisibilityUncheckedRecursive() const;
203 
209  QList< QgsMapLayer * > checkedLayers() const;
210 
212  bool isExpanded() const;
214  void setExpanded( bool expanded );
215 
217  void setCustomProperty( const QString &key, const QVariant &value );
219  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
221  void removeCustomProperty( const QString &key );
223  QStringList customProperties() const;
225  bool takeChild( QgsLayerTreeNode *node );
226 
227  signals:
228 
230  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
232  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
234  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
236  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
238  void visibilityChanged( QgsLayerTreeNode *node );
240  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
242  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
243 
248  void nameChanged( QgsLayerTreeNode *node, QString name );
249 
250  protected:
251 
253  QgsLayerTreeNode( NodeType t, bool checked = true );
254  QgsLayerTreeNode( const QgsLayerTreeNode &other );
255 
256  // low-level utility functions
257 
259  void readCommonXml( QDomElement &element );
261  void writeCommonXml( QDomElement &element );
262 
264  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
266  void removeChildrenPrivate( int from, int count, bool destroy = true );
267 
268  protected:
271  bool mChecked;
273  QgsLayerTreeNode *mParent = nullptr;
275  QList<QgsLayerTreeNode *> mChildren;
277  bool mExpanded;
280 };
281 
282 
283 
284 
285 #endif // QGSLAYERTREENODE_H
The class is used as a container of context for various read/write operations on other objects...
Base class for all map layer types.
Definition: qgsmaplayer.h:79
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
bool mExpanded
whether the node should be shown in GUI as expanded
NodeType
Enumeration of possible tree node types.
NodeType mNodeType
type of the node - determines which subclass is used
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
#define SIP_END
Definition: qgis_sip.h:189
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
This class is a base class for nodes in a layer tree.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:91
#define SIP_FACTORY
Definition: qgis_sip.h:76
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
QList< QgsLayerTreeNode * > children() const
Gets list of children of the node. Children are owned by the parent.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
Container of other groups and layers.
QgsObjectCustomProperties mProperties
custom properties attached to the node