QGIS API Documentation 3.29.0-Master (da8bb1db43)
qgsgrouplayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgrouplayer.cpp
3 ----------------
4 Date : September 2021
5 Copyright : (C) 2021 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsgrouplayer.h"
19#include "qgsmaplayerfactory.h"
20#include "qgspainting.h"
23#include "qgsmaplayerref.h"
24#include "qgsvectorlayer.h"
26#include "qgspainteffect.h"
27#include "qgsmessagelog.h"
29#include "qgsapplication.h"
30#include "qgsmaplayerutils.h"
31#include "qgsthreadingutils.h"
32
33QgsGroupLayer::QgsGroupLayer( const QString &name, const LayerOptions &options )
35 , mTransformContext( options.transformContext )
36{
37 mShouldValidateCrs = false;
38 mValid = true;
39
40 mPaintEffect.reset( QgsPaintEffectRegistry::defaultStack() );
41 mPaintEffect->setEnabled( false );
42
44 providerOptions.transformContext = options.transformContext;
45 mDataProvider = new QgsGroupLayerDataProvider( providerOptions, QgsDataProvider::ReadFlags() );
46}
47
49{
50 emit willBeDeleted();
51 delete mDataProvider;
52}
53
55{
57
58 const QgsGroupLayer::LayerOptions options( mTransformContext );
59 std::unique_ptr< QgsGroupLayer > layer = std::make_unique< QgsGroupLayer >( name(), options );
60 QgsMapLayer::clone( layer.get() );
61 layer->setChildLayers( _qgis_listRefToRaw( mChildren ) );
62 layer->setPaintEffect( mPaintEffect ? mPaintEffect->clone() : nullptr );
63 return layer.release();
64}
65
67{
69
70 return new QgsGroupLayerRenderer( this, context );
71}
72
74{
76
77 return QgsMapLayerUtils::combinedExtent( childLayers(), crs(), mTransformContext );
78}
79
81{
83
84 if ( mDataProvider )
85 mDataProvider->setTransformContext( context );
86
87 mTransformContext = context;
89}
90
91bool QgsGroupLayer::readXml( const QDomNode &layerNode, QgsReadWriteContext &context )
92{
94
96 {
97 return false;
98 }
99
100 const QList< QgsMapLayer * > currentLayers = _qgis_listRefToRaw( mChildren );
101 for ( QgsMapLayer *layer : currentLayers )
102 {
104 }
105
106 mChildren.clear();
107 const QDomNodeList childLayersElements = layerNode.toElement().elementsByTagName( QStringLiteral( "childLayers" ) );
108 const QDomNodeList children = childLayersElements.at( 0 ).childNodes();
109 for ( int i = 0; i < children.size(); ++i )
110 {
111 const QDomElement childElement = children.at( i ).toElement();
112 const QString id = childElement.attribute( QStringLiteral( "layerid" ) );
113 mChildren.append( QgsMapLayerRef( id ) );
114 }
116
117 QString errorMsg;
118 readSymbology( layerNode, errorMsg, context );
119
121
122 return mValid;
123}
124
125bool QgsGroupLayer::writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const
126{
128
129 // first get the layer element so that we can append the type attribute
130 QDomElement mapLayerNode = layer_node.toElement();
131
132 if ( mapLayerNode.isNull() )
133 {
134 QgsDebugMsgLevel( QStringLiteral( "can't find maplayer node" ), 2 );
135 return false;
136 }
137
138 mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( QgsMapLayerType::GroupLayer ) );
139
140 QDomElement childLayersElement = doc.createElement( QStringLiteral( "childLayers" ) );
141 for ( auto it = mChildren.constBegin(); it != mChildren.constEnd(); ++it )
142 {
143 QDomElement childElement = doc.createElement( QStringLiteral( "child" ) );
144 childElement.setAttribute( QStringLiteral( "layerid" ), it->layerId );
145 childLayersElement.appendChild( childElement );
146 }
147 mapLayerNode.appendChild( childLayersElement );
148
149 // renderer specific settings
150 QString errorMsg;
151 return writeSymbology( layer_node, doc, errorMsg, context );
152}
153
154bool QgsGroupLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &, const QgsReadWriteContext &, QgsMapLayer::StyleCategories categories ) const
155{
157
158 // add the layer opacity
159 if ( categories.testFlag( Rendering ) )
160 {
161 QDomElement layerOpacityElem = doc.createElement( QStringLiteral( "layerOpacity" ) );
162 const QDomText layerOpacityText = doc.createTextNode( QString::number( opacity() ) );
163 layerOpacityElem.appendChild( layerOpacityText );
164 node.appendChild( layerOpacityElem );
165
166 if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect.get() ) )
167 {
168 QDomElement paintEffectElement = doc.createElement( QStringLiteral( "paintEffect" ) );
169 mPaintEffect->saveProperties( doc, paintEffectElement );
170 node.appendChild( paintEffectElement );
171 }
172 }
173
174 if ( categories.testFlag( Symbology ) )
175 {
176 // add the blend mode field
177 QDomElement blendModeElem = doc.createElement( QStringLiteral( "blendMode" ) );
178 const QDomText blendModeText = doc.createTextNode( QString::number( QgsPainting::getBlendModeEnum( blendMode() ) ) );
179 blendModeElem.appendChild( blendModeText );
180 node.appendChild( blendModeElem );
181 }
182
183 return true;
184}
185
186bool QgsGroupLayer::readSymbology( const QDomNode &node, QString &, QgsReadWriteContext &, QgsMapLayer::StyleCategories categories )
187{
189
190 if ( categories.testFlag( Rendering ) )
191 {
192 const QDomNode layerOpacityNode = node.namedItem( QStringLiteral( "layerOpacity" ) );
193 if ( !layerOpacityNode.isNull() )
194 {
195 const QDomElement e = layerOpacityNode.toElement();
196 setOpacity( e.text().toDouble() );
197 }
198
199 //restore layer effect
200 const QDomElement effectElem = node.namedItem( QStringLiteral( "paintEffect" ) ).toElement();
201 if ( !effectElem.isNull() )
202 {
203 const QDomElement effectPropertiesElem = effectElem.firstChildElement( QStringLiteral( "effect" ) ).toElement();
204 mPaintEffect.reset( QgsApplication::paintEffectRegistry()->createEffect( effectPropertiesElem ) );
205 }
206 else
207 {
208 mPaintEffect.reset( QgsPaintEffectRegistry::defaultStack() );
209 mPaintEffect->setEnabled( false );
210 }
211 }
212
213 if ( categories.testFlag( Symbology ) )
214 {
215 // get and set the blend mode if it exists
216 const QDomNode blendModeNode = node.namedItem( QStringLiteral( "blendMode" ) );
217 if ( !blendModeNode.isNull() )
218 {
219 const QDomElement e = blendModeNode.toElement();
220 setBlendMode( QgsPainting::getCompositionMode( static_cast< QgsPainting::BlendMode >( e.text().toInt() ) ) );
221 }
222 }
223
224 return true;
225}
226
228{
230
231 return mDataProvider;
232}
233
235{
237
238 return mDataProvider;
239}
240
242{
244
245 QString metadata = QStringLiteral( "<html>\n<body>\n<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
246
247 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );
248
249 // Extent
250 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Extent" ) + QStringLiteral( "</td><td>" ) + extent().toString() + QStringLiteral( "</td></tr>\n" );
251
252
253 metadata += QLatin1String( "\n</body>\n</html>\n" );
254 return metadata;
255}
256
258{
260
262 for ( int i = 0; i < mChildren.size(); ++i )
263 {
264 mChildren[i].resolve( project );
265
266 if ( mChildren[i].layer )
267 {
268 connect( mChildren[i].layer, &QgsMapLayer::repaintRequested, this, &QgsMapLayer::triggerRepaint, Qt::UniqueConnection );
269
270 // group layer inherits first valid child layer's crs
271 if ( !crs().isValid() )
272 {
273 setCrs( mChildren[i].layer->crs() );
274 mDataProvider->setCrs( crs() );
275 }
276 }
277 }
279}
280
281void QgsGroupLayer::setChildLayers( const QList< QgsMapLayer * > &layers )
282{
284
285 const QList< QgsMapLayer * > currentLayers = _qgis_listRefToRaw( mChildren );
286 for ( QgsMapLayer *layer : layers )
287 {
288 if ( !currentLayers.contains( layer ) )
289 {
290 connect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapLayer::triggerRepaint, Qt::UniqueConnection );
291 }
292 }
293 for ( QgsMapLayer *layer : currentLayers )
294 {
295 if ( !layers.contains( layer ) )
296 {
298 }
299 }
300 mChildren = _qgis_listRawToRef( layers );
301
302 // group layer inherits first valid child layer's crs
303 for ( const QgsMapLayer *layer : layers )
304 {
305 if ( layer->isValid() && layer->crs().isValid( ) )
306 {
307 setCrs( layer->crs() );
308 mDataProvider->setCrs( crs() );
309 break;
310 }
311 }
312
314}
315
316QList< QgsMapLayer * > QgsGroupLayer::childLayers() const
317{
319
320 return _qgis_listRefToRaw( mChildren );
321}
322
324{
326
327 return mPaintEffect.get();
328}
329
331{
333
334 mPaintEffect.reset( effect );
335}
336
337//
338// QgsGroupLayerDataProvider
339//
341QgsGroupLayerDataProvider::QgsGroupLayerDataProvider(
342 const ProviderOptions &options,
343 QgsDataProvider::ReadFlags flags )
344 : QgsDataProvider( QString(), options, flags )
345{}
346
347void QgsGroupLayerDataProvider::setCrs( const QgsCoordinateReferenceSystem &crs )
348{
350
351 mCrs = crs;
352}
353
355{
357
358 return mCrs;
359}
360
361QString QgsGroupLayerDataProvider::name() const
362{
364
365 return QStringLiteral( "annotation" );
366}
367
368QString QgsGroupLayerDataProvider::description() const
369{
371
372 return QString();
373}
374
375QgsRectangle QgsGroupLayerDataProvider::extent() const
376{
378
379 return QgsRectangle();
380}
381
382bool QgsGroupLayerDataProvider::isValid() const
383{
385
386 return true;
387}
389
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Abstract base class for spatial data provider implementations.
Implementation of threaded rendering for group layers.
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Definition: qgsgrouplayer.h:42
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the renderer.
void resolveReferences(QgsProject *project) override
Resolve references to other layers (kept as layer IDs after reading XML) into layer objects.
bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) override
Read the symbology for the current layer from the DOM node supplied.
QgsGroupLayer(const QString &name, const QgsGroupLayer::LayerOptions &options)
Constructor for a new QgsGroupLayer with the specified layer name.
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the group layer.
QgsGroupLayer * clone() const override
Returns a new instance equivalent to this one except for the id which is still unique.
QList< QgsMapLayer * > childLayers() const
Returns the child layers contained by the group.
~QgsGroupLayer() override
QgsMapLayerRenderer * createMapRenderer(QgsRenderContext &rendererContext) override
Returns new instance of QgsMapLayerRenderer that will be used for rendering of given context.
QgsDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
void setChildLayers(const QList< QgsMapLayer * > &layers)
Sets the child layers contained by the group.
bool writeXml(QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context) const override
Called by writeLayerXML(), used by children to write state specific to them to project files.
void setTransformContext(const QgsCoordinateTransformContext &context) override
Sets the coordinate transform context to transformContext.
bool readXml(const QDomNode &layerNode, QgsReadWriteContext &context) override
Called by readLayerXML(), used by children to read state specific to them from project files.
bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &, StyleCategories categories=AllStyleCategories) const override
Write the style for the layer into the document provided.
QString htmlMetadata() const override
Obtain a formatted HTML string containing assorted metadata for this layer.
QgsRectangle extent() const override
Returns the extent of the layer.
static QString typeToString(QgsMapLayerType type)
Converts a map layer type to a string value.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
static QgsRectangle combinedExtent(const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext)
Returns the combined extent of a list of layers.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QString name
Definition: qgsmaplayer.h:76
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QgsLayerMetadata metadata
Definition: qgsmaplayer.h:78
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
virtual void resolveReferences(QgsProject *project)
Resolve references to other layers (kept as layer IDs after reading XML) into layer objects.
bool isValid
Definition: qgsmaplayer.h:81
@ FlagDontResolveLayers
Don't resolve layer paths or create data providers for layers.
Definition: qgsmaplayer.h:633
QgsMapLayer::ReadFlags mReadFlags
Read flags. It's up to the subclass to respect these when restoring state from XML.
Definition: qgsmaplayer.h:2037
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
double opacity
Definition: qgsmaplayer.h:82
bool mValid
Indicates if the layer is valid and can be drawn.
Definition: qgsmaplayer.h:1988
@ Symbology
Symbology.
Definition: qgsmaplayer.h:162
@ Rendering
Rendering: scale visibility, simplify method, opacity.
Definition: qgsmaplayer.h:171
void invalidateWgs84Extent()
Invalidates the WGS84 extent.
bool mShouldValidateCrs
true if the layer's CRS should be validated and invalid CRSes are not permitted.
Definition: qgsmaplayer.h:2044
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
Base class for visual effects which can be applied to QPicture drawings.
static QgsPainting::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a BlendMode corresponding to a QPainter::CompositionMode.
Definition: qgspainting.cpp:80
static QPainter::CompositionMode getCompositionMode(QgsPainting::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a BlendMode.
Definition: qgspainting.cpp:20
BlendMode
Blending modes enum defining the available composition modes that can be used when rendering a layer.
Definition: qgspainting.h:37
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:106
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
Contains information about the context of a rendering operation.
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgis.h:47
@ GroupLayer
Composite group layer. Added in QGIS 3.24.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
_LayerRef< QgsMapLayer > QgsMapLayerRef
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
const QgsCoordinateReferenceSystem & crs
Setting options for creating vector data providers.
QgsCoordinateTransformContext transformContext
Coordinate transform context.
Setting options for loading group layers.
Definition: qgsgrouplayer.h:52
QgsCoordinateTransformContext transformContext
Coordinate transform context.
Definition: qgsgrouplayer.h:64