QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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
6  email : [email protected]
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 
27 QgsLayerRestorer::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  {
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  }
51  {
52  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
53 
54  if ( rLayer )
55  {
56  settings.mOpacity = rLayer->renderer()->opacity();
57  }
58  break;
59  }
60 
66  break;
67  }
68 
69  mLayerSettings[layer] = settings;
70  }
71 }
72 
74 {
75  for ( QgsMapLayer *layer : mLayerSettings.keys() )
76  {
77  // Firstly check if a SLD file has been loaded for rendering and removed it
78  const QString sldStyleName { layer->customProperty( "sldStyleName", "" ).toString() };
79  if ( !sldStyleName.isEmpty() )
80  {
81  layer->styleManager()->removeStyle( sldStyleName );
82  layer->removeCustomProperty( "sldStyleName" );
83  }
84 
85  // Then restore the previous style
86  const QgsLayerSettings settings = mLayerSettings[layer];
87  layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
88  layer->setName( mLayerSettings[layer].name );
89 
90  switch ( layer->type() )
91  {
93  {
94  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
95 
96  if ( vLayer )
97  {
98  vLayer->setOpacity( settings.mOpacity );
99  vLayer->selectByIds( settings.mSelectedFeatureIds );
100  vLayer->setSubsetString( settings.mFilter );
101  }
102  break;
103  }
105  {
106  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
107 
108  if ( rLayer )
109  {
110  rLayer->renderer()->setOpacity( settings.mOpacity );
111  }
112  break;
113  }
114 
120  break;
121  }
122  }
123 }
124 
125 namespace QgsWms
126 {
128  : mLayerRestorer( context.layers() )
129  {
130  }
131 }
~QgsLayerRestorer()
Destructor.
QgsLayerRestorer(const QList< QgsMapLayer * > &layers)
Constructor for QgsLayerRestorer.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
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.
@ PointCloudLayer
Added in 3.18.
@ MeshLayer
Added in 3.2.
@ VectorTileLayer
Added in 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Median cut implementation.