QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <utility>
22
23#include "qgsdataprovider.h"
24#include "qgsmaplayer.h"
25#include "qgsproject.h"
26
27#include <QDomElement>
28#include <QPointer>
29
35template<typename TYPE>
37{
38
44 {
45 Name = 1 << 2,
46 Provider = 1 << 3,
47 Source = 1 << 4,
49 };
50
51
57 _LayerRef( TYPE *l = nullptr )
58 : layer( l )
59 , layerId( l ? l->id() : QString() )
60 , source( l ? l->publicSource() : QString() )
61 , name( l ? l->name() : QString() )
62 , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
63 {}
64
69 _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
70 : layer()
71 , layerId( id )
72 , source( source )
73 , name( name )
75 {}
76
80 void setLayer( TYPE *l )
81 {
82 layer = l;
83 layerId = l ? l->id() : QString();
84 source = l ? l->publicSource() : QString();
85 name = l ? l->name() : QString();
86 provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
87 }
88
92 bool operator==( const _LayerRef &other ) = delete;
93
98 explicit operator bool() const
99 {
100 return static_cast< bool >( layer.data() );
101 }
102
106 TYPE *operator->() const
107 {
108 return layer.data();
109 }
110
115 TYPE *get() const
116 {
117 return layer.data();
118 }
119
121 QPointer<TYPE> layer;
122
124 QString layerId;
125
127 QString source;
129 QString name;
131 QString provider;
132
139 {
140 if ( layer->publicSource() != source ||
141 layer->name() != name )
142 return false;
143
144 if ( layer->providerType() != provider )
145 return false;
146
147 return true;
148 }
149
157 TYPE *resolve( const QgsProject *project )
158 {
159 if ( project && !layerId.isEmpty() )
160 {
161 if ( TYPE *l = qobject_cast<TYPE *>( project->mapLayer( layerId ) ) )
162 {
163 setLayer( l );
164 return l;
165 }
166 }
167 return nullptr;
168 }
169
171 {
172 // First match the name
173 if ( matchType & MatchType::Name && ( layer->name().isEmpty() || layer->name() != name ) )
174 {
175 return false;
176 }
177 else
178 {
179 // We have found a match by name, now check the other
180 // criteria
181 if ( matchType & MatchType::Provider && layer->providerType() != provider )
182 {
183 return false;
184 }
185 if ( matchType & MatchType::Source && layer->publicSource() != source )
186 {
187 return false;
188 }
189 // All tests passed
190 return true;
191 }
192 }
193
218 TYPE *resolveWeakly( const QgsProject *project, MatchType matchType = MatchType::All )
219 {
220 // first try matching by layer ID
221 if ( resolve( project ) )
222 return layer;
223
224 if ( project )
225 {
226 QList<QgsMapLayer *> layers;
227 // If matching by name ...
228 if ( matchType & MatchType::Name )
229 {
230 if ( name.isEmpty() )
231 {
232 return nullptr;
233 }
234 layers = project->mapLayersByName( name );
235 }
236 else // ... we need all layers
237 {
238 layers = project->mapLayers().values();
239 }
240 for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
241 {
242 if ( TYPE *tl = qobject_cast< TYPE *>( *it ) )
243 {
244 if ( layerMatchesWeakly( tl, matchType ) )
245 {
246 setLayer( tl );
247 return tl;
248 }
249 }
250 }
251 }
252 return nullptr;
253 }
254
277 TYPE *resolveByIdOrNameOnly( const QgsProject *project )
278 {
279 // first try by matching by layer ID, or weakly by source, name and provider
280 if ( resolveWeakly( project ) )
281 return layer;
282
283 // fallback to checking by name only
284 if ( project && !name.isEmpty() )
285 {
286 const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
287 for ( QgsMapLayer *l : layers )
288 {
289 if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
290 {
291 setLayer( tl );
292 return tl;
293 }
294 }
295 }
296 return nullptr;
297 }
298
305 bool readXml( const QDomElement &element, const QgsReadWriteContext &context )
306 {
307 Q_UNUSED( context )
308
309 layerId = element.attribute( QStringLiteral( "id" ) );
310 name = element.attribute( QStringLiteral( "name" ) );
311 source = element.attribute( QStringLiteral( "source" ) );
312 provider = element.attribute( QStringLiteral( "provider" ) );
313 return true;
314 }
315
322 void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
323 {
324 Q_UNUSED( context )
325
326 element.setAttribute( QStringLiteral( "id" ), layerId );
327 element.setAttribute( QStringLiteral( "name" ), name );
328 element.setAttribute( QStringLiteral( "source" ), source );
329 element.setAttribute( QStringLiteral( "provider" ), provider );
330 }
331
332};
333
335
336#endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition qgsmaplayer.h:80
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
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.
A container for the context for various read/write operations on 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.
void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes the layer's properties to a XML element.
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.
bool layerMatchesSource(QgsMapLayer *layer) const
Returns true if a layer matches the weak references to layer public source, layer name and data provi...
QPointer< QgsMapLayer > 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.