QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
30
31using namespace Qt::StringLiterals;
32
38template<typename TYPE>
40{
41
47 {
48 Name = 1 << 2,
49 Provider = 1 << 3,
50 Source = 1 << 4,
52 };
53
54
60 _LayerRef( TYPE *l = nullptr )
61 : layer( l )
62 , layerId( l ? l->id() : QString() )
63 , source( l ? l->publicSource() : QString() )
64 , name( l ? l->name() : QString() )
65 , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
66 {}
67
72 _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
73 : layer()
74 , layerId( id )
75 , source( source )
76 , name( name )
78 {}
79
83 void setLayer( TYPE *l )
84 {
85 layer = l;
86 layerId = l ? l->id() : QString();
87 source = l ? l->publicSource() : QString();
88 name = l ? l->name() : QString();
89 provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
90 }
91
95 bool operator==( const _LayerRef &other ) = delete;
96
101 explicit operator bool() const
102 {
103 return static_cast< bool >( layer.data() );
104 }
105
109 TYPE *operator->() const
110 {
111 return layer.data();
112 }
113
118 TYPE *get() const
119 {
120 return layer.data();
121 }
122
124 QPointer<TYPE> layer;
125
127 QString layerId;
128
130 QString source;
132 QString name;
134 QString provider;
135
142 {
143 if ( layer->publicSource() != source ||
144 layer->name() != name )
145 return false;
146
147 if ( layer->providerType() != provider )
148 return false;
149
150 return true;
151 }
152
160 TYPE *resolve( const QgsProject *project )
161 {
162 if ( project && !layerId.isEmpty() )
163 {
164 if ( TYPE *l = qobject_cast<TYPE *>( project->mapLayer( layerId ) ) )
165 {
166 setLayer( l );
167 return l;
168 }
169 }
170 return nullptr;
171 }
172
174 {
175 // First match the name
176 if ( matchType & MatchType::Name && ( layer->name().isEmpty() || layer->name() != name ) )
177 {
178 return false;
179 }
180 else
181 {
182 // We have found a match by name, now check the other
183 // criteria
184 if ( matchType & MatchType::Provider && layer->providerType() != provider )
185 {
186 return false;
187 }
188 if ( matchType & MatchType::Source && layer->publicSource() != source )
189 {
190 return false;
191 }
192 // All tests passed
193 return true;
194 }
195 }
196
221 TYPE *resolveWeakly( const QgsProject *project, MatchType matchType = MatchType::All )
222 {
223 // first try matching by layer ID
224 if ( resolve( project ) )
225 return layer;
226
227 if ( project )
228 {
229 QList<QgsMapLayer *> layers;
230 // If matching by name ...
231 if ( matchType & MatchType::Name )
232 {
233 if ( name.isEmpty() )
234 {
235 return nullptr;
236 }
237 layers = project->mapLayersByName( name );
238 }
239 else // ... we need all layers
240 {
241 layers = project->mapLayers().values();
242 }
243 for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
244 {
245 if ( TYPE *tl = qobject_cast< TYPE *>( *it ) )
246 {
247 if ( layerMatchesWeakly( tl, matchType ) )
248 {
249 setLayer( tl );
250 return tl;
251 }
252 }
253 }
254 }
255 return nullptr;
256 }
257
280 TYPE *resolveByIdOrNameOnly( const QgsProject *project )
281 {
282 // first try by matching by layer ID, or weakly by source, name and provider
283 if ( resolveWeakly( project ) )
284 return layer;
285
286 // fallback to checking by name only
287 if ( project && !name.isEmpty() )
288 {
289 const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
290 for ( QgsMapLayer *l : layers )
291 {
292 if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
293 {
294 setLayer( tl );
295 return tl;
296 }
297 }
298 }
299 return nullptr;
300 }
301
308 bool readXml( const QDomElement &element, const QgsReadWriteContext &context )
309 {
310 Q_UNUSED( context )
311
312 layerId = element.attribute( u"id"_s );
313 name = element.attribute( u"name"_s );
314 source = element.attribute( u"source"_s );
315 provider = element.attribute( u"provider"_s );
316 return true;
317 }
318
325 void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
326 {
327 Q_UNUSED( context )
328
329 element.setAttribute( u"id"_s, layerId );
330 element.setAttribute( u"name"_s, name );
331 element.setAttribute( u"source"_s, source );
332 element.setAttribute( u"provider"_s, provider );
333 }
334
335};
336
338
339#endif // QGSMAPLAYERREF_H
Base class for all map layer types.
Definition qgsmaplayer.h:83
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
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.