QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include <QDomElement>
23
24#include "qgsmaplayer.h"
25#include "qgsdataprovider.h"
26#include "qgsproject.h"
27#include <utility>
28
34template<typename TYPE>
36{
37
43 {
44 Name = 1 << 2,
45 Provider = 1 << 3,
46 Source = 1 << 4,
48 };
49
50
56 _LayerRef( TYPE *l = nullptr )
57 : layer( l )
58 , layerId( l ? l->id() : QString() )
59 , source( l ? l->publicSource() : QString() )
60 , name( l ? l->name() : QString() )
61 , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
62 {}
63
68 _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
69 : layer()
70 , layerId( id )
71 , source( source )
72 , name( name )
74 {}
75
79 void setLayer( TYPE *l )
80 {
81 layer = l;
82 layerId = l ? l->id() : QString();
83 source = l ? l->publicSource() : QString();
84 name = l ? l->name() : QString();
85 provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
86 }
87
91 bool operator==( const _LayerRef &other ) = delete;
92
97 explicit operator bool() const
98 {
99 return static_cast< bool >( layer.data() );
100 }
101
105 TYPE *operator->() const
106 {
107 return layer.data();
108 }
109
114 TYPE *get() const
115 {
116 return layer.data();
117 }
118
120 QPointer<TYPE> layer;
121
123 QString layerId;
124
126 QString source;
128 QString name;
130 QString provider;
131
138 {
139 if ( layer->publicSource() != source ||
140 layer->name() != name )
141 return false;
142
143 if ( layer->providerType() != provider )
144 return false;
145
146 return true;
147 }
148
156 TYPE *resolve( const QgsProject *project )
157 {
158 if ( project && !layerId.isEmpty() )
159 {
160 if ( TYPE *l = qobject_cast<TYPE *>( project->mapLayer( layerId ) ) )
161 {
162 setLayer( l );
163 return l;
164 }
165 }
166 return nullptr;
167 }
168
170 {
171 // First match the name
172 if ( matchType & MatchType::Name && ( layer->name().isEmpty() || layer->name() != name ) )
173 {
174 return false;
175 }
176 else
177 {
178 // We have found a match by name, now check the other
179 // criteria
180 if ( matchType & MatchType::Provider && layer->providerType() != provider )
181 {
182 return false;
183 }
184 if ( matchType & MatchType::Source && layer->publicSource() != source )
185 {
186 return false;
187 }
188 // All tests passed
189 return true;
190 }
191 }
192
217 TYPE *resolveWeakly( const QgsProject *project, MatchType matchType = MatchType::All )
218 {
219 // first try matching by layer ID
220 if ( resolve( project ) )
221 return layer;
222
223 if ( project )
224 {
225 QList<QgsMapLayer *> layers;
226 // If matching by name ...
227 if ( matchType & MatchType::Name )
228 {
229 if ( name.isEmpty() )
230 {
231 return nullptr;
232 }
233 layers = project->mapLayersByName( name );
234 }
235 else // ... we need all layers
236 {
237 layers = project->mapLayers().values();
238 }
239 for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
240 {
241 if ( TYPE *tl = qobject_cast< TYPE *>( *it ) )
242 {
243 if ( layerMatchesWeakly( tl, matchType ) )
244 {
245 setLayer( tl );
246 return tl;
247 }
248 }
249 }
250 }
251 return nullptr;
252 }
253
276 TYPE *resolveByIdOrNameOnly( const QgsProject *project )
277 {
278 // first try by matching by layer ID, or weakly by source, name and provider
279 if ( resolveWeakly( project ) )
280 return layer;
281
282 // fallback to checking by name only
283 if ( project && !name.isEmpty() )
284 {
285 const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
286 for ( QgsMapLayer *l : layers )
287 {
288 if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
289 {
290 setLayer( tl );
291 return tl;
292 }
293 }
294 }
295 return nullptr;
296 }
297
304 bool readXml( const QDomElement &element, const QgsReadWriteContext &context )
305 {
306 Q_UNUSED( context )
307
308 layerId = element.attribute( QStringLiteral( "id" ) );
309 name = element.attribute( QStringLiteral( "name" ) );
310 source = element.attribute( QStringLiteral( "source" ) );
311 provider = element.attribute( QStringLiteral( "provider" ) );
312 return true;
313 }
314
321 void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
322 {
323 Q_UNUSED( context )
324
325 element.setAttribute( QStringLiteral( "id" ), layerId );
326 element.setAttribute( QStringLiteral( "name" ), name );
327 element.setAttribute( QStringLiteral( "source" ), source );
328 element.setAttribute( QStringLiteral( "provider" ), provider );
329 }
330
331};
332
334
335#endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition qgsmaplayer.h:76
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Q_INVOKABLE 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.
The class is used as a container of context for various read/write operations on other objects.
_LayerRef< QgsMapLayer > QgsMapLayerRef
Internal structure to keep weak pointer to QgsMapLayer or layerId if the layer is not available yet.
TYPE * operator->() const
Forwards the to map layer.
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.
_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...
_LayerRef(TYPE *l=nullptr)
Constructor for a layer reference from an existing map layer.
MatchType
Flag for match type in weak resolution.
@ Name
Match layer name.
@ All
Match all.
@ Provider
Match layer provider name.
@ Source
Match layer source.
void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes the layer's properties to a XML element.
QString source
Weak reference to layer public source.
QString name
Weak reference to layer name.
bool layerMatchesWeakly(QgsMapLayer *layer, MatchType matchType=MatchType::All) const
TYPE * resolveByIdOrNameOnly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
QString provider
Weak reference to layer provider.
bool layerMatchesSource(QgsMapLayer *layer) const
Returns true if a layer matches the weak references to layer public source, layer name and data provi...
QPointer< TYPE > layer
Weak pointer to map layer.
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
bool operator==(const _LayerRef &other)=delete
Equality operator is deleted to avoid confusion as there are multiple ways two _LayerRef objects can ...
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the layer's properties from an XML element.
QString layerId
Original layer ID.