QGIS API Documentation  3.2.0-Bonn (bc43194)
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 #define SIP_NO_FILE
20 
21 #include <QPointer>
22 
23 #include "qgsmaplayer.h"
24 #include "qgsdataprovider.h"
25 #include "qgsproject.h"
26 
32 template<typename TYPE>
33 struct _LayerRef
34 {
35 
41  _LayerRef( TYPE *l = nullptr )
42  : layer( l )
43  , layerId( l ? l->id() : QString() )
44  , source( l ? l->publicSource() : QString() )
45  , name( l ? l->name() : QString() )
46  , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
47  {}
48 
53  _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
54  : layer()
55  , layerId( id )
56  , source( source )
57  , name( name )
58  , provider( provider )
59  {}
60 
64  void setLayer( TYPE *l )
65  {
66  layer = l;
67  layerId = l ? l->id() : QString();
68  source = l ? l->publicSource() : QString();
69  name = l ? l->name() : QString();
70  provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
71  }
72 
77  operator bool() const
78  {
79  return static_cast< bool >( layer.data() );
80  }
81 
85  TYPE *operator->() const
86  {
87  return layer.data();
88  }
89 
94  TYPE *get() const
95  {
96  return layer.data();
97  }
98 
100  QPointer<TYPE> layer;
101 
103  QString layerId;
104 
106  QString source;
108  QString name;
110  QString provider;
111 
117  bool layerMatchesSource( QgsMapLayer *layer ) const
118  {
119  if ( layer->publicSource() != source ||
120  layer->name() != name )
121  return false;
122 
123  if ( layer->dataProvider()->name() != provider )
124  return false;
125 
126  return true;
127  }
128 
136  TYPE *resolve( const QgsProject *project )
137  {
138  if ( project && !layerId.isEmpty() )
139  {
140  if ( TYPE *l = qobject_cast<TYPE *>( project->mapLayer( layerId ) ) )
141  {
142  setLayer( l );
143  return l;
144  }
145  }
146  return nullptr;
147  }
148 
166  TYPE *resolveWeakly( const QgsProject *project )
167  {
168  // first try matching by layer ID
169  if ( resolve( project ) )
170  return layer;
171 
172  if ( project && !name.isEmpty() )
173  {
174  const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
175  for ( QgsMapLayer *l : layers )
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 
192 
193 #endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition: qgsmaplayer.h:61
Internal structure to keep weak pointer to QgsMapLayer or layerId if the layer is not available yet...
_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...
virtual QgsDataProvider * dataProvider()
Returns the layer&#39;s data provider.
TYPE * operator->() const
Forwards the to map layer.
TYPE * resolveWeakly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match...
virtual QString name() const =0
Returns 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.
QPointer< TYPE > layer
Weak pointer to map layer.
QString name
Weak reference to layer name.
Reads and writes project states.
Definition: qgsproject.h:85
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.
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
_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...
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QString name
Definition: qgsmaplayer.h:65
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.