QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
109  NodeType nodeType() const { return mNodeType; }
111  QgsLayerTreeNode *parent() { return mParent; }
113  QList<QgsLayerTreeNode *> children() { return mChildren; }
115  QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
116 
122  QList<QgsLayerTreeNode *> abandonChildren() SIP_SKIP;
123 
128  virtual QString name() const = 0;
129 
134  virtual void setName( const QString &name ) = 0;
135 
140  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
141 
147  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
148 
150  virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
151 
153  virtual QString dump() const = 0;
154 
156  virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
157 
167  virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
168 
173  bool isVisible() const;
174 
179  bool itemVisibilityChecked() const { return mChecked; }
180 
188  void setItemVisibilityChecked( bool checked );
189 
194  virtual void setItemVisibilityCheckedRecursive( bool checked );
195 
200  void setItemVisibilityCheckedParentRecursive( bool checked );
201 
206  bool isItemVisibilityCheckedRecursive() const;
207 
212  bool isItemVisibilityUncheckedRecursive() const;
213 
219  QList< QgsMapLayer * > checkedLayers() const;
220 
225  int depth() const;
226 
228  bool isExpanded() const;
230  void setExpanded( bool expanded );
231 
233  void setCustomProperty( const QString &key, const QVariant &value );
235  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
237  void removeCustomProperty( const QString &key );
239  QStringList customProperties() const;
241  bool takeChild( QgsLayerTreeNode *node );
242 
243  signals:
244 
246  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
248  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
250  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
252  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
256  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
258  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
259 
264  void nameChanged( QgsLayerTreeNode *node, QString name );
265 
266  protected:
267 
269  QgsLayerTreeNode( NodeType t, bool checked = true );
270  QgsLayerTreeNode( const QgsLayerTreeNode &other );
271 
272  // low-level utility functions
273 
275  void readCommonXml( QDomElement &element );
277  void writeCommonXml( QDomElement &element );
278 
280  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
282  void removeChildrenPrivate( int from, int count, bool destroy = true );
283 
284  protected:
287  bool mChecked;
289  QgsLayerTreeNode *mParent = nullptr;
291  QList<QgsLayerTreeNode *> mChildren;
293  bool mExpanded;
296 
298  virtual void makeOrphan() SIP_SKIP;
299 
300  private:
301  QgsLayerTreeNode &operator=( const QgsLayerTreeNode & ) = delete;
302 
303 };
304 
305 
306 
307 
308 #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:85
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:99
The class is used as a container of context for various read/write operations on other objects.
#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