QGIS API Documentation 4.1.0-Master (201b6d107df)
Loading...
Searching...
No Matches
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 "qgis_sip.h"
22#include "qgsreadwritecontext.h"
23
24#include <QObject>
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
29class QDomElement;
30
31class QgsProject;
32class QgsMapLayer;
33
80class CORE_EXPORT QgsLayerTreeNode : public QObject
81{
82 Q_OBJECT
83
84#ifdef SIP_RUN
86 if ( sipCpp->inherits( "QgsLayerTreeNode" ) )
87 {
88 sipType = sipType_QgsLayerTreeNode;
89 QgsLayerTreeNode *node = qobject_cast<QgsLayerTreeNode *>( sipCpp );
90 if ( QgsLayerTree::isLayer( node ) )
91 sipType = sipType_QgsLayerTreeLayer;
92 else if ( qobject_cast<QgsLayerTree *>( sipCpp ) )
93 sipType = sipType_QgsLayerTree;
94 else if ( QgsLayerTree::isGroup( node ) )
95 sipType = sipType_QgsLayerTreeGroup;
96 else if ( QgsLayerTree::isCustomNode( node ) )
97 sipType = sipType_QgsLayerTreeCustomNode;
98 }
99 else
100 sipType = 0;
101 SIP_END
102#endif
103
104 public:
105
113
114 ~QgsLayerTreeNode() override;
115
116#ifdef SIP_RUN
117 // clang-format off
118 SIP_PYOBJECT __repr__();
119 % MethodCode
120 QString str = u"<QgsLayerTreeNode: %1>"_s.arg( sipCpp->name() );
121 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
122 % End
123
129 int __len__() const;
130 % MethodCode
131 sipRes = sipCpp->children().count();
132 % End
133
135 int __bool__() const;
136 % MethodCode
137 sipRes = true;
138 % End
139
146 SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsLayerTreeNode );
147 % MethodCode
148 const QList< QgsLayerTreeNode * > children = sipCpp->children();
149 const int count = children.count();
150 if ( a0 < 0 || a0 >= count )
151 {
152 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
153 sipIsErr = 1;
154 }
155 else
156 {
157 QgsLayerTreeNode *child = children.at( a0 );
158 sipRes = sipConvertFromType( child, sipType_QgsLayerTreeNode, NULL );
159 }
160 % End
161// clang-format on
162#endif
163
166 {
167 return mNodeType;
168 }
169
172 QList<QgsLayerTreeNode *> children() { return mChildren; }
174 QList<QgsLayerTreeNode *> children() const SIP_SKIP { return mChildren; }
175
181 QList<QgsLayerTreeNode *> abandonChildren() SIP_SKIP;
182
186 virtual QString name() const = 0;
187
191 virtual void setName( const QString &name ) = 0;
192
197 static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
198
203 static QgsLayerTreeNode *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
204
206 virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
207
209 virtual QString dump() const = 0;
210
212 virtual QgsLayerTreeNode *clone() const = 0 SIP_FACTORY;
213
222 virtual void resolveReferences( const QgsProject *project, bool looseMatching = false ) = 0;
223
227 bool isVisible() const;
228
232 bool itemVisibilityChecked() const { return mChecked; }
233
240 void setItemVisibilityChecked( bool checked );
241
245 virtual void setItemVisibilityCheckedRecursive( bool checked );
246
250 void setItemVisibilityCheckedParentRecursive( bool checked );
251
255 bool isItemVisibilityCheckedRecursive() const;
256
260 bool isItemVisibilityUncheckedRecursive() const;
261
266 QList< QgsMapLayer * > checkedLayers() const;
267
272 int depth() const;
273
275 bool isExpanded() const;
277 void setExpanded( bool expanded );
278
280 void setCustomProperty( const QString &key, const QVariant &value );
282 QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
284 void removeCustomProperty( const QString &key );
286 QStringList customProperties() const;
288 bool takeChild( QgsLayerTreeNode *node );
289
290 signals:
291
293 void willAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
295 void addedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
297 void willRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
299 void removedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
303 void customPropertyChanged( QgsLayerTreeNode *node, const QString &key );
305 void expandedChanged( QgsLayerTreeNode *node, bool expanded );
306
310 void nameChanged( QgsLayerTreeNode *node, QString name );
311
312 protected:
314 QgsLayerTreeNode( NodeType t, bool checked = true );
315 QgsLayerTreeNode( const QgsLayerTreeNode &other );
316
317 // low-level utility functions
318
320 void readCommonXml( const QDomElement &element );
322 void writeCommonXml( QDomElement &element );
323
325 void insertChildrenPrivate( int index, const QList<QgsLayerTreeNode *> &nodes );
327 void removeChildrenPrivate( int from, int count, bool destroy = true );
328
329 protected:
336 QList<QgsLayerTreeNode *> mChildren;
338 bool mExpanded = true;
341
343 virtual void makeOrphan() SIP_SKIP;
344
345 private:
346 QgsLayerTreeNode &operator=( const QgsLayerTreeNode & ) = delete;
347};
348
349
350#endif // QGSLAYERTREENODE_H
Base class for nodes in a layer tree.
virtual void setName(const QString &name)=0
Set name of the node.
virtual void makeOrphan()
Sets parent to nullptr and disconnects all external and forwarded signals.
NodeType
Enumeration of possible tree node types.
@ NodeCustom
Leaf node pointing to a custom object.
@ NodeGroup
Container of other groups and layers.
@ NodeLayer
Leaf node pointing to a layer.
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.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well).
virtual QString dump() const =0
Returns string with layer tree structure. For debug purposes only.
virtual void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context)=0
Write layer tree to XML.
static QgsLayerTreeNode * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read layer tree from XML.
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.
virtual QString name() const =0
Returns name of the node.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
QgsLayerTreeNode(NodeType t, bool checked=true)
Constructor.
void writeCommonXml(QDomElement &element)
Write common XML elements.
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
void insertChildrenPrivate(int index, const QList< QgsLayerTreeNode * > &nodes)
Low-level insertion of children to the node. The children must not have any parent yet!
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 readCommonXml(const QDomElement &element)
Read common XML elements.
QgsLayerTreeNode * mParent
pointer to the parent node - nullptr in case of root node
virtual QgsLayerTreeNode * clone() const =0
Create a copy of the node. Returns new instance.
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children).
virtual void resolveReferences(const QgsProject *project, bool looseMatching=false)=0
Turn textual references to layers into map layer object from project.
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
void removeChildrenPrivate(int from, int count, bool destroy=true)
Low-level removal of children from the node.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static bool isCustomNode(const QgsLayerTreeNode *node)
Check whether the node is a valid custom node.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:114
A container for the context for various read/write operations on objects.
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:237
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:198
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_END
Definition qgis_sip.h:215