Skip to content

Commit

Permalink
QgsProcessingAlgorithm::addParameter(): add a variant that accepts a …
Browse files Browse the repository at this point in the history
…unique_ptr for QgsProcessingParameterDefinition
  • Loading branch information
rouault authored and nyalldawson committed Dec 9, 2024
1 parent d865df1 commit a389b1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ occur.
.. seealso:: :py:func:`addOutput`
%End


void removeParameter( const QString &name ) /HoldGIL/;
%Docstring
Removes the parameter with matching ``name`` from the algorithm, and deletes any existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ occur.
.. seealso:: :py:func:`addOutput`
%End


void removeParameter( const QString &name ) /HoldGIL/;
%Docstring
Removes the parameter with matching ``name`` from the algorithm, and deletes any existing
Expand Down
17 changes: 11 additions & 6 deletions src/core/processing/qgsprocessingalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ QVariantMap QgsProcessingAlgorithm::asMap( const QVariantMap &parameters, QgsPro
}

bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *definition, bool createOutput )
{
return addParameter( std::unique_ptr<QgsProcessingParameterDefinition>( definition ), createOutput );
}

bool QgsProcessingAlgorithm::addParameter( std::unique_ptr<QgsProcessingParameterDefinition> definition, bool createOutput )
{
if ( !definition )
return false;
Expand All @@ -400,22 +405,22 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names
{
QgsMessageLog::logMessage( QObject::tr( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
delete definition;
return false;
}

if ( definition->isDestination() && mProvider )
{
QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition );
QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition.get() );
if ( !mProvider->supportsNonFileBasedOutput() )
destParam->setSupportsNonFileBasedOutput( false );
}

mParameters << definition;
definition->mAlgorithm = this;
mParameters << definition.release();
const QgsProcessingParameterDefinition *definitionRawPtr = mParameters.back();

if ( createOutput )
return createAutoOutputForParameter( definition );
return createAutoOutputForParameter( definitionRawPtr );
else
return true;
}
Expand Down Expand Up @@ -1043,12 +1048,12 @@ bool QgsProcessingAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) cons
}


bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
bool QgsProcessingAlgorithm::createAutoOutputForParameter( const QgsProcessingParameterDefinition *parameter )
{
if ( !parameter->isDestination() )
return true; // nothing created, but nothing went wrong - so return true

QgsProcessingDestinationParameter *dest = static_cast< QgsProcessingDestinationParameter * >( parameter );
const QgsProcessingDestinationParameter *dest = static_cast< const QgsProcessingDestinationParameter * >( parameter );
QgsProcessingOutputDefinition *output( dest->toOutputDefinition() );
if ( !output )
return true; // nothing created - but nothing went wrong - so return true
Expand Down
8 changes: 7 additions & 1 deletion src/core/processing/qgsprocessingalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true ) SIP_HOLDGIL;

/**
* Same as above addParameter(QgsProcessingParameterDefinition*, bool), but using
* a smart pointer for safer use.
*/
bool addParameter( std::unique_ptr<QgsProcessingParameterDefinition> parameterDefinition, bool createOutput = true ) SIP_SKIP;

/**
* Removes the parameter with matching \a name from the algorithm, and deletes any existing
* definition.
Expand Down Expand Up @@ -1110,7 +1116,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
bool mHasPostProcessed = false;
std::unique_ptr< QgsProcessingContext > mLocalContext;

bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
bool createAutoOutputForParameter( const QgsProcessingParameterDefinition *parameter );


friend class QgsProcessingProvider;
Expand Down

0 comments on commit a389b1a

Please sign in to comment.