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