QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
35 : QgsLayerTreeNode( other )
36 , mId( other.mId )
37 , mName( other.mName )
38{}
39
41{
42 return mName;
43}
44
46{
47 if ( mName == name )
48 return;
49
50 mName = name;
51 emit nameChanged( this, name );
52}
53
54QgsLayerTreeCustomNode *QgsLayerTreeCustomNode::readXml( const QDomElement &element, const QgsReadWriteContext & ) // cppcheck-suppress duplInheritedMember
55{
56 if ( element.tagName() != "layer-tree-custom-node"_L1 )
57 return nullptr;
58
59 const QString nodeId = element.attribute( u"id"_s );
60 const QString name = element.attribute( u"name"_s );
61 bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( u"checked"_s ) ) != Qt::Unchecked;
62
63 QgsLayerTreeCustomNode *customNode = new QgsLayerTreeCustomNode( nodeId, name, checked );
64 customNode->readCommonXml( element );
65
66 return customNode;
67}
68
69void QgsLayerTreeCustomNode::writeXml( QDomElement &parentElement, const QgsReadWriteContext & )
70{
71 QDomDocument doc = parentElement.ownerDocument();
72 QDomElement elem = doc.createElement( u"layer-tree-custom-node"_s );
73 elem.setAttribute( u"id"_s, mId );
74 elem.setAttribute( u"name"_s, mName );
75 elem.setAttribute( u"checked"_s, mChecked ? u"Qt::Checked"_s : u"Qt::Unchecked"_s );
76
77 writeCommonXml( elem );
78
79 parentElement.appendChild( elem );
80}
81
83{
84 return u"CUSTOM NODE: %1 checked=%2 id=%3\n"_s.arg( mName ).arg( mChecked ).arg( mId );
85}
86
91
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.