QGIS API Documentation 4.1.0-Master (60fea48833c)
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 <QString>
24#include <QStringList>
25
26#include "moc_qgslayertreenode.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : mNodeType( t )
32 , mChecked( checked )
33
34{}
35
37 : QObject( nullptr )
38 , mNodeType( other.mNodeType )
39 , mChecked( other.mChecked )
40 , mExpanded( other.mExpanded )
41 , mProperties( other.mProperties )
42{
43 QList<QgsLayerTreeNode *> clonedChildren;
44
45 for ( QgsLayerTreeNode *child : std::as_const( other.mChildren ) )
46 clonedChildren << child->clone();
47 insertChildrenPrivate( -1, clonedChildren );
48}
49
54
55QList<QgsLayerTreeNode *> QgsLayerTreeNode::abandonChildren()
56{
57 const QList<QgsLayerTreeNode *> orphans { mChildren };
58 mChildren.clear();
59 for ( auto orphan : std::as_const( orphans ) )
60 {
61 orphan->makeOrphan();
62 }
63 return orphans;
64}
65
67{
68 disconnect();
69 mParent = nullptr;
70}
71
72QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsReadWriteContext &context )
73{
74 QgsLayerTreeNode *node = nullptr;
75 if ( element.tagName() == "layer-tree-group"_L1 )
76 node = QgsLayerTreeGroup::readXml( element, context );
77 else if ( element.tagName() == "layer-tree-layer"_L1 )
78 node = QgsLayerTreeLayer::readXml( element, context );
79 else if ( element.tagName() == "layer-tree-custom-node"_L1 )
80 node = QgsLayerTreeCustomNode::readXml( element, context );
81
82 return node;
83}
84
85QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProject *project )
86{
87 QgsReadWriteContext context;
88 QgsPathResolver resolver;
89 if ( project )
90 resolver = project->pathResolver();
91 context.setPathResolver( resolver );
92 context.setProjectTranslator( const_cast<QgsProject *>( project ) );
93
94 QgsLayerTreeNode *node = readXml( element, context );
95 if ( node && node->nodeType() != NodeCustom )
96 node->resolveReferences( project );
97 return node;
98}
99
100
102{
103 if ( mChecked == checked )
104 return;
105 mChecked = checked;
106 emit visibilityChanged( this );
107}
108
113
115{
116 setItemVisibilityChecked( checked );
117 if ( mParent )
118 mParent->setItemVisibilityCheckedParentRecursive( checked );
119}
120
122{
123 return mChecked && ( !mParent || mParent->isVisible() );
124}
125
126
128{
129 return mExpanded;
130}
131
133{
134 if ( !mChecked )
135 return false;
136 const auto constMChildren = mChildren;
137 for ( QgsLayerTreeNode *child : constMChildren )
138 {
139 if ( !child->isItemVisibilityCheckedRecursive() )
140 return false;
141 }
142
143 return true;
144}
145
147{
148 if ( mChecked )
149 return false;
150 const auto constMChildren = mChildren;
151 for ( QgsLayerTreeNode *child : constMChildren )
152 {
153 if ( !child->isItemVisibilityUncheckedRecursive() )
154 return false;
155 }
156
157 return true;
158}
159
160void fetchCheckedLayers( const QgsLayerTreeNode *node, QList<QgsMapLayer *> &layers )
161{
162 if ( QgsLayerTree::isLayer( node ) )
163 {
164 const QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
165 if ( nodeLayer->isVisible() )
166 layers << nodeLayer->layer();
167 }
168
169 const auto constChildren = node->children();
170 for ( QgsLayerTreeNode *child : constChildren )
171 {
172 if ( QgsLayerTreeGroup *group = qobject_cast< QgsLayerTreeGroup * >( child ) )
173 {
174 if ( QgsGroupLayer *groupLayer = group->groupLayer() )
175 {
176 layers << groupLayer;
177 continue;
178 }
179 }
180
181 fetchCheckedLayers( child, layers );
182 }
183}
184
185QList<QgsMapLayer *> QgsLayerTreeNode::checkedLayers() const
186{
187 QList<QgsMapLayer *> layers;
188 fetchCheckedLayers( this, layers );
189 return layers;
190}
191
193{
194 int depth = 0;
196 while ( node )
197 {
198 node = node->parent();
199 ++depth;
200 }
201 return depth;
202}
203
204void QgsLayerTreeNode::setExpanded( bool expanded )
205{
206 if ( mExpanded == expanded )
207 return;
208
209 mExpanded = expanded;
210 emit expandedChanged( this, expanded );
211}
212
213
214void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
215{
216 if ( !mProperties.contains( key ) || mProperties.value( key ) != value )
217 {
218 mProperties.setValue( key, value );
219 emit customPropertyChanged( this, key );
220 }
221}
222
223QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
224{
225 return mProperties.value( key, defaultValue );
226}
227
229{
230 if ( mProperties.contains( key ) )
231 {
232 mProperties.remove( key );
233 emit customPropertyChanged( this, key );
234 }
235}
236
238{
239 return mProperties.keys();
240}
241
242void QgsLayerTreeNode::readCommonXml( const QDomElement &element )
243{
244 mProperties.readXml( element );
245}
246
247void QgsLayerTreeNode::writeCommonXml( QDomElement &element )
248{
249 QDomDocument doc( element.ownerDocument() );
250 mProperties.writeXml( element, doc );
251}
252
253void QgsLayerTreeNode::insertChildrenPrivate( int index, const QList<QgsLayerTreeNode *> &nodes )
254{
255 if ( nodes.isEmpty() )
256 return;
257
258 for ( QgsLayerTreeNode *node : nodes )
259 {
260 Q_ASSERT( !node->mParent );
261 node->mParent = this;
262 }
263
264 if ( index < 0 || index >= mChildren.count() )
265 index = mChildren.count();
266
267 for ( int i = 0; i < nodes.count(); ++i )
268 {
269 QgsLayerTreeNode *node = nodes.at( i );
270
271 const QList<QgsLayerTreeNode *> orphans { node->abandonChildren() };
272
273 emit willAddChildren( this, index + i, index + i );
274 mChildren.insert( index + i, node );
275 emit addedChildren( this, index + i, index + i );
276
277 // forward the signal towards the root
286
287 // Now add children
288 if ( !orphans.isEmpty() )
289 {
290 node->insertChildrenPrivate( -1, orphans );
291 }
292
293 // ensure initial expanded state for node is respected
294 emit expandedChanged( node, node->isExpanded() );
295 }
296}
297
298void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
299{
300 if ( from < 0 || count <= 0 )
301 return;
302
303 const int to = from + count - 1;
304 if ( to >= mChildren.count() )
305 return;
306
307 // Remove in reverse order
308 while ( --count >= 0 )
309 {
310 const int last { from + count };
311 Q_ASSERT( last >= 0 && last < mChildren.count() );
312 QgsLayerTreeNode *node = mChildren.at( last );
313
314 // Remove children first
315 if ( !node->children().isEmpty() )
316 {
317 node->removeChildrenPrivate( 0, node->children().count(), destroy );
318 }
319
320 emit willRemoveChildren( this, last, last );
321 node = mChildren.takeAt( last );
322 if ( destroy )
323 {
324 delete node;
325 }
326 else
327 {
328 node->makeOrphan();
329 }
330 emit removedChildren( this, last, last );
331 }
332}
333
335{
336 int index = mChildren.indexOf( node );
337 if ( index < 0 )
338 return false;
339
340 int n = mChildren.size();
341
342 removeChildrenPrivate( index, 1, false );
343
344 return mChildren.size() < n;
345}
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:113
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)