QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgslayertreegroup.cpp"
18
19#include "qgslayertree.h"
20#include "qgslayertreeutils.h"
21#include "qgsmaplayer.h"
22#include "qgsgrouplayer.h"
23#include "qgspainting.h"
24
25#include <QDomElement>
26#include <QStringList>
27
28
29QgsLayerTreeGroup::QgsLayerTreeGroup( const QString &name, bool checked )
30 : QgsLayerTreeNode( NodeGroup, checked )
31 , mName( name )
32{
33 init();
34}
35
37 : QgsLayerTreeNode( other )
38 , mName( other.mName )
39 , mChangingChildVisibility( other.mChangingChildVisibility )
40 , mMutuallyExclusive( other.mMutuallyExclusive )
41 , mMutuallyExclusiveChildIndex( other.mMutuallyExclusiveChildIndex )
42 , mGroupLayer( other.mGroupLayer )
43{
44 init();
45}
46
47void QgsLayerTreeGroup::init()
48{
50 connect( this, &QgsLayerTreeNode::addedChildren, this, &QgsLayerTreeGroup::updateGroupLayers );
51 connect( this, &QgsLayerTreeNode::removedChildren, this, &QgsLayerTreeGroup::updateGroupLayers );
52 connect( this, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeGroup::updateGroupLayers );
53}
54
56{
57 return mName;
58}
59
60void QgsLayerTreeGroup::setName( const QString &n )
61{
62 if ( mName == n )
63 return;
64
65 mName = n;
66 emit nameChanged( this, n );
67}
68
69
70QgsLayerTreeGroup *QgsLayerTreeGroup::insertGroup( int index, const QString &name )
71{
73 insertChildNode( index, grp );
74 return grp;
75}
76
78{
80 addChildNode( grp );
81 return grp;
82}
83
85{
86 if ( !layer )
87 return nullptr;
88
89 QgsLayerTreeLayer *ll = new QgsLayerTreeLayer( layer );
90 insertChildNode( index, ll );
91
92 updateGroupLayers();
93 return ll;
94}
95
97{
98 if ( !layer )
99 return nullptr;
100
101 QgsLayerTreeLayer *ll = new QgsLayerTreeLayer( layer );
102 addChildNode( ll );
103
104 updateGroupLayers();
105 return ll;
106}
107
109{
110 QList<QgsLayerTreeNode *> nodes;
111 nodes << node;
112 insertChildNodes( index, nodes );
113}
114
115void QgsLayerTreeGroup::insertChildNodes( int index, const QList<QgsLayerTreeNode *> &nodes )
116{
117 QgsLayerTreeNode *meChild = nullptr;
120
121 // low-level insert
122 insertChildrenPrivate( index, nodes );
123
124 if ( mMutuallyExclusive )
125 {
126 if ( meChild )
127 {
128 // the child could have change its index - or the new children may have been also set as visible
129 mMutuallyExclusiveChildIndex = mChildren.indexOf( meChild );
130 }
131 else if ( mChecked )
132 {
133 // we have not picked a child index yet, but we should pick one now
134 // ... so pick the first one from the newly added
135 if ( index == -1 )
136 index = mChildren.count() - nodes.count(); // get real insertion index
138 }
140 }
141
142 updateGroupLayers();
143}
144
146{
147 insertChildNode( -1, node );
148
149 updateGroupLayers();
150}
151
153{
154 int i = mChildren.indexOf( node );
155 if ( i >= 0 )
156 removeChildren( i, 1 );
157
158 updateGroupLayers();
159}
160
162{
163 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
164 {
165 if ( QgsLayerTree::isLayer( child ) )
166 {
167 QgsLayerTreeLayer *childLayer = QgsLayerTree::toLayer( child );
168 if ( childLayer->layer() == layer )
169 {
170 removeChildren( mChildren.indexOf( child ), 1 );
171 break;
172 }
173 }
174 }
175
176 updateGroupLayers();
177}
178
179void QgsLayerTreeGroup::removeChildren( int from, int count )
180{
181 QgsLayerTreeNode *meChild = nullptr;
184
185 removeChildrenPrivate( from, count );
186
187 if ( meChild )
188 {
189 // the child could have change its index - or may have been removed completely
190 mMutuallyExclusiveChildIndex = mChildren.indexOf( meChild );
191 // we need to uncheck this group
192 //if ( mMutuallyExclusiveChildIndex == -1 )
193 // setItemVisibilityChecked( false );
194 }
195
196 updateGroupLayers();
197}
198
200{
201 // clean the layer tree by removing empty group
202 const auto childNodes = children();
203 for ( QgsLayerTreeNode *treeNode : childNodes )
204 {
205 if ( treeNode->nodeType() == QgsLayerTreeNode::NodeGroup )
206 {
207 QgsLayerTreeGroup *treeGroup = qobject_cast<QgsLayerTreeGroup *>( treeNode );
208 if ( treeGroup->findLayerIds().isEmpty() )
209 removeChildNode( treeNode );
210 else
212 }
213 }
214
215 updateGroupLayers();
216}
217
222
224{
225 if ( !layer )
226 return nullptr;
227
228 return findLayer( layer->id() );
229}
230
231QgsLayerTreeLayer *QgsLayerTreeGroup::findLayer( const QString &layerId ) const
232{
233 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
234 {
235 if ( QgsLayerTree::isLayer( child ) )
236 {
237 QgsLayerTreeLayer *childLayer = QgsLayerTree::toLayer( child );
238 if ( childLayer->layerId() == layerId )
239 return childLayer;
240 }
241 else if ( QgsLayerTree::isGroup( child ) )
242 {
243 QgsLayerTreeLayer *res = QgsLayerTree::toGroup( child )->findLayer( layerId );
244 if ( res )
245 return res;
246 }
247 }
248 return nullptr;
249}
250
251QList<QgsLayerTreeLayer *> QgsLayerTreeGroup::findLayers() const
252{
253 QList<QgsLayerTreeLayer *> list;
254 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
255 {
256 if ( QgsLayerTree::isLayer( child ) )
257 list << QgsLayerTree::toLayer( child );
258 else if ( QgsLayerTree::isGroup( child ) )
259 list << QgsLayerTree::toGroup( child )->findLayers();
260 }
261 return list;
262}
263
264void QgsLayerTreeGroup::reorderGroupLayers( const QList<QgsMapLayer *> &order )
265{
266 const QList< QgsLayerTreeLayer * > childLayers = findLayers();
267 int targetIndex = 0;
268 for ( QgsMapLayer *targetLayer : order )
269 {
270 for ( QgsLayerTreeLayer *layerNode : childLayers )
271 {
272 if ( layerNode->layer() == targetLayer )
273 {
274 QgsLayerTreeLayer *cloned = layerNode->clone();
275 insertChildNode( targetIndex, cloned );
276 removeChildNode( layerNode );
277 targetIndex++;
278 break;
279 }
280 }
281 }
282}
283
285{
286 QList<QgsMapLayer *> list;
287 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
288 {
289 if ( QgsLayerTree::isLayer( child ) )
290 {
291 QgsMapLayer *layer = QgsLayerTree::toLayer( child )->layer();
292 if ( !layer || !layer->isSpatial() )
293 continue;
294 list << layer;
295 }
296 else if ( QgsLayerTree::isGroup( child ) )
297 {
299 if ( group->groupLayer() )
300 {
301 list << group->groupLayer();
302 }
303 else
304 {
305 list << group->layerOrderRespectingGroupLayers();
306 }
307 }
308 }
309 return list;
310}
311
313{
314 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
315 {
316 if ( QgsLayerTree::isGroup( child ) )
317 {
318 QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
319 if ( childGroup->name() == name )
320 return childGroup;
321 else
322 {
323 QgsLayerTreeGroup *grp = childGroup->findGroup( name );
324 if ( grp )
325 return grp;
326 }
327 }
328 }
329 return nullptr;
330}
331
332QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups( bool recursive ) const
333{
334 QList<QgsLayerTreeGroup *> list;
335
336 for ( QgsLayerTreeNode *child : mChildren )
337 {
338 if ( QgsLayerTree::isGroup( child ) )
339 {
340 QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
341 list << childGroup;
342 if ( recursive )
343 list << childGroup->findGroups( recursive );
344 }
345 }
346 return list;
347}
348
349QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsReadWriteContext &context ) // cppcheck-suppress duplInheritedMember
350{
351 if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
352 return nullptr;
353
354 QString name = context.projectTranslator()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
355 bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
356 bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
357 bool isMutuallyExclusive = element.attribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
358 int mutuallyExclusiveChildIndex = element.attribute( QStringLiteral( "mutually-exclusive-child" ), QStringLiteral( "-1" ) ).toInt();
359
360 QgsLayerTreeGroup *groupNode = new QgsLayerTreeGroup( name, checked );
361 groupNode->setExpanded( isExpanded );
362
363 groupNode->readCommonXml( element );
364
365 groupNode->readChildrenFromXml( element, context );
366
367 groupNode->setIsMutuallyExclusive( isMutuallyExclusive, mutuallyExclusiveChildIndex );
368
369 groupNode->mGroupLayer = QgsMapLayerRef( element.attribute( QStringLiteral( "groupLayer" ) ) );
370
371 return groupNode;
372}
373
374QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
375{
376 QgsLayerTreeGroup *node = readXml( element, context );
377 if ( node )
378 node->resolveReferences( project );
379 return node;
380}
381
382void QgsLayerTreeGroup::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
383{
384 QDomDocument doc = parentElement.ownerDocument();
385 QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-group" ) );
386 elem.setAttribute( QStringLiteral( "name" ), mName );
387 elem.setAttribute( QStringLiteral( "expanded" ), mExpanded ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
388 elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
389 if ( mMutuallyExclusive )
390 {
391 elem.setAttribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "1" ) );
392 elem.setAttribute( QStringLiteral( "mutually-exclusive-child" ), mMutuallyExclusiveChildIndex );
393 }
394 elem.setAttribute( QStringLiteral( "groupLayer" ), mGroupLayer.layerId );
395
396 writeCommonXml( elem );
397
398 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
399 node->writeXml( elem, context );
400
401 parentElement.appendChild( elem );
402}
403
404void QgsLayerTreeGroup::readChildrenFromXml( QDomElement &element, const QgsReadWriteContext &context )
405{
406 QList<QgsLayerTreeNode *> nodes;
407 QDomElement childElem = element.firstChildElement();
408 while ( !childElem.isNull() )
409 {
410 QgsLayerTreeNode *newNode = QgsLayerTreeNode::readXml( childElem, context );
411 if ( newNode )
412 nodes << newNode;
413
414 childElem = childElem.nextSiblingElement();
415 }
416
417 insertChildNodes( -1, nodes );
418}
419
421{
422 QString header = QStringLiteral( "GROUP: %1 checked=%2 expanded=%3\n" ).arg( name() ).arg( mChecked ).arg( mExpanded );
423 QStringList childrenDump;
424 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
425 childrenDump << node->dump().split( '\n' );
426 for ( int i = 0; i < childrenDump.count(); ++i )
427 childrenDump[i].prepend( " " );
428 return header + childrenDump.join( QLatin1Char( '\n' ) );
429}
430
432{
433 return new QgsLayerTreeGroup( *this );
434}
435
436void QgsLayerTreeGroup::resolveReferences( const QgsProject *project, bool looseMatching )
437{
438 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
439 node->resolveReferences( project, looseMatching );
440
441 mGroupLayer.resolve( project );
442}
443
444static bool _nodeIsChecked( QgsLayerTreeNode *node )
445{
446 return node->itemVisibilityChecked();
447}
448
449
454
455void QgsLayerTreeGroup::setIsMutuallyExclusive( bool enabled, int initialChildIndex )
456{
457 mMutuallyExclusive = enabled;
458 mMutuallyExclusiveChildIndex = initialChildIndex;
459
460 if ( !enabled )
461 {
462 return;
463 }
464
465 if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >= mChildren.count() )
466 {
467 // try to use first checked index
468 int index = 0;
469 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
470 {
471 if ( _nodeIsChecked( child ) )
472 {
474 break;
475 }
476 index++;
477 }
478 }
479
481}
482
484{
485 return qobject_cast< QgsGroupLayer * >( mGroupLayer.layer );
486}
487
489{
490 if ( QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() ) )
491 {
492 if ( !layer )
493 {
495 }
496 }
497 mGroupLayer.setLayer( layer );
498 refreshParentGroupLayerMembers();
499}
500
502{
503 if ( !mGroupLayer.layerId.isEmpty() )
504 return nullptr;
505
506 std::unique_ptr< QgsGroupLayer > res = std::make_unique< QgsGroupLayer >( name(), options );
507
508 mGroupLayer.setLayer( res.get() );
509 updateGroupLayers();
510
511 return res.release();
512}
513
514void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
515{
516 QgsLayerTreeGroup *parentGroup = qobject_cast< QgsLayerTreeGroup * >( parent() );
517 while ( parentGroup )
518 {
519 if ( QgsLayerTree *layerTree = qobject_cast< QgsLayerTree * >( parentGroup ) )
520 layerTree->emit layerOrderChanged();
521
522 parentGroup->updateGroupLayers();
523 parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->parent() );
524 }
525}
526
528{
529 QStringList lst;
530 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
531 {
532 if ( QgsLayerTree::isGroup( child ) )
533 lst << QgsLayerTree::toGroup( child )->findLayerIds();
534 else if ( QgsLayerTree::isLayer( child ) )
535 lst << QgsLayerTree::toLayer( child )->layerId();
536 }
537 return lst;
538}
539
541{
542 int childIndex = mChildren.indexOf( node );
543 if ( childIndex == -1 )
544 {
545 updateGroupLayers();
546 return; // not a direct child - ignore
547 }
548
549 if ( mMutuallyExclusive )
550 {
551 if ( _nodeIsChecked( node ) )
552 mMutuallyExclusiveChildIndex = childIndex;
553 else if ( mMutuallyExclusiveChildIndex == childIndex )
555
556 // we need to make sure there is only one child node checked
558 }
559
560 updateGroupLayers();
561}
562
564{
565 if ( mChildren.isEmpty() )
566 return;
567
568 mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
569
570 int index = 0;
571 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
572 {
573 child->setItemVisibilityChecked( index == mMutuallyExclusiveChildIndex );
574 ++index;
575 }
576
578
579 updateGroupLayers();
580}
581
583{
585 // Reconnect internal signals
587}
588
589void QgsLayerTreeGroup::updateGroupLayers()
590{
591 QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() );
592 if ( !groupLayer )
593 return;
594
595 QList< QgsMapLayer * > layers;
596
597 std::function< void( QgsLayerTreeGroup * ) > findGroupLayerChildren;
598 findGroupLayerChildren = [&layers, &findGroupLayerChildren]( QgsLayerTreeGroup * group )
599 {
600 for ( auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
601 {
602 if ( QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
603 {
604 if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
605 layers << layerTreeLayer->layer();
606 }
607 else if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
608 {
609 if ( childGroup->isVisible() )
610 {
611 if ( QgsGroupLayer *groupLayer = childGroup->groupLayer() )
612 layers << groupLayer;
613 else
614 findGroupLayerChildren( childGroup );
615 }
616 }
617 }
618 };
619 findGroupLayerChildren( this );
620
621 groupLayer->setChildLayers( layers );
622 refreshParentGroupLayerMembers();
623}
624
626{
628
629 mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
630
631 int index = 0;
632 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
633 {
634 child->setItemVisibilityCheckedRecursive( checked && ( mMutuallyExclusiveChildIndex < 0 || index == mMutuallyExclusiveChildIndex ) );
635 ++index;
636 }
637
639
640 updateGroupLayers();
641}
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
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.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
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.
void writeCommonXml(QDomElement &element)
Write common XML elements.
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.
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.
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.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Base class for all map layer types.
Definition qgsmaplayer.h:76
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
Definition qgsmaplayer.h:79
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
Translates a string using the Qt QTranslator mechanism.
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.
_LayerRef< QgsMapLayer > QgsMapLayerRef
Setting options for loading group layers.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
QPointer< TYPE > layer
Weak pointer to map layer.
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.