Skip to content

Commit

Permalink
allow to save metadata as default in copy and apply metadata algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Nov 11, 2024
1 parent 4593987 commit 8af31a6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/analysis/processing/qgsmetadataalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ 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" ) ) );
}

bool QgsCopyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
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" ) );
Expand All @@ -76,6 +78,15 @@ bool QgsCopyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &paramet

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;
}

Expand Down Expand Up @@ -130,13 +141,15 @@ 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" ) ) );
}

bool QgsApplyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
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" ) );
Expand All @@ -150,6 +163,16 @@ bool QgsApplyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parame
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;
}

Expand Down

0 comments on commit 8af31a6

Please sign in to comment.