QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgswmsrestorer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswmsrestorer.cpp
3 ------------------
4 begin : April 24, 2017
5 copyright : (C) 2017 by Paul Blottiere
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 "qgswmsrestorer.h"
19#include "qgsmessagelog.h"
20#include "qgsmaplayer.h"
21#include "qgsvectorlayer.h"
22#include "qgsrasterlayer.h"
23#include "qgsrasterrenderer.h"
25#include "qgsreadwritecontext.h"
26
27QgsLayerRestorer::QgsLayerRestorer( const QList<QgsMapLayer *> &layers )
28{
29 for ( QgsMapLayer *layer : layers )
30 {
31 mLayerSettings.emplace( layer, QgsLayerSettings() );
32 QgsLayerSettings &settings = mLayerSettings[layer];
33
34 settings.name = layer->name();
35 settings.mNamedStyle = layer->styleManager()->currentStyle();
36
37 switch ( layer->type() )
38 {
40 {
41 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
42
43 if ( vLayer )
44 {
45 settings.mOpacity = vLayer->opacity();
46 settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
47 settings.mFilter = vLayer->subsetString();
48 // Labeling opacity
49 if ( vLayer->labelsEnabled() && vLayer->labeling() )
50 {
51 settings.mLabeling.reset( vLayer->labeling()->clone() );
52 }
53 }
54 break;
55 }
57 {
58 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
59
60 if ( rLayer && rLayer->renderer() )
61 {
62 settings.mOpacity = rLayer->renderer()->opacity();
63 }
64 break;
65 }
66
74 break;
75 }
76 }
77}
78
80{
81 for ( auto it = mLayerSettings.begin(); it != mLayerSettings.end(); it++ )
82 {
83 QgsMapLayer *layer = it->first;
84
85 // Firstly check if a SLD file has been loaded for rendering and removed it
86 const QString sldStyleName { layer->customProperty( "sldStyleName", "" ).toString() };
87 if ( !sldStyleName.isEmpty() )
88 {
89 layer->styleManager()->removeStyle( sldStyleName );
90 layer->removeCustomProperty( "sldStyleName" );
91 }
92
93 // Then restore the previous style
94 QgsLayerSettings &settings = it->second;
95 layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
96 layer->setName( settings.name );
97
98 switch ( layer->type() )
99 {
101 {
102 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
103
104 if ( vLayer )
105 {
106 vLayer->setOpacity( settings.mOpacity );
107 vLayer->selectByIds( settings.mSelectedFeatureIds );
108 vLayer->setSubsetString( settings.mFilter );
109 if ( settings.mLabeling )
110 {
111 vLayer->setLabeling( settings.mLabeling.release() );
112 }
113 }
114 break;
115 }
117 {
118 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
119
120 if ( rLayer && rLayer->renderer() )
121 {
122 rLayer->renderer()->setOpacity( settings.mOpacity );
123 }
124 break;
125 }
126
134 break;
135 }
136 }
137}
138
139namespace QgsWms
140{
142 : mLayerRestorer( context.layers() )
143 {
144 }
145} // namespace QgsWms
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
virtual QgsAbstractVectorLayerLabeling * clone() const =0
Returns a new copy of the object.
~QgsLayerRestorer()
Destructor.
QgsLayerRestorer(const QList< QgsMapLayer * > &layers)
Constructor for QgsLayerRestorer.
bool removeStyle(const QString &name)
Remove a stored style.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
Qgis::LayerType type
Definition qgsmaplayer.h:86
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
void setName(const QString &name)
Set the display name of the layer.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
double opacity
Definition qgsmaplayer.h:88
Represents a raster layer.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
double opacity() const
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
void setOpacity(double opacity)
Sets the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
Represents a vector layer which manages a vector based data sets.
void setLabeling(QgsAbstractVectorLayerLabeling *labeling)
Sets labeling configuration.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
virtual bool setSubsetString(const QString &subset)
Sets the string (typically sql) used to define a subset of the layer.
Rendering context for the WMS renderer.
QgsWmsRestorer(const QgsWmsRenderContext &context)
Constructor for QgsWmsRestorer.
Median cut implementation.