QGIS API Documentation  3.6.0-Noosa (5873452)
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  QString style = layer->styleManager()->currentStyle();
33  settings.mNamedStyle = layer->styleManager()->currentStyle();
34 
35  // set a custom property allowing to keep in memory if a SLD file has
36  // been loaded for rendering
37  layer->setCustomProperty( "readSLD", false );
38 
39  QString errMsg;
40  QDomDocument sldDoc;
41  layer->exportSldStyle( sldDoc, errMsg );
42  ( void )settings.mSldStyle.setContent( sldDoc.toString(), true ); // for namespace processing
43 
44  switch ( layer->type() )
45  {
47  {
48  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
49 
50  if ( vLayer )
51  {
52  settings.mOpacity = vLayer->opacity();
53  settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
54  settings.mFilter = vLayer->subsetString();
55  }
56  break;
57  }
58  case QgsMapLayer::LayerType::RasterLayer:
59  {
60  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
61 
62  if ( rLayer )
63  {
64  settings.mOpacity = rLayer->renderer()->opacity();
65  }
66  break;
67  }
68 
71  break;
72  }
73 
74  mLayerSettings[layer] = settings;
75  }
76 }
77 
79 {
80  for ( QgsMapLayer *layer : mLayerSettings.keys() )
81  {
82  QgsLayerSettings settings = mLayerSettings[layer];
83  layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
84  layer->setName( mLayerSettings[layer].name );
85 
86  // if a SLD file has been loaded for rendering, we restore the previous one
87  QString errMsg;
88  QDomElement root = settings.mSldStyle.firstChildElement( "StyledLayerDescriptor" );
89  QDomElement el = root.firstChildElement( "NamedLayer" );
90  if ( layer->customProperty( "readSLD", false ).toBool() )
91  {
92  layer->readSld( el, errMsg );
93  }
94  layer->removeCustomProperty( "readSLD" );
95 
96  switch ( layer->type() )
97  {
98  case QgsMapLayer::LayerType::VectorLayer:
99  {
100  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
101 
102  if ( vLayer )
103  {
104  vLayer->setOpacity( settings.mOpacity );
105  vLayer->selectByIds( settings.mSelectedFeatureIds );
106  vLayer->setSubsetString( settings.mFilter );
107  }
108  break;
109  }
110  case QgsMapLayer::LayerType::RasterLayer:
111  {
112  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
113 
114  if ( rLayer )
115  {
116  rLayer->renderer()->setOpacity( settings.mOpacity );
117  }
118  break;
119  }
120 
123  break;
124  }
125  }
126 }
Base class for all map layer types.
Definition: qgsmaplayer.h:64
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)
Select matching features using a list of feature IDs.
QgsRasterRenderer * renderer() const
Added in 3.2.
Definition: qgsmaplayer.h:111
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)
Set 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...