QGIS API Documentation  2.99.0-Master (75367e4)
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 
22 
24  : QgsLayerTreeNode( NodeLayer, true )
25  , mRef( layer )
26  , mLayerName( layer->name() )
27 {
28  attachToLayer();
29 }
30 
31 QgsLayerTreeLayer::QgsLayerTreeLayer( const QString& layerId, const QString& name )
32  : QgsLayerTreeNode( NodeLayer, true )
33  , mRef( layerId )
34  , mLayerName( name.isEmpty() ? QStringLiteral( "(?)" ) : name )
35 {
36 }
37 
39  : QgsLayerTreeNode( other )
40  , mRef( other.mRef )
41  , mLayerName( other.mLayerName )
42 {
43  attachToLayer();
44 }
45 
47 {
48  if ( mRef.layer )
49  return; // already assigned
50 
51  QgsMapLayer* layer = project->mapLayer( mRef.layerId );
52  if ( !layer )
53  return;
54 
55  mRef.layer = layer;
56  mRef.layerId = layer->id();
57  attachToLayer();
58  emit layerLoaded();
59 }
60 
62 {
63  if ( !mRef.layer )
64  return;
65 
66  connect( mRef.layer, &QgsMapLayer::nameChanged, this, &QgsLayerTreeLayer::layerNameChanged );
67  connect( mRef.layer, &QgsMapLayer::willBeDeleted, this, &QgsLayerTreeLayer::layerWillBeDeleted );
68 }
69 
70 
71 QString QgsLayerTreeLayer::name() const
72 {
73  return mRef.layer ? mRef.layer->name() : mLayerName;
74 }
75 
76 void QgsLayerTreeLayer::setName( const QString& n )
77 {
78  if ( mRef.layer )
79  {
80  if ( mRef.layer->name() == n )
81  return;
82  mRef.layer->setName( n );
83  // no need to emit signal: we will be notified from layer's nameChanged() signal
84  }
85  else
86  {
87  if ( mLayerName == n )
88  return;
89  mLayerName = n;
90  emit nameChanged( this, n );
91  }
92 }
93 
95 {
96  if ( element.tagName() != QLatin1String( "layer-tree-layer" ) )
97  return nullptr;
98 
99  QString layerID = element.attribute( QStringLiteral( "id" ) );
100  QString layerName = element.attribute( QStringLiteral( "name" ) );
101  Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) );
102  bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
103 
104  // needs to have the layer reference resolved later
105  QgsLayerTreeLayer* nodeLayer = new QgsLayerTreeLayer( layerID, layerName );
106 
107  nodeLayer->readCommonXml( element );
108 
109  nodeLayer->setItemVisibilityChecked( checked != Qt::Unchecked );
110  nodeLayer->setExpanded( isExpanded );
111  return nodeLayer;
112 }
113 
114 QgsLayerTreeLayer* QgsLayerTreeLayer::readXml( QDomElement& element, const QgsProject* project )
115 {
116  QgsLayerTreeLayer* node = readXml( element );
117  if ( node )
118  node->resolveReferences( project );
119  return node;
120 }
121 
122 void QgsLayerTreeLayer::writeXml( QDomElement& parentElement )
123 {
124  QDomDocument doc = parentElement.ownerDocument();
125  QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-layer" ) );
126  elem.setAttribute( QStringLiteral( "id" ), layerId() );
127  elem.setAttribute( QStringLiteral( "name" ), name() );
128  elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
129  elem.setAttribute( QStringLiteral( "expanded" ), mExpanded ? "1" : "0" );
130 
131  writeCommonXml( elem );
132 
133  parentElement.appendChild( elem );
134 }
135 
136 QString QgsLayerTreeLayer::dump() const
137 {
138  return QStringLiteral( "LAYER: %1 checked=%2 expanded=%3 id=%4\n" ).arg( name() ).arg( mChecked ).arg( mExpanded ).arg( layerId() );
139 }
140 
142 {
143  return new QgsLayerTreeLayer( *this );
144 }
145 
146 void QgsLayerTreeLayer::layerWillBeDeleted()
147 {
148  Q_ASSERT( mRef.layer );
149 
150  mLayerName = mRef.layer->name();
151  // in theory we do not even need to do this - the weak ref should clear itself
152  mRef.layer.clear();
153  // layerId stays in the reference
154 
155  emit layerWillBeUnloaded();
156 }
157 
158 
159 void QgsLayerTreeLayer::layerNameChanged()
160 {
161  Q_ASSERT( mRef.layer );
162  emit nameChanged( this, mRef.layer->name() );
163 }
Base class for all map layer types.
Definition: qgsmaplayer.h:52
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
bool mExpanded
whether the node should be shown in GUI as expanded
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
virtual void resolveReferences(const QgsProject *project) override
Resolves reference to layer from stored layer ID (if it has not been resolved already) ...
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
void layerLoaded()
emitted when a previously unavailable layer got loaded
QString layerId() const
QgsMapLayerRef mRef
Weak reference to the layer (or just it's ID if the reference is not resolved yet) ...
QString mLayerName
Layer name - only used if layer does not exist.
QString layerId
QString name() const override
Get layer's name.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QPointer< TYPE > layer
void nameChanged()
Emitted when the name has been changed.
void writeCommonXml(QDomElement &element)
Write common XML elements.
static QgsLayerTreeLayer * readXml(QDomElement &element)
Read layer node from XML.
This class is a base class for nodes in a layer tree.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
Reads and writes project states.
Definition: qgsproject.h:74
void setName(const QString &n) override
Set layer&#39;s name.
void setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
QgsMapLayer * layer() const
Leaf node pointing to a layer.
void readCommonXml(QDomElement &element)
Read common XML elements.
void layerWillBeUnloaded()
emitted when a previously available layer got unloaded (from layer registry)
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
virtual QString dump() const override
Return string with layer tree structure. For debug purposes only.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
virtual void writeXml(QDomElement &parentElement) override
Write layer tree to XML.
virtual QgsLayerTreeLayer * clone() const override
Create a copy of the node. Returns new instance.
Layer tree node points to a map layer.
QgsLayerTreeLayer(QgsMapLayer *layer)