QGIS API Documentation  2.99.0-Master (23ddace)
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 "qgis.h"
24 
25 class QDomElement;
26 
27 class QgsProject;
28 class QgsMapLayer;
29 
70 class CORE_EXPORT QgsLayerTreeNode : public QObject
71 {
72  Q_OBJECT
73 
74 #ifdef SIP_RUN
76  if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
77  {
78  sipType = sipType_QgsLayerTreeNode;
79  QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
80  if ( QgsLayerTree::isLayer( node ) )
81  sipType = sipType_QgsLayerTreeLayer;
82  else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
83  sipType = sipType_QgsLayerTree;
84  else if ( QgsLayerTree::isGroup( node ) )
85  sipType = sipType_QgsLayerTreeGroup;
86  }
87  else
88  sipType = 0;
89  SIP_END
90 #endif
91 
92  public:
93 
95  enum NodeType
96  {
98  NodeLayer
99  };
100 
101  ~QgsLayerTreeNode();
102 
104  NodeType nodeType() const { return mNodeType; }
106  QgsLayerTreeNode *parent() { return mParent; }
108  QList<QgsLayerTreeNode *> children() { return mChildren; }
110  QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
111 
114  virtual QString name() const = 0;
117  virtual void setName( const QString &name ) = 0;
118 
121  static QgsLayerTreeNode *readXml( QDomElement &element ) SIP_FACTORY;
125  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
126 
128  virtual void writeXml( QDomElement &parentElement ) = 0;
129 
131  virtual QString dump() const = 0;
132 
134  virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
135 
145  virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
146 
149  bool isVisible() const;
150 
153  bool itemVisibilityChecked() const { return mChecked; }
154 
157  void setItemVisibilityChecked( bool checked );
158 
161  virtual void setItemVisibilityCheckedRecursive( bool checked );
162 
165  void setItemVisibilityCheckedParentRecursive( bool checked );
166 
169  bool isItemVisibilityCheckedRecursive() const;
170 
173  bool isItemVisibilityUncheckedRecursive() const;
174 
180  QList< QgsMapLayer * > checkedLayers() const;
181 
183  bool isExpanded() const;
185  void setExpanded( bool expanded );
186 
188  void setCustomProperty( const QString &key, const QVariant &value );
190  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
192  void removeCustomProperty( const QString &key );
194  QStringList customProperties() const;
196  bool takeChild( QgsLayerTreeNode *node );
197 
198  signals:
199 
201  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
203  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
205  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
207  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
209  void visibilityChanged( QgsLayerTreeNode *node );
211  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
213  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
216  void nameChanged( QgsLayerTreeNode *node, QString name );
217 
218  protected:
219 
221  QgsLayerTreeNode( NodeType t, bool checked = true );
222  QgsLayerTreeNode( const QgsLayerTreeNode &other );
223 
224  // low-level utility functions
225 
227  void readCommonXml( QDomElement &element );
229  void writeCommonXml( QDomElement &element );
230 
232  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
234  void removeChildrenPrivate( int from, int count, bool destroy = true );
235 
236  protected:
239  bool mChecked;
241  QgsLayerTreeNode *mParent = nullptr;
243  QList<QgsLayerTreeNode *> mChildren;
245  bool mExpanded;
248 };
249 
250 
251 
252 
253 #endif // QGSLAYERTREENODE_H
Base class for all map layer types.
Definition: qgsmaplayer.h:54
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:42
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.
#define SIP_SKIP
Definition: qgis_sip.h:107
QgsLayerTreeNode * parent()
Get pointer to the parent. If parent is a null pointer, the node is a root node.
#define SIP_END
Definition: qgis_sip.h:163
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:52
This class is a base class for nodes in a layer tree.
Reads and writes project states.
Definition: qgsproject.h:78
#define SIP_FACTORY
Definition: qgis_sip.h:57
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.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:153
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