QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 <QString>
23
24#include "moc_qgslayertreecustomnode.cpp"
25
26using namespace Qt::StringLiterals;
27
28QgsLayerTreeCustomNode::QgsLayerTreeCustomNode( const QString &nodeId, const QString &nodeName, bool checked )
29 : QgsLayerTreeNode( NodeCustom, checked )
30 , mId( nodeId )
31 , mName( nodeName.isEmpty() ? nodeId : nodeName )
32{
33}
34
36 : QgsLayerTreeNode( other )
37 , mId( other.mId )
38 , mName( other.mName )
39{
40}
41
43{
44 return mName;
45}
46
48{
49 if ( mName == name )
50 return;
51
52 mName = name;
53 emit nameChanged( this, name );
54}
55
56QgsLayerTreeCustomNode *QgsLayerTreeCustomNode::readXml( const QDomElement &element, const QgsReadWriteContext & ) // cppcheck-suppress duplInheritedMember
57{
58 if ( element.tagName() != "layer-tree-custom-node"_L1 )
59 return nullptr;
60
61 const QString nodeId = element.attribute( u"id"_s );
62 const QString name = element.attribute( u"name"_s );
63 bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( u"checked"_s ) ) != Qt::Unchecked;
64
65 QgsLayerTreeCustomNode *customNode = new QgsLayerTreeCustomNode( nodeId, name, checked );
66 customNode->readCommonXml( element );
67
68 return customNode;
69}
70
71void QgsLayerTreeCustomNode::writeXml( QDomElement &parentElement, const QgsReadWriteContext & )
72{
73 QDomDocument doc = parentElement.ownerDocument();
74 QDomElement elem = doc.createElement( u"layer-tree-custom-node"_s );
75 elem.setAttribute( u"id"_s, mId );
76 elem.setAttribute( u"name"_s, mName );
77 elem.setAttribute( u"checked"_s, mChecked ? u"Qt::Checked"_s : u"Qt::Unchecked"_s );
78
79 writeCommonXml( elem );
80
81 parentElement.appendChild( elem );
82}
83
85{
86 return u"CUSTOM NODE: %1 checked=%2 id=%3\n"_s.arg( mName ).arg( mChecked ).arg( mId );
87}
88
93
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:113
A container for the context for various read/write operations on objects.