QGIS API Documentation  2.99.0-Master (c558d51)
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 "qgsmaplayerregistry.h"
21 
22 
24  : QgsLayerTreeNode( NodeLayer )
25  , mLayerId( layer->id() )
26  , mLayer( nullptr )
27  , mVisible( Qt::Checked )
28 {
29  Q_ASSERT( QgsMapLayerRegistry::instance()->mapLayer( mLayerId ) == layer );
30  attachToLayer();
31 }
32 
33 QgsLayerTreeLayer::QgsLayerTreeLayer( const QString& layerId, const QString& name )
35  , mLayerId( layerId )
36  , mLayerName( name )
37  , mLayer( nullptr )
38  , mVisible( Qt::Checked )
39 {
40  attachToLayer();
41 }
42 
44  : QgsLayerTreeNode( other )
45  , mLayerId( other.mLayerId )
46  , mLayerName( other.mLayerName )
47  , mLayer( nullptr )
48  , mVisible( other.mVisible )
49 {
50  attachToLayer();
51 }
52 
54 {
55  // layer is not necessarily already loaded
57  if ( l )
58  {
59  mLayer = l;
60  mLayerName = l->name();
61  connect( l, SIGNAL( nameChanged() ), this, SLOT( layerNameChanged() ) );
62  // make sure we are notified if the layer is removed
63  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( registryLayersWillBeRemoved( QStringList ) ) );
64  }
65  else
66  {
67  if ( mLayerName.isEmpty() )
68  mLayerName = QStringLiteral( "(?)" );
69  // wait for the layer to be eventually loaded
70  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
71  }
72 }
73 
74 QString QgsLayerTreeLayer::name() const
75 {
76  return mLayer ? mLayer->name() : mLayerName;
77 }
78 
79 void QgsLayerTreeLayer::setName( const QString& n )
80 {
81  if ( mLayer )
82  {
83  if ( mLayer->name() == n )
84  return;
85  mLayer->setName( n );
86  // no need to emit signal: we will be notified from layer's nameChanged() signal
87  }
88  else
89  {
90  if ( mLayerName == n )
91  return;
92  mLayerName = n;
93  emit nameChanged( this, n );
94  }
95 }
96 
97 void QgsLayerTreeLayer::setVisible( Qt::CheckState state )
98 {
99  if ( mVisible == state )
100  return;
101 
102  mVisible = state;
103  emit visibilityChanged( this, state );
104 }
105 
107 {
108  if ( element.tagName() != QLatin1String( "layer-tree-layer" ) )
109  return nullptr;
110 
111  QString layerID = element.attribute( QStringLiteral( "id" ) );
112  QString layerName = element.attribute( QStringLiteral( "name" ) );
113  Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) );
114  bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
115 
116  QgsLayerTreeLayer* nodeLayer = nullptr;
117 
119 
120  if ( layer )
121  nodeLayer = new QgsLayerTreeLayer( layer );
122  else
123  nodeLayer = new QgsLayerTreeLayer( layerID, layerName );
124 
125  nodeLayer->readCommonXml( element );
126 
127  nodeLayer->setVisible( checked );
128  nodeLayer->setExpanded( isExpanded );
129  return nodeLayer;
130 }
131 
132 void QgsLayerTreeLayer::writeXml( QDomElement& parentElement )
133 {
134  QDomDocument doc = parentElement.ownerDocument();
135  QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-layer" ) );
136  elem.setAttribute( QStringLiteral( "id" ), mLayerId );
137  elem.setAttribute( QStringLiteral( "name" ), name() );
138  elem.setAttribute( QStringLiteral( "checked" ), QgsLayerTreeUtils::checkStateToXml( mVisible ) );
139  elem.setAttribute( QStringLiteral( "expanded" ), mExpanded ? "1" : "0" );
140 
141  writeCommonXml( elem );
142 
143  parentElement.appendChild( elem );
144 }
145 
146 QString QgsLayerTreeLayer::dump() const
147 {
148  return QStringLiteral( "LAYER: %1 visible=%2 expanded=%3 id=%4\n" ).arg( name() ).arg( mVisible ).arg( mExpanded ).arg( layerId() );
149 }
150 
152 {
153  return new QgsLayerTreeLayer( *this );
154 }
155 
156 void QgsLayerTreeLayer::registryLayersAdded( const QList<QgsMapLayer*>& layers )
157 {
158  Q_FOREACH ( QgsMapLayer* l, layers )
159  {
160  if ( l->id() == mLayerId )
161  {
162  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
163  attachToLayer();
164  emit layerLoaded();
165  break;
166  }
167  }
168 }
169 
170 void QgsLayerTreeLayer::registryLayersWillBeRemoved( const QStringList& layerIds )
171 {
172  if ( layerIds.contains( mLayerId ) )
173  {
174  emit layerWillBeUnloaded();
175 
176  // stop listening to removal signals and start hoping that the layer may be added again
177  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( registryLayersWillBeRemoved( QStringList ) ) );
178  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
179 
180  mLayer = nullptr;
181  }
182 }
183 
185 {
186  Q_ASSERT( mLayer );
187  emit nameChanged( this, mLayer->name() );
188 }
Base class for all map layer types.
Definition: qgsmaplayer.h:49
static QString checkStateToXml(Qt::CheckState state)
Convert Qt::CheckState to QString.
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsMapLayer * layer() const
bool mExpanded
whether the node should be shown in GUI as expanded
void layerLoaded()
emitted when a previously unavailable layer got loaded
void registryLayersWillBeRemoved(const QStringList &layerIds)
void registryLayersAdded(const QList< QgsMapLayer * > &layers)
QgsMapLayer * mLayer
void visibilityChanged(QgsLayerTreeNode *node, Qt::CheckState state)
Emitted when check state of a node within the tree has been changed.
void layerNameChanged()
Emits a nameChanged() signal if layer&#39;s name has changed.
QString name() const override
Get layer&#39;s name.
void writeCommonXml(QDomElement &element)
static QgsLayerTreeLayer * readXml(QDomElement &element)
This class is a base class for nodes in a layer tree.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsMapLayerRegistry.
QString layerId() const
void setVisible(Qt::CheckState visible)
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
void setName(const QString &name)
Set the display name of the layer.
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.
Leaf node pointing to a layer.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void readCommonXml(QDomElement &element)
void layerWillBeUnloaded()
emitted when a previously available layer got unloaded (from layer registry)
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
Qt::CheckState mVisible
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)