Skip to content
Permalink
Browse files

Merge pull request #38475 from rldhont/server-restorer-sld-style-enha…

…ncement

Server: Enhance WMS Layer style restorer for SLD
  • Loading branch information
rldhont committed Sep 4, 2020
2 parents 8ece6ed + 26ab9f7 commit 0eee749c7e0e651e30c629f77063b86e4d049260
@@ -3162,8 +3162,17 @@ namespace QgsWms
void QgsRenderer::setLayerSld( QgsMapLayer *layer, const QDomElement &sld ) const
{
QString err;
// Defined sld style name
const QStringList styles = layer->styleManager()->styles();
QString sldStyleName = "__sld_style";
while ( styles.contains( sldStyleName ) )
{
sldStyleName.append( '@' );
}
layer->styleManager()->addStyleFromLayer( sldStyleName );
layer->styleManager()->setCurrentStyle( sldStyleName );
layer->readSld( sld, err );
layer->setCustomProperty( "readSLD", true );
layer->setCustomProperty( "sldStyleName", sldStyleName );
}

QgsLegendSettings QgsRenderer::legendSettings() const
@@ -33,20 +33,6 @@ QgsLayerRestorer::QgsLayerRestorer( const QList<QgsMapLayer *> &layers )

settings.mNamedStyle = layer->styleManager()->currentStyle();

// set a custom property allowing to keep in memory if a SLD file has
// been loaded for rendering
layer->setCustomProperty( "readSLD", false );

QString errMsg;
QDomDocument styleDoc( QStringLiteral( "style" ) );
QDomElement styleXml = styleDoc.createElement( QStringLiteral( "style" ) );
styleDoc.appendChild( styleXml );
if ( !layer->writeStyle( styleXml, styleDoc, errMsg, QgsReadWriteContext() ) )
{
QgsMessageLog::logMessage( QStringLiteral( "QGIS Style has not been added to layer restorer for layer %1: %2" ).arg( layer->name(), errMsg ) );
}
( void )settings.mQgisStyle.setContent( styleDoc.toString() );

switch ( layer->type() )
{
case QgsMapLayerType::VectorLayer:
@@ -92,17 +78,12 @@ QgsLayerRestorer::~QgsLayerRestorer()
layer->setName( mLayerSettings[layer].name );

// if a SLD file has been loaded for rendering, we restore the previous style
if ( layer->customProperty( "readSLD", false ).toBool() )
const QString sldStyleName { layer->customProperty( "sldStyleName", "" ).toString() };
if ( !sldStyleName.isEmpty() )
{
QString errMsg;
QDomElement root = settings.mQgisStyle.documentElement();
QgsReadWriteContext context = QgsReadWriteContext();
if ( !layer->readStyle( root, errMsg, context ) )
{
QgsMessageLog::logMessage( QStringLiteral( "QGIS Style has not been read from layer restorer for layer %1: %2" ).arg( layer->name(), errMsg ) );
}
layer->styleManager()->removeStyle( sldStyleName );
layer->removeCustomProperty( "sldStyleName" );
}
layer->removeCustomProperty( "readSLD" );

switch ( layer->type() )
{
@@ -57,7 +57,6 @@ class QgsLayerRestorer
QString name;
double mOpacity;
QString mNamedStyle;
QDomDocument mQgisStyle;
QString mFilter;
QgsFeatureIds mSelectedFeatureIds;
};

0 comments on commit 0eee749

Please sign in to comment.
You can’t perform that action at this time.