QGIS API Documentation  3.0.2-Girona (307d082)
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.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 
175  void setItemVisibilityChecked( bool checked );
176 
181  virtual void setItemVisibilityCheckedRecursive( bool checked );
182 
187  void setItemVisibilityCheckedParentRecursive( bool checked );
188 
193  bool isItemVisibilityCheckedRecursive() const;
194 
199  bool isItemVisibilityUncheckedRecursive() const;
200 
206  QList< QgsMapLayer * > checkedLayers() const;
207 
209  bool isExpanded() const;
211  void setExpanded( bool expanded );
212 
214  void setCustomProperty( const QString &key, const QVariant &value );
216  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
218  void removeCustomProperty( const QString &key );
220  QStringList customProperties() const;
222  bool takeChild( QgsLayerTreeNode *node );
223 
224  signals:
225 
227  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
229  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
231  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
233  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
235  void visibilityChanged( QgsLayerTreeNode *node );
237  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
239  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
240 
245  void nameChanged( QgsLayerTreeNode *node, QString name );
246 
247  protected:
248 
250  QgsLayerTreeNode( NodeType t, bool checked = true );
251  QgsLayerTreeNode( const QgsLayerTreeNode &other );
252 
253  // low-level utility functions
254 
256  void readCommonXml( QDomElement &element );
258  void writeCommonXml( QDomElement &element );
259 
261  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
263  void removeChildrenPrivate( int from, int count, bool destroy = true );
264 
265  protected:
268  bool mChecked;
270  QgsLayerTreeNode *mParent = nullptr;
272  QList<QgsLayerTreeNode *> mChildren;
274  bool mExpanded;
277 };
278 
279 
280 
281 
282 #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:56
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()
Get list of children of the node. Children are owned by the parent.
#define SIP_SKIP
Definition: qgis_sip.h:119
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:175
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.
Reads and writes project states.
Definition: qgsproject.h:82
#define SIP_FACTORY
Definition: qgis_sip.h:69
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:165
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