QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsmaplayerref.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerref.h
3  --------------------------------------
4  Date : January 2017
5  Copyright : (C) 2017 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 #ifndef QGSMAPLAYERREF_H
17 #define QGSMAPLAYERREF_H
18 
19 #include <QPointer>
20 
21 #include "qgsmaplayer.h"
22 #include "qgsdataprovider.h"
23 #include "qgsmaplayerregistry.h"
24 #include "qgsvectorlayer.h"
25 #include "qgsvectordataprovider.h"
26 #include "qgsrasterlayer.h"
27 #include "qgsrasterdataprovider.h"
28 
33 template<typename TYPE>
34 struct _LayerRef
35 {
36 
42  _LayerRef( TYPE *l = nullptr )
43  : layer( l )
44  , layerId( l ? l->id() : QString() )
45  , source( l ? l->publicSource() : QString() )
46  , name( l ? l->name() : QString() )
47  , provider( layerProviderName( l ) )
48  {}
49 
54  _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
55  : layer()
56  , layerId( id )
57  , source( source )
58  , name( name )
59  , provider( provider )
60  {}
61 
65  void setLayer( TYPE *l )
66  {
67  layer = l;
68  layerId = l ? l->id() : QString();
69  source = l ? l->publicSource() : QString();
70  name = l ? l->name() : QString();
71  provider = layerProviderName( l );
72  }
73 
78  operator bool() const
79  {
80  return static_cast< bool >( layer.data() );
81  }
82 
86  TYPE *operator->() const
87  {
88  return layer.data();
89  }
90 
95  TYPE *get() const
96  {
97  return layer.data();
98  }
99 
102 
105 
112 
118  bool layerMatchesSource( QgsMapLayer *layer ) const
119  {
120  if ( layer->publicSource() != source ||
121  layer->name() != name )
122  return false;
123 
124  if ( layerProviderName( layer ) != provider )
125  return false;
126 
127  return true;
128  }
129 
137  TYPE *resolve()
138  {
139  if ( !layerId.isEmpty() )
140  {
141  if ( TYPE *l = qobject_cast<TYPE *>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) ) )
142  {
143  setLayer( l );
144  return l;
145  }
146  }
147  return nullptr;
148  }
149 
168  {
169  // first try matching by layer ID
170  if ( resolve() )
171  return layer;
172 
173  if ( !name.isEmpty() )
174  {
175  Q_FOREACH ( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayersByName( name ) )
176  {
177  if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
178  {
179  if ( layerMatchesSource( tl ) )
180  {
181  setLayer( tl );
182  return tl;
183  }
184  }
185  }
186  }
187  return nullptr;
188  }
189 
190 private:
191 
192  static QString layerProviderName( const QgsMapLayer *layer )
193  {
194  if ( !layer )
195  return QString();
196 
197  switch ( layer->type() )
198  {
200  {
201  const QgsVectorLayer *vl = qobject_cast< const QgsVectorLayer *>( layer );
202  return vl->dataProvider()->name();
203  }
204 
206  {
207  const QgsRasterLayer *rl = qobject_cast< const QgsRasterLayer *>( layer );
208  return rl->dataProvider()->name();
209  }
211  return QString();
212  }
213  return QString();
214 
215  }
216 };
217 
219 
220 #endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition: qgsmaplayer.h:49
Internal structure to keep weak pointer to QgsMapLayer or layerId if the layer is not available yet...
T * data() const
_LayerRef< QgsMapLayer > QgsMapLayerRef
bool layerMatchesSource(QgsMapLayer *layer) const
Returns true if a layer matches the weak references to layer public source, layer name and data provi...
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
TYPE * operator->() const
Forwards the to map layer.
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
virtual QString name() const =0
Return a provider name.
_LayerRef(TYPE *l=nullptr)
Constructor for a layer reference from an existing map layer.
QString provider
Weak reference to layer provider.
QString layerId
Original layer ID.
TYPE * resolveWeakly()
Resolves the map layer by attempting to find a matching layer in the map layer registry using a weak ...
QPointer< TYPE > layer
Weak pointer to map layer.
bool isEmpty() const
QString name
Weak reference to layer name.
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example...
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString source
Weak reference to layer public source.
TYPE * resolve()
Resolves the map layer by attempting to find a layer with matching ID within the map layer registry...
_LayerRef(const QString &id, const QString &name=QString(), const QString &source=QString(), const QString &provider=QString())
Constructor for a weak layer reference, using a combination of layer ID, name, public source and prov...
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
QgsRasterDataProvider * dataProvider()
Returns the data provider.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
Represents a vector layer which manages a vector based data sets.