QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  // set a custom property allowing to keep in memory if a SLD file has
37  // been loaded for rendering
38  layer->setCustomProperty( "readSLD", false );
39 
40  QString errMsg;
41  QDomDocument styleDoc( QStringLiteral( "style" ) );
42  QDomElement styleXml = styleDoc.createElement( QStringLiteral( "style" ) );
43  styleDoc.appendChild( styleXml );
44  if ( !layer->writeStyle( styleXml, styleDoc, errMsg, QgsReadWriteContext() ) )
45  {
46  QgsMessageLog::logMessage( QStringLiteral( "QGIS Style has not been added to layer restorer for layer %1: %2" ).arg( layer->name(), errMsg ) );
47  }
48  ( void )settings.mQgisStyle.setContent( styleDoc.toString() );
49 
50  switch ( layer->type() )
51  {
53  {
54  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
55 
56  if ( vLayer )
57  {
58  settings.mOpacity = vLayer->opacity();
59  settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
60  settings.mFilter = vLayer->subsetString();
61  }
62  break;
63  }
65  {
66  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
67 
68  if ( rLayer )
69  {
70  settings.mOpacity = rLayer->renderer()->opacity();
71  }
72  break;
73  }
74 
78  break;
79  }
80 
81  mLayerSettings[layer] = settings;
82  }
83 }
84 
86 {
87  for ( QgsMapLayer *layer : mLayerSettings.keys() )
88  {
89  QgsLayerSettings settings = mLayerSettings[layer];
90  layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
91  layer->setName( mLayerSettings[layer].name );
92 
93  // if a SLD file has been loaded for rendering, we restore the previous style
94  if ( layer->customProperty( "readSLD", false ).toBool() )
95  {
96  QString errMsg;
97  QDomElement root = settings.mQgisStyle.documentElement();
99  if ( !layer->readStyle( root, errMsg, context ) )
100  {
101  QgsMessageLog::logMessage( QStringLiteral( "QGIS Style has not been read from layer restorer for layer %1: %2" ).arg( layer->name(), errMsg ) );
102  }
103  }
104  layer->removeCustomProperty( "readSLD" );
105 
106  switch ( layer->type() )
107  {
109  {
110  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
111 
112  if ( vLayer )
113  {
114  vLayer->setOpacity( settings.mOpacity );
115  vLayer->selectByIds( settings.mSelectedFeatureIds );
116  vLayer->setSubsetString( settings.mFilter );
117  }
118  break;
119  }
121  {
122  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
123 
124  if ( rLayer )
125  {
126  rLayer->renderer()->setOpacity( settings.mOpacity );
127  }
128  break;
129  }
130 
134  break;
135  }
136  }
137 }
138 
139 namespace QgsWms
140 {
142  : mLayerRestorer( context.layers() )
143  {
144  }
145 }
qgsmaplayerstylemanager.h
qgsrasterlayer.h
QgsMapLayerType::MeshLayer
@ MeshLayer
Added in 3.2.
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
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:47
qgswmsrestorer.h
qgsmaplayer.h
QgsMapLayerType::RasterLayer
@ RasterLayer
QgsVectorLayer::selectedFeatureIds
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3433
QgsRasterLayer
Definition: qgsrasterlayer.h:72
qgsvectorlayer.h
QgsWms
WMS implementation.
Definition: qgsdxfwriter.cpp:22
QgsWms::QgsWmsRestorer::QgsWmsRestorer
QgsWmsRestorer(const QgsWmsRenderContext &context)
Constructor for QgsWmsRestorer.
Definition: qgswmsrestorer.cpp:141
QgsLayerRestorer::QgsLayerRestorer
QgsLayerRestorer(const QList< QgsMapLayer * > &layers)
Constructor for QgsLayerRestorer.
Definition: qgswmsrestorer.cpp:27
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Added in 3.14.
QgsVectorLayer::subsetString
QString subsetString
Definition: qgsvectorlayer.h:389
QgsRasterLayer::renderer
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
Definition: qgsrasterlayer.h:247
QgsVectorLayer::opacity
double opacity
Definition: qgsvectorlayer.h:394
QgsLayerRestorer::~QgsLayerRestorer
~QgsLayerRestorer()
Destructor.
Definition: qgswmsrestorer.cpp:85
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:4324
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