QGIS API Documentation  2.99.0-Master (b8fd1fd)
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 
24 class QDomElement;
25 
26 class QgsProject;
27 class QgsMapLayer;
28 
69 class CORE_EXPORT QgsLayerTreeNode : public QObject
70 {
71  Q_OBJECT
72  public:
73 
75  enum NodeType
76  {
78  NodeLayer
79  };
80 
82 
84  NodeType nodeType() const { return mNodeType; }
86  QgsLayerTreeNode *parent() { return mParent; }
88  QList<QgsLayerTreeNode *> children() { return mChildren; }
90  QList<QgsLayerTreeNode *> children() const { return mChildren; }
91 
94  virtual QString name() const = 0;
97  virtual void setName( const QString &name ) = 0;
98 
101  static QgsLayerTreeNode *readXml( QDomElement &element );
105  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project );
106 
108  virtual void writeXml( QDomElement &parentElement ) = 0;
109 
111  virtual QString dump() const = 0;
112 
114  virtual QgsLayerTreeNode *clone() const = 0;
115 
119  virtual void resolveReferences( const QgsProject *project ) = 0;
120 
123  bool isVisible() const;
124 
127  bool itemVisibilityChecked() const { return mChecked; }
128 
131  void setItemVisibilityChecked( bool checked );
132 
135  virtual void setItemVisibilityCheckedRecursive( bool checked );
136 
139  void setItemVisibilityCheckedParentRecursive( bool checked );
140 
143  bool isItemVisibilityCheckedRecursive() const;
144 
147  bool isItemVisibilityUncheckedRecursive() const;
148 
154  QList< QgsMapLayer * > checkedLayers() const;
155 
157  bool isExpanded() const;
159  void setExpanded( bool expanded );
160 
162  void setCustomProperty( const QString &key, const QVariant &value );
164  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
166  void removeCustomProperty( const QString &key );
168  QStringList customProperties() const;
170  bool takeChild( QgsLayerTreeNode *node );
171 
172  signals:
173 
175  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
177  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
179  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
181  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
183  void visibilityChanged( QgsLayerTreeNode *node );
185  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
187  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
190  void nameChanged( QgsLayerTreeNode *node, QString name );
191 
192  protected:
193 
195  QgsLayerTreeNode( NodeType t, bool checked = true );
196  QgsLayerTreeNode( const QgsLayerTreeNode &other );
197 
198  // low-level utility functions
199 
201  void readCommonXml( QDomElement &element );
203  void writeCommonXml( QDomElement &element );
204 
206  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
208  void removeChildrenPrivate( int from, int count, bool destroy = true );
209 
210  protected:
213  bool mChecked;
215  QgsLayerTreeNode *mParent = nullptr;
217  QList<QgsLayerTreeNode *> mChildren;
219  bool mExpanded;
222 };
223 
224 
225 
226 
227 #endif // QGSLAYERTREENODE_H
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:52
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()
Get list of children of the node. Children are owned by the parent.
QgsLayerTreeNode * parent()
Get pointer to the parent. If parent is a null pointer, the node is a root node.
This class is a base class for nodes in a layer tree.
Reads and writes project states.
Definition: qgsproject.h:75
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
QList< QgsLayerTreeNode * > children() const
Get list of children of the node. Children are owned by the parent.
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