QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 
76 class CORE_EXPORT QgsLayerTreeNode : public QObject
77 {
78  Q_OBJECT
79 
80 #ifdef SIP_RUN
82  if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
83  {
84  sipType = sipType_QgsLayerTreeNode;
85  QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
86  if ( QgsLayerTree::isLayer( node ) )
87  sipType = sipType_QgsLayerTreeLayer;
88  else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
89  sipType = sipType_QgsLayerTree;
90  else if ( QgsLayerTree::isGroup( node ) )
91  sipType = sipType_QgsLayerTreeGroup;
92  }
93  else
94  sipType = 0;
95  SIP_END
96 #endif
97 
98  public:
99 
101  enum NodeType
102  {
104  NodeLayer
105  };
106 
107  ~QgsLayerTreeNode() override;
108 
109 #ifdef SIP_RUN
110  SIP_PYOBJECT __repr__();
111  % MethodCode
112  QString str = QStringLiteral( "<QgsLayerTreeNode: %1>" ).arg( sipCpp->name() );
113  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
114  % End
115 #endif
116 
118  NodeType nodeType() const { return mNodeType; }
120  QgsLayerTreeNode *parent() { return mParent; }
122  QList<QgsLayerTreeNode *> children() { return mChildren; }
124  QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
125 
131  QList<QgsLayerTreeNode *> abandonChildren() SIP_SKIP;
132 
136  virtual QString name() const = 0;
137 
141  virtual void setName( const QString &name ) = 0;
142 
147  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
148 
153  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
154 
156  virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
157 
159  virtual QString dump() const = 0;
160 
162  virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
163 
172  virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
173 
177  bool isVisible() const;
178 
182  bool itemVisibilityChecked() const { return mChecked; }
183 
190  void setItemVisibilityChecked( bool checked );
191 
195  virtual void setItemVisibilityCheckedRecursive( bool checked );
196 
200  void setItemVisibilityCheckedParentRecursive( bool checked );
201 
205  bool isItemVisibilityCheckedRecursive() const;
206 
210  bool isItemVisibilityUncheckedRecursive() const;
211 
216  QList< QgsMapLayer * > checkedLayers() const;
217 
222  int depth() const;
223 
225  bool isExpanded() const;
227  void setExpanded( bool expanded );
228 
230  void setCustomProperty( const QString &key, const QVariant &value );
232  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
234  void removeCustomProperty( const QString &key );
236  QStringList customProperties() const;
238  bool takeChild( QgsLayerTreeNode *node );
239 
240  signals:
241 
243  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
245  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
247  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
249  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
253  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
255  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
256 
260  void nameChanged( QgsLayerTreeNode *node, QString name );
261 
262  protected:
263 
265  QgsLayerTreeNode( NodeType t, bool checked = true );
266  QgsLayerTreeNode( const QgsLayerTreeNode &other );
267 
268  // low-level utility functions
269 
271  void readCommonXml( QDomElement &element );
273  void writeCommonXml( QDomElement &element );
274 
276  void insertChildrenPrivate( int index, const QList<QgsLayerTreeNode *> &nodes );
278  void removeChildrenPrivate( int from, int count, bool destroy = true );
279 
280  protected:
283  bool mChecked;
285  QgsLayerTreeNode *mParent = nullptr;
287  QList<QgsLayerTreeNode *> mChildren;
289  bool mExpanded;
292 
294  virtual void makeOrphan() SIP_SKIP;
295 
296  private:
297  QgsLayerTreeNode &operator=( const QgsLayerTreeNode & ) = delete;
298 
299 };
300 
301 
302 
303 
304 #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:50
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:75
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:107
The class is used as a container of context for various read/write operations on other objects.
#define str(x)
Definition: qgis.cpp:38
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:191
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:208