Allow child algorithm configuration to be stored and handled by models

This commit is contained in:
Nyall Dawson 2017-07-10 17:02:23 +10:00
parent 9e8a114553
commit 326d6f5fc7
8 changed files with 93 additions and 10 deletions

View File

@ -70,6 +70,31 @@ class QgsProcessingModelChildAlgorithm : QgsProcessingModelComponent
should be set to an existing QgsProcessingAlgorithm algorithm ID.
.. seealso:: algorithm()
.. seealso:: algorithmId()
%End
QVariantMap configuration() const;
%Docstring
Returns the child algorithm's configuration map.
This map specifies configuration settings which are passed
to the algorithm, allowing it to dynamically adjust its initialized parameters
and outputs according to this configuration. This allows child algorithms in the model
to adjust their behavior at run time according to some user configuration.
.. seealso:: setConfiguration()
:rtype: QVariantMap
%End
void setConfiguration( const QVariantMap &configuration );
%Docstring
Sets the child algorithm's ``configuration`` map.
This map specifies configuration settings which are passed
to the algorithm, allowing it to dynamically adjust its initialized parameters
and outputs according to this configuration. This allows child algorithms in the model
to adjust their behavior at run time according to some user configuration.
.. seealso:: configuration()
%End
const QgsProcessingAlgorithm *algorithm() const;

View File

@ -89,10 +89,17 @@ class QgsProcessingRegistry : QObject
:rtype: QgsProcessingAlgorithm
%End
QgsProcessingAlgorithm *createAlgorithmById( const QString &id ) const /Factory/;
QgsProcessingAlgorithm *createAlgorithmById( const QString &id, const QVariantMap &configuration = QVariantMap() ) const /Factory/;
%Docstring
Creates a new instance of an algorithm by its ID. If no matching algorithm is found, a None
is returned. Callers take responsibility for deleting the returned object.`
is returned. Callers take responsibility for deleting the returned object.
The ``configuration`` argument allows passing of a map of configuration settings
to the algorithm, allowing it to dynamically adjust its initialized parameters
and outputs according to this configuration. This is generally used only for
algorithms in a model, allowing them to adjust their behavior at run time
according to some user configuration.
.. seealso:: algorithms()
.. seealso:: algorithmById()
:rtype: QgsProcessingAlgorithm

View File

@ -259,7 +259,7 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
childTime.start();
bool ok = false;
std::unique_ptr< QgsProcessingAlgorithm > childAlg( child.algorithm()->create() );
std::unique_ptr< QgsProcessingAlgorithm > childAlg( child.algorithm()->create( child.configuration() ) );
QVariantMap results = childAlg->run( childParams, context, feedback, &ok );
childAlg.reset( nullptr );
if ( !ok )

View File

@ -30,6 +30,7 @@ QgsProcessingModelChildAlgorithm::QgsProcessingModelChildAlgorithm( const QStrin
QgsProcessingModelChildAlgorithm::QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other )
: QgsProcessingModelComponent( other )
, mId( other.mId )
, mConfiguration( other.mConfiguration )
, mParams( other.mParams )
, mModelOutputs( other.mModelOutputs )
, mActive( other.mActive )
@ -44,6 +45,7 @@ QgsProcessingModelChildAlgorithm &QgsProcessingModelChildAlgorithm::operator=( c
{
QgsProcessingModelComponent::operator =( other );
mId = other.mId;
mConfiguration = other.mConfiguration;
setAlgorithmId( other.algorithmId() );
mParams = other.mParams;
mModelOutputs = other.mModelOutputs;
@ -76,6 +78,7 @@ QVariant QgsProcessingModelChildAlgorithm::toVariant() const
QVariantMap map;
map.insert( QStringLiteral( "id" ), mId );
map.insert( QStringLiteral( "alg_id" ), mAlgorithmId );
map.insert( QStringLiteral( "alg_config" ), mConfiguration );
map.insert( QStringLiteral( "active" ), mActive );
map.insert( QStringLiteral( "dependencies" ), mDependencies );
map.insert( QStringLiteral( "parameters_collapsed" ), mParametersCollapsed );
@ -112,6 +115,7 @@ bool QgsProcessingModelChildAlgorithm::loadVariant( const QVariant &child )
QVariantMap map = child.toMap();
mId = map.value( QStringLiteral( "id" ) ).toString();
mConfiguration = map.value( QStringLiteral( "alg_config" ) ).toMap();
setAlgorithmId( map.value( QStringLiteral( "alg_id" ) ).toString() );
mActive = map.value( QStringLiteral( "active" ) ).toBool();
mDependencies = map.value( QStringLiteral( "dependencies" ) ).toStringList();
@ -186,9 +190,16 @@ QString QgsProcessingModelChildAlgorithm::asPythonCode() const
return lines.join( '\n' );
}
QVariantMap QgsProcessingModelChildAlgorithm::configuration() const
{
return mConfiguration;
}
void QgsProcessingModelChildAlgorithm::setConfiguration( const QVariantMap &configuration )
{
mConfiguration = configuration;
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId, mConfiguration ) );
}
void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModelAlgorithm &model )
{
@ -207,7 +218,7 @@ void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModel
void QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId )
{
mAlgorithmId = algorithmId;
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId ) );
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId, mConfiguration ) );
}
///@endcond

View File

@ -87,6 +87,30 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
*/
void setAlgorithmId( const QString &algorithmId );
/**
* Returns the child algorithm's configuration map.
*
* This map specifies configuration settings which are passed
* to the algorithm, allowing it to dynamically adjust its initialized parameters
* and outputs according to this configuration. This allows child algorithms in the model
* to adjust their behavior at run time according to some user configuration.
*
* \see setConfiguration()
*/
QVariantMap configuration() const;
/**
* Sets the child algorithm's \a configuration map.
*
* This map specifies configuration settings which are passed
* to the algorithm, allowing it to dynamically adjust its initialized parameters
* and outputs according to this configuration. This allows child algorithms in the model
* to adjust their behavior at run time according to some user configuration.
*
* \see configuration()
*/
void setConfiguration( const QVariantMap &configuration );
/**
* Returns the underlying child algorithm, or a nullptr
* if a matching algorithm is not available.
@ -237,6 +261,8 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
QString mAlgorithmId;
std::unique_ptr< QgsProcessingAlgorithm > mAlgorithm;
QVariantMap mConfiguration;
//! A map of parameter sources. Keys are algorithm parameter names.
QMap< QString, QgsProcessingModelChildParameterSources > mParams;

View File

@ -98,13 +98,13 @@ const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QStrin
return nullptr;
}
QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id ) const
QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
{
const QgsProcessingAlgorithm *alg = algorithmById( id );
if ( !alg )
return nullptr;
std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create() );
std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
return creation.release();
}

View File

@ -102,11 +102,18 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject
/**
* Creates a new instance of an algorithm by its ID. If no matching algorithm is found, a nullptr
* is returned. Callers take responsibility for deleting the returned object.`
* is returned. Callers take responsibility for deleting the returned object.
*
* The \a configuration argument allows passing of a map of configuration settings
* to the algorithm, allowing it to dynamically adjust its initialized parameters
* and outputs according to this configuration. This is generally used only for
* algorithms in a model, allowing them to adjust their behavior at run time
* according to some user configuration.
*
* \see algorithms()
* \see algorithmById()
*/
QgsProcessingAlgorithm *createAlgorithmById( const QString &id ) const SIP_FACTORY;
QgsProcessingAlgorithm *createAlgorithmById( const QString &id, const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
signals:

View File

@ -4284,6 +4284,11 @@ void TestQgsProcessing::modelerAlgorithm()
child.setAlgorithmId( QStringLiteral( "native:centroids" ) );
QVERIFY( child.algorithm() );
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );
QVariantMap myConfig;
myConfig.insert( QStringLiteral( "some_key" ), 11 );
child.setConfiguration( myConfig );
QCOMPARE( child.configuration(), myConfig );
child.setDescription( QStringLiteral( "desc" ) );
QCOMPARE( child.description(), QStringLiteral( "desc" ) );
QVERIFY( child.isActive() );
@ -4593,6 +4598,7 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelChildAlgorithm alg5c1;
alg5c1.setChildId( "cx1" );
alg5c1.setAlgorithmId( "buffer" );
alg5c1.setConfiguration( myConfig );
alg5c1.addParameterSources( "x", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromModelParameter( "p1" ) );
alg5c1.addParameterSources( "y", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromChildOutput( "cx2", "out3" ) );
alg5c1.addParameterSources( "z", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromStaticValue( 5 ) );
@ -4639,6 +4645,7 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelChildAlgorithm alg6c1 = alg6.childAlgorithm( "cx1" );
QCOMPARE( alg6c1.childId(), QStringLiteral( "cx1" ) );
QCOMPARE( alg6c1.algorithmId(), QStringLiteral( "buffer" ) );
QCOMPARE( alg6c1.configuration(), myConfig );
QVERIFY( alg6c1.isActive() );
QVERIFY( alg6c1.outputsCollapsed() );
QVERIFY( alg6c1.parametersCollapsed() );