QGIS API Documentation  3.17.0-Master (8af46bc54f)
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 
40  enum MatchType
41  {
42  Name = 1 << 2,
43  Provider = 1 << 3,
44  Source = 1 << 4,
46  };
47 
48 
54  _LayerRef( TYPE *l = nullptr )
55  : layer( l )
56  , layerId( l ? l->id() : QString() )
57  , source( l ? l->publicSource() : QString() )
58  , name( l ? l->name() : QString() )
59  , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
60  {}
61 
66  _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
67  : layer()
68  , layerId( id )
69  , source( source )
70  , name( name )
71  , provider( provider )
72  {}
73 
77  void setLayer( TYPE *l )
78  {
79  layer = l;
80  layerId = l ? l->id() : QString();
81  source = l ? l->publicSource() : QString();
82  name = l ? l->name() : QString();
83  provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
84  }
85 
90  operator bool() const
91  {
92  return static_cast< bool >( layer.data() );
93  }
94 
98  TYPE *operator->() const
99  {
100  return layer.data();
101  }
102 
107  TYPE *get() const
108  {
109  return layer.data();
110  }
111 
113  QPointer<TYPE> layer;
114 
116  QString layerId;
117 
119  QString source;
121  QString name;
123  QString provider;
124 
130  bool layerMatchesSource( QgsMapLayer *layer ) const
131  {
132  if ( layer->publicSource() != source ||
133  layer->name() != name )
134  return false;
135 
136  if ( layer->providerType() != provider )
137  return false;
138 
139  return true;
140  }
141 
149  TYPE *resolve( const QgsProject *project )
150  {
151  if ( project && !layerId.isEmpty() )
152  {
153  if ( TYPE *l = qobject_cast<TYPE *>( project->mapLayer( layerId ) ) )
154  {
155  setLayer( l );
156  return l;
157  }
158  }
159  return nullptr;
160  }
161 
162  bool layerMatchesWeakly( QgsMapLayer *layer, MatchType matchType = MatchType::All )
163  {
164  // First match the name
165  if ( matchType & MatchType::Name && ( layer->name().isEmpty() || layer->name() != name ) )
166  {
167  return false;
168  }
169  else
170  {
171  // We have found a match by name, now check the other
172  // criteria
173  if ( matchType & MatchType::Provider && layer->providerType() != provider )
174  {
175  return false;
176  }
177  if ( matchType & MatchType::Source && layer->publicSource() != source )
178  {
179  return false;
180  }
181  // All tests passed
182  return true;
183  }
184  }
185 
210  TYPE *resolveWeakly( const QgsProject *project, MatchType matchType = MatchType::All )
211  {
212  // first try matching by layer ID
213  if ( resolve( project ) )
214  return layer;
215 
216  if ( project )
217  {
218  QList<QgsMapLayer *> layers;
219  // If matching by name ...
220  if ( matchType & MatchType::Name )
221  {
222  if ( name.isEmpty() )
223  {
224  return nullptr;
225  }
226  layers = project->mapLayersByName( name );
227  }
228  else // ... we need all layers
229  {
230  layers = project->mapLayers().values();
231  }
232  for ( QgsMapLayer *l : qgis::as_const( layers ) )
233  {
234  if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
235  {
236  if ( layerMatchesWeakly( tl, matchType ) )
237  {
238  setLayer( tl );
239  return tl;
240  }
241  }
242  }
243  }
244  return nullptr;
245  }
246 
269  TYPE *resolveByIdOrNameOnly( const QgsProject *project )
270  {
271  // first try by matching by layer ID, or weakly by source, name and provider
272  if ( resolveWeakly( project ) )
273  return layer;
274 
275  // fallback to checking by name only
276  if ( project && !name.isEmpty() )
277  {
278  const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
279  for ( QgsMapLayer *l : layers )
280  {
281  if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
282  {
283  setLayer( tl );
284  return tl;
285  }
286  }
287  }
288  return nullptr;
289  }
290 
291 
292 };
293 
295 
296 #endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition: qgsmaplayer.h:83
Internal structure to keep weak pointer to QgsMapLayer or layerId if the layer is not available yet...
bool layerMatchesWeakly(QgsMapLayer *layer, MatchType matchType=MatchType::All)
_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 * resolveWeakly(const QgsProject *project, MatchType matchType=MatchType::All)
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.
_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.
Match layer source.
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:94
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example...
Match layer provider name.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
MatchType
Flag for match type in weak resolution.
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.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
_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:87
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
Match layer name.