QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgslayertreecustomnode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreecustomnode.cpp
3 --------------------------
4 begin : July 2025
5 copyright : (C) 2025 by Germán Carrillo
6 email : german at opengis dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgslayertreeutils.h"
21
22#include "moc_qgslayertreecustomnode.cpp"
23
24QgsLayerTreeCustomNode::QgsLayerTreeCustomNode( const QString &nodeId, const QString &nodeName, bool checked )
25 : QgsLayerTreeNode( NodeCustom, checked )
26 , mId( nodeId )
27 , mName( nodeName.isEmpty() ? nodeId : nodeName )
28{
29}
30
32 : QgsLayerTreeNode( other )
33 , mId( other.mId )
34 , mName( other.mName )
35{
36}
37
39{
40 return mName;
41}
42
44{
45 if ( mName == name )
46 return;
47
48 mName = name;
49 emit nameChanged( this, name );
50}
51
52QgsLayerTreeCustomNode *QgsLayerTreeCustomNode::readXml( const QDomElement &element, const QgsReadWriteContext & ) // cppcheck-suppress duplInheritedMember
53{
54 if ( element.tagName() != QLatin1String( "layer-tree-custom-node" ) )
55 return nullptr;
56
57 const QString nodeId = element.attribute( QStringLiteral( "id" ) );
58 const QString name = element.attribute( QStringLiteral( "name" ) );
59 bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
60
61 QgsLayerTreeCustomNode *customNode = new QgsLayerTreeCustomNode( nodeId, name, checked );
62 customNode->readCommonXml( element );
63
64 return customNode;
65}
66
67void QgsLayerTreeCustomNode::writeXml( QDomElement &parentElement, const QgsReadWriteContext & )
68{
69 QDomDocument doc = parentElement.ownerDocument();
70 QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-custom-node" ) );
71 elem.setAttribute( QStringLiteral( "id" ), mId );
72 elem.setAttribute( QStringLiteral( "name" ), mName );
73 elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
74
75 writeCommonXml( elem );
76
77 parentElement.appendChild( elem );
78}
79
81{
82 return QStringLiteral( "CUSTOM NODE: %1 checked=%2 id=%3\n" ).arg( mName ).arg( mChecked ).arg( mId );
83}
84
89
QString name() const override
Returns the node's name.
QString dump() const override
Returns string with layer tree structure. For debug purposes only.
QgsLayerTreeCustomNode * clone() const override
Create a copy of the node. Returns new instance.
void resolveReferences(const QgsProject *project, bool looseMatching=false) override
Turn textual references to layers into map layer object from project.
QString nodeId() const
Returns the node's unique identifier.
void setName(const QString &name) override
Sets the node's name.
QgsLayerTreeCustomNode(const QString &nodeId, const QString &nodeName=QString(), bool checked=true)
Constructor to create custom nodes that represent application objects other than layers and groups.
void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context) override
Write custom node as XML element <layer-tree-custom-node> and add it to the given parent element.
static QgsLayerTreeCustomNode * readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read custom node from XML element <layer-tree-custom-node> and return the newly created node (or null...
@ NodeCustom
Leaf node pointing to a custom object.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
QgsLayerTreeNode(NodeType t, bool checked=true)
Constructor.
void writeCommonXml(QDomElement &element)
Write common XML elements.
void readCommonXml(const QDomElement &element)
Read common XML elements.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
A container for the context for various read/write operations on objects.