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

QgsProcessingAlgorithm: add variants of addParameter() and addOutput() that accept a std::unique_ptr #59512

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 @@ -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 All @@ -541,6 +542,7 @@ See the notes in :py:func:`~QgsProcessingAlgorithm.addParameter` for a descripti
.. seealso:: :py:func:`initAlgorithm`
%End


virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) /VirtualErrorHandler=processing_exception_handler/;
%Docstring
Prepares the algorithm to run using the specified ``parameters``. Algorithms should implement
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 All @@ -541,6 +542,7 @@ See the notes in :py:func:`~QgsProcessingAlgorithm.addParameter` for a descripti
.. seealso:: :py:func:`initAlgorithm`
%End


virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException ) /VirtualErrorHandler=processing_exception_handler/;
%Docstring
Prepares the algorithm to run using the specified ``parameters``. Algorithms should implement
Expand Down
30 changes: 16 additions & 14 deletions src/analysis/processing/qgsalgorithmpointstopaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,29 @@ QString QgsPointsToPathsAlgorithm::groupId() const

void QgsPointsToPathsAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint ) ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "CLOSE_PATH" ), QObject::tr( "Create closed paths" ), false, true ) );
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "ORDER_EXPRESSION" ), QObject::tr( "Order expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "NATURAL_SORT" ), QObject::tr( "Sort text containing numbers naturally" ), false, true ) );
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "GROUP_EXPRESSION" ), QObject::tr( "Path group expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Paths" ), Qgis::ProcessingSourceType::VectorLine ) );
addParameter( std::make_unique<QgsProcessingParameterFeatureSource>( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint ) ) );
addParameter( std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral( "CLOSE_PATH" ), QObject::tr( "Create closed paths" ), false, true ) );
addParameter( std::make_unique<QgsProcessingParameterExpression>( QStringLiteral( "ORDER_EXPRESSION" ), QObject::tr( "Order expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
addParameter( std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral( "NATURAL_SORT" ), QObject::tr( "Sort text containing numbers naturally" ), false, true ) );
addParameter( std::make_unique<QgsProcessingParameterExpression>( QStringLiteral( "GROUP_EXPRESSION" ), QObject::tr( "Path group expression" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
addParameter( std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral( "OUTPUT" ), QObject::tr( "Paths" ), Qgis::ProcessingSourceType::VectorLine ) );
// TODO QGIS 4: remove parameter. move logic to separate algorithm if needed.
addParameter( new QgsProcessingParameterFolderDestination( QStringLiteral( "OUTPUT_TEXT_DIR" ), QObject::tr( "Directory for text output" ), QVariant(), true, false ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NUM_PATHS" ), QObject::tr( "Number of paths" ) ) );
addParameter( std::make_unique<QgsProcessingParameterFolderDestination>( QStringLiteral( "OUTPUT_TEXT_DIR" ), QObject::tr( "Directory for text output" ), QVariant(), true, false ) );
addOutput( std::make_unique<QgsProcessingOutputNumber>( QStringLiteral( "NUM_PATHS" ), QObject::tr( "Number of paths" ) ) );

// backwards compatibility parameters
// TODO QGIS 4: remove compatibility parameters and their logic
QgsProcessingParameterField *orderField = new QgsProcessingParameterField( QStringLiteral( "ORDER_FIELD" ), QObject::tr( "Order field" ), QVariant(), QString(), Qgis::ProcessingFieldParameterDataType::Any, false, true );
auto orderField = std::make_unique<QgsProcessingParameterField>( QStringLiteral( "ORDER_FIELD" ), QObject::tr( "Order field" ), QVariant(), QString(), Qgis::ProcessingFieldParameterDataType::Any, false, true );
orderField->setFlags( orderField->flags() | Qgis::ProcessingParameterFlag::Hidden );
addParameter( orderField );
QgsProcessingParameterField *groupField = new QgsProcessingParameterField( QStringLiteral( "GROUP_FIELD" ), QObject::tr( "Group field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, false, true );
addParameter( std::move( orderField ) );

auto groupField = std::make_unique<QgsProcessingParameterField>( QStringLiteral( "GROUP_FIELD" ), QObject::tr( "Group field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, false, true );
groupField->setFlags( groupField->flags() | Qgis::ProcessingParameterFlag::Hidden );
addParameter( groupField );
QgsProcessingParameterString *dateFormat = new QgsProcessingParameterString( QStringLiteral( "DATE_FORMAT" ), QObject::tr( "Date format (if order field is DateTime)" ), QVariant(), false, true );
addParameter( std::move( groupField ) );

auto dateFormat = std::make_unique<QgsProcessingParameterString>( QStringLiteral( "DATE_FORMAT" ), QObject::tr( "Date format (if order field is DateTime)" ), QVariant(), false, true );
dateFormat->setFlags( dateFormat->flags() | Qgis::ProcessingParameterFlag::Hidden );
addParameter( dateFormat );
addParameter( std::move( dateFormat ) );
}

QgsPointsToPathsAlgorithm *QgsPointsToPathsAlgorithm::createInstance() const
Expand Down
25 changes: 17 additions & 8 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 All @@ -439,6 +444,11 @@ void QgsProcessingAlgorithm::removeParameter( const QString &name )
}

bool QgsProcessingAlgorithm::addOutput( QgsProcessingOutputDefinition *definition )
{
return addOutput( std::unique_ptr<QgsProcessingOutputDefinition>( definition ) );
}

bool QgsProcessingAlgorithm::addOutput( std::unique_ptr<QgsProcessingOutputDefinition> definition )
{
if ( !definition )
return false;
Expand All @@ -447,11 +457,10 @@ bool QgsProcessingAlgorithm::addOutput( QgsProcessingOutputDefinition *definitio
if ( QgsProcessingAlgorithm::outputDefinition( definition->name() ) )
{
QgsMessageLog::logMessage( QObject::tr( "Duplicate output %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
delete definition;
return false;
}

mOutputs << definition;
mOutputs << definition.release();
return true;
}

Expand Down Expand Up @@ -1043,12 +1052,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
14 changes: 13 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 All @@ -548,6 +554,12 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER ) SIP_HOLDGIL;

/**
* Same as above addOutput(QgsProcessingOutputDefinition*), but using
* a smart pointer for safer use.
*/
bool addOutput( std::unique_ptr<QgsProcessingOutputDefinition> outputDefinition ) SIP_SKIP;

/**
* Prepares the algorithm to run using the specified \a parameters. Algorithms should implement
* their logic for evaluating parameter values here. The evaluated parameter results should
Expand Down Expand Up @@ -1110,7 +1122,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
Loading