QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "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  QString style = layer->styleManager()->currentStyle();
35  settings.mNamedStyle = layer->styleManager()->currentStyle();
36 
37  // set a custom property allowing to keep in memory if a SLD file has
38  // been loaded for rendering
39  layer->setCustomProperty( "readSLD", false );
40 
41  QString errMsg;
42  QDomDocument styleDoc( QStringLiteral( "style" ) );
43  QDomElement styleXml = styleDoc.createElement( QStringLiteral( "style" ) );
44  styleDoc.appendChild( styleXml );
45  if ( !layer->writeStyle( styleXml, styleDoc, errMsg, QgsReadWriteContext() ) )
46  {
47  QgsMessageLog::logMessage( QStringLiteral( "QGIS Style has not been added to layer restorer for layer %1: %2" ).arg( layer->name(), errMsg ) );
48  }
49  ( void )settings.mQgisStyle.setContent( styleDoc.toString() );
50 
51  if ( layer->type() == QgsMapLayer::LayerType::VectorLayer )
52  {
53  QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );
54 
55  if ( vLayer )
56  {
57  settings.mOpacity = vLayer->opacity();
58  settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
59  settings.mFilter = vLayer->subsetString();
60  }
61  }
62  else if ( layer->type() == QgsMapLayer::LayerType::RasterLayer )
63  {
64  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
65 
66  if ( rLayer )
67  {
68  settings.mOpacity = rLayer->renderer()->opacity();
69  }
70  }
71 
72  mLayerSettings[layer] = settings;
73  }
74 }
75 
77 {
78  for ( QgsMapLayer *layer : mLayerSettings.keys() )
79  {
80  QgsLayerSettings settings = mLayerSettings[layer];
81  layer->styleManager()->setCurrentStyle( settings.mNamedStyle );
82  layer->setName( mLayerSettings[layer].name );
83 
84  // if a SLD file has been loaded for rendering, we restore the previous style
85  if ( layer->customProperty( "readSLD", false ).toBool() )
86  {
87  QString errMsg;
88  QDomElement root = settings.mQgisStyle.documentElement();
90  if ( !layer->readStyle( root, errMsg, context ) )
91  {
92  QgsMessageLog::logMessage( QStringLiteral( "QGIS Style has not been read from layer restorer for layer %1: %2" ).arg( layer->name(), errMsg ) );
93  }
94  }
95  layer->removeCustomProperty( "readSLD" );
96 
97  if ( layer->type() == QgsMapLayer::LayerType::VectorLayer )
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  }
108  else if ( layer->type() == QgsMapLayer::LayerType::RasterLayer )
109  {
110  QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
111 
112  if ( rLayer )
113  {
114  rLayer->renderer()->setOpacity( settings.mOpacity );
115  }
116  }
117  }
118 }
The class is used as a container of context for various read/write operations on other objects...
Base class for all map layer types.
Definition: qgsmaplayer.h:63
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.
double opacity() const
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1...
QgsLayerRestorer(const QList< QgsMapLayer * > &layers)
Constructor for QgsLayerRestorer.
QgsRasterRenderer * renderer() const
~QgsLayerRestorer()
Destructor.
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).
QString subsetString
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
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...