QGIS API Documentation 3.43.0-Master (a6cade80589)
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
24#include <QDomElement>
25#include <QStringList>
26
27
28QgsLayerTreeGroup::QgsLayerTreeGroup( const QString &name, bool checked )
29 : QgsLayerTreeNode( NodeGroup, checked )
30 , mName( name )
31 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>() )
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 , mWmsHasTimeDimension( other.mWmsHasTimeDimension )
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->mWmsHasTimeDimension = element.attribute( QStringLiteral( "wms-has-time-dimension" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
374
375 groupNode->mGroupLayer = QgsMapLayerRef( element.attribute( QStringLiteral( "groupLayer" ) ) );
376
377 readLegacyServerProperties( groupNode );
378
379 groupNode->serverProperties()->readXml( element );
380
381 return groupNode;
382}
383
384void QgsLayerTreeGroup::readLegacyServerProperties( QgsLayerTreeGroup *groupNode )
385{
386 const QVariant wmsShortName = groupNode->customProperty( QStringLiteral( "wmsShortName" ) );
387 if ( wmsShortName.isValid() )
388 {
389 groupNode->serverProperties()->setShortName( wmsShortName.toString() );
390 groupNode->removeCustomProperty( QStringLiteral( "wmsShortName" ) );
391 }
392
393 const QVariant wmsTitle = groupNode->customProperty( QStringLiteral( "wmsTitle" ) );
394 if ( wmsTitle.isValid() )
395 {
396 groupNode->serverProperties()->setTitle( wmsTitle.toString() );
397 groupNode->removeCustomProperty( QStringLiteral( "wmsTitle" ) );
398 }
399
400 const QVariant wmsAbstract = groupNode->customProperty( QStringLiteral( "wmsAbstract" ) );
401 if ( wmsAbstract.isValid() )
402 {
403 groupNode->serverProperties()->setAbstract( wmsAbstract.toString() );
404 groupNode->removeCustomProperty( QStringLiteral( "wmsAbstract" ) );
405 }
406}
407
408QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( const QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
409{
410 QgsLayerTreeGroup *node = readXml( element, context );
411 if ( !node )
412 return nullptr;
413
414 node->resolveReferences( project );
415
416 return node;
417}
418
419void QgsLayerTreeGroup::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
420{
421 QDomDocument doc = parentElement.ownerDocument();
422 QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-group" ) );
423 elem.setAttribute( QStringLiteral( "name" ), mName );
424 elem.setAttribute( QStringLiteral( "expanded" ), mExpanded ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
425 elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
426 if ( mMutuallyExclusive )
427 {
428 elem.setAttribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "1" ) );
429 elem.setAttribute( QStringLiteral( "mutually-exclusive-child" ), mMutuallyExclusiveChildIndex );
430 }
431
433 {
434 elem.setAttribute( QStringLiteral( "wms-has-time-dimension" ), QStringLiteral( "1" ) );
435 }
436
437 elem.setAttribute( QStringLiteral( "groupLayer" ), mGroupLayer.layerId );
438
439 writeCommonXml( elem );
440
441 serverProperties()->writeXml( elem, doc );
442
443 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
444 node->writeXml( elem, context );
445
446 parentElement.appendChild( elem );
447}
448
449void QgsLayerTreeGroup::readChildrenFromXml( const QDomElement &element, const QgsReadWriteContext &context )
450{
451 QList<QgsLayerTreeNode *> nodes;
452 QDomElement childElem = element.firstChildElement();
453 while ( !childElem.isNull() )
454 {
455 QgsLayerTreeNode *newNode = QgsLayerTreeNode::readXml( childElem, context );
456 if ( newNode )
457 nodes << newNode;
458
459 childElem = childElem.nextSiblingElement();
460 }
461
462 insertChildNodes( -1, nodes );
463}
464
466{
467 QString header = QStringLiteral( "GROUP: %1 checked=%2 expanded=%3\n" ).arg( name() ).arg( mChecked ).arg( mExpanded );
468 QStringList childrenDump;
469 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
470 childrenDump << node->dump().split( '\n' );
471 for ( int i = 0; i < childrenDump.count(); ++i )
472 childrenDump[i].prepend( " " );
473 return header + childrenDump.join( QLatin1Char( '\n' ) );
474}
475
477{
478 return new QgsLayerTreeGroup( *this );
479}
480
481void QgsLayerTreeGroup::resolveReferences( const QgsProject *project, bool looseMatching )
482{
483 for ( QgsLayerTreeNode *node : std::as_const( mChildren ) )
484 node->resolveReferences( project, looseMatching );
485
486 mGroupLayer.resolve( project );
487}
488
489static bool _nodeIsChecked( QgsLayerTreeNode *node )
490{
491 return node->itemVisibilityChecked();
492}
493
494
499
500void QgsLayerTreeGroup::setIsMutuallyExclusive( bool enabled, int initialChildIndex )
501{
502 mMutuallyExclusive = enabled;
503 mMutuallyExclusiveChildIndex = initialChildIndex;
504
505 if ( !enabled )
506 {
507 return;
508 }
509
510 if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >= mChildren.count() )
511 {
512 // try to use first checked index
513 int index = 0;
514 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
515 {
516 if ( _nodeIsChecked( child ) )
517 {
519 break;
520 }
521 index++;
522 }
523 }
524
526}
527
529{
530 return qobject_cast< QgsGroupLayer * >( mGroupLayer.layer );
531}
532
534{
535 if ( QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() ) )
536 {
537 if ( !layer )
538 {
540 }
541 }
542 mGroupLayer.setLayer( layer );
543 refreshParentGroupLayerMembers();
544}
545
547{
548 if ( !mGroupLayer.layerId.isEmpty() )
549 return nullptr;
550
551 auto res = std::make_unique< QgsGroupLayer >( name(), options );
552
553 mGroupLayer.setLayer( res.get() );
554 updateGroupLayers();
555
556 return res.release();
557}
558
559void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
560{
561 QgsLayerTreeGroup *parentGroup = qobject_cast< QgsLayerTreeGroup * >( parent() );
562 while ( parentGroup )
563 {
564 if ( QgsLayerTree *layerTree = qobject_cast< QgsLayerTree * >( parentGroup ) )
565 layerTree->emit layerOrderChanged();
566
567 parentGroup->updateGroupLayers();
568 parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->parent() );
569 }
570}
571
573{
574 QStringList lst;
575 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
576 {
577 if ( QgsLayerTree::isGroup( child ) )
578 lst << QgsLayerTree::toGroup( child )->findLayerIds();
579 else if ( QgsLayerTree::isLayer( child ) )
580 lst << QgsLayerTree::toLayer( child )->layerId();
581 }
582 return lst;
583}
584
586{
587 int childIndex = mChildren.indexOf( node );
588 if ( childIndex == -1 )
589 {
590 updateGroupLayers();
591 return; // not a direct child - ignore
592 }
593
594 if ( mMutuallyExclusive )
595 {
596 if ( _nodeIsChecked( node ) )
597 mMutuallyExclusiveChildIndex = childIndex;
598 else if ( mMutuallyExclusiveChildIndex == childIndex )
600
601 // we need to make sure there is only one child node checked
603 }
604
605 updateGroupLayers();
606}
607
609{
610 if ( mChildren.isEmpty() )
611 return;
612
613 mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
614
615 int index = 0;
616 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
617 {
618 child->setItemVisibilityChecked( index == mMutuallyExclusiveChildIndex );
619 ++index;
620 }
621
623
624 updateGroupLayers();
625}
626
628{
630 // Reconnect internal signals
632}
633
634void QgsLayerTreeGroup::updateGroupLayers()
635{
636 QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( mGroupLayer.get() );
637 if ( !groupLayer )
638 return;
639
640 QList< QgsMapLayer * > layers;
641
642 std::function< void( QgsLayerTreeGroup * ) > findGroupLayerChildren;
643 findGroupLayerChildren = [&layers, &findGroupLayerChildren]( QgsLayerTreeGroup * group )
644 {
645 for ( auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
646 {
647 if ( QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
648 {
649 if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
650 layers << layerTreeLayer->layer();
651 }
652 else if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
653 {
654 if ( childGroup->isVisible() )
655 {
656 if ( QgsGroupLayer *groupLayer = childGroup->groupLayer() )
657 layers << groupLayer;
658 else
659 findGroupLayerChildren( childGroup );
660 }
661 }
662 }
663 };
664 findGroupLayerChildren( this );
665
666 groupLayer->setChildLayers( layers );
667 refreshParentGroupLayerMembers();
668}
669
671{
673
674 mChangingChildVisibility = true; // guard against running again setVisible() triggered from children
675
676 int index = 0;
677 for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
678 {
679 child->setItemVisibilityCheckedRecursive( checked && ( mMutuallyExclusiveChildIndex < 0 || index == mMutuallyExclusiveChildIndex ) );
680 ++index;
681 }
682
684
685 updateGroupLayers();
686}
687
689{
690 return mServerProperties.get();
691}
692
694{
695 return mServerProperties.get();
696}
697
698void QgsLayerTreeGroup::setHasWmsTimeDimension( const bool hasWmsTimeDimension )
699{
701}
702
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.
void setHasWmsTimeDimension(const bool hasWmsTimeDimension)
Sets whether the WMS time dimension should be computed for this group or not.
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.
bool hasWmsTimeDimension() const
Returns whether the WMS time dimension should be computed for this group or not.
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.