Skip to content

Commit

Permalink
[processing] Fix destination layer name ignored in Load Layer into Pr…
Browse files Browse the repository at this point in the history
…oject

algorithm

Fixes #38046
  • Loading branch information
nyalldawson committed Jul 31, 2020
1 parent 7087d5d commit b9f1f2c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
Expand Up @@ -206,6 +206,8 @@ Default constructor

QString name;

bool forceName;

QString outputName;

QgsProcessingUtils::LayerHint layerTypeHint;
Expand Down
4 changes: 3 additions & 1 deletion src/analysis/processing/qgsalgorithmloadlayer.cpp
Expand Up @@ -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() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingcontext.cpp
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/core/processing/qgsprocessingcontext.h
Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit b9f1f2c

Please sign in to comment.