QGIS API Documentation 3.99.0-Master (c03dd32cbdd)
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
20#include <utility>
21
22#include "qgsdataprovider.h"
23#include "qgsmaplayer.h"
24#include "qgsproject.h"
25
26#include <QDomElement>
27#include <QPointer>
28#include <QString>
29
30#define SIP_NO_FILE
31
32using namespace Qt::StringLiterals;
33
39template<typename TYPE>
41{
42
48 {
49 Name = 1 << 2,
50 Provider = 1 << 3,
51 Source = 1 << 4,
53 };
54
55
61 _LayerRef( TYPE *l = nullptr )
62 : layer( l )
63 , layerId( l ? l->id() : QString() )
64 , source( l ? l->publicSource() : QString() )
65 , name( l ? l->name() : QString() )
66 , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
67 {}
68
73 _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
74 : layer()
75 , layerId( id )
76 , source( source )
77 , name( name )
79 {}
80
84 void setLayer( TYPE *l )
85 {
86 layer = l;
87 layerId = l ? l->id() : QString();
88 source = l ? l->publicSource() : QString();
89 name = l ? l->name() : QString();
90 provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
91 }
92
96 bool operator==( const _LayerRef &other ) = delete;
97
102 explicit operator bool() const
103 {
104 return static_cast< bool >( layer.data() );
105 }
106
110 TYPE *operator->() const
111 {
112 return layer.data();
113 }
114
119 TYPE *get() const
120 {
121 return layer.data();
122 }
123
125 QPointer<TYPE> layer;
126
128 QString layerId;
129
131 QString source;
133 QString name;
135 QString provider;
136
143 {
144 if ( layer->publicSource() != source ||
145 layer->name() != name )
146 return false;
147
148 if ( layer->providerType() != provider )
149 return false;
150
151 return true;
152 }
153
161 TYPE *resolve( const QgsProject *project )
162 {
163 if ( project && !layerId.isEmpty() )
164 {
165 if ( TYPE *l = qobject_cast<TYPE *>( project->mapLayer( layerId ) ) )
166 {
167 setLayer( l );
168 return l;
169 }
170 }
171 return nullptr;
172 }
173
175 {
176 // First match the name
177 if ( matchType & MatchType::Name && ( layer->name().isEmpty() || layer->name() != name ) )
178 {
179 return false;
180 }
181 else
182 {
183 // We have found a match by name, now check the other
184 // criteria
185 if ( matchType & MatchType::Provider && layer->providerType() != provider )
186 {
187 return false;
188 }
189 if ( matchType & MatchType::Source && layer->publicSource() != source )
190 {
191 return false;
192 }
193 // All tests passed
194 return true;
195 }
196 }
197
222 TYPE *resolveWeakly( const QgsProject *project, MatchType matchType = MatchType::All )
223 {
224 // first try matching by layer ID
225 if ( resolve( project ) )
226 return layer;
227
228 if ( project )
229 {
230 QList<QgsMapLayer *> layers;
231 // If matching by name ...
232 if ( matchType & MatchType::Name )
233 {
234 if ( name.isEmpty() )
235 {
236 return nullptr;
237 }
238 layers = project->mapLayersByName( name );
239 }
240 else // ... we need all layers
241 {
242 layers = project->mapLayers().values();
243 }
244 for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
245 {
246 if ( TYPE *tl = qobject_cast< TYPE *>( *it ) )
247 {
248 if ( layerMatchesWeakly( tl, matchType ) )
249 {
250 setLayer( tl );
251 return tl;
252 }
253 }
254 }
255 }
256 return nullptr;
257 }
258
281 TYPE *resolveByIdOrNameOnly( const QgsProject *project )
282 {
283 // first try by matching by layer ID, or weakly by source, name and provider
284 if ( resolveWeakly( project ) )
285 return layer;
286
287 // fallback to checking by name only
288 if ( project && !name.isEmpty() )
289 {
290 const QList<QgsMapLayer *> layers = project->mapLayersByName( name );
291 for ( QgsMapLayer *l : layers )
292 {
293 if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
294 {
295 setLayer( tl );
296 return tl;
297 }
298 }
299 }
300 return nullptr;
301 }
302
309 bool readXml( const QDomElement &element, const QgsReadWriteContext &context )
310 {
311 Q_UNUSED( context )
312
313 layerId = element.attribute( u"id"_s );
314 name = element.attribute( u"name"_s );
315 source = element.attribute( u"source"_s );
316 provider = element.attribute( u"provider"_s );
317 return true;
318 }
319
326 void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
327 {
328 Q_UNUSED( context )
329
330 element.setAttribute( u"id"_s, layerId );
331 element.setAttribute( u"name"_s, name );
332 element.setAttribute( u"source"_s, source );
333 element.setAttribute( u"provider"_s, provider );
334 }
335
336};
337
339
340#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:113
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.