QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
qgslayertreegroup.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertreegroup.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 "qgslayertreegroup.h"
17 
18 #include "qgslayertree.h"
19 #include "qgslayertreeutils.h"
20 #include "qgsmaplayer.h"
21 #include "qgsgrouplayer.h"
22 #include "qgspainting.h"
23 
24 #include <QDomElement>
25 #include <QStringList>
26 
27 
28 QgsLayerTreeGroup::QgsLayerTreeGroup( const QString &name, bool checked )
29  : QgsLayerTreeNode( NodeGroup, checked )
30  , mName( name )
31 {
32  init();
33 }
34 
36  : QgsLayerTreeNode( other )
37  , mName( other.mName )
38  , mChangingChildVisibility( other.mChangingChildVisibility )
39  , mMutuallyExclusive( other.mMutuallyExclusive )
40  , mMutuallyExclusiveChildIndex( other.mMutuallyExclusiveChildIndex )
41  , mGroupLayer( other.mGroupLayer )
42 {
43  init();
44 }
45 
46 void QgsLayerTreeGroup::init()
47 {
49  connect( this, &QgsLayerTreeNode::addedChildren, this, &QgsLayerTreeGroup::updateGroupLayers );
50  connect( this, &QgsLayerTreeNode::removedChildren, this, &QgsLayerTreeGroup::updateGroupLayers );
51  connect( this, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeGroup::updateGroupLayers );
52 }
53 
54 QString QgsLayerTreeGroup::name() const
55 {
56  return mName;
57 }
58 
59 void QgsLayerTreeGroup::setName( const QString &n )
60 {
61  if ( mName == n )
62  return;
63 
64  mName = n;
65  emit nameChanged( this, n );
66 }
67 
68 
69 QgsLayerTreeGroup *QgsLayerTreeGroup::insertGroup( int index, const QString &name )
70 {
72  insertChildNode( index, grp );
73  return grp;
74 }
75 
77 {
79  addChildNode( grp );
80  return grp;
81 }
82 
84 {
85  if ( !layer )
86  return nullptr;
87 
88  QgsLayerTreeLayer *ll = new QgsLayerTreeLayer( layer );
89  insertChildNode( index, ll );
90 
91  updateGroupLayers();
92  return ll;
93 }
94 
96 {
97  if ( !layer )
98  return nullptr;
99 
100  QgsLayerTreeLayer *ll = new QgsLayerTreeLayer( layer );
101  addChildNode( ll );
102 
103  updateGroupLayers();
104  return ll;
105 }
106 
108 {
109  QList<QgsLayerTreeNode *> nodes;
110  nodes << node;
111  insertChildNodes( index, nodes );
112 }
113 
114 void QgsLayerTreeGroup::insertChildNodes( int index, const QList<QgsLayerTreeNode *> &nodes )
115 {
116  QgsLayerTreeNode *meChild = nullptr;
118  meChild = mChildren.at( mMutuallyExclusiveChildIndex );
119 
120  // low-level insert
121  insertChildrenPrivate( index, nodes );
122 
123  if ( mMutuallyExclusive )
124  {
125  if ( meChild )
126  {
127  // the child could have change its index - or the new children may have been also set as visible
128  mMutuallyExclusiveChildIndex = mChildren.indexOf( meChild );
129  }
130  else if ( mChecked )
131  {
132  // we have not picked a child index yet, but we should pick one now
133  // ... so pick the first one from the newly added
134  if ( index == -1 )
135  index = mChildren.count() - nodes.count(); // get real insertion index
137  }
139  }
140 
141  updateGroupLayers();
142 }
143 
145 {
146  insertChildNode( -1, node );
147 
148  updateGroupLayers();
149 }
150 
152 {
153  int i = mChildren.indexOf( node );
154  if ( i >= 0 )
155  removeChildren( i, 1 );
156 
157  updateGroupLayers();
158 }
159 
161 {
162  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
163  {
164  if ( QgsLayerTree::isLayer( child ) )
165  {
166  QgsLayerTreeLayer *childLayer = QgsLayerTree::toLayer( child );
167  if ( childLayer->layer() == layer )
168  {
169  removeChildren( mChildren.indexOf( child ), 1 );
170  break;
171  }
172  }
173  }
174 
175  updateGroupLayers();
176 }
177 
178 void QgsLayerTreeGroup::removeChildren( int from, int count )
179 {
180  QgsLayerTreeNode *meChild = nullptr;
182  meChild = mChildren.at( mMutuallyExclusiveChildIndex );
183 
184  removeChildrenPrivate( from, count );
185 
186  if ( meChild )
187  {
188  // the child could have change its index - or may have been removed completely
189  mMutuallyExclusiveChildIndex = mChildren.indexOf( meChild );
190  // we need to uncheck this group
191  //if ( mMutuallyExclusiveChildIndex == -1 )
192  // setItemVisibilityChecked( false );
193  }
194 
195  updateGroupLayers();
196 }
197 
199 {
200  // clean the layer tree by removing empty group
201  const auto childNodes = children();
202  for ( QgsLayerTreeNode *treeNode : childNodes )
203  {
204  if ( treeNode->nodeType() == QgsLayerTreeNode::NodeGroup )
205  {
206  QgsLayerTreeGroup *treeGroup = qobject_cast<QgsLayerTreeGroup *>( treeNode );
207  if ( treeGroup->findLayerIds().isEmpty() )
208  removeChildNode( treeNode );
209  else
211  }
212  }
213 
214  updateGroupLayers();
215 }
216 
218 {
219  removeChildren( 0, mChildren.count() );
220 }
221 
223 {
224  if ( !layer )
225  return nullptr;
226 
227  return findLayer( layer->id() );
228 }
229 
230 QgsLayerTreeLayer *QgsLayerTreeGroup::findLayer( const QString &layerId ) const
231 {
232  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
233  {
234  if ( QgsLayerTree::isLayer( child ) )
235  {
236  QgsLayerTreeLayer *childLayer = QgsLayerTree::toLayer( child );
237  if ( childLayer->layerId() == layerId )
238  return childLayer;
239  }
240  else if ( QgsLayerTree::isGroup( child ) )
241  {
242  QgsLayerTreeLayer *res = QgsLayerTree::toGroup( child )->findLayer( layerId );
243  if ( res )
244  return res;
245  }
246  }
247  return nullptr;
248 }
249 
250 QList<QgsLayerTreeLayer *> QgsLayerTreeGroup::findLayers() const
251 {
252  QList<QgsLayerTreeLayer *> list;
253  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
254  {
255  if ( QgsLayerTree::isLayer( child ) )
256  list << QgsLayerTree::toLayer( child );
257  else if ( QgsLayerTree::isGroup( child ) )
258  list << QgsLayerTree::toGroup( child )->findLayers();
259  }
260  return list;
261 }
262 
263 void QgsLayerTreeGroup::reorderGroupLayers( const QList<QgsMapLayer *> &order )
264 {
265  const QList< QgsLayerTreeLayer * > childLayers = findLayers();
266  int targetIndex = 0;
267  for ( QgsMapLayer *targetLayer : order )
268  {
269  for ( QgsLayerTreeLayer *layerNode : childLayers )
270  {
271  if ( layerNode->layer() == targetLayer )
272  {
273  QgsLayerTreeLayer *cloned = layerNode->clone();
274  insertChildNode( targetIndex, cloned );
275  removeChildNode( layerNode );
276  targetIndex++;
277  break;
278  }
279  }
280  }
281 }
282 
284 {
285  QList<QgsMapLayer *> list;
286  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
287  {
288  if ( QgsLayerTree::isLayer( child ) )
289  {
290  QgsMapLayer *layer = QgsLayerTree::toLayer( child )->layer();
291  if ( !layer || !layer->isSpatial() )
292  continue;
293  list << layer;
294  }
295  else if ( QgsLayerTree::isGroup( child ) )
296  {
297  QgsLayerTreeGroup *group = QgsLayerTree::toGroup( child );
298  if ( group->groupLayer() )
299  {
300  list << group->groupLayer();
301  }
302  else
303  {
304  list << group->layerOrderRespectingGroupLayers();
305  }
306  }
307  }
308  return list;
309 }
310 
312 {
313  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
314  {
315  if ( QgsLayerTree::isGroup( child ) )
316  {
317  QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
318  if ( childGroup->name() == name )
319  return childGroup;
320  else
321  {
322  QgsLayerTreeGroup *grp = childGroup->findGroup( name );
323  if ( grp )
324  return grp;
325  }
326  }
327  }
328  return nullptr;
329 }
330 
331 QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups( bool recursive ) const
332 {
333  QList<QgsLayerTreeGroup *> list;
334 
335  for ( QgsLayerTreeNode *child : mChildren )
336  {
337  if ( QgsLayerTree::isGroup( child ) )
338  {
339  QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
340  list << childGroup;
341  if ( recursive )
342  list << childGroup->findGroups( recursive );
343  }
344  }
345  return list;
346 }
347 
348 QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsReadWriteContext &context )
349 {
350  if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
351  return nullptr;
352 
353  QString name = context.projectTranslator()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
354  bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
355  bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
356  bool isMutuallyExclusive = element.attribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
357  int mutuallyExclusiveChildIndex = element.attribute( QStringLiteral( "mutually-exclusive-child" ), QStringLiteral( "-1" ) ).toInt();
358 
359  QgsLayerTreeGroup *groupNode = new QgsLayerTreeGroup( name, checked );
360  groupNode->setExpanded( isExpanded );
361 
362  groupNode->readCommonXml( element );
363 
364  groupNode->readChildrenFromXml( element, context );
365 
366  groupNode->setIsMutuallyExclusive( isMutuallyExclusive, mutuallyExclusiveChildIndex );
367 
368  groupNode->mGroupLayer = QgsMapLayerRef( element.attribute( QStringLiteral( "groupLayer" ) ) );
369 
370  return groupNode;
371 }
372 
373 QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
374 {
375  QgsLayerTreeGroup *node = readXml( element, context );
376  if ( node )
377  node->resolveReferences( project );
378  return node;
379 }
380 
381 void QgsLayerTreeGroup::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
382 {
383  QDomDocument doc = parentElement.ownerDocument();
384  QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-group" ) );
385  elem.setAttribute( QStringLiteral( "name" ), mName );
386  elem.setAttribute( QStringLiteral( "expanded" ), mExpanded ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
387  elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
388  if ( mMutuallyExclusive )
389  {
390  elem.setAttribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "1" ) );
391  elem.setAttribute( QStringLiteral( "mutually-exclusive-child" ), mMutuallyExclusiveChildIndex );
392  }
393  elem.setAttribute( QStringLiteral( "groupLayer" ), mGroupLayer.layerId );
394 
395  writeCommonXml( elem );
396 
397  for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
398  node->writeXml( elem, context );
399 
400  parentElement.appendChild( elem );
401 }
402 
403 void QgsLayerTreeGroup::readChildrenFromXml( QDomElement &element, const QgsReadWriteContext &context )
404 {
405  QList<QgsLayerTreeNode *> nodes;
406  QDomElement childElem = element.firstChildElement();
407  while ( !childElem.isNull() )
408  {
409  QgsLayerTreeNode *newNode = QgsLayerTreeNode::readXml( childElem, context );
410  if ( newNode )
411  nodes << newNode;
412 
413  childElem = childElem.nextSiblingElement();
414  }
415 
416  insertChildNodes( -1, nodes );
417 }
418 
419 QString QgsLayerTreeGroup::dump() const
420 {
421  QString header = QStringLiteral( "GROUP: %1 checked=%2 expanded=%3\n" ).arg( name() ).arg( mChecked ).arg( mExpanded );
422  QStringList childrenDump;
423  for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
424  childrenDump << node->dump().split( '\n' );
425  for ( int i = 0; i < childrenDump.count(); ++i )
426  childrenDump[i].prepend( " " );
427  return header + childrenDump.join( QLatin1Char( '\n' ) );
428 }
429 
431 {
432  return new QgsLayerTreeGroup( *this );
433 }
434 
435 void QgsLayerTreeGroup::resolveReferences( const QgsProject *project, bool looseMatching )
436 {
437  for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
438  node->resolveReferences( project, looseMatching );
439 
440  mGroupLayer.resolve( project );
441 }
442 
443 static bool _nodeIsChecked( QgsLayerTreeNode *node )
444 {
445  return node->itemVisibilityChecked();
446 }
447 
448 
450 {
451  return mMutuallyExclusive;
452 }
453 
454 void QgsLayerTreeGroup::setIsMutuallyExclusive( bool enabled, int initialChildIndex )
455 {
456  mMutuallyExclusive = enabled;
457  mMutuallyExclusiveChildIndex = initialChildIndex;
458 
459  if ( !enabled )
460  {
461  return;
462  }
463 
464  if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >= mChildren.count() )
465  {
466  // try to use first checked index
467  int index = 0;
468  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
469  {
470  if ( _nodeIsChecked( child ) )
471  {
473  break;
474  }
475  index++;
476  }
477  }
478 
480 }
481 
483 {
484  return qobject_cast< QgsGroupLayer * >( mGroupLayer.layer );
485 }
486 
488 {
489  if ( QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() ) )
490  {
491  if ( !layer )
492  {
494  }
495  }
496  mGroupLayer.setLayer( layer );
497  refreshParentGroupLayerMembers();
498 }
499 
501 {
502  if ( !mGroupLayer.layerId.isEmpty() )
503  return nullptr;
504 
505  std::unique_ptr< QgsGroupLayer > res = std::make_unique< QgsGroupLayer >( name(), options );
506 
507  mGroupLayer.setLayer( res.get() );
508  updateGroupLayers();
509 
510  return res.release();
511 }
512 
513 void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
514 {
515  QgsLayerTreeGroup *parentGroup = qobject_cast< QgsLayerTreeGroup * >( parent() );
516  while ( parentGroup )
517  {
518  if ( QgsLayerTree *layerTree = qobject_cast< QgsLayerTree * >( parentGroup ) )
519  layerTree->emit layerOrderChanged();
520 
521  parentGroup->updateGroupLayers();
522  parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->parent() );
523  }
524 }
525 
527 {
528  QStringList lst;
529  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
530  {
531  if ( QgsLayerTree::isGroup( child ) )
532  lst << QgsLayerTree::toGroup( child )->findLayerIds();
533  else if ( QgsLayerTree::isLayer( child ) )
534  lst << QgsLayerTree::toLayer( child )->layerId();
535  }
536  return lst;
537 }
538 
540 {
541  int childIndex = mChildren.indexOf( node );
542  if ( childIndex == -1 )
543  {
544  updateGroupLayers();
545  return; // not a direct child - ignore
546  }
547 
548  if ( mMutuallyExclusive )
549  {
550  if ( _nodeIsChecked( node ) )
551  mMutuallyExclusiveChildIndex = childIndex;
552  else if ( mMutuallyExclusiveChildIndex == childIndex )
554 
555  // we need to make sure there is only one child node checked
557  }
558 
559  updateGroupLayers();
560 }
561 
563 {
564  if ( mChildren.isEmpty() )
565  return;
566 
567  mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
568 
569  int index = 0;
570  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
571  {
572  child->setItemVisibilityChecked( index == mMutuallyExclusiveChildIndex );
573  ++index;
574  }
575 
576  mChangingChildVisibility = false;
577 
578  updateGroupLayers();
579 }
580 
582 {
584  // Reconnect internal signals
586 }
587 
588 void QgsLayerTreeGroup::updateGroupLayers()
589 {
590  QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() );
591  if ( !groupLayer )
592  return;
593 
594  QList< QgsMapLayer * > layers;
595 
596  std::function< void( QgsLayerTreeGroup * ) > findGroupLayerChildren;
597  findGroupLayerChildren = [&layers, &findGroupLayerChildren]( QgsLayerTreeGroup * group )
598  {
599  for ( auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
600  {
601  if ( QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
602  {
603  if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
604  layers << layerTreeLayer->layer();
605  }
606  else if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
607  {
608  if ( childGroup->isVisible() )
609  {
610  if ( QgsGroupLayer *groupLayer = childGroup->groupLayer() )
611  layers << groupLayer;
612  else
613  findGroupLayerChildren( childGroup );
614  }
615  }
616  }
617  };
618  findGroupLayerChildren( this );
619 
620  groupLayer->setChildLayers( layers );
621  refreshParentGroupLayerMembers();
622 }
623 
625 {
627 
628  mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
629 
630  int index = 0;
631  for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
632  {
633  child->setItemVisibilityCheckedRecursive( checked && ( mMutuallyExclusiveChildIndex < 0 || index == mMutuallyExclusiveChildIndex ) );
634  ++index;
635  }
636 
637  mChangingChildVisibility = false;
638 
639  updateGroupLayers();
640 }
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Definition: qgsgrouplayer.h:42
void prepareLayersForRemovalFromGroup()
Prepares all child layers in the group prior to removal from the group.
void setChildLayers(const QList< QgsMapLayer * > &layers)
Sets the child layers contained by the group.
Layer tree group node serves as a container for layers and further groups.
void insertChildNode(int index, QgsLayerTreeNode *node)
Insert existing node at specified position.
void setName(const QString &n) override
Sets the group's name.
void resolveReferences(const QgsProject *project, bool looseMatching=false) override
Calls resolveReferences() on child tree nodes.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
QgsGroupLayer * convertToGroupLayer(const QgsGroupLayer::LayerOptions &options)
Converts the group to a QgsGroupLayer.
QgsLayerTreeGroup * insertGroup(int index, const QString &name)
Insert a new group node with given name at specified position.
void removeChildNode(QgsLayerTreeNode *node)
Remove a child node from this group.
QList< QgsLayerTreeGroup * > findGroups(bool recursive=false) const
Find group layer nodes.
void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context) override
Write group (tree) as XML element <layer-tree-group> and add it to the given parent element.
QString name() const override
Returns the group's name.
QgsLayerTreeGroup(const QString &name=QString(), bool checked=true)
Constructor.
QStringList findLayerIds() const
Find layer IDs used in all layer nodes.
QList< QgsMapLayer * > layerOrderRespectingGroupLayers() const
Returns an ordered list of map layers in the group, ignoring any layers which are child layers of Qgs...
void addChildNode(QgsLayerTreeNode *node)
Append an existing node.
void insertChildNodes(int index, const QList< QgsLayerTreeNode * > &nodes)
Insert existing nodes at specified position.
void removeAllChildren()
Remove all child nodes.
bool mMutuallyExclusive
Whether the group is mutually exclusive (i.e. only one child can be checked at a time)
void setIsMutuallyExclusive(bool enabled, int initialChildIndex=-1)
Set whether the group is mutually exclusive (only one child can be checked at a time).
void readChildrenFromXml(QDomElement &element, const QgsReadWriteContext &context)
Read children from XML and append them to the group.
void setItemVisibilityCheckedRecursive(bool checked) override
Check or uncheck a node and all its children (taking into account exclusion rules)
QgsLayerTreeGroup * clone() const override
Returns a clone of the group.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
bool isMutuallyExclusive() const
Returns whether the group is mutually exclusive (only one child can be checked at a time)
void updateChildVisibilityMutuallyExclusive()
Set check state of children - if mutually exclusive.
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 ...
void setGroupLayer(QgsGroupLayer *layer)
Sets the associated group layer, if the layer tree group will be treated as group layer during map re...
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name.
void removeChildren(int from, int count)
Remove child nodes from index "from".
void removeChildrenGroupWithoutLayers()
Remove all child group nodes without layers.
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer.
virtual void makeOrphan() override
Sets parent to nullptr and disconnects all external and forwarded signals.
void removeLayer(QgsMapLayer *layer)
Remove map layer's node from this group.
QgsLayerTreeLayer * insertLayer(int index, QgsMapLayer *layer)
Insert a new layer node for given map layer at specified position.
QString dump() const override
Returns text representation of the tree.
void nodeVisibilityChanged(QgsLayerTreeNode *node)
void reorderGroupLayers(const QList< QgsMapLayer * > &order)
Reorders layers in the group to match the order specified by order.
int mMutuallyExclusiveChildIndex
Keeps track which child has been most recently selected (so if the whole group is unchecked and check...
QgsGroupLayer * groupLayer()
Returns a reference to the associated group layer, if the layer tree group will be treated as group l...
Layer tree node points to a map layer.
QString layerId() const
Returns the ID for the map layer associated with this node.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QgsLayerTreeLayer * clone() const override
Create a copy of the node. Returns new instance.
This class is a base class for nodes in a layer tree.
void readCommonXml(QDomElement &element)
Read common XML elements.
virtual void makeOrphan()
Sets parent to nullptr and disconnects all external and forwarded signals.
@ NodeGroup
Container of other groups and layers.
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.
static QgsLayerTreeNode * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read layer tree from XML.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
void writeCommonXml(QDomElement &element)
Write common XML elements.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
void insertChildrenPrivate(int index, const QList< QgsLayerTreeNode * > &nodes)
Low-level insertion of children to the node. The children must not have any parent yet!
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
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
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
void removeChildrenPrivate(int from, int count, bool destroy=true)
Low-level removal of children from the node.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:50
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:70
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:41
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Definition: qgslayertree.h:60
Base class for all map layer types.
Definition: qgsmaplayer.h:75
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
The derived translate() translates with QTranslator and qm file the sourceText.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
The class is used as a container of context for various read/write operations on other objects.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
QgsLayerTree * layerTree(const QgsWmsRenderContext &context)
_LayerRef< QgsMapLayer > QgsMapLayerRef
Setting options for loading group layers.
Definition: qgsgrouplayer.h:52
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
QPointer< TYPE > layer
Weak pointer to map layer.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.