Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main annotation layer should be used as a valid option even if type hint is set to UnknownType (fix #49010) #60341

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Normalizes a layer ``source`` string for safe comparison across different
operating system environments.
%End

static QString layerToStringIdentifier( const QgsMapLayer *layer ) /HoldGIL/;
static QString layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName = QString() ) /HoldGIL/;
%Docstring
Returns a string representation of the source for a ``layer``. The returned
value is suitable for storage for subsequent executions of an algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Normalizes a layer ``source`` string for safe comparison across different
operating system environments.
%End

static QString layerToStringIdentifier( const QgsMapLayer *layer ) /HoldGIL/;
static QString layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName = QString() ) /HoldGIL/;
%Docstring
Returns a string representation of the source for a ``layer``. The returned
value is suitable for storage for subsequent executions of an algorithm
Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2704,7 +2704,7 @@ QVariant QgsProcessingParameterDefinition::valueAsJsonObjectPrivate( const QVari
p.insert( name(), value );
if ( QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context ) )
{
return QgsProcessingUtils::layerToStringIdentifier( layer );
return QgsProcessingUtils::layerToStringIdentifier( layer, value.toString() );
}
}

Expand Down Expand Up @@ -2882,7 +2882,7 @@ QString QgsProcessingParameterDefinition::valueAsStringPrivate( const QVariant &
p.insert( name(), value );
if ( QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context ) )
{
return QgsProcessingUtils::layerToStringIdentifier( layer );
return QgsProcessingUtils::layerToStringIdentifier( layer, value.toString() );
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/core/processing/qgsprocessingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
return nullptr;

// prefer project layers
if ( context.project() && typeHint == LayerHint::Annotation && string.compare( QLatin1String( "main" ), Qt::CaseInsensitive ) == 0 )
if ( context.project() && ( typeHint == LayerHint::Annotation ) && string.compare( QLatin1String( "main" ), Qt::CaseInsensitive ) == 0 )
return context.project()->mainAnnotationLayer();

QgsMapLayer *layer = nullptr;
Expand All @@ -531,6 +531,11 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
if ( layer )
return layer;

// check main annotation layer
if ( context.project() && ( typeHint == LayerHint::UnknownType ) && string.compare( QLatin1String( "main" ), Qt::CaseInsensitive ) == 0 )
return context.project()->mainAnnotationLayer();


if ( !allowLoadingNewLayers )
return nullptr;

Expand Down Expand Up @@ -743,7 +748,7 @@ QString QgsProcessingUtils::normalizeLayerSource( const QString &source )
return normalized.trimmed();
}

QString QgsProcessingUtils::layerToStringIdentifier( const QgsMapLayer *layer )
QString QgsProcessingUtils::layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName )
{
if ( !layer )
return QString();
Expand All @@ -761,6 +766,12 @@ QString QgsProcessingUtils::layerToStringIdentifier( const QgsMapLayer *layer )

return QStringLiteral( "%1://%2" ).arg( provider, source );
}

if ( layer->type() == Qgis::LayerType::Annotation && layerName.compare( QLatin1String( "main" ), Qt::CaseInsensitive ) == 0 )
{
return layerName;
}

return layer->id();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class CORE_EXPORT QgsProcessingUtils
*
* \since QGIS 3.34
*/
static QString layerToStringIdentifier( const QgsMapLayer *layer ) SIP_HOLDGIL;
static QString layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName = QString() ) SIP_HOLDGIL;

/**
* Converts a variant to a Python literal.
Expand Down