QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgslayertreelayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreelayer.cpp
3 --------------------------------------
4 Date : May 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgslayertreelayer.h"
17
18#include "qgslayertreeutils.h"
19#include "qgsmaplayer.h"
20#include "qgsproject.h"
21#include "qgsproviderregistry.h"
22#include "qgssymbollayerutils.h"
23
24#include <QString>
25
26#include "moc_qgslayertreelayer.cpp"
27
28using namespace Qt::StringLiterals;
29
37
38QgsLayerTreeLayer::QgsLayerTreeLayer( const QString &layerId, const QString &name, const QString &source, const QString &provider )
40 , mRef( layerId, name, source, provider )
41 , mLayerName( name.isEmpty() ? u"(?)"_s : name )
42{
43}
44
46 : QgsLayerTreeNode( other )
47 , mRef( other.mRef )
48 , mLayerName( other.mLayerName )
49 , mPatchShape( other.mPatchShape )
50 , mPatchSize( other.mPatchSize )
51 , mSplitBehavior( other.mSplitBehavior )
52{
54}
55
56void QgsLayerTreeLayer::resolveReferences( const QgsProject *project, bool looseMatching )
57{
58 if ( mRef )
59 return; // already assigned
60
61 if ( !looseMatching )
62 {
63 mRef.resolve( project );
64 }
65 else
66 {
67 mRef.resolveWeakly( project );
68 }
69
70 if ( !mRef )
71 return;
72
74 emit layerLoaded();
75}
76
78{
79 if ( !mRef )
80 return;
81
82 connect( mRef.layer, &QgsMapLayer::nameChanged, this, &QgsLayerTreeLayer::layerNameChanged );
83 connect( mRef.layer, &QgsMapLayer::willBeDeleted, this, &QgsLayerTreeLayer::layerWillBeDeleted );
84}
85
86
88{
89 return ( mRef && mUseLayerName ) ? mRef->name() : mLayerName;
90}
91
92void QgsLayerTreeLayer::setName( const QString &n )
93{
94 if ( mRef && mUseLayerName )
95 {
96 if ( mRef->name() == n )
97 return;
98 mRef->setName( n );
99 // no need to emit signal: we will be notified from layer's nameChanged() signal
100 }
101 else
102 {
103 if ( mLayerName == n )
104 return;
105 mLayerName = n;
106 emit nameChanged( this, n );
107 }
108}
109
110QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsReadWriteContext &context ) // cppcheck-suppress duplInheritedMember
111{
112 if ( element.tagName() != "layer-tree-layer"_L1 )
113 return nullptr;
114
115 const QString layerID = element.attribute( u"id"_s );
116 const QString layerName = element.attribute( u"name"_s );
117
118 const QString providerKey = element.attribute( u"providerKey"_s );
119 const QString sourceRaw = element.attribute( u"source"_s );
120 const QString source = providerKey.isEmpty() ? sourceRaw : QgsProviderRegistry::instance()->relativeToAbsoluteUri( providerKey, sourceRaw, context );
121
122 const Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( u"checked"_s ) );
123 const bool isExpanded = ( element.attribute( u"expanded"_s, u"1"_s ) == "1"_L1 );
124 const QString labelExpression = element.attribute( u"legend_exp"_s );
125
126 // needs to have the layer reference resolved later
127 QgsLayerTreeLayer *nodeLayer = new QgsLayerTreeLayer( layerID, layerName, source, providerKey );
128
129 nodeLayer->readCommonXml( element );
130
131 nodeLayer->setItemVisibilityChecked( checked != Qt::Unchecked );
132 nodeLayer->setExpanded( isExpanded );
134
135 const QDomElement patchElem = element.firstChildElement( u"patch"_s );
136 if ( !patchElem.isNull() )
137 {
139 patch.readXml( patchElem, context );
140 nodeLayer->setPatchShape( patch );
141 }
142
143 nodeLayer->setPatchSize( QgsSymbolLayerUtils::decodeSize( element.attribute( u"patch_size"_s ) ) );
144
145 nodeLayer->setLegendSplitBehavior( static_cast< LegendNodesSplitBehavior >( element.attribute( u"legend_split_behavior"_s, u"0"_s ).toInt() ) );
146
147 return nodeLayer;
148}
149
150QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
151{
152 QgsLayerTreeLayer *node = readXml( element, context );
153 if ( node )
154 node->resolveReferences( project );
155 return node;
156}
157
158void QgsLayerTreeLayer::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
159{
160 QDomDocument doc = parentElement.ownerDocument();
161 QDomElement elem = doc.createElement( u"layer-tree-layer"_s );
162 elem.setAttribute( u"id"_s, layerId() );
163 elem.setAttribute( u"name"_s, name() );
164
165 if ( mRef )
166 {
167 const QString providerKey = mRef->dataProvider() ? mRef->dataProvider()->name() : QString();
168 const QString source = providerKey.isEmpty() ? mRef->publicSource() : QgsProviderRegistry::instance()->absoluteToRelativeUri( providerKey, mRef->publicSource(), context );
169 elem.setAttribute( u"source"_s, source );
170 elem.setAttribute( u"providerKey"_s, providerKey );
171 }
172
173 elem.setAttribute( u"checked"_s, mChecked ? u"Qt::Checked"_s : u"Qt::Unchecked"_s );
174 elem.setAttribute( u"expanded"_s, mExpanded ? "1" : "0" );
175 elem.setAttribute( u"legend_exp"_s, mLabelExpression );
176
177 if ( !mPatchShape.isNull() )
178 {
179 QDomElement patchElem = doc.createElement( u"patch"_s );
180 mPatchShape.writeXml( patchElem, doc, context );
181 elem.appendChild( patchElem );
182 }
183 elem.setAttribute( u"patch_size"_s, QgsSymbolLayerUtils::encodeSize( mPatchSize ) );
184
185 elem.setAttribute( u"legend_split_behavior"_s, mSplitBehavior );
186
187 writeCommonXml( elem );
188
189 parentElement.appendChild( elem );
190}
191
193{
194 return u"LAYER: %1 checked=%2 expanded=%3 id=%4\n"_s.arg( name() ).arg( mChecked ).arg( mExpanded ).arg( layerId() );
195}
196
198{
199 return new QgsLayerTreeLayer( *this );
200}
201
202void QgsLayerTreeLayer::layerWillBeDeleted()
203{
204 Q_ASSERT( mRef );
205
206 emit layerWillBeUnloaded();
207
208 mLayerName = mRef->name();
209 // in theory we do not even need to do this - the weak ref should clear itself
210 mRef.layer.clear();
211 // layerId stays in the reference
212
213}
214
216{
217 mUseLayerName = use;
218}
219
221{
222 return mUseLayerName;
223}
224
225void QgsLayerTreeLayer::layerNameChanged()
226{
227 Q_ASSERT( mRef );
228 emit nameChanged( this, mRef->name() );
229}
230
231void QgsLayerTreeLayer::setLabelExpression( const QString &expression )
232{
233 mLabelExpression = expression;
234}
235
237{
238 return mPatchShape;
239}
240
242{
243 mPatchShape = shape;
244}
245
QString dump() const override
Returns string with layer tree structure. For debug purposes only.
void resolveReferences(const QgsProject *project, bool looseMatching=false) override
Resolves reference to layer from stored layer ID (if it has not been resolved already).
void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context) override
Write layer tree to XML.
QString layerId() const
Returns the ID for the map layer associated with this node.
QString mLabelExpression
Expression to evaluate in the legend.
void setPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
QgsMapLayerRef mRef
Weak reference to the layer (or just it's ID if the reference is not resolved yet).
void layerWillBeUnloaded()
Emitted when a previously available layer got unloaded (from layer registry).
void setName(const QString &n) override
Sets the layer's name.
QString labelExpression() const
Returns the expression member of the LayerTreeNode.
LegendNodesSplitBehavior
Legend node column split behavior.
void setUseLayerName(bool use=true)
Uses the layer's name if use is true, or the name manually set if false.
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
void setLabelExpression(const QString &expression)
set the expression to evaluate
QgsLayerTreeLayer(QgsMapLayer *layer)
bool useLayerName() const
Returns whether the layer's name is used, or the name manually set.
void setPatchShape(const QgsLegendPatchShape &shape)
Sets the symbol patch shape to use when rendering the legend node symbol.
QString name() const override
Returns the layer's name.
void layerLoaded()
Emitted when a previously unavailable layer got loaded.
static QgsLayerTreeLayer * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read layer node from XML.
void setLegendSplitBehavior(LegendNodesSplitBehavior behavior)
Sets the column split behavior for the node.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QString mLayerName
Layer name - only used if layer does not exist or if mUseLayerName is false.
QgsLayerTreeLayer * clone() const override
Create a copy of the node. Returns new instance.
@ NodeLayer
Leaf node pointing to a layer.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
QgsLayerTreeNode(NodeType t, bool checked=true)
Constructor.
void writeCommonXml(QDomElement &element)
Write common XML elements.
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.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
Represents a patch shape for use in map legends.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
Base class for all map layer types.
Definition qgsmaplayer.h:83
QString name
Definition qgsmaplayer.h:87
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
void nameChanged()
Emitted when the name has been changed.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
QString absoluteToRelativeUri(const QString &providerKey, const QString &uri, const QgsReadWriteContext &context) const
Converts absolute path(s) to relative path(s) in the given provider-specific URI.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QString relativeToAbsoluteUri(const QString &providerKey, const QString &uri, const QgsReadWriteContext &context) const
Converts relative path(s) to absolute path(s) in the given provider-specific URI.
A container for the context for various read/write operations on objects.
static QString encodeSize(QSizeF size)
Encodes a QSizeF to a string.
static QSizeF decodeSize(const QString &string)
Decodes a QSizeF from a string.
QPointer< TYPE > layer
Weak pointer to map layer.