From a174cbdfd0152b60e711330b42b5ddd2387f2b43 Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Wed, 22 Jan 2025 10:01:55 +0000 Subject: [PATCH] use last used WMS image encoding when adding WMS layers from Browser dock (fix #57666) --- src/providers/wms/qgswmsdataitems.cpp | 28 ++++++++++++++++++------ src/providers/wms/qgswmssourceselect.cpp | 11 ++++++++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/providers/wms/qgswmsdataitems.cpp b/src/providers/wms/qgswmsdataitems.cpp index 63e88ef856a6..22ba17f9cc3f 100644 --- a/src/providers/wms/qgswmsdataitems.cpp +++ b/src/providers/wms/qgswmsdataitems.cpp @@ -19,6 +19,7 @@ #include "qgslogger.h" #include "qgsdatasourceuri.h" +#include "qgssettings.h" #include "qgswmscapabilities.h" #include "qgswmsconnection.h" #include "qgsxyzconnection.h" @@ -428,15 +429,28 @@ QString QgsWMSItemBase::createUri( bool withStyle ) } QString format; - // get first supported by qt and server - QVector formats( QgsWmsProvider::supportedFormats() ); - const auto constFormats = formats; - for ( const QgsWmsSupportedFormat &f : constFormats ) + bool first = true; + const QString defaultEncoding = QgsSettings().value( QStringLiteral( "qgis/lastWmsImageEncoding" ), "image/png" ).toString(); + const QVector formats( QgsWmsProvider::supportedFormats() ); + QStringList supportedFormats; + supportedFormats.reserve( formats.size() ); + for ( const QgsWmsSupportedFormat &f : formats ) { - if ( mCapabilitiesProperty.capability.request.getMap.format.indexOf( f.format ) >= 0 ) + supportedFormats.append( f.format ); + } + + for ( const QString &f : mCapabilitiesProperty.capability.request.getMap.format ) + { + if ( !supportedFormats.contains( f ) ) { - format = f.format; - break; + QgsDebugError( QStringLiteral( "encoding %1 not supported." ).arg( f ) ); + continue; + } + + if ( first || f == defaultEncoding ) + { + format = f; + first = false; } } mDataSourceUri.setParam( QStringLiteral( "format" ), format ); diff --git a/src/providers/wms/qgswmssourceselect.cpp b/src/providers/wms/qgswmssourceselect.cpp index ef43ed890e54..81bed8c9588f 100644 --- a/src/providers/wms/qgswmssourceselect.cpp +++ b/src/providers/wms/qgswmssourceselect.cpp @@ -299,9 +299,10 @@ bool QgsWMSSourceSelect::populateLayerList( const QgsWmsCapabilities &capabiliti const QVector layers = capabilities.supportedLayers(); mLayerProperties = layers; - QString defaultEncoding = QgsSettings().value( "qgis/WMSDefaultFormat", "" ).toString(); + QString defaultEncoding = QgsSettings().value( QStringLiteral( "qgis/lastWmsImageEncoding" ), "image/png" ).toString(); bool first = true; + bool found = false; QSet alreadyAddedLabels; const auto supportedImageEncodings = capabilities.supportedImageEncodings(); for ( const QString &encoding : supportedImageEncodings ) @@ -323,7 +324,11 @@ bool QgsWMSSourceSelect::populateLayerList( const QgsWmsCapabilities &capabiliti mImageFormatGroup->button( id )->setVisible( true ); if ( first || encoding == defaultEncoding ) { - mImageFormatGroup->button( id )->setChecked( true ); + if ( !found ) + { + mImageFormatGroup->button( id )->setChecked( true ); + found = true; + } first = false; } } @@ -611,6 +616,8 @@ void QgsWMSSourceSelect::addButtonClicked() uri.setParam( QStringLiteral( "crs" ), crs ); QgsDebugMsgLevel( QStringLiteral( "crs=%2 " ).arg( crs ), 2 ); + QgsSettings().setValue( QStringLiteral( "/qgis/lastWmsImageEncoding" ), format ); + // Remove in case the default value from the connection settings // is being overridden here uri.removeParam( QStringLiteral( "featureCount" ) );