QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
74 class CORE_EXPORT QgsLayerTreeNode : public QObject
75 {
76  Q_OBJECT
77 
78 #ifdef SIP_RUN
80  if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
81  {
82  sipType = sipType_QgsLayerTreeNode;
83  QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
84  if ( QgsLayerTree::isLayer( node ) )
85  sipType = sipType_QgsLayerTreeLayer;
86  else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
87  sipType = sipType_QgsLayerTree;
88  else if ( QgsLayerTree::isGroup( node ) )
89  sipType = sipType_QgsLayerTreeGroup;
90  }
91  else
92  sipType = 0;
93  SIP_END
94 #endif
95 
96  public:
97 
99  enum NodeType
100  {
102  NodeLayer
103  };
104 
105  ~QgsLayerTreeNode() override;
106 
108  NodeType nodeType() const { return mNodeType; }
110  QgsLayerTreeNode *parent() { return mParent; }
112  QList<QgsLayerTreeNode *> children() { return mChildren; }
114  QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
115 
120  virtual QString name() const = 0;
121 
126  virtual void setName( const QString &name ) = 0;
127 
132  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
133 
139  static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
140 
142  virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
143 
145  virtual QString dump() const = 0;
146 
148  virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
149 
159  virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
160 
165  bool isVisible() const;
166 
171  bool itemVisibilityChecked() const { return mChecked; }
172 
180  void setItemVisibilityChecked( bool checked );
181 
186  virtual void setItemVisibilityCheckedRecursive( bool checked );
187 
192  void setItemVisibilityCheckedParentRecursive( bool checked );
193 
198  bool isItemVisibilityCheckedRecursive() const;
199 
204  bool isItemVisibilityUncheckedRecursive() const;
205 
211  QList< QgsMapLayer * > checkedLayers() const;
212 
217  int depth() const;
218 
220  bool isExpanded() const;
222  void setExpanded( bool expanded );
223 
225  void setCustomProperty( const QString &key, const QVariant &value );
227  QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
229  void removeCustomProperty( const QString &key );
231  QStringList customProperties() const;
233  bool takeChild( QgsLayerTreeNode *node );
234 
235  signals:
236 
238  void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
240  void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
242  void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
244  void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
248  void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
250  void expandedChanged( QgsLayerTreeNode *node, bool expanded );
251 
256  void nameChanged( QgsLayerTreeNode *node, QString name );
257 
258  protected:
259 
261  QgsLayerTreeNode( NodeType t, bool checked = true );
262  QgsLayerTreeNode( const QgsLayerTreeNode &other );
263 
264  // low-level utility functions
265 
267  void readCommonXml( QDomElement &element );
269  void writeCommonXml( QDomElement &element );
270 
272  void insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes );
274  void removeChildrenPrivate( int from, int count, bool destroy = true );
275 
276  protected:
279  bool mChecked;
281  QgsLayerTreeNode *mParent = nullptr;
283  QList<QgsLayerTreeNode *> mChildren;
285  bool mExpanded;
288 
289  private:
290  QgsLayerTreeNode &operator=( const QgsLayerTreeNode & ) = delete;
291 };
292 
293 
294 
295 
296 #endif // QGSLAYERTREENODE_H
QgsLayerTreeNode::NodeGroup
@ NodeGroup
Container of other groups and layers.
Definition: qgslayertreenode.h:101
QgsLayerTreeNode
This class is a base class for nodes in a layer tree.
Definition: qgslayertreenode.h:75
QgsLayerTreeNode::removedChildren
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsLayerTreeNode::mChecked
bool mChecked
Definition: qgslayertreenode.h:279
qgsreadwritecontext.h
QgsLayerTreeNode::resolveReferences
virtual void resolveReferences(const QgsProject *project, bool looseMatching=false)=0
Turn textual references to layers into map layer object from project.
QgsLayerTreeNode::expandedChanged
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
QgsLayerTreeNode::mChildren
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
Definition: qgslayertreenode.h:283
QgsLayerTreeNode::customPropertyChanged
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
QgsLayerTreeNode::nameChanged
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayerTreeNode::nodeType
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
Definition: qgslayertreenode.h:108
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
qgsobjectcustomproperties.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTreeNode::dump
virtual QString dump() const =0
Returns string with layer tree structure. For debug purposes only.
QgsLayerTreeNode::writeXml
virtual void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context)=0
Write layer tree to XML.
qgis_sip.h
QgsLayerTreeNode::mProperties
QgsObjectCustomProperties mProperties
custom properties attached to the node
Definition: qgslayertreenode.h:287
QgsLayerTreeNode::NodeType
NodeType
Enumeration of possible tree node types.
Definition: qgslayertreenode.h:100
QgsLayerTreeNode::willAddChildren
void willAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be added to a node within the tree.
QgsLayerTree::isLayer
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
QgsLayerTreeNode::setName
virtual void setName(const QString &name)=0
Set name of the node.
QgsObjectCustomProperties
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
Definition: qgsobjectcustomproperties.h:36
QgsLayerTreeNode::itemVisibilityChecked
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
Definition: qgslayertreenode.h:171
QgsLayerTreeNode::mNodeType
NodeType mNodeType
type of the node - determines which subclass is used
Definition: qgslayertreenode.h:278
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsLayerTreeNode::children
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
Definition: qgslayertreenode.h:112
QgsLayerTreeNode::clone
virtual QgsLayerTreeNode * clone() const =0
Create a copy of the node. Returns new instance.
QgsLayerTreeNode::name
virtual QString name() const =0
Returns name of the node.
QgsLayerTreeNode::mExpanded
bool mExpanded
whether the node should be shown in GUI as expanded
Definition: qgslayertreenode.h:285
QgsLayerTreeNode::addedChildren
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
QgsLayerTreeNode::parent
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
Definition: qgslayertreenode.h:110
QgsLayerTreeNode::willRemoveChildren
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
QgsLayerTree::isGroup
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43
SIP_END
#define SIP_END
Definition: qgis_sip.h:194
QgsLayerTreeNode::children
QList< QgsLayerTreeNode * > children() const
Gets list of children of the node. Children are owned by the parent.
Definition: qgslayertreenode.h:114
QgsLayerTreeNode::visibilityChanged
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.