Skip to content
Permalink
Browse files

Fix destination layer name ignored in Load Layer into Project algorithm

  • Loading branch information
github-actions authored and nyalldawson committed Jul 31, 2020
1 parent 2d236a8 commit 16719641df5a0fbd7ae2dae5e0a0cd19607780e2
@@ -138,6 +138,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() );
@@ -133,7 +133,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();
@@ -180,6 +180,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.
*/
@@ -2255,6 +2255,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 1671964

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