QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
26class QDomElement;
27
28class QgsProject;
29class QgsMapLayer;
30
77class CORE_EXPORT QgsLayerTreeNode : public QObject
78{
79 Q_OBJECT
80
81#ifdef SIP_RUN
83 if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
84 {
85 sipType = sipType_QgsLayerTreeNode;
86 QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
87 if ( QgsLayerTree::isLayer( node ) )
88 sipType = sipType_QgsLayerTreeLayer;
89 else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
90 sipType = sipType_QgsLayerTree;
91 else if ( QgsLayerTree::isGroup( node ) )
92 sipType = sipType_QgsLayerTreeGroup;
93 }
94 else
95 sipType = 0;
97#endif
98
99 public:
100
103 {
105 NodeLayer
106 };
107
108 ~QgsLayerTreeNode() override;
109
110#ifdef SIP_RUN
111 SIP_PYOBJECT __repr__();
112 % MethodCode
113 QString str = QStringLiteral( "<QgsLayerTreeNode: %1>" ).arg( sipCpp->name() );
114 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
115 % End
116#endif
117
119 NodeType nodeType() const { return mNodeType; }
121 QgsLayerTreeNode *parent() { return mParent; }
123 QList<QgsLayerTreeNode *> children() { return mChildren; }
125 QList<QgsLayerTreeNode *> children() const { return mChildren; } SIP_SKIP
126
132 QList<QgsLayerTreeNode *> abandonChildren() SIP_SKIP;
133
138 virtual QString name() const = 0;
139
144 virtual void setName( const QString &name ) = 0;
145
150 static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
151
157 static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
158
160 virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
161
163 virtual QString dump() const = 0;
164
166 virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
167
177 virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
178
183 bool isVisible() const;
184
189 bool itemVisibilityChecked() const { return mChecked; }
190
198 void setItemVisibilityChecked( bool checked );
199
204 virtual void setItemVisibilityCheckedRecursive( bool checked );
205
210 void setItemVisibilityCheckedParentRecursive( bool checked );
211
216 bool isItemVisibilityCheckedRecursive() const;
217
222 bool isItemVisibilityUncheckedRecursive() const;
223
229 QList< QgsMapLayer * > checkedLayers() const;
230
235 int depth() const;
236
238 bool isExpanded() const;
240 void setExpanded( bool expanded );
241
243 void setCustomProperty( const QString &key, const QVariant &value );
245 QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
247 void removeCustomProperty( const QString &key );
249 QStringList customProperties() const;
251 bool takeChild( QgsLayerTreeNode *node );
252
253 signals:
254
256 void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
258 void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
260 void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
262 void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
266 void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
268 void expandedChanged( QgsLayerTreeNode *node, bool expanded );
269
274 void nameChanged( QgsLayerTreeNode *node, QString name );
275
276 protected:
277
279 QgsLayerTreeNode( NodeType t, bool checked = true );
280 QgsLayerTreeNode( const QgsLayerTreeNode &other );
281
282 // low-level utility functions
283
285 void readCommonXml( QDomElement &element );
287 void writeCommonXml( QDomElement &element );
288
290 void insertChildrenPrivate( int index, const QList<QgsLayerTreeNode *> &nodes );
292 void removeChildrenPrivate( int from, int count, bool destroy = true );
293
294 protected:
299 QgsLayerTreeNode *mParent = nullptr;
301 QList<QgsLayerTreeNode *> mChildren;
306
308 virtual void makeOrphan() SIP_SKIP;
309
310 private:
311 QgsLayerTreeNode &operator=( const QgsLayerTreeNode & ) = delete;
312
313};
314
315
316
317
318#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.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
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
QList< QgsLayerTreeNode * > children() const
Gets list of children of the node. Children are owned by the parent.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
void willAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be added to a node within the tree.
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:73
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:105
The class is used as a container of context for various read/write operations on other objects.
#define str(x)
Definition: qgis.cpp:37
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:203