QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
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
_LayerRef::layerMatchesWeakly
bool layerMatchesWeakly(QgsMapLayer *layer, MatchType matchType=MatchType::All)
Definition: qgsmaplayerref.h:162
_LayerRef::resolveWeakly
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.
Definition: qgsmaplayerref.h:210
_LayerRef::All
@ All
Match layer source.
Definition: qgsmaplayerref.h:45
QgsProject::mapLayersByName
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
Definition: qgsproject.cpp:3129
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3347
_LayerRef::layerMatchesSource
bool layerMatchesSource(QgsMapLayer *layer) const
Returns true if a layer matches the weak references to layer public source, layer name and data provi...
Definition: qgsmaplayerref.h:130
_LayerRef::_LayerRef
_LayerRef(TYPE *l=nullptr)
Constructor for a layer reference from an existing map layer.
Definition: qgsmaplayerref.h:54
_LayerRef::Provider
@ Provider
Match layer name.
Definition: qgsmaplayerref.h:43
QgsMapLayerRef
_LayerRef< QgsMapLayer > QgsMapLayerRef
Definition: qgsmaplayerref.h:294
_LayerRef::resolveByIdOrNameOnly
TYPE * resolveByIdOrNameOnly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
Definition: qgsmaplayerref.h:269
_LayerRef::Source
@ Source
Match layer provider name.
Definition: qgsmaplayerref.h:44
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3124
_LayerRef::layer
QPointer< TYPE > layer
Weak pointer to map layer.
Definition: qgsmaplayerref.h:113
_LayerRef::operator->
TYPE * operator->() const
Forwards the to map layer.
Definition: qgsmaplayerref.h:98
QgsProject
Definition: qgsproject.h:92
_LayerRef::layerId
QString layerId
Original layer ID.
Definition: qgsmaplayerref.h:116
_LayerRef::setLayer
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
Definition: qgsmaplayerref.h:77
_LayerRef::provider
QString provider
Weak reference to layer provider.
Definition: qgsmaplayerref.h:123
qgsmaplayer.h
_LayerRef::_LayerRef
_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...
Definition: qgsmaplayerref.h:66
_LayerRef::name
QString name
Weak reference to layer name.
Definition: qgsmaplayerref.h:121
_LayerRef< QgsVectorLayer >::MatchType
MatchType
Flag for match type in weak resolution.
Definition: qgsmaplayerref.h:40
_LayerRef::Name
@ Name
Definition: qgsmaplayerref.h:42
_LayerRef::source
QString source
Weak reference to layer public source.
Definition: qgsmaplayerref.h:119
QgsMapLayer
Definition: qgsmaplayer.h:81
qgsdataprovider.h
_LayerRef
Internal structure to keep weak pointer to QgsMapLayer or layerId if the layer is not available yet.
Definition: qgsmaplayerref.h:33
qgsproject.h
_LayerRef::resolve
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
Definition: qgsmaplayerref.h:149
_LayerRef::get
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
Definition: qgsmaplayerref.h:107