17#include "moc_qgslayertreegroup.cpp"
39 , mName( other.mName )
40 , mChangingChildVisibility( other.mChangingChildVisibility )
41 , mMutuallyExclusive( other.mMutuallyExclusive )
42 , mMutuallyExclusiveChildIndex( other.mMutuallyExclusiveChildIndex )
43 , mGroupLayer( other.mGroupLayer )
51void QgsLayerTreeGroup::init()
114 QList<QgsLayerTreeNode *> nodes;
140 index =
mChildren.count() - nodes.count();
172 if ( childLayer->
layer() == layer )
242 if ( childLayer->
layerId() == layerId )
257 QList<QgsLayerTreeLayer *> list;
270 const QList< QgsLayerTreeLayer * > childLayers =
findLayers();
276 if ( layerNode->layer() == targetLayer )
290 QList<QgsMapLayer *> list;
338 QList<QgsLayerTreeGroup *> list;
355 if ( element.tagName() != QLatin1String(
"layer-tree-group" ) )
359 bool isExpanded = ( element.attribute( QStringLiteral(
"expanded" ), QStringLiteral(
"1" ) ) == QLatin1String(
"1" ) );
361 bool isMutuallyExclusive = element.attribute( QStringLiteral(
"mutually-exclusive" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" );
362 int mutuallyExclusiveChildIndex = element.attribute( QStringLiteral(
"mutually-exclusive-child" ), QStringLiteral(
"-1" ) ).toInt();
373 groupNode->mGroupLayer =
QgsMapLayerRef( element.attribute( QStringLiteral(
"groupLayer" ) ) );
375 readLegacyServerProperties( groupNode );
382void QgsLayerTreeGroup::readLegacyServerProperties(
QgsLayerTreeGroup *groupNode )
384 const QVariant wmsShortName = groupNode->
customProperty( QStringLiteral(
"wmsShortName" ) );
385 if ( wmsShortName.isValid() )
391 const QVariant wmsTitle = groupNode->
customProperty( QStringLiteral(
"wmsTitle" ) );
392 if ( wmsTitle.isValid() )
398 const QVariant wmsAbstract = groupNode->
customProperty( QStringLiteral(
"wmsAbstract" ) );
399 if ( wmsAbstract.isValid() )
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" ) );
426 elem.setAttribute( QStringLiteral(
"mutually-exclusive" ), QStringLiteral(
"1" ) );
429 elem.setAttribute( QStringLiteral(
"groupLayer" ), mGroupLayer.
layerId );
436 node->writeXml( elem, context );
438 parentElement.appendChild( elem );
443 QList<QgsLayerTreeNode *> nodes;
444 QDomElement childElem = element.firstChildElement();
445 while ( !childElem.isNull() )
451 childElem = childElem.nextSiblingElement();
459 QString header = QStringLiteral(
"GROUP: %1 checked=%2 expanded=%3\n" ).arg(
name() ).arg(
mChecked ).arg(
mExpanded );
460 QStringList childrenDump;
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' ) );
476 node->resolveReferences( project, looseMatching );
478 mGroupLayer.
resolve( project );
502 if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >=
mChildren.count() )
508 if ( _nodeIsChecked( child ) )
522 return qobject_cast< QgsGroupLayer * >( mGroupLayer.
layer );
535 refreshParentGroupLayerMembers();
540 if ( !mGroupLayer.
layerId.isEmpty() )
543 auto res = std::make_unique< QgsGroupLayer >(
name(), options );
548 return res.release();
551void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
554 while ( parentGroup )
556 if (
QgsLayerTree *layerTree = qobject_cast< QgsLayerTree * >( parentGroup ) )
557 layerTree->emit layerOrderChanged();
559 parentGroup->updateGroupLayers();
560 parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->
parent() );
579 int childIndex =
mChildren.indexOf( node );
580 if ( childIndex == -1 )
588 if ( _nodeIsChecked( node ) )
626void QgsLayerTreeGroup::updateGroupLayers()
632 QList< QgsMapLayer * > layers;
635 findGroupLayerChildren = [&layers, &findGroupLayerChildren](
QgsLayerTreeGroup * group )
637 for (
auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
639 if (
QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
641 if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
642 layers << layerTreeLayer->layer();
644 else if (
QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
646 if ( childGroup->isVisible() )
651 findGroupLayerChildren( childGroup );
656 findGroupLayerChildren(
this );
659 refreshParentGroupLayerMembers();
682 return mServerProperties.get();
687 return mServerProperties.get();
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...
bool mChangingChildVisibility
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.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
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,...
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.