QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 
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
70  }
71 }
72 
73 
75 {
76  return mLayer ? mLayer->name() : mLayerName;
77 }
78 
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 
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  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 = 0;
171  }
172 }
virtual QgsLayerTreeNode * clone() const override
Create a copy of the node. Returns new instance.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
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.
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)
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)
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.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:99
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 setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
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 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
Layer tree node points to a map layer.
QgsLayerTreeLayer(QgsMapLayer *layer)