16#ifndef QGSMAPLAYERREF_H
17#define QGSMAPLAYERREF_H
34template<
typename TYPE>
58 ,
layerId( l ? l->id() : QString() )
59 ,
source( l ? l->publicSource() : QString() )
61 ,
provider( l && l->dataProvider() ? l->dataProvider()->
name() : QString() )
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();
97 explicit operator bool()
const
99 return static_cast< bool >(
layer.data() );
158 if ( project && !
layerId.isEmpty() )
225 QList<QgsMapLayer *> layers;
229 if (
name.isEmpty() )
239 for (
auto it = layers.constBegin(); it != layers.constEnd(); ++it )
241 if ( TYPE *tl = qobject_cast< TYPE *>( *it ) )
283 if ( project && !
name.isEmpty() )
288 if ( TYPE *tl = qobject_cast< TYPE *>( l ) )
308 layerId = element.attribute( QStringLiteral(
"id" ) );
309 name = element.attribute( QStringLiteral(
"name" ) );
310 source = element.attribute( QStringLiteral(
"source" ) );
311 provider = element.attribute( QStringLiteral(
"provider" ) );
325 element.setAttribute( QStringLiteral(
"id" ),
layerId );
326 element.setAttribute( QStringLiteral(
"name" ),
name );
327 element.setAttribute( QStringLiteral(
"source" ),
source );
328 element.setAttribute( QStringLiteral(
"provider" ),
provider );
Base class for all map layer types.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
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.
@ 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.