From 2ac6d39ae84913ed461696626a96425f6dc3f98b Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Mon, 11 Nov 2024 20:07:13 +0000 Subject: [PATCH] allow to save metadata as default in copy and apply metadata algorithms --- .../processing/qgsmetadataalgorithms.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/analysis/processing/qgsmetadataalgorithms.cpp b/src/analysis/processing/qgsmetadataalgorithms.cpp index 14fb4c677654..94cc78a96f02 100644 --- a/src/analysis/processing/qgsmetadataalgorithms.cpp +++ b/src/analysis/processing/qgsmetadataalgorithms.cpp @@ -58,6 +58,7 @@ void QgsCopyLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & ) { addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Source layer" ) ) ); addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "TARGET" ), QObject::tr( "Target layer" ) ) ); + addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DEFAULT" ), QObject::tr( "Save metadata as default" ), false ) ); addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated layer" ) ) ); } @@ -65,6 +66,7 @@ bool QgsCopyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap ¶met { QgsMapLayer *inputLayer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ); QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context ); + const bool saveAsDefault = parameterAsBool( parameters, QStringLiteral( "DEFAULT" ), context ); if ( !inputLayer ) throw QgsProcessingException( QObject::tr( "Invalid input layer" ) ); @@ -76,6 +78,15 @@ bool QgsCopyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap ¶met QgsLayerMetadata *metadata = inputLayer->metadata().clone(); targetLayer->setMetadata( *metadata ); + if ( saveAsDefault ) + { + bool ok; + targetLayer->saveDefaultMetadata( ok ); + if ( !ok ) + { + throw QgsProcessingException( QObject::tr( "Failed to save metadata as default metadata." ) ); + } + } return true; } @@ -130,6 +141,7 @@ void QgsApplyLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & ) { addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) ); addParameter( new QgsProcessingParameterFile( QStringLiteral( "METADATA" ), QObject::tr( "Metadata file" ), Qgis::ProcessingFileParameterBehavior::File, QStringLiteral( "qmd" ) ) ); + addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DEFAULT" ), QObject::tr( "Save metadata as default" ), false ) ); addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) ); } @@ -137,6 +149,7 @@ bool QgsApplyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap ¶me { QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ); const QString metadata = parameterAsFile( parameters, QStringLiteral( "METADATA" ), context ); + const bool saveAsDefault = parameterAsBool( parameters, QStringLiteral( "DEFAULT" ), context ); if ( !layer ) throw QgsProcessingException( QObject::tr( "Invalid input layer" ) ); @@ -150,6 +163,16 @@ bool QgsApplyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap ¶me throw QgsProcessingException( QObject::tr( "Failed to apply metadata. Error: %1" ).arg( msg ) ); } + if ( saveAsDefault ) + { + bool ok; + layer->saveDefaultMetadata( ok ); + if ( !ok ) + { + throw QgsProcessingException( QObject::tr( "Failed to save metadata as default metadata." ) ); + } + } + return true; }