QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgslayertreenode.cpp"
18
19#include "qgslayertree.h"
20#include "qgslayertreeutils.h"
21
22#include <QDomElement>
23#include <QStringList>
24
25
27 : mNodeType( t )
28 , mChecked( checked )
29 , mExpanded( true )
30{
31}
32
34 : QObject( nullptr )
35 , mNodeType( other.mNodeType )
36 , mChecked( other.mChecked )
37 , mExpanded( other.mExpanded )
38 , mProperties( other.mProperties )
39{
40 QList<QgsLayerTreeNode *> clonedChildren;
41
42 for ( QgsLayerTreeNode *child : std::as_const( other.mChildren ) )
43 clonedChildren << child->clone();
44 insertChildrenPrivate( -1, clonedChildren );
45}
46
51
52QList<QgsLayerTreeNode *> QgsLayerTreeNode::abandonChildren()
53{
54 const QList<QgsLayerTreeNode *> orphans { mChildren };
55 mChildren.clear();
56 for ( auto orphan : std::as_const( orphans ) )
57 {
58 orphan->makeOrphan( );
59 }
60 return orphans;
61}
62
64{
65 disconnect();
66 mParent = nullptr;
67}
68
69QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsReadWriteContext &context )
70{
71 QgsLayerTreeNode *node = nullptr;
72 if ( element.tagName() == QLatin1String( "layer-tree-group" ) )
73 node = QgsLayerTreeGroup::readXml( element, context );
74 else if ( element.tagName() == QLatin1String( "layer-tree-layer" ) )
75 node = QgsLayerTreeLayer::readXml( element, context );
76
77 return node;
78}
79
80QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProject *project )
81{
82 QgsReadWriteContext context;
83 QgsPathResolver resolver;
84 if ( project )
85 resolver = project->pathResolver();
86 context.setPathResolver( resolver );
87 context.setProjectTranslator( const_cast<QgsProject *>( project ) );
88
89 QgsLayerTreeNode *node = readXml( element, context );
90 if ( node )
91 node->resolveReferences( project );
92 return node;
93}
94
95
97{
98 if ( mChecked == checked )
99 return;
100 mChecked = checked;
101 emit visibilityChanged( this );
102}
103
108
115
117{
118 return mChecked && ( !mParent || mParent->isVisible() );
119}
120
121
123{
124 return mExpanded;
125}
126
128{
129 if ( !mChecked )
130 return false;
131 const auto constMChildren = mChildren;
132 for ( QgsLayerTreeNode *child : constMChildren )
133 {
134 if ( !child->isItemVisibilityCheckedRecursive() )
135 return false;
136 }
137
138 return true;
139}
140
142{
143 if ( mChecked )
144 return false;
145 const auto constMChildren = mChildren;
146 for ( QgsLayerTreeNode *child : constMChildren )
147 {
148 if ( !child->isItemVisibilityUncheckedRecursive() )
149 return false;
150 }
151
152 return true;
153}
154
155void fetchCheckedLayers( const QgsLayerTreeNode *node, QList<QgsMapLayer *> &layers )
156{
157 if ( QgsLayerTree::isLayer( node ) )
158 {
159 const QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
160 if ( nodeLayer->isVisible() )
161 layers << nodeLayer->layer();
162 }
163
164 const auto constChildren = node->children();
165 for ( QgsLayerTreeNode *child : constChildren )
166 {
167 if ( QgsLayerTreeGroup *group = qobject_cast< QgsLayerTreeGroup * >( child ) )
168 {
169 if ( QgsGroupLayer *groupLayer = group->groupLayer() )
170 {
171 layers << groupLayer;
172 continue;
173 }
174 }
175
176 fetchCheckedLayers( child, layers );
177 }
178}
179
180QList<QgsMapLayer *> QgsLayerTreeNode::checkedLayers() const
181{
182 QList<QgsMapLayer *> layers;
183 fetchCheckedLayers( this, layers );
184 return layers;
185}
186
188{
189 int depth = 0;
191 while ( node )
192 {
193 node = node->parent();
194 ++depth;
195 }
196 return depth;
197}
198
199void QgsLayerTreeNode::setExpanded( bool expanded )
200{
201 if ( mExpanded == expanded )
202 return;
203
204 mExpanded = expanded;
205 emit expandedChanged( this, expanded );
206}
207
208
209void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
210{
211 if ( !mProperties.contains( key ) || mProperties.value( key ) != value )
212 {
213 mProperties.setValue( key, value );
214 emit customPropertyChanged( this, key );
215 }
216}
217
218QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
219{
220 return mProperties.value( key, defaultValue );
221}
222
224{
225 if ( mProperties.contains( key ) )
226 {
227 mProperties.remove( key );
228 emit customPropertyChanged( this, key );
229 }
230}
231
233{
234 return mProperties.keys();
235}
236
237void QgsLayerTreeNode::readCommonXml( QDomElement &element )
238{
239 mProperties.readXml( element );
240}
241
242void QgsLayerTreeNode::writeCommonXml( QDomElement &element )
243{
244 QDomDocument doc( element.ownerDocument() );
245 mProperties.writeXml( element, doc );
246}
247
248void QgsLayerTreeNode::insertChildrenPrivate( int index, const QList<QgsLayerTreeNode *> &nodes )
249{
250 if ( nodes.isEmpty() )
251 return;
252
253 for ( QgsLayerTreeNode *node : nodes )
254 {
255 Q_ASSERT( !node->mParent );
256 node->mParent = this;
257 }
258
259 if ( index < 0 || index >= mChildren.count() )
260 index = mChildren.count();
261
262 for ( int i = 0; i < nodes.count(); ++i )
263 {
264 QgsLayerTreeNode *node = nodes.at( i );
265
266 const QList<QgsLayerTreeNode *> orphans { node->abandonChildren() };
267
268 emit willAddChildren( this, index + i, index + i );
269 mChildren.insert( index + i, node );
270 emit addedChildren( this, index + i, index + i );
271
272 // forward the signal towards the root
281
282 // Now add children
283 if ( ! orphans.isEmpty() )
284 {
285 node->insertChildrenPrivate( -1, orphans );
286 }
287
288 // ensure initial expanded state for node is respected
289 emit expandedChanged( node, node->isExpanded() );
290 }
291}
292
293void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
294{
295 if ( from < 0 || count <= 0 )
296 return;
297
298 const int to = from + count - 1;
299 if ( to >= mChildren.count() )
300 return;
301
302 // Remove in reverse order
303 while ( --count >= 0 )
304 {
305 const int last { from + count };
306 Q_ASSERT( last >= 0 && last < mChildren.count( ) );
307 QgsLayerTreeNode *node = mChildren.at( last );
308
309 // Remove children first
310 if ( ! node->children().isEmpty() )
311 {
312 node->removeChildrenPrivate( 0, node->children().count( ), destroy );
313 }
314
315 emit willRemoveChildren( this, last, last );
316 node = mChildren.takeAt( last );
317 if ( destroy )
318 {
319 delete node;
320 }
321 else
322 {
323 node->makeOrphan();
324 }
325 emit removedChildren( this, last, last );
326 }
327}
328
330{
331 int index = mChildren.indexOf( node );
332 if ( index < 0 )
333 return false;
334
335 int n = mChildren.size();
336
337 removeChildrenPrivate( index, 1, false );
338
339 return mChildren.size() < n;
340}
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Layer tree group node serves as a container for layers and further groups.
static QgsLayerTreeGroup * readXml(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.
This class is a base class for nodes in a layer tree.
void readCommonXml(QDomElement &element)
Read common XML elements.
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.
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.
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.
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.
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.
void setValue(const QString &key, const QVariant &value)
Add an entry to the store with the specified key.
QStringList keys() const
Returns a list of all stored keys.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Writes the store contents to an XML node.
void remove(const QString &key)
Removes a key (entry) from the store.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for the given key.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
bool contains(const QString &key) const
Returns true if the properties contains a key with the specified name.
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:107
QgsPathResolver pathResolver() const
Returns path resolver object with considering whether the project uses absolute or relative paths and...
The class is used as a container of context for various read/write operations on other 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)