QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( layer )
27  , mVisible( Qt::Checked )
28 {
29  Q_ASSERT( QgsMapLayerRegistry::instance()->mapLayer( mLayerId ) == layer );
30 }
31 
32 QgsLayerTreeLayer::QgsLayerTreeLayer( QString layerId, QString name )
33  : QgsLayerTreeNode( NodeLayer )
34  , mLayerId( layerId )
35  , mLayerName( name )
36  , mLayer( 0 )
37  , mVisible( Qt::Checked )
38 {
39  attachToLayer();
40 }
41 
43  : QgsLayerTreeNode( other )
44  , mLayerId( other.mLayerId )
45  , mLayerName( other.mLayerName )
46  , mLayer( 0 )
47  , mVisible( other.mVisible )
48 {
49  attachToLayer();
50 }
51 
53 {
54  // layer is not necessarily already loaded
56  if ( l )
57  {
58  mLayer = l;
59  mLayerName = l->name();
60  // make sure we are notified if the layer is removed
61  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( registryLayersWillBeRemoved( QStringList ) ) );
62  }
63  else
64  {
65  if ( mLayerName.isEmpty() )
66  mLayerName = "(?)";
67  // wait for the layer to be eventually loaded
68  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
69  }
70 }
71 
72 
74 {
75  return mLayer ? mLayer->name() : mLayerName;
76 }
77 
78 void QgsLayerTreeLayer::setLayerName( const QString& n )
79 {
80  if ( mLayer )
81  mLayer->setLayerName( n );
82  else
83  mLayerName = n;
84 }
85 
86 void QgsLayerTreeLayer::setVisible( Qt::CheckState state )
87 {
88  if ( mVisible == state )
89  return;
90 
91  mVisible = state;
92  emit visibilityChanged( this, state );
93 }
94 
96 {
97  if ( element.tagName() != "layer-tree-layer" )
98  return 0;
99 
100  QString layerID = element.attribute( "id" );
101  QString layerName = element.attribute( "name" );
102  Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( "checked" ) );
103  bool isExpanded = ( element.attribute( "expanded", "1" ) == "1" );
104 
105  QgsLayerTreeLayer* nodeLayer = 0;
106 
108 
109  if ( layer )
110  nodeLayer = new QgsLayerTreeLayer( layer );
111  else
112  nodeLayer = new QgsLayerTreeLayer( layerID, layerName );
113 
114  nodeLayer->readCommonXML( element );
115 
116  nodeLayer->setVisible( checked );
117  nodeLayer->setExpanded( isExpanded );
118  return nodeLayer;
119 }
120 
121 void QgsLayerTreeLayer::writeXML( QDomElement& parentElement )
122 {
123  QDomDocument doc = parentElement.ownerDocument();
124  QDomElement elem = doc.createElement( "layer-tree-layer" );
125  elem.setAttribute( "id", mLayerId );
126  elem.setAttribute( "name", layerName() );
127  elem.setAttribute( "checked", QgsLayerTreeUtils::checkStateToXml( mVisible ) );
128  elem.setAttribute( "expanded", mExpanded ? "1" : "0" );
129 
130  writeCommonXML( elem );
131 
132  parentElement.appendChild( elem );
133 }
134 
135 QString QgsLayerTreeLayer::dump() const
136 {
137  return QString( "LAYER: %1 visible=%2 expanded=%3 id=%4\n" ).arg( layerName() ).arg( mVisible ).arg( mExpanded ).arg( layerId() );
138 }
139 
141 {
142  return new QgsLayerTreeLayer( *this );
143 }
144 
145 void QgsLayerTreeLayer::registryLayersAdded( QList<QgsMapLayer*> layers )
146 {
147  foreach ( QgsMapLayer* l, layers )
148  {
149  if ( l->id() == mLayerId )
150  {
151  mLayer = l;
152  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
153  emit layerLoaded();
154  break;
155  }
156  }
157 }
158 
159 void QgsLayerTreeLayer::registryLayersWillBeRemoved( const QStringList& layerIds )
160 {
161  if ( layerIds.contains( mLayerId ) )
162  {
163  emit layerWillBeUnloaded();
164 
165  // stop listening to removal signals and start hoping that the layer may be added again
166  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( registryLayersWillBeRemoved( QStringList ) ) );
167  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
168 
169  mLayer = 0;
170  }
171 }
Base class for all map layer types.
Definition: qgsmaplayer.h:48
static Qt::CheckState checkStateFromXml(QString txt)
Convert QString to Qt::CheckState.
void setLayerName(const QString &n)
static QString checkStateToXml(Qt::CheckState state)
Convert Qt::CheckState to QString.
QgsMapLayer * layer() const
bool mExpanded
whether the node should be shown in GUI as expanded
void readCommonXML(QDomElement &element)
void layerLoaded()
emitted when a previously unavailable layer got loaded
void registryLayersWillBeRemoved(const QStringList &layerIds)
QgsMapLayer * mLayer
const QString & name() const
Get the display name of the layer.
void visibilityChanged(QgsLayerTreeNode *node, Qt::CheckState state)
Emitted when check state of a node within the tree has been changed.
void registryLayersAdded(QList< QgsMapLayer * > layers)
This class is a base class for nodes in a layer tree.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
QString layerId() const
void setVisible(Qt::CheckState visible)
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
virtual QString dump() const
Return string with layer tree structure. For debug purposes only.
void setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
virtual void writeXML(QDomElement &parentElement)
Write layer tree to XML.
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)
void setLayerName(const QString &name)
Set the display name of the layer.
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
Qt::CheckState mVisible
virtual QgsLayerTreeNode * clone() const
Create a copy of the node. Returns new instance.
QString layerName() const
Layer tree node points to a map layer.
QgsLayerTreeLayer(QgsMapLayer *layer)