QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
19 #include "qgslayertreeutils.h"
20 
21 #include <QDomElement>
22 #include <QStringList>
23 
24 
26  : mNodeType( t )
27  , mChecked( checked )
28  , mExpanded( true )
29 {
30 }
31 
33  : QObject( nullptr )
34  , mNodeType( other.mNodeType )
35  , mChecked( other.mChecked )
36  , mExpanded( other.mExpanded )
37  , mProperties( other.mProperties )
38 {
39  QList<QgsLayerTreeNode *> clonedChildren;
40 
41  for ( QgsLayerTreeNode *child : std::as_const( other.mChildren ) )
42  clonedChildren << child->clone();
43  insertChildrenPrivate( -1, clonedChildren );
44 }
45 
47 {
48  qDeleteAll( mChildren );
49 }
50 
51 QList<QgsLayerTreeNode *> QgsLayerTreeNode::abandonChildren()
52 {
53  const QList<QgsLayerTreeNode *> orphans { mChildren };
54  mChildren.clear();
55  for ( auto orphan : std::as_const( orphans ) )
56  {
57  orphan->makeOrphan( );
58  }
59  return orphans;
60 }
61 
63 {
64  disconnect();
65  mParent = nullptr;
66 }
67 
68 QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsReadWriteContext &context )
69 {
70  QgsLayerTreeNode *node = nullptr;
71  if ( element.tagName() == QLatin1String( "layer-tree-group" ) )
72  node = QgsLayerTreeGroup::readXml( element, context );
73  else if ( element.tagName() == QLatin1String( "layer-tree-layer" ) )
74  node = QgsLayerTreeLayer::readXml( element, context );
75 
76  return node;
77 }
78 
79 QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProject *project )
80 {
81  QgsReadWriteContext context;
82  QgsPathResolver resolver;
83  if ( project )
84  resolver = project->pathResolver();
85  context.setPathResolver( resolver );
86  context.setProjectTranslator( const_cast<QgsProject *>( project ) );
87 
88  QgsLayerTreeNode *node = readXml( element, context );
89  if ( node )
90  node->resolveReferences( project );
91  return node;
92 }
93 
94 
96 {
97  if ( mChecked == checked )
98  return;
99  mChecked = checked;
100  emit visibilityChanged( this );
101 }
102 
104 {
105  setItemVisibilityChecked( checked );
106 }
107 
109 {
110  setItemVisibilityChecked( checked );
111  if ( mParent )
113 }
114 
116 {
117  return mChecked && ( !mParent || mParent->isVisible() );
118 }
119 
120 
122 {
123  return mExpanded;
124 }
125 
127 {
128  if ( !mChecked )
129  return false;
130  const auto constMChildren = mChildren;
131  for ( QgsLayerTreeNode *child : constMChildren )
132  {
133  if ( !child->isItemVisibilityCheckedRecursive() )
134  return false;
135  }
136 
137  return true;
138 }
139 
141 {
142  if ( mChecked )
143  return false;
144  const auto constMChildren = mChildren;
145  for ( QgsLayerTreeNode *child : constMChildren )
146  {
147  if ( !child->isItemVisibilityUncheckedRecursive() )
148  return false;
149  }
150 
151  return true;
152 }
153 
154 void fetchCheckedLayers( const QgsLayerTreeNode *node, QList<QgsMapLayer *> &layers )
155 {
156  if ( QgsLayerTree::isLayer( node ) )
157  {
158  const QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
159  if ( nodeLayer->isVisible() )
160  layers << nodeLayer->layer();
161  }
162 
163  const auto constChildren = node->children();
164  for ( QgsLayerTreeNode *child : constChildren )
165  fetchCheckedLayers( child, layers );
166 }
167 
168 QList<QgsMapLayer *> QgsLayerTreeNode::checkedLayers() const
169 {
170  QList<QgsMapLayer *> layers;
171  fetchCheckedLayers( this, layers );
172  return layers;
173 }
174 
176 {
177  int depth = 0;
178  QgsLayerTreeNode *node = mParent;
179  while ( node )
180  {
181  node = node->parent();
182  ++depth;
183  }
184  return depth;
185 }
186 
187 void QgsLayerTreeNode::setExpanded( bool expanded )
188 {
189  if ( mExpanded == expanded )
190  return;
191 
192  mExpanded = expanded;
193  emit expandedChanged( this, expanded );
194 }
195 
196 
197 void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
198 {
199  if ( !mProperties.contains( key ) || mProperties.value( key ) != value )
200  {
201  mProperties.setValue( key, value );
202  emit customPropertyChanged( this, key );
203  }
204 }
205 
206 QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
207 {
208  return mProperties.value( key, defaultValue );
209 }
210 
211 void QgsLayerTreeNode::removeCustomProperty( const QString &key )
212 {
213  if ( mProperties.contains( key ) )
214  {
215  mProperties.remove( key );
216  emit customPropertyChanged( this, key );
217  }
218 }
219 
221 {
222  return mProperties.keys();
223 }
224 
225 void QgsLayerTreeNode::readCommonXml( QDomElement &element )
226 {
227  mProperties.readXml( element );
228 }
229 
230 void QgsLayerTreeNode::writeCommonXml( QDomElement &element )
231 {
232  QDomDocument doc( element.ownerDocument() );
233  mProperties.writeXml( element, doc );
234 }
235 
236 void QgsLayerTreeNode::insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes )
237 {
238  if ( nodes.isEmpty() )
239  return;
240 
241  const auto constNodes = nodes;
242  for ( QgsLayerTreeNode *node : constNodes )
243  {
244  Q_ASSERT( !node->mParent );
245  node->mParent = this;
246  }
247 
248  if ( index < 0 || index >= mChildren.count() )
249  index = mChildren.count();
250 
251  for ( int i = 0; i < nodes.count(); ++i )
252  {
253  QgsLayerTreeNode *node = nodes.at( i );
254 
255  const QList<QgsLayerTreeNode *> orphans { node->abandonChildren() };
256 
257  emit willAddChildren( this, index + i, index + i );
258  mChildren.insert( index + i, node );
259  emit addedChildren( this, index + i, index + i );
260 
261  // forward the signal towards the root
270 
271  // Now add children
272  if ( ! orphans.isEmpty() )
273  {
274  node->insertChildrenPrivate( -1, orphans );
275  }
276 
277  }
278 }
279 
280 void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
281 {
282  if ( from < 0 || count <= 0 )
283  return;
284 
285  const int to = from + count - 1;
286  if ( to >= mChildren.count() )
287  return;
288 
289  // Remove in reverse order
290  while ( --count >= 0 )
291  {
292  const int last { from + count };
293  Q_ASSERT( last >= 0 && last < mChildren.count( ) );
294  QgsLayerTreeNode *node = mChildren.at( last );
295 
296  // Remove children first
297  if ( ! node->children().isEmpty() )
298  {
299  node->removeChildrenPrivate( 0, node->children().count( ), destroy );
300  }
301 
302  emit willRemoveChildren( this, last, last );
303  node = mChildren.takeAt( last );
304  if ( destroy )
305  {
306  delete node;
307  }
308  else
309  {
310  node->makeOrphan();
311  }
312  emit removedChildren( this, last, last );
313  }
314 }
315 
317 {
318  int index = mChildren.indexOf( node );
319  if ( index < 0 )
320  return false;
321 
322  int n = mChildren.size();
323 
324  removeChildrenPrivate( index, 1, false );
325 
326  return mChildren.size() < n;
327 }
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 childrens, disconnect all the forwarded and external signals and sets their parent to nul...
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 insertChildrenPrivate(int index, QList< QgsLayerTreeNode * > nodes)
Low-level insertion of children to the node. The children must not have any parent yet!
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.
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(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.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
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.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
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 bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
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:99
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)