QGIS API Documentation  2.8.2-Wien
 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( 0 )
27  , mVisible( Qt::Checked )
28 {
29  Q_ASSERT( QgsMapLayerRegistry::instance()->mapLayer( mLayerId ) == layer );
30  attachToLayer();
31 }
32 
33 QgsLayerTreeLayer::QgsLayerTreeLayer( QString layerId, QString name )
34  : QgsLayerTreeNode( NodeLayer )
35  , mLayerId( layerId )
36  , mLayerName( name )
37  , mLayer( 0 )
38  , mVisible( Qt::Checked )
39 {
40  attachToLayer();
41 }
42 
44  : QgsLayerTreeNode( other )
45  , mLayerId( other.mLayerId )
46  , mLayerName( other.mLayerName )
47  , mLayer( 0 )
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
69  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
70  }
71 }
72 
73 
75 {
76  return mLayer ? mLayer->name() : mLayerName;
77 }
78 
79 void QgsLayerTreeLayer::setLayerName( const QString& n )
80 {
81  if ( mLayer )
82  mLayer->setLayerName( 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 0;
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 = 0;
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 
122 void QgsLayerTreeLayer::writeXML( QDomElement& parentElement )
123 {
124  QDomDocument doc = parentElement.ownerDocument();
125  QDomElement elem = doc.createElement( "layer-tree-layer" );
126  elem.setAttribute( "id", mLayerId );
127  elem.setAttribute( "name", layerName() );
128  elem.setAttribute( "checked", QgsLayerTreeUtils::checkStateToXml( mVisible ) );
129  elem.setAttribute( "expanded", mExpanded ? "1" : "0" );
130 
131  writeCommonXML( elem );
132 
133  parentElement.appendChild( elem );
134 }
135 
136 QString QgsLayerTreeLayer::dump() const
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 
146 void QgsLayerTreeLayer::registryLayersAdded( QList<QgsMapLayer*> layers )
147 {
148  foreach ( QgsMapLayer* l, layers )
149  {
150  if ( l->id() == mLayerId )
151  {
152  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
153  attachToLayer();
154  emit layerLoaded();
155  break;
156  }
157  }
158 }
159 
160 void QgsLayerTreeLayer::registryLayersWillBeRemoved( const QStringList& layerIds )
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 ) ) );
168  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( registryLayersAdded( QList<QgsMapLayer*> ) ) );
169 
170  mLayer = 0;
171  }
172 }