QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
75 class CORE_EXPORT QgsLayerTreeNode : public QObject
76 {
77  Q_OBJECT
78 
79 #ifdef SIP_RUN
81  if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
82  {
83  sipType = sipType_QgsLayerTreeNode;
84  QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
85  if ( QgsLayerTree::isLayer( node ) )
86  sipType = sipType_QgsLayerTreeLayer;
87  else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
88  sipType = sipType_QgsLayerTree;
89  else if ( QgsLayerTree::isGroup( node ) )
90  sipType = sipType_QgsLayerTreeGroup;
91  }
92  else
93  sipType = 0;
94  SIP_END
95 #endif
96 
97  public:
98 
100  enum NodeType
101  {
103  NodeLayer
104  };
105 
106  ~QgsLayerTreeNode() override;
107 
108 #ifdef SIP_RUN
109  SIP_PYOBJECT __repr__();
110  % MethodCode
111  QString str = QStringLiteral( "<QgsLayerTreeNode: %1>" ).arg( sipCpp->name() );
112  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
113  % End
114 #endif
115 
117  NodeType nodeType() const { return mNodeType; }
119  QgsLayerTreeNode *parent() { return mParent; }
121  QList<QgsLayerTreeNode *> children() { return mChildren; }
123  QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
124 
130  QList<QgsLayerTreeNode *> abandonChildren() SIP_SKIP;
131 
136  virtual QString name() const = 0;
137 
142  virtual void setName( const QString &name ) = 0;
143 
148  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
149 
155  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
156 
158  virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
159 
161  virtual QString dump() const = 0;
162 
164  virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
165 
175  virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
176 
181  bool isVisible() const;
182 
187  bool itemVisibilityChecked() const { return mChecked; }
188 
196  void setItemVisibilityChecked( bool checked );
197 
202  virtual void setItemVisibilityCheckedRecursive( bool checked );
203 
208  void setItemVisibilityCheckedParentRecursive( bool checked );
209 
214  bool isItemVisibilityCheckedRecursive() const;
215 
220  bool isItemVisibilityUncheckedRecursive() const;
221 
227  QList< QgsMapLayer * > checkedLayers() const;
228 
233  int depth() const;
234 
236  bool isExpanded() const;
238  void setExpanded( bool expanded );
239 
241  void setCustomProperty( const QString &key, const QVariant &value );
243  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
245  void removeCustomProperty( const QString &key );
247  QStringList customProperties() const;
249  bool takeChild( QgsLayerTreeNode *node );
250 
251  signals:
252 
254  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
256  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
258  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
260  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
264  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
266  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
267 
272  void nameChanged( QgsLayerTreeNode *node, QString name );
273 
274  protected:
275 
277  QgsLayerTreeNode( NodeType t, bool checked = true );
278  QgsLayerTreeNode( const QgsLayerTreeNode &other );
279 
280  // low-level utility functions
281 
283  void readCommonXml( QDomElement &element );
285  void writeCommonXml( QDomElement &element );
286 
288  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
290  void removeChildrenPrivate( int from, int count, bool destroy = true );
291 
292  protected:
295  bool mChecked;
297  QgsLayerTreeNode *mParent = nullptr;
299  QList<QgsLayerTreeNode *> mChildren;
301  bool mExpanded;
304 
306  virtual void makeOrphan() SIP_SKIP;
307 
308  private:
309  QgsLayerTreeNode &operator=( const QgsLayerTreeNode & ) = delete;
310 
311 };
312 
313 
314 
315 
316 #endif // QGSLAYERTREENODE_H
This class is a base class for nodes in a layer tree.
NodeType
Enumeration of possible tree node types.
@ NodeGroup
Container of other groups and layers.
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
QgsObjectCustomProperties mProperties
custom properties attached to the node
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
NodeType mNodeType
type of the node - determines which subclass is used
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void willAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be added to a node within the tree.
QList< QgsLayerTreeNode * > children() const
Gets list of children of the node. Children are owned by the parent.
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
bool mExpanded
whether the node should be shown in GUI as expanded
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:101
The class is used as a container of context for various read/write operations on other objects.
#define str(x)
Definition: qgis.cpp:37
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194