QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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->providerType() != 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 
167  TYPE *resolveWeakly( const QgsProject *project )
168  {
169  // first try matching by layer ID
170  if ( resolve( project ) )
171  return layer;
172 
173  if ( project && !name.isEmpty() )
174  {
175  const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
176  for ( QgsMapLayer *l : layers )
177  {
178  if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
179  {
180  if ( layerMatchesSource( tl ) )
181  {
182  setLayer( tl );
183  return tl;
184  }
185  }
186  }
187  }
188  return nullptr;
189  }
190 
213  TYPE *resolveByIdOrNameOnly( const QgsProject *project )
214  {
215  // first try by matching by layer ID, or weakly by source, name and provider
216  if ( resolveWeakly( project ) )
217  return layer;
218 
219  // fallback to checking by name only
220  if ( project && !name.isEmpty() )
221  {
222  const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
223  for ( QgsMapLayer *l : layers )
224  {
225  if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
226  {
227  setLayer( tl );
228  return tl;
229  }
230  }
231  }
232  return nullptr;
233  }
234 
235 
236 };
237 
239 
240 #endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition: qgsmaplayer.h:79
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...
TYPE * resolveByIdOrNameOnly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match...
TYPE * operator->() const
Forwards the to map layer.
QString providerType() const
Returns the provider type (provider key) for this layer.
TYPE * resolveWeakly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match...
_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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:89
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...
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QString name
Definition: qgsmaplayer.h:83
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.