QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
65  break;
66  }
67 
68  mLayerSettings[layer] = settings;
69  }
70 }
71 
73 {
74  for ( QgsMapLayer *layer : mLayerSettings.keys() )
75  {
76  QgsLayerSettings settings = mLayerSettings[layer];
77  layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
78  layer->setName( mLayerSettings[layer].name );
79 
80  // if a SLD file has been loaded for rendering, we restore the previous style
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  switch ( layer->type() )
89  {
91  {
92  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
93 
94  if ( vLayer )
95  {
96  vLayer->setOpacity( settings.mOpacity );
97  vLayer->selectByIds( settings.mSelectedFeatureIds );
98  vLayer->setSubsetString( settings.mFilter );
99  }
100  break;
101  }
103  {
104  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
105 
106  if ( rLayer )
107  {
108  rLayer->renderer()->setOpacity( settings.mOpacity );
109  }
110  break;
111  }
112 
117  break;
118  }
119  }
120 }
121 
122 namespace QgsWms
123 {
125  : mLayerRestorer( context.layers() )
126  {
127  }
128 }
qgsmaplayerstylemanager.h
qgsrasterlayer.h
QgsMapLayerType::MeshLayer
@ MeshLayer
Added in 3.2.
QgsMapLayerType::VectorLayer
@ VectorLayer
qgsreadwritecontext.h
qgsrasterrenderer.h
QgsVectorLayer::selectByIds
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using a list of feature IDs.
Definition: qgsvectorlayer.cpp:523
QgsWms::QgsWmsRenderContext
Rendering context for the WMS renderer.
Definition: qgswmsrendercontext.h:35
qgswmsrestorer.h
qgsmaplayer.h
QgsMapLayerType::RasterLayer
@ RasterLayer
QgsVectorLayer::selectedFeatureIds
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3441
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
qgsvectorlayer.h
QgsWms
Median cut implementation.
Definition: qgsdxfwriter.cpp:23
QgsWms::QgsWmsRestorer::QgsWmsRestorer
QgsWmsRestorer(const QgsWmsRenderContext &context)
Constructor for QgsWmsRestorer.
Definition: qgswmsrestorer.cpp:124
QgsLayerRestorer::QgsLayerRestorer
QgsLayerRestorer(const QList< QgsMapLayer * > &layers)
Constructor for QgsLayerRestorer.
Definition: qgswmsrestorer.cpp:27
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Added in 3.14.
QgsVectorLayer::subsetString
QString subsetString
Definition: qgsvectorlayer.h:390
QgsRasterLayer::renderer
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
Definition: qgsrasterlayer.h:246
QgsVectorLayer::opacity
double opacity
Definition: qgsvectorlayer.h:395
QgsLayerRestorer::~QgsLayerRestorer
~QgsLayerRestorer()
Destructor.
Definition: qgswmsrestorer.cpp:72
QgsRasterRenderer::setOpacity
void setOpacity(double opacity)
Sets the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
Definition: qgsrasterrenderer.h:81
QgsMapLayerType::PluginLayer
@ PluginLayer
QgsVectorLayer::setOpacity
void setOpacity(double opacity)
Sets the opacity for the vector layer, where opacity is a value between 0 (totally transparent) and 1...
Definition: qgsvectorlayer.cpp:4343
QgsVectorLayer::setSubsetString
virtual bool setSubsetString(const QString &subset)
Sets the string (typically sql) used to define a subset of the layer.
Definition: qgsvectorlayer.cpp:943
qgsmessagelog.h
QgsRasterRenderer::opacity
double opacity() const
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
Definition: qgsrasterrenderer.h:88