Skip to content
Permalink
Browse files

[processing] Fix destination layer name ignored in Load Layer into Pr…

…oject

algorithm

Fixes #38046
  • Loading branch information
nyalldawson committed Jul 31, 2020
1 parent 7087d5d commit b9f1f2ca32635f8fe926c0640c092a324082100b
@@ -206,6 +206,8 @@ Default constructor

QString name;

bool forceName;

QString outputName;

QgsProcessingUtils::LayerHint layerTypeHint;
@@ -78,7 +78,9 @@ QVariantMap QgsLoadLayerAlgorithm::processAlgorithm( const QVariantMap &paramete
throw QgsProcessingException( QObject::tr( "Invalid (empty) layer name" ) );

layer->setName( name );
context.addLayerToLoadOnCompletion( layer->id(), QgsProcessingContext::LayerDetails( name, context.project(), name ) );
QgsProcessingContext::LayerDetails details( name, context.project(), name );
details.forceName = true;
context.addLayerToLoadOnCompletion( layer->id(), details );

QVariantMap results;
results.insert( QStringLiteral( "OUTPUT" ), layer->id() );
@@ -161,7 +161,7 @@ void QgsProcessingContext::LayerDetails::setOutputLayerName( QgsMapLayer *layer
const bool preferFilenameAsLayerName = QgsSettings().value( QStringLiteral( "Processing/Configuration/PREFER_FILENAME_AS_LAYER_NAME" ), true ).toBool();

// note - for temporary layers, we don't use the filename, regardless of user setting (it will be meaningless!)
if ( ( preferFilenameAsLayerName && !layer->isTemporary() ) || name.isEmpty() )
if ( ( !forceName && preferFilenameAsLayerName && !layer->isTemporary() ) || name.isEmpty() )
{
const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );
const QString layerName = sourceParts.value( QStringLiteral( "layerName" ) ).toString();
@@ -251,6 +251,13 @@ class CORE_EXPORT QgsProcessingContext
*/
QString name;

/**
* Set to TRUE if LayerDetails::name should always be used as the loaded layer name, regardless
* of the user's local Processing settings.
* \since QGIS 3.16
*/
bool forceName = false;

/**
* Associated output name from algorithm which generated the layer.
*/
@@ -2275,6 +2275,14 @@ void TestQgsProcessing::parameters()
vl = qgis::make_unique< QgsVectorLayer >( QStringLiteral( TEST_DATA_DIR ) + "/points_gpkg.gpkg|layername=points_small", QString() );
context2.layersToLoadOnCompletion().values().at( 0 ).setOutputLayerName( vl.get() );
QCOMPARE( vl->name(), QStringLiteral( "points_small" ) );
// if forced name is true, that should always be used, regardless of the user's local setting
QgsProcessingContext::LayerDetails details( QStringLiteral( "my name" ), context2.project(), QStringLiteral( "my name" ) );
details.forceName = false;
details.setOutputLayerName( vl.get() );
QCOMPARE( vl->name(), QStringLiteral( "points_small" ) );
details.forceName = true;
details.setOutputLayerName( vl.get() );
QCOMPARE( vl->name(), QStringLiteral( "my name" ) );
}

void TestQgsProcessing::algorithmParameters()

0 comments on commit b9f1f2c

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