QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
23 #include <QDomElement>
24 #include <QStringList>
36 , mName( other.mName )
37 , mChangingChildVisibility( other.mChangingChildVisibility )
38 , mMutuallyExclusive( other.mMutuallyExclusive )
39 , mMutuallyExclusiveChildIndex( other.mMutuallyExclusiveChildIndex )
40 , mGroupLayer( other.mGroupLayer )
45 void QgsLayerTreeGroup::init()
108 QList<QgsLayerTreeNode *> nodes;
134 index =
mChildren.count() - nodes.count();
166 if ( childLayer->
layer() == layer )
236 if ( childLayer->
layerId() == layerId )
251 QList<QgsLayerTreeLayer *> list;
264 QList<QgsMapLayer *> list;
312 QList<QgsLayerTreeGroup *> list;
329 if ( element.tagName() != QLatin1String(
"layer-tree-group" ) )
333 bool isExpanded = ( element.attribute( QStringLiteral(
"expanded" ), QStringLiteral(
"1" ) ) == QLatin1String(
"1" ) );
335 bool isMutuallyExclusive = element.attribute( QStringLiteral(
"mutually-exclusive" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" );
336 int mutuallyExclusiveChildIndex = element.attribute( QStringLiteral(
"mutually-exclusive-child" ), QStringLiteral(
"-1" ) ).toInt();
347 groupNode->mGroupLayer =
QgsMapLayerRef( element.attribute( QStringLiteral(
"groupLayer" ) ) );
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" ) );
369 elem.setAttribute( QStringLiteral(
"mutually-exclusive" ), QStringLiteral(
"1" ) );
372 elem.setAttribute( QStringLiteral(
"groupLayer" ), mGroupLayer.
layerId );
377 node->writeXml( elem, context );
379 parentElement.appendChild( elem );
384 QList<QgsLayerTreeNode *> nodes;
385 QDomElement childElem = element.firstChildElement();
386 while ( !childElem.isNull() )
392 childElem = childElem.nextSiblingElement();
400 QString header = QStringLiteral(
"GROUP: %1 checked=%2 expanded=%3\n" ).arg(
name() ).arg(
mChecked ).arg(
mExpanded );
401 QStringList childrenDump;
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' ) );
417 node->resolveReferences( project, looseMatching );
419 mGroupLayer.
resolve( project );
443 if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >=
mChildren.count() )
449 if ( _nodeIsChecked( child ) )
463 return qobject_cast< QgsGroupLayer * >( mGroupLayer.
layer );
469 refreshParentGroupLayerMembers();
474 if ( !mGroupLayer.
layerId.isEmpty() )
477 std::unique_ptr< QgsGroupLayer > res = std::make_unique< QgsGroupLayer >(
name(), options );
482 return res.release();
485 void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
488 while ( parentGroup )
493 parentGroup->updateGroupLayers();
494 parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->
parent() );
513 int childIndex =
mChildren.indexOf( node );
514 if ( childIndex == -1 )
522 if ( _nodeIsChecked( node ) )
560 void QgsLayerTreeGroup::updateGroupLayers()
566 QList< QgsMapLayer * > layers;
569 findGroupLayerChildren = [&layers, &findGroupLayerChildren](
QgsLayerTreeGroup * group )
571 for (
auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
573 if (
QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
575 if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
576 layers << layerTreeLayer->layer();
578 else if (
QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
580 if ( childGroup->isVisible() )
585 findGroupLayerChildren( childGroup );
590 findGroupLayerChildren(
this );
593 refreshParentGroupLayerMembers();
@ NodeGroup
Container of other groups and layers.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
This class is a base class for nodes in a layer tree.
void removeChildren(int from, int count)
Remove child nodes from index "from".
void removeChildNode(QgsLayerTreeNode *node)
Remove a child node from this group.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
QgsGroupLayer * groupLayer()
Returns a reference to the associated group layer, if the layer tree group will be treated as group l...
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
The class is used as a container of context for various read/write operations on other objects.
void removeAllChildren()
Remove all child nodes.
QgsLayerTree * layerTree(const QgsWmsRenderContext &context)
void resolveReferences(const QgsProject *project, bool looseMatching=false) override
Calls resolveReferences() on child tree nodes.
QgsLayerTreeGroup * insertGroup(int index, const QString &name)
Insert a new group node with given name at specified position.
QString layerId() const
Returns the ID for the map layer associated with this node.
void writeCommonXml(QDomElement &element)
Write common XML elements.
void setIsMutuallyExclusive(bool enabled, int initialChildIndex=-1)
Set whether the group is mutually exclusive (only one child can be checked at a time).
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.
void nodeVisibilityChanged(QgsLayerTreeNode *node)
void readChildrenFromXml(QDomElement &element, const QgsReadWriteContext &context)
Read children from XML and append them to the group.
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
_LayerRef< QgsMapLayer > QgsMapLayerRef
void removeLayer(QgsMapLayer *layer)
Remove map layer's node from this group.
QPointer< TYPE > layer
Weak pointer to map layer.
Setting options for loading group layers.
void setItemVisibilityCheckedRecursive(bool checked) override
Check or uncheck a node and all its children (taking into account exclusion rules)
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
bool isMutuallyExclusive() const
Returns whether the group is mutually exclusive (only one child can be checked at a time)
QList< QgsMapLayer * > layerOrderRespectingGroupLayers() const
Returns an ordered list of map layers in the group, ignoring any layers which are child layers of Qgs...
QString layerId
Original layer ID.
QgsGroupLayer * convertToGroupLayer(const QgsGroupLayer::LayerOptions &options)
Converts the group to a QgsGroupLayer.
Namespace with helper functions for layer tree operations.
void insertChildrenPrivate(int index, const QList< QgsLayerTreeNode * > &nodes)
Low-level insertion of children to the node. The children must not have any parent yet!
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer.
Layer tree node points to a map layer.
int mMutuallyExclusiveChildIndex
Keeps track which child has been most recently selected (so if the whole group is unchecked and check...
void setGroupLayer(QgsGroupLayer *layer)
Sets the associated group layer, if the layer tree group will be treated as group layer during map re...
Layer tree group node serves as a container for layers and further groups.
void setName(const QString &n) override
Sets the group's name.
void insertChildNodes(int index, const QList< QgsLayerTreeNode * > &nodes)
Insert existing nodes at specified position.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QgsLayerTreeLayer * insertLayer(int index, QgsMapLayer *layer)
Insert a new layer node for given map layer at specified position.
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
bool mChangingChildVisibility
static QgsLayerTreeNode * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read layer tree from XML.
void insertChildNode(int index, QgsLayerTreeNode *node)
Insert existing node at specified position.
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 ...
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
QStringList findLayerIds() const
Find layer IDs used in all layer nodes.
void setChildLayers(const QList< QgsMapLayer * > &layers)
Sets the child layers contained by the group.
QgsLayerTreeGroup(const QString &name=QString(), bool checked=true)
Constructor.
void addChildNode(QgsLayerTreeNode *node)
Append an existing node.
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
Base class for all map layer types. This is the base class for all map layer types (vector,...
virtual void makeOrphan()
Sets parent to nullptr and disconnects all external and forwarded signals.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
virtual void makeOrphan() override
Sets parent to nullptr and disconnects all external and forwarded signals.
QString name() const override
Returns the group's name.
bool mExpanded
whether the node should be shown in GUI as expanded
void updateChildVisibilityMutuallyExclusive()
Set check state of children - if mutually exclusive.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
bool mMutuallyExclusive
Whether the group is mutually exclusive (i.e. only one child can be checked at a time)
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
QString dump() const override
Returns text representation of the tree.
QList< QgsLayerTreeGroup * > findGroups(bool recursive=false) const
Find group layer nodes.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QgsLayerTreeGroup * clone() const override
Returns a clone of the group.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
void removeChildrenPrivate(int from, int count, bool destroy=true)
Low-level removal of children from the node.
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
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.
void removeChildrenGroupWithoutLayers()
Remove all child group nodes without layers.
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
void readCommonXml(QDomElement &element)
Read common XML elements.