QGIS API Documentation 4.1.0-Master (60fea48833c)
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> struct _LayerRef
40{
46 {
47 Name = 1 << 2,
48 Provider = 1 << 3,
49 Source = 1 << 4,
51 };
52
53
59 _LayerRef( TYPE *l = nullptr )
60 : layer( l )
61 , layerId( l ? l->id() : QString() )
62 , source( l ? l->publicSource() : QString() )
63 , name( l ? l->name() : QString() )
64 , provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
65 {}
66
71 _LayerRef( const QString &id, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() )
72 : layer()
73 , layerId( id )
74 , source( source )
75 , name( name )
77 {}
78
82 void setLayer( TYPE *l )
83 {
84 layer = l;
85 layerId = l ? l->id() : QString();
86 source = l ? l->publicSource() : QString();
87 name = l ? l->name() : QString();
88 provider = l && l->dataProvider() ? l->dataProvider()->name() : QString();
89 }
90
94 bool operator==( const _LayerRef &other ) = delete;
95
100 explicit operator bool() const { return static_cast< bool >( layer.data() ); }
101
105 TYPE *operator->() const { return layer.data(); }
106
111 TYPE *get() const { return layer.data(); }
112
114 QPointer<TYPE> layer;
115
117 QString layerId;
118
120 QString source;
122 QString name;
124 QString provider;
125
132 {
133 if ( layer->publicSource() != source || 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
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 ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
233 {
234 if ( TYPE *tl = qobject_cast< TYPE *>( *it ) )
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
297 bool readXml( const QDomElement &element, const QgsReadWriteContext &context )
298 {
299 Q_UNUSED( context )
300
301 layerId = element.attribute( u"id"_s );
302 name = element.attribute( u"name"_s );
303 source = element.attribute( u"source"_s );
304 provider = element.attribute( u"provider"_s );
305 return true;
306 }
307
314 void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
315 {
316 Q_UNUSED( context )
317
318 element.setAttribute( u"id"_s, layerId );
319 element.setAttribute( u"name"_s, name );
320 element.setAttribute( u"source"_s, source );
321 element.setAttribute( u"provider"_s, provider );
322 }
323};
324
326
327#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.