QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
22 #include <QDomElement>
23 #include <QStringList>
35 , mName( other.mName )
36 , mChangingChildVisibility( other.mChangingChildVisibility )
37 , mMutuallyExclusive( other.mMutuallyExclusive )
38 , mMutuallyExclusiveChildIndex( other.mMutuallyExclusiveChildIndex )
94 QList<QgsLayerTreeNode *> nodes;
120 index =
mChildren.count() - nodes.count();
146 if ( childLayer->
layer() == layer )
210 if ( childLayer->
layerId() == layerId )
225 QList<QgsLayerTreeLayer *> list;
258 QList<QgsLayerTreeGroup *> list;
270 if ( element.tagName() != QLatin1String(
"layer-tree-group" ) )
274 bool isExpanded = ( element.attribute( QStringLiteral(
"expanded" ), QStringLiteral(
"1" ) ) == QLatin1String(
"1" ) );
276 bool isMutuallyExclusive = element.attribute( QStringLiteral(
"mutually-exclusive" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" );
277 int mutuallyExclusiveChildIndex = element.attribute( QStringLiteral(
"mutually-exclusive-child" ), QStringLiteral(
"-1" ) ).toInt();
301 QDomDocument doc = parentElement.ownerDocument();
302 QDomElement elem = doc.createElement( QStringLiteral(
"layer-tree-group" ) );
303 elem.setAttribute( QStringLiteral(
"name" ),
mName );
304 elem.setAttribute( QStringLiteral(
"expanded" ),
mExpanded ?
"1" :
"0" );
305 elem.setAttribute( QStringLiteral(
"checked" ),
mChecked ? QStringLiteral(
"Qt::Checked" ) : QStringLiteral(
"Qt::Unchecked" ) );
308 elem.setAttribute( QStringLiteral(
"mutually-exclusive" ), QStringLiteral(
"1" ) );
315 node->writeXml( elem, context );
317 parentElement.appendChild( elem );
322 QList<QgsLayerTreeNode *> nodes;
323 QDomElement childElem = element.firstChildElement();
324 while ( !childElem.isNull() )
330 childElem = childElem.nextSiblingElement();
338 QString header = QStringLiteral(
"GROUP: %1 checked=%2 expanded=%3\n" ).arg(
name() ).arg(
mChecked ).arg(
mExpanded );
339 QStringList childrenDump;
341 childrenDump << node->dump().split(
'\n' );
342 for (
int i = 0; i < childrenDump.count(); ++i )
343 childrenDump[i].prepend(
" " );
344 return header + childrenDump.join( QStringLiteral(
"\n" ) );
355 node->resolveReferences( project, looseMatching );
379 if ( mMutuallyExclusiveChildIndex < 0 || mMutuallyExclusiveChildIndex >=
mChildren.count() )
385 if ( _nodeIsChecked( child ) )
412 int childIndex =
mChildren.indexOf( node );
413 if ( childIndex == -1 )
418 if ( _nodeIsChecked( node ) )
@ 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.
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)
void removeAllChildren()
Remove all child nodes.
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.
void insertChildrenPrivate(int index, QList< QgsLayerTreeNode * > nodes)
Low-level insertion of children to the node. The children must not have any parent yet!
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.
void removeLayer(QgsMapLayer *layer)
Remove map layer's node from this group.
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.
QList< QgsLayerTreeGroup * > findGroups() const
Find all group layer nodes.
bool isMutuallyExclusive() const
Returns whether the group is mutually exclusive (only one child can be checked at a time)
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer.
int mMutuallyExclusiveChildIndex
Keeps track which child has been most recently selected (so if the whole group is unchecked and check...
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.
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)
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
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.
bool mMutuallyExclusive
Whether the group is mutually exclusive (i.e. only one child can be checked at a time)
QString dump() const override
Returns text representation of the tree.
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.
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.