QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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 
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  // make sure we are notified if the layer is removed
62  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( registryLayersWillBeRemoved( QStringList ) ) );
63  }
64  else
65  {
66  if ( mLayerName.isEmpty() )
67  mLayerName = "(?)";
68  // wait for the layer to be eventually loaded
70  }
71 }
72 
73 
75 {
76  return mLayer ? mLayer->name() : mLayerName;
77 }
78 
80 {
81  if ( mLayer )
82  mLayer->setName( n );
83  else
84  mLayerName = n;
85 }
86 
87 void QgsLayerTreeLayer::setVisible( Qt::CheckState state )
88 {
89  if ( mVisible == state )
90  return;
91 
92  mVisible = state;
93  emit visibilityChanged( this, state );
94 }
95 
97 {
98  if ( element.tagName() != "layer-tree-layer" )
99  return nullptr;
100 
101  QString layerID = element.attribute( "id" );
102  QString layerName = element.attribute( "name" );
103  Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( "checked" ) );
104  bool isExpanded = ( element.attribute( "expanded", "1" ) == "1" );
105 
106  QgsLayerTreeLayer* nodeLayer = nullptr;
107 
109 
110  if ( layer )
111  nodeLayer = new QgsLayerTreeLayer( layer );
112  else
113  nodeLayer = new QgsLayerTreeLayer( layerID, layerName );
114 
115  nodeLayer->readCommonXML( element );
116 
117  nodeLayer->setVisible( checked );
118  nodeLayer->setExpanded( isExpanded );
119  return nodeLayer;
120 }
121 
123 {
124  QDomDocument doc = parentElement.ownerDocument();
125  QDomElement elem = doc.createElement( "layer-tree-layer" );
126  elem.setAttribute( "id", mLayerId );
127  elem.setAttribute( "name", layerName() );
129  elem.setAttribute( "expanded", mExpanded ? "1" : "0" );
130 
131  writeCommonXML( elem );
132 
133  parentElement.appendChild( elem );
134 }
135 
137 {
138  return QString( "LAYER: %1 visible=%2 expanded=%3 id=%4\n" ).arg( layerName() ).arg( mVisible ).arg( mExpanded ).arg( layerId() );
139 }
140 
142 {
143  return new QgsLayerTreeLayer( *this );
144 }
145 
147 {
148  Q_FOREACH ( QgsMapLayer* l, layers )
149  {
150  if ( l->id() == mLayerId )
151  {
153  attachToLayer();
154  emit layerLoaded();
155  break;
156  }
157  }
158 }
159 
161 {
162  if ( layerIds.contains( mLayerId ) )
163  {
164  emit layerWillBeUnloaded();
165 
166  // stop listening to removal signals and start hoping that the layer may be added again
167  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( registryLayersWillBeRemoved( QStringList ) ) );
169 
170  mLayer = nullptr;
171  }
172 }
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setLayerName(const QString &n)
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.
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
QgsMapLayer * layer() const
bool mExpanded
whether the node should be shown in GUI as expanded
bool contains(const QString &str, Qt::CaseSensitivity cs) const
void readCommonXML(QDomElement &element)
void layerLoaded()
emitted when a previously unavailable layer got loaded
void registryLayersWillBeRemoved(const QStringList &layerIds)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void registryLayersAdded(const QList< QgsMapLayer * > &layers)
QgsMapLayer * mLayer
const char * name() const
void visibilityChanged(QgsLayerTreeNode *node, Qt::CheckState state)
Emitted when check state of a node within the tree has been changed.
QDomDocument ownerDocument() const
void setAttribute(const QString &name, const QString &value)
virtual void writeXML(QDomElement &parentElement) override
Write layer tree to XML.
bool isEmpty() const
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
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
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 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 writeCommonXML(QDomElement &element)
void layerWillBeUnloaded()
emitted when a previously available layer got unloaded (from layer registry)
static QgsLayerTreeLayer * readXML(QDomElement &element)
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.
QString tagName() const
QDomElement createElement(const QString &tagName)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString layerName() const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
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)