QGIS API Documentation 3.43.0-Master (c67cf405802)
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 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>() )
33{
34 init();
35}
36
38 : QgsLayerTreeNode( other )
39 , mName( other.mName )
40 , mChangingChildVisibility( other.mChangingChildVisibility )
41 , mMutuallyExclusive( other.mMutuallyExclusive )
42 , mMutuallyExclusiveChildIndex( other.mMutuallyExclusiveChildIndex )
43 , mGroupLayer( other.mGroupLayer )
44 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>() )
45{
46 other.serverProperties()->copyTo( mServerProperties.get() );
47
48 init();
49}
50
51void QgsLayerTreeGroup::init()
52{
54 connect( this, &QgsLayerTreeNode::addedChildren, this, &QgsLayerTreeGroup::updateGroupLayers );
55 connect( this, &QgsLayerTreeNode::removedChildren, this, &QgsLayerTreeGroup::updateGroupLayers );
56 connect( this, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeGroup::updateGroupLayers );
57}
58
60{
61 return mName;
62}
63
64void QgsLayerTreeGroup::setName( const QString &n )
65{
66 if ( mName == n )
67 return;
68
69 mName = n;
70 emit nameChanged( this, n );
71}
72
73
74QgsLayerTreeGroup *QgsLayerTreeGroup::insertGroup( int index, const QString &name )
75{
77 insertChildNode( index, grp );
78 return grp;
79}
80
82{
84 addChildNode( grp );
85 return grp;
86}
87
89{
90 if ( !layer )
91 return nullptr;
92
93 QgsLayerTreeLayer *ll = new QgsLayerTreeLayer( layer );
94 insertChildNode( index, ll );
95
96 updateGroupLayers();
97 return ll;
98}
99
101{
102 if ( !layer )
103 return nullptr;
104
105 QgsLayerTreeLayer *ll = new QgsLayerTreeLayer( layer );
106 addChildNode( ll );
107
108 updateGroupLayers();
109 return ll;
110}
111
113{
114 QList<QgsLayerTreeNode *> nodes;
115 nodes << node;
116 insertChildNodes( index, nodes );
117}
118
119void QgsLayerTreeGroup::insertChildNodes( int index, const QList<QgsLayerTreeNode *> &nodes )
120{
121 QgsLayerTreeNode *meChild = nullptr;
124
125 // low-level insert
126 insertChildrenPrivate( index, nodes );
127
128 if ( mMutuallyExclusive )
129 {
130 if ( meChild )
131 {
132 // the child could have change its index - or the new children may have been also set as visible
133 mMutuallyExclusiveChildIndex = mChildren.indexOf( meChild );
134 }
135 else if ( mChecked )
136 {
137 // we have not picked a child index yet, but we should pick one now
138 // ... so pick the first one from the newly added
139 if ( index == -1 )
140 index = mChildren.count() - nodes.count(); // get real insertion index
142 }
144 }
145
146 updateGroupLayers();
147}
148
150{
151 insertChildNode( -1, node );
152
153 updateGroupLayers();
154}
155
157{
158 int i = mChildren.indexOf( node );
159 if ( i >= 0 )
160 removeChildren( i, 1 );
161
162 updateGroupLayers();
163}
164
166{
167 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
168 {
169 if ( QgsLayerTree::isLayer( child ) )
170 {
171 QgsLayerTreeLayer *childLayer = QgsLayerTree::toLayer( child );
172 if ( childLayer->layer() == layer )
173 {
174 removeChildren( mChildren.indexOf( child ), 1 );
175 break;
176 }
177 }
178 }
179
180 updateGroupLayers();
181}
182
183void QgsLayerTreeGroup::removeChildren( int from, int count )
184{
185 QgsLayerTreeNode *meChild = nullptr;
188
189 removeChildrenPrivate( from, count );
190
191 if ( meChild )
192 {
193 // the child could have change its index - or may have been removed completely
194 mMutuallyExclusiveChildIndex = mChildren.indexOf( meChild );
195 // we need to uncheck this group
196 //if ( mMutuallyExclusiveChildIndex == -1 )
197 // setItemVisibilityChecked( false );
198 }
199
200 updateGroupLayers();
201}
202
204{
205 // clean the layer tree by removing empty group
206 const auto childNodes = children();
207 for ( QgsLayerTreeNode *treeNode : childNodes )
208 {
209 if ( treeNode->nodeType() == QgsLayerTreeNode::NodeGroup )
210 {
211 QgsLayerTreeGroup *treeGroup = qobject_cast<QgsLayerTreeGroup *>( treeNode );
212 if ( treeGroup->findLayerIds().isEmpty() )
213 removeChildNode( treeNode );
214 else
216 }
217 }
218
219 updateGroupLayers();
220}
221
226
228{
229 if ( !layer )
230 return nullptr;
231
232 return findLayer( layer->id() );
233}
234
235QgsLayerTreeLayer *QgsLayerTreeGroup::findLayer( const QString &layerId ) const
236{
237 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
238 {
239 if ( QgsLayerTree::isLayer( child ) )
240 {
241 QgsLayerTreeLayer *childLayer = QgsLayerTree::toLayer( child );
242 if ( childLayer->layerId() == layerId )
243 return childLayer;
244 }
245 else if ( QgsLayerTree::isGroup( child ) )
246 {
247 QgsLayerTreeLayer *res = QgsLayerTree::toGroup( child )->findLayer( layerId );
248 if ( res )
249 return res;
250 }
251 }
252 return nullptr;
253}
254
255QList<QgsLayerTreeLayer *> QgsLayerTreeGroup::findLayers() const
256{
257 QList<QgsLayerTreeLayer *> list;
258 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
259 {
260 if ( QgsLayerTree::isLayer( child ) )
261 list << QgsLayerTree::toLayer( child );
262 else if ( QgsLayerTree::isGroup( child ) )
263 list << QgsLayerTree::toGroup( child )->findLayers();
264 }
265 return list;
266}
267
268void QgsLayerTreeGroup::reorderGroupLayers( const QList<QgsMapLayer *> &order )
269{
270 const QList< QgsLayerTreeLayer * > childLayers = findLayers();
271 int targetIndex = 0;
272 for ( QgsMapLayer *targetLayer : order )
273 {
274 for ( QgsLayerTreeLayer *layerNode : childLayers )
275 {
276 if ( layerNode->layer() == targetLayer )
277 {
278 QgsLayerTreeLayer *cloned = layerNode->clone();
279 insertChildNode( targetIndex, cloned );
280 removeChildNode( layerNode );
281 targetIndex++;
282 break;
283 }
284 }
285 }
286}
287
289{
290 QList<QgsMapLayer *> list;
291 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
292 {
293 if ( QgsLayerTree::isLayer( child ) )
294 {
295 QgsMapLayer *layer = QgsLayerTree::toLayer( child )->layer();
296 if ( !layer || !layer->isSpatial() )
297 continue;
298 list << layer;
299 }
300 else if ( QgsLayerTree::isGroup( child ) )
301 {
303 if ( group->groupLayer() )
304 {
305 list << group->groupLayer();
306 }
307 else
308 {
309 list << group->layerOrderRespectingGroupLayers();
310 }
311 }
312 }
313 return list;
314}
315
317{
318 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
319 {
320 if ( QgsLayerTree::isGroup( child ) )
321 {
322 QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
323 if ( childGroup->name() == name )
324 return childGroup;
325 else
326 {
327 QgsLayerTreeGroup *grp = childGroup->findGroup( name );
328 if ( grp )
329 return grp;
330 }
331 }
332 }
333 return nullptr;
334}
335
336QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups( bool recursive ) const
337{
338 QList<QgsLayerTreeGroup *> list;
339
340 for ( QgsLayerTreeNode *child : mChildren )
341 {
342 if ( QgsLayerTree::isGroup( child ) )
343 {
344 QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
345 list << childGroup;
346 if ( recursive )
347 list << childGroup->findGroups( recursive );
348 }
349 }
350 return list;
351}
352
353QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( const QDomElement &element, const QgsReadWriteContext &context ) // cppcheck-suppress duplInheritedMember
354{
355 if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
356 return nullptr;
357
358 QString name = context.projectTranslator()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
359 bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
360 bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
361 bool isMutuallyExclusive = element.attribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
362 int mutuallyExclusiveChildIndex = element.attribute( QStringLiteral( "mutually-exclusive-child" ), QStringLiteral( "-1" ) ).toInt();
363
364 QgsLayerTreeGroup *groupNode = new QgsLayerTreeGroup( name, checked );
365 groupNode->setExpanded( isExpanded );
366
367 groupNode->readCommonXml( element );
368
369 groupNode->readChildrenFromXml( element, context );
370
371 groupNode->setIsMutuallyExclusive( isMutuallyExclusive, mutuallyExclusiveChildIndex );
372
373 groupNode->mGroupLayer = QgsMapLayerRef( element.attribute( QStringLiteral( "groupLayer" ) ) );
374
375 readLegacyServerProperties( groupNode );
376
377 groupNode->serverProperties()->readXml( element );
378
379 return groupNode;
380}
381
382void QgsLayerTreeGroup::readLegacyServerProperties( QgsLayerTreeGroup *groupNode )
383{
384 const QVariant wmsShortName = groupNode->customProperty( QStringLiteral( "wmsShortName" ) );
385 if ( wmsShortName.isValid() )
386 {
387 groupNode->serverProperties()->setShortName( wmsShortName.toString() );
388 groupNode->removeCustomProperty( QStringLiteral( "wmsShortName" ) );
389 }
390
391 const QVariant wmsTitle = groupNode->customProperty( QStringLiteral( "wmsTitle" ) );
392 if ( wmsTitle.isValid() )
393 {
394 groupNode->serverProperties()->setTitle( wmsTitle.toString() );
395 groupNode->removeCustomProperty( QStringLiteral( "wmsTitle" ) );
396 }
397
398 const QVariant wmsAbstract = groupNode->customProperty( QStringLiteral( "wmsAbstract" ) );
399 if ( wmsAbstract.isValid() )
400 {
401 groupNode->serverProperties()->setAbstract( wmsAbstract.toString() );
402 groupNode->removeCustomProperty( QStringLiteral( "wmsAbstract" ) );
403 }
404}
405
406QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( const QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
407{
408 QgsLayerTreeGroup *node = readXml( element, context );
409 if ( !node )
410 return nullptr;
411
412 node->resolveReferences( project );
413
414 return node;
415}
416
417void QgsLayerTreeGroup::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
418{
419 QDomDocument doc = parentElement.ownerDocument();
420 QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-group" ) );
421 elem.setAttribute( QStringLiteral( "name" ), mName );
422 elem.setAttribute( QStringLiteral( "expanded" ), mExpanded ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
423 elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
424 if ( mMutuallyExclusive )
425 {
426 elem.setAttribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "1" ) );
427 elem.setAttribute( QStringLiteral( "mutually-exclusive-child" ), mMutuallyExclusiveChildIndex );
428 }
429 elem.setAttribute( QStringLiteral( "groupLayer" ), mGroupLayer.layerId );
430
431 writeCommonXml( elem );
432
433 serverProperties()->writeXml( elem, doc );
434
435 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
436 node->writeXml( elem, context );
437
438 parentElement.appendChild( elem );
439}
440
441void QgsLayerTreeGroup::readChildrenFromXml( const QDomElement &element, const QgsReadWriteContext &context )
442{
443 QList<QgsLayerTreeNode *> nodes;
444 QDomElement childElem = element.firstChildElement();
445 while ( !childElem.isNull() )
446 {
447 QgsLayerTreeNode *newNode = QgsLayerTreeNode::readXml( childElem, context );
448 if ( newNode )
449 nodes << newNode;
450
451 childElem = childElem.nextSiblingElement();
452 }
453
454 insertChildNodes( -1, nodes );
455}
456
458{
459 QString header = QStringLiteral( "GROUP: %1 checked=%2 expanded=%3\n" ).arg( name() ).arg( mChecked ).arg( mExpanded );
460 QStringList childrenDump;
461 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
462 childrenDump << node->dump().split( '\n' );
463 for ( int i = 0; i < childrenDump.count(); ++i )
464 childrenDump[i].prepend( " " );
465 return header + childrenDump.join( QLatin1Char( '\n' ) );
466}
467
469{
470 return new QgsLayerTreeGroup( *this );
471}
472
473void QgsLayerTreeGroup::resolveReferences( const QgsProject *project, bool looseMatching )
474{
475 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
476 node->resolveReferences( project, looseMatching );
477
478 mGroupLayer.resolve( project );
479}
480
481static bool _nodeIsChecked( QgsLayerTreeNode *node )
482{
483 return node->itemVisibilityChecked();
484}
485
486
491
492void QgsLayerTreeGroup::setIsMutuallyExclusive( bool enabled, int initialChildIndex )
493{
494 mMutuallyExclusive = enabled;
495 mMutuallyExclusiveChildIndex = initialChildIndex;
496
497 if ( !enabled )
498 {
499 return;
500 }
501
502 if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >= mChildren.count() )
503 {
504 // try to use first checked index
505 int index = 0;
506 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
507 {
508 if ( _nodeIsChecked( child ) )
509 {
511 break;
512 }
513 index++;
514 }
515 }
516
518}
519
521{
522 return qobject_cast< QgsGroupLayer * >( mGroupLayer.layer );
523}
524
526{
527 if ( QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() ) )
528 {
529 if ( !layer )
530 {
532 }
533 }
534 mGroupLayer.setLayer( layer );
535 refreshParentGroupLayerMembers();
536}
537
539{
540 if ( !mGroupLayer.layerId.isEmpty() )
541 return nullptr;
542
543 auto res = std::make_unique< QgsGroupLayer >( name(), options );
544
545 mGroupLayer.setLayer( res.get() );
546 updateGroupLayers();
547
548 return res.release();
549}
550
551void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
552{
553 QgsLayerTreeGroup *parentGroup = qobject_cast< QgsLayerTreeGroup * >( parent() );
554 while ( parentGroup )
555 {
556 if ( QgsLayerTree *layerTree = qobject_cast< QgsLayerTree * >( parentGroup ) )
557 layerTree->emit layerOrderChanged();
558
559 parentGroup->updateGroupLayers();
560 parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->parent() );
561 }
562}
563
565{
566 QStringList lst;
567 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
568 {
569 if ( QgsLayerTree::isGroup( child ) )
570 lst << QgsLayerTree::toGroup( child )->findLayerIds();
571 else if ( QgsLayerTree::isLayer( child ) )
572 lst << QgsLayerTree::toLayer( child )->layerId();
573 }
574 return lst;
575}
576
578{
579 int childIndex = mChildren.indexOf( node );
580 if ( childIndex == -1 )
581 {
582 updateGroupLayers();
583 return; // not a direct child - ignore
584 }
585
586 if ( mMutuallyExclusive )
587 {
588 if ( _nodeIsChecked( node ) )
589 mMutuallyExclusiveChildIndex = childIndex;
590 else if ( mMutuallyExclusiveChildIndex == childIndex )
592
593 // we need to make sure there is only one child node checked
595 }
596
597 updateGroupLayers();
598}
599
601{
602 if ( mChildren.isEmpty() )
603 return;
604
605 mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
606
607 int index = 0;
608 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
609 {
610 child->setItemVisibilityChecked( index == mMutuallyExclusiveChildIndex );
611 ++index;
612 }
613
615
616 updateGroupLayers();
617}
618
620{
622 // Reconnect internal signals
624}
625
626void QgsLayerTreeGroup::updateGroupLayers()
627{
628 QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() );
629 if ( !groupLayer )
630 return;
631
632 QList< QgsMapLayer * > layers;
633
634 std::function< void( QgsLayerTreeGroup * ) > findGroupLayerChildren;
635 findGroupLayerChildren = [&layers, &findGroupLayerChildren]( QgsLayerTreeGroup * group )
636 {
637 for ( auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
638 {
639 if ( QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
640 {
641 if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
642 layers << layerTreeLayer->layer();
643 }
644 else if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
645 {
646 if ( childGroup->isVisible() )
647 {
648 if ( QgsGroupLayer *groupLayer = childGroup->groupLayer() )
649 layers << groupLayer;
650 else
651 findGroupLayerChildren( childGroup );
652 }
653 }
654 }
655 };
656 findGroupLayerChildren( this );
657
658 groupLayer->setChildLayers( layers );
659 refreshParentGroupLayerMembers();
660}
661
663{
665
666 mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
667
668 int index = 0;
669 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
670 {
671 child->setItemVisibilityCheckedRecursive( checked && ( mMutuallyExclusiveChildIndex < 0 || index == mMutuallyExclusiveChildIndex ) );
672 ++index;
673 }
674
676
677 updateGroupLayers();
678}
679
681{
682 return mServerProperties.get();
683}
684
686{
687 return mServerProperties.get();
688}
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 readChildrenFromXml(const QDomElement &element, const QgsReadWriteContext &context)
Read children from XML and append them to the group.
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...
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the layer tree group.
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 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(const 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.
Base class for nodes in a layer tree.
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 removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
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)
void readCommonXml(const QDomElement &element)
Read common XML elements.
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.
Manages QGIS Server properties for a map layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
void setShortName(const QString &name)
Sets the short name of the layer used by QGIS Server to identify the layer.
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
Base class for all map layer types.
Definition qgsmaplayer.h:77
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:80
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
A container for the context for various read/write operations on 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.