QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgslayerrestorer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayerrestorer.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 "qgslayerrestorer.h"
19 #include "qgsmaplayer.h"
20 #include "qgsvectorlayer.h"
21 #include "qgsrasterlayer.h"
22 #include "qgsrasterrenderer.h"
24 
25 QgsLayerRestorer::QgsLayerRestorer( const QList<QgsMapLayer *> &layers )
26 {
27  for ( QgsMapLayer *layer : layers )
28  {
29  QgsLayerSettings settings;
30  settings.name = layer->name();
31 
32  settings.mNamedStyle = layer->styleManager()->currentStyle();
33 
34  // set a custom property allowing to keep in memory if a SLD file has
35  // been loaded for rendering
36  layer->setCustomProperty( "readSLD", false );
37 
38  QString errMsg;
39  QDomDocument sldDoc;
40  layer->exportSldStyle( sldDoc, errMsg );
41  ( void )settings.mSldStyle.setContent( sldDoc.toString(), true ); // for namespace processing
42 
43  switch ( layer->type() )
44  {
46  {
47  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
48 
49  if ( vLayer )
50  {
51  settings.mOpacity = vLayer->opacity();
52  settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
53  settings.mFilter = vLayer->subsetString();
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 
70  break;
71  }
72 
73  mLayerSettings[layer] = settings;
74  }
75 }
76 
78 {
79  for ( QgsMapLayer *layer : mLayerSettings.keys() )
80  {
81  QgsLayerSettings settings = mLayerSettings[layer];
82  layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
83  layer->setName( mLayerSettings[layer].name );
84 
85  // if a SLD file has been loaded for rendering, we restore the previous one
86  QString errMsg;
87  QDomElement root = settings.mSldStyle.firstChildElement( "StyledLayerDescriptor" );
88  QDomElement el = root.firstChildElement( "NamedLayer" );
89  if ( layer->customProperty( "readSLD", false ).toBool() )
90  {
91  layer->readSld( el, errMsg );
92  }
93  layer->removeCustomProperty( "readSLD" );
94 
95  switch ( layer->type() )
96  {
98  {
99  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
100 
101  if ( vLayer )
102  {
103  vLayer->setOpacity( settings.mOpacity );
104  vLayer->selectByIds( settings.mSelectedFeatureIds );
105  vLayer->setSubsetString( settings.mFilter );
106  }
107  break;
108  }
110  {
111  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
112 
113  if ( rLayer )
114  {
115  rLayer->renderer()->setOpacity( settings.mOpacity );
116  }
117  break;
118  }
119 
122  break;
123  }
124  }
125 }
Base class for all map layer types.
Definition: qgsmaplayer.h:79
double opacity() const
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1...
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void selectByIds(const QgsFeatureIds &ids, SelectBehavior behavior=SetSelection)
Selects matching features using a list of feature IDs.
QgsRasterRenderer * renderer() const
Returns the raster&#39;s renderer.
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
~QgsLayerRestorer()
Destructor.
QString subsetString
QgsLayerRestorer(const QList< QgsMapLayer *> &layers)
Constructor for QgsLayerRestorer.
virtual bool setSubsetString(const QString &subset)
Sets the string (typically sql) used to define a subset of the layer.
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 setOpacity(double opacity)
Sets the opacity for the vector layer, where opacity is a value between 0 (totally transparent) and 1...