28#include "moc_qgslayertreegroup.cpp"
30using namespace Qt::StringLiterals;
48 , mGroupLayer( other.mGroupLayer )
50 , mWmsGroupRequestMode( other.mWmsGroupRequestMode )
57void QgsLayerTreeGroup::init()
79void QgsLayerTreeGroup::setId(
const QString &
id )
125 if (
id.trimmed().isEmpty() )
130 if ( customNodeIds.contains(
id ) )
140 if ( node->id().trimmed().isEmpty() )
145 if ( customNodeIds.contains( node->id() ) )
154 if (
id.trimmed().isEmpty() )
159 if ( customNodeIds.contains(
id ) )
169 QList<QgsLayerTreeNode *> nodes;
195 index =
mChildren.count() - nodes.count();
227 if ( childLayer->
layer() == layer )
245 if ( childCustom->
id() == customNode->
id() )
315 if ( childLayer->
layerId() == layerId )
330 QList<QgsLayerTreeLayer *> list;
348 if ( childCustom->
id() ==
id )
363 QList<QgsLayerTreeNode *> list;
376 const QList< QgsLayerTreeLayer * > childLayers =
findLayers();
382 if ( layerNode->layer() == targetLayer )
402 if ( childNode == targetNode )
416 QList<QgsMapLayer *> list;
444 QList<QgsLayerTreeNode *> list;
496 QList<QgsLayerTreeGroup *> list;
513 if ( element.tagName() !=
"layer-tree-group"_L1 )
517 bool isExpanded = ( element.attribute( u
"expanded"_s, u
"1"_s ) ==
"1"_L1 );
520 int mutuallyExclusiveChildIndex = element.attribute( u
"mutually-exclusive-child"_s, u
"-1"_s ).toInt();
524 const QString
id = element.attribute( u
"id"_s );
537 groupNode->mGroupLayer =
QgsMapLayerRef( element.attribute( u
"groupLayer"_s ) );
539 readLegacyServerProperties( groupNode );
545 if ( element.attribute( u
"wms-has-time-dimension"_s, u
"0"_s ) ==
"1"_L1 )
555void QgsLayerTreeGroup::readLegacyServerProperties(
QgsLayerTreeGroup *groupNode )
557 const QVariant wmsShortName = groupNode->
customProperty( u
"wmsShortName"_s );
558 if ( wmsShortName.isValid() )
564 const QVariant wmsTitle = groupNode->
customProperty( u
"wmsTitle"_s );
565 if ( wmsTitle.isValid() )
571 const QVariant wmsAbstract = groupNode->
customProperty( u
"wmsAbstract"_s );
572 if ( wmsAbstract.isValid() )
592 QDomDocument doc = parentElement.ownerDocument();
593 QDomElement elem = doc.createElement( u
"layer-tree-group"_s );
594 elem.setAttribute( u
"name"_s,
mName );
595 elem.setAttribute( u
"id"_s,
mId );
596 elem.setAttribute( u
"expanded"_s,
mExpanded ? u
"1"_s : u
"0"_s );
597 elem.setAttribute( u
"checked"_s,
mChecked ? u
"Qt::Checked"_s : u
"Qt::Unchecked"_s );
600 elem.setAttribute( u
"mutually-exclusive"_s, u
"1"_s );
604 elem.setAttribute( u
"wms-group-request-mode"_s,
qgsEnumValueToKey( mWmsGroupRequestMode ) );
606 elem.setAttribute( u
"groupLayer"_s, mGroupLayer.layerId );
613 node->writeXml( elem, context );
615 parentElement.appendChild( elem );
620 QList<QgsLayerTreeNode *> nodes;
621 QDomElement childElem = element.firstChildElement();
622 while ( !childElem.isNull() )
628 childElem = childElem.nextSiblingElement();
636 QString header = u
"GROUP: %1 checked=%2 expanded=%3\n"_s.arg(
name() ).arg(
mChecked ).arg(
mExpanded );
637 QStringList childrenDump;
639 childrenDump << node->dump().split(
'\n' );
640 for (
int i = 0; i < childrenDump.count(); ++i )
641 childrenDump[i].prepend(
" " );
642 return header + childrenDump.join( QLatin1Char(
'\n' ) );
653 node->resolveReferences( project, looseMatching );
655 mGroupLayer.resolve( project );
685 if ( _nodeIsChecked( child ) )
699 return qobject_cast< QgsGroupLayer * >( mGroupLayer.layer );
708 groupLayer->prepareLayersForRemovalFromGroup();
711 mGroupLayer.setLayer( layer );
712 refreshParentGroupLayerMembers();
717 if ( !mGroupLayer.layerId.isEmpty() )
720 auto res = std::make_unique< QgsGroupLayer >(
name(), options );
722 mGroupLayer.setLayer( res.get() );
725 return res.release();
728void QgsLayerTreeGroup::refreshParentGroupLayerMembers()
731 while ( parentGroup )
733 if (
QgsLayerTree *layerTree = qobject_cast< QgsLayerTree * >( parentGroup ) )
734 layerTree->emit layerOrderChanged();
736 parentGroup->updateGroupLayers();
737 parentGroup = qobject_cast< QgsLayerTreeGroup * >( parentGroup->
parent() );
769 int childIndex =
mChildren.indexOf( node );
770 if ( childIndex == -1 )
778 if ( _nodeIsChecked( node ) )
816void QgsLayerTreeGroup::updateGroupLayers()
822 QList< QgsMapLayer * > layers;
825 findGroupLayerChildren = [&layers, &findGroupLayerChildren](
QgsLayerTreeGroup *group ) {
826 for (
auto it = group->mChildren.crbegin(); it != group->mChildren.crend(); ++it )
828 if (
QgsLayerTreeLayer *layerTreeLayer = qobject_cast< QgsLayerTreeLayer * >( *it ) )
830 if ( layerTreeLayer->layer() && layerTreeLayer->isVisible() )
831 layers << layerTreeLayer->layer();
833 else if (
QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( *it ) )
835 if ( childGroup->isVisible() )
837 if ( QgsGroupLayer *
groupLayer = childGroup->groupLayer() )
840 findGroupLayerChildren( childGroup );
845 findGroupLayerChildren(
this );
848 refreshParentGroupLayerMembers();
871 return mServerProperties.get();
876 return mServerProperties.get();
881 if ( !mServerProperties )
886 if ( !lHasTimeDimension && hasTimeDimension )
890 else if ( lHasTimeDimension && !hasTimeDimension )
898 if ( !mServerProperties )
902 return dim.name == QgsServerWmsDimensionProperties::TIME_DIMENSION_NAME;
905 return it != mServerProperties->wmsDimensions().constEnd();
910 return mWmsGroupRequestMode;
915 mWmsGroupRequestMode = groupRequestMode;
WmsGroupRequestMode
Request mode of groups in a WMS context.
@ Normal
Group and children can be requested.
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Layer tree custom node serves as a node for objects that are not layers nor groups.
QString id() const override
Returns the node's unique identifier.
Layer tree group node serves as a container for layers and further groups.
QStringList findCustomNodeIds() const
Find custom node IDs.
void insertChildNode(int index, QgsLayerTreeNode *node)
Insert existing node at specified position.
QgsLayerTreeCustomNode * insertCustomNode(int index, const QString &id, const QString &name=QString())
Insert a new custom node with the given id and name at specified index.
QgsLayerTreeCustomNode * findCustomNode(const QString &id) const
Find custom node representing an object specified by its ID.
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.
Q_DECL_DEPRECATED 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< QgsLayerTreeNode * > findLayersAndCustomNodes() const
Find all layer and custom nodes.
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 removeCustomNode(QgsLayerTreeCustomNode *customNode)
Remove a custom node from this group.
bool mChangingChildVisibility
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the layer tree group.
void addChildNode(QgsLayerTreeNode *node)
Append an existing node.
QList< QgsLayerTreeNode * > layerAndCustomNodeOrderRespectingGroupLayers() const
Returns an ordered list of map layers and custom nodes in the group, ignoring any layers which are ch...
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).
QString id() const override
Returns the node's unique identifier.
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).
QgsLayerTreeCustomNode * addCustomNode(const QString &id, const QString &name=QString())
Append a new custom node with the given id and name.
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.
Qgis::WmsGroupRequestMode wmsGroupRequestMode() const
Returns the request mode of the group.
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.
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.
void setWmsGroupRequestMode(Qgis::WmsGroupRequestMode groupRequestMode)
Sets the request mode of the 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...
void reorderGroupLayersAndCustomNodes(const QList< QgsLayerTreeNode * > &order)
Reorders layers and custom nodes in the group to match the order specified by order.
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.
QgsLayerTreeNode(NodeType t, bool checked=true)
Constructor.
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.
static bool isCustomNode(const QgsLayerTreeNode *node)
Check whether the node is a valid custom node.
static QgsLayerTreeCustomNode * toCustomNode(QgsLayerTreeNode *node)
Cast node to a custom node.
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 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.
static constexpr const char * TIME_DIMENSION_NAME
Utility functions for working with strings.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
#define Q_NOWARN_DEPRECATED_POP
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define Q_NOWARN_DEPRECATED_PUSH
_LayerRef< QgsMapLayer > QgsMapLayerRef
Setting options for loading group layers.
Setting to define QGIS Server WMS Dimension.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.