QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgslayertreenode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreenode.cpp
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#include "qgslayertreenode.h"
17
18#include "qgslayertree.h"
20#include "qgslayertreeutils.h"
21
22#include <QDomElement>
23#include <QStringList>
24
25#include "moc_qgslayertreenode.cpp"
26
28 : mNodeType( t )
29 , mChecked( checked )
30
31{
32}
33
35 : QObject( nullptr )
36 , mNodeType( other.mNodeType )
37 , mChecked( other.mChecked )
38 , mExpanded( other.mExpanded )
39 , mProperties( other.mProperties )
40{
41 QList<QgsLayerTreeNode *> clonedChildren;
42
43 for ( QgsLayerTreeNode *child : std::as_const( other.mChildren ) )
44 clonedChildren << child->clone();
45 insertChildrenPrivate( -1, clonedChildren );
46}
47
52
53QList<QgsLayerTreeNode *> QgsLayerTreeNode::abandonChildren()
54{
55 const QList<QgsLayerTreeNode *> orphans { mChildren };
56 mChildren.clear();
57 for ( auto orphan : std::as_const( orphans ) )
58 {
59 orphan->makeOrphan( );
60 }
61 return orphans;
62}
63
65{
66 disconnect();
67 mParent = nullptr;
68}
69
70QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsReadWriteContext &context )
71{
72 QgsLayerTreeNode *node = nullptr;
73 if ( element.tagName() == QLatin1String( "layer-tree-group" ) )
74 node = QgsLayerTreeGroup::readXml( element, context );
75 else if ( element.tagName() == QLatin1String( "layer-tree-layer" ) )
76 node = QgsLayerTreeLayer::readXml( element, context );
77 else if ( element.tagName() == QLatin1String( "layer-tree-custom-node" ) )
78 node = QgsLayerTreeCustomNode::readXml( element, context );
79
80 return node;
81}
82
83QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProject *project )
84{
85 QgsReadWriteContext context;
86 QgsPathResolver resolver;
87 if ( project )
88 resolver = project->pathResolver();
89 context.setPathResolver( resolver );
90 context.setProjectTranslator( const_cast<QgsProject *>( project ) );
91
92 QgsLayerTreeNode *node = readXml( element, context );
93 if ( node && node->nodeType() != NodeCustom )
94 node->resolveReferences( project );
95 return node;
96}
97
98
100{
101 if ( mChecked == checked )
102 return;
103 mChecked = checked;
104 emit visibilityChanged( this );
105}
106
111
113{
114 setItemVisibilityChecked( checked );
115 if ( mParent )
116 mParent->setItemVisibilityCheckedParentRecursive( checked );
117}
118
120{
121 return mChecked && ( !mParent || mParent->isVisible() );
122}
123
124
126{
127 return mExpanded;
128}
129
131{
132 if ( !mChecked )
133 return false;
134 const auto constMChildren = mChildren;
135 for ( QgsLayerTreeNode *child : constMChildren )
136 {
137 if ( !child->isItemVisibilityCheckedRecursive() )
138 return false;
139 }
140
141 return true;
142}
143
145{
146 if ( mChecked )
147 return false;
148 const auto constMChildren = mChildren;
149 for ( QgsLayerTreeNode *child : constMChildren )
150 {
151 if ( !child->isItemVisibilityUncheckedRecursive() )
152 return false;
153 }
154
155 return true;
156}
157
158void fetchCheckedLayers( const QgsLayerTreeNode *node, QList<QgsMapLayer *> &layers )
159{
160 if ( QgsLayerTree::isLayer( node ) )
161 {
162 const QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
163 if ( nodeLayer->isVisible() )
164 layers << nodeLayer->layer();
165 }
166
167 const auto constChildren = node->children();
168 for ( QgsLayerTreeNode *child : constChildren )
169 {
170 if ( QgsLayerTreeGroup *group = qobject_cast< QgsLayerTreeGroup * >( child ) )
171 {
172 if ( QgsGroupLayer *groupLayer = group->groupLayer() )
173 {
174 layers << groupLayer;
175 continue;
176 }
177 }
178
179 fetchCheckedLayers( child, layers );
180 }
181}
182
183QList<QgsMapLayer *> QgsLayerTreeNode::checkedLayers() const
184{
185 QList<QgsMapLayer *> layers;
186 fetchCheckedLayers( this, layers );
187 return layers;
188}
189
191{
192 int depth = 0;
194 while ( node )
195 {
196 node = node->parent();
197 ++depth;
198 }
199 return depth;
200}
201
202void QgsLayerTreeNode::setExpanded( bool expanded )
203{
204 if ( mExpanded == expanded )
205 return;
206
207 mExpanded = expanded;
208 emit expandedChanged( this, expanded );
209}
210
211
212void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
213{
214 if ( !mProperties.contains( key ) || mProperties.value( key ) != value )
215 {
216 mProperties.setValue( key, value );
217 emit customPropertyChanged( this, key );
218 }
219}
220
221QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
222{
223 return mProperties.value( key, defaultValue );
224}
225
227{
228 if ( mProperties.contains( key ) )
229 {
230 mProperties.remove( key );
231 emit customPropertyChanged( this, key );
232 }
233}
234
236{
237 return mProperties.keys();
238}
239
240void QgsLayerTreeNode::readCommonXml( const QDomElement &element )
241{
242 mProperties.readXml( element );
243}
244
245void QgsLayerTreeNode::writeCommonXml( QDomElement &element )
246{
247 QDomDocument doc( element.ownerDocument() );
248 mProperties.writeXml( element, doc );
249}
250
251void QgsLayerTreeNode::insertChildrenPrivate( int index, const QList<QgsLayerTreeNode *> &nodes )
252{
253 if ( nodes.isEmpty() )
254 return;
255
256 for ( QgsLayerTreeNode *node : nodes )
257 {
258 Q_ASSERT( !node->mParent );
259 node->mParent = this;
260 }
261
262 if ( index < 0 || index >= mChildren.count() )
263 index = mChildren.count();
264
265 for ( int i = 0; i < nodes.count(); ++i )
266 {
267 QgsLayerTreeNode *node = nodes.at( i );
268
269 const QList<QgsLayerTreeNode *> orphans { node->abandonChildren() };
270
271 emit willAddChildren( this, index + i, index + i );
272 mChildren.insert( index + i, node );
273 emit addedChildren( this, index + i, index + i );
274
275 // forward the signal towards the root
284
285 // Now add children
286 if ( ! orphans.isEmpty() )
287 {
288 node->insertChildrenPrivate( -1, orphans );
289 }
290
291 // ensure initial expanded state for node is respected
292 emit expandedChanged( node, node->isExpanded() );
293 }
294}
295
296void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
297{
298 if ( from < 0 || count <= 0 )
299 return;
300
301 const int to = from + count - 1;
302 if ( to >= mChildren.count() )
303 return;
304
305 // Remove in reverse order
306 while ( --count >= 0 )
307 {
308 const int last { from + count };
309 Q_ASSERT( last >= 0 && last < mChildren.count( ) );
310 QgsLayerTreeNode *node = mChildren.at( last );
311
312 // Remove children first
313 if ( ! node->children().isEmpty() )
314 {
315 node->removeChildrenPrivate( 0, node->children().count( ), destroy );
316 }
317
318 emit willRemoveChildren( this, last, last );
319 node = mChildren.takeAt( last );
320 if ( destroy )
321 {
322 delete node;
323 }
324 else
325 {
326 node->makeOrphan();
327 }
328 emit removedChildren( this, last, last );
329 }
330}
331
333{
334 int index = mChildren.indexOf( node );
335 if ( index < 0 )
336 return false;
337
338 int n = mChildren.size();
339
340 removeChildrenPrivate( index, 1, false );
341
342 return mChildren.size() < n;
343}
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
static QgsLayerTreeCustomNode * readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read custom node from XML element <layer-tree-custom-node> and return the newly created node (or null...
Layer tree group node serves as a container for layers and further groups.
static QgsLayerTreeGroup * readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or nullptr ...
Layer tree node points to a map layer.
static QgsLayerTreeLayer * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read layer node from XML.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Base class for nodes in a layer tree.
QList< QgsLayerTreeNode * > abandonChildren()
Removes the children, disconnect all the forwarded and external signals and sets their parent to null...
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.
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).
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
bool isItemVisibilityUncheckedRecursive() const
Returns whether this node is unchecked and all its children.
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.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
~QgsLayerTreeNode() override
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
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!
int depth() const
Returns the depth of this node, i.e.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
bool takeChild(QgsLayerTreeNode *node)
Remove a child from a node.
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
virtual void setItemVisibilityCheckedRecursive(bool checked)
Check or uncheck a node and all its children (taking into account exclusion rules).
bool mExpanded
whether the node should be shown in GUI as expanded
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
QStringList customProperties() const
Returns list of keys stored in custom properties.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children).
QList< QgsMapLayer * > checkedLayers() const
Returns a list of any checked layers which belong to this node or its children.
void readCommonXml(const QDomElement &element)
Read common XML elements.
QgsLayerTreeNode * mParent
pointer to the parent node - nullptr in case of root node
bool isItemVisibilityCheckedRecursive() const
Returns whether this node is checked and all its 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.
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Resolves relative paths into absolute paths and vice versa.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
QgsPathResolver pathResolver() const
Returns path resolver object with considering whether the project uses absolute or relative paths and...
A container for the context for various read/write operations on objects.
void setProjectTranslator(QgsProjectTranslator *projectTranslator)
Sets the project translator.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
void fetchCheckedLayers(const QgsLayerTreeNode *node, QList< QgsMapLayer * > &layers)