QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
32 mLayerSettings.emplace( layer, QgsLayerSettings() );
33 QgsLayerSettings &settings = mLayerSettings[layer ];
34
35 settings.name = layer->name();
36 settings.mNamedStyle = layer->styleManager()->currentStyle();
37
38 switch ( layer->type() )
39 {
41 {
42 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
43
44 if ( vLayer )
45 {
46 settings.mOpacity = vLayer->opacity();
47 settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
48 settings.mFilter = vLayer->subsetString();
49 // Labeling opacity
50 if ( vLayer->labelsEnabled() && vLayer->labeling() )
51 {
52 settings.mLabeling.reset( vLayer->labeling()->clone() );
53 }
54 }
55 break;
56 }
58 {
59 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
60
61 if ( rLayer )
62 {
63 settings.mOpacity = rLayer->renderer()->opacity();
64 }
65 break;
66 }
67
75 break;
76 }
77
78 }
79}
80
82{
83 for ( auto it = mLayerSettings.begin(); it != mLayerSettings.end(); it++ )
84 {
85 QgsMapLayer *layer = it->first;
86
87 // Firstly check if a SLD file has been loaded for rendering and removed it
88 const QString sldStyleName { layer->customProperty( "sldStyleName", "" ).toString() };
89 if ( !sldStyleName.isEmpty() )
90 {
91 layer->styleManager()->removeStyle( sldStyleName );
92 layer->removeCustomProperty( "sldStyleName" );
93 }
94
95 // Then restore the previous style
96 QgsLayerSettings &settings = it->second;
97 layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
98 layer->setName( settings.name );
99
100 switch ( layer->type() )
101 {
103 {
104 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
105
106 if ( vLayer )
107 {
108 vLayer->setOpacity( settings.mOpacity );
109 vLayer->selectByIds( settings.mSelectedFeatureIds );
110 vLayer->setSubsetString( settings.mFilter );
111 if ( settings.mLabeling )
112 {
113 vLayer->setLabeling( settings.mLabeling.release() );
114 }
115 }
116 break;
117 }
119 {
120 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
121
122 if ( rLayer )
123 {
124 rLayer->renderer()->setOpacity( settings.mOpacity );
125 }
126 break;
127 }
128
136 break;
137 }
138 }
139}
140
141namespace QgsWms
142{
144 : mLayerRestorer( context.layers() )
145 {
146 }
147}
@ 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:74
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:81
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:83
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.