QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 QgsLayerSettings settings;
32 settings.name = layer->name();
33
34 settings.mNamedStyle = layer->styleManager()->currentStyle();
35
36 switch ( layer->type() )
37 {
38 case Qgis::LayerType::Vector:
39 {
40 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
41
42 if ( vLayer )
43 {
44 settings.mOpacity = vLayer->opacity();
45 settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
46 settings.mFilter = vLayer->subsetString();
47 }
48 break;
49 }
50 case Qgis::LayerType::Raster:
51 {
52 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
53
54 if ( rLayer )
55 {
56 settings.mOpacity = rLayer->renderer()->opacity();
57 }
58 break;
59 }
60
61 case Qgis::LayerType::Mesh:
62 case Qgis::LayerType::VectorTile:
63 case Qgis::LayerType::Plugin:
64 case Qgis::LayerType::Annotation:
65 case Qgis::LayerType::PointCloud:
66 case Qgis::LayerType::Group:
67 break;
68 }
69
70 mLayerSettings[layer] = settings;
71 }
72}
73
75{
76 for ( auto it = mLayerSettings.constBegin(); it != mLayerSettings.constEnd(); it++ )
77 {
78 QgsMapLayer *layer = it.key();
79
80 // Firstly check if a SLD file has been loaded for rendering and removed it
81 const QString sldStyleName { layer->customProperty( "sldStyleName", "" ).toString() };
82 if ( !sldStyleName.isEmpty() )
83 {
84 layer->styleManager()->removeStyle( sldStyleName );
85 layer->removeCustomProperty( "sldStyleName" );
86 }
87
88 // Then restore the previous style
89 const QgsLayerSettings settings = it.value();
90 layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
91 layer->setName( it.value().name );
92
93 switch ( layer->type() )
94 {
95 case Qgis::LayerType::Vector:
96 {
97 QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
98
99 if ( vLayer )
100 {
101 vLayer->setOpacity( settings.mOpacity );
102 vLayer->selectByIds( settings.mSelectedFeatureIds );
103 vLayer->setSubsetString( settings.mFilter );
104 }
105 break;
106 }
107 case Qgis::LayerType::Raster:
108 {
109 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
110
111 if ( rLayer )
112 {
113 rLayer->renderer()->setOpacity( settings.mOpacity );
114 }
115 break;
116 }
117
118 case Qgis::LayerType::Mesh:
119 case Qgis::LayerType::VectorTile:
120 case Qgis::LayerType::Plugin:
121 case Qgis::LayerType::Annotation:
122 case Qgis::LayerType::PointCloud:
123 case Qgis::LayerType::Group:
124 break;
125 }
126 }
127}
128
129namespace QgsWms
130{
132 : mLayerRestorer( context.layers() )
133 {
134 }
135}
~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:73
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:80
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:82
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.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
QString subsetString
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.