Skip to content

Commit 326d6f5

Browse files
committedJul 10, 2017
Allow child algorithm configuration to be stored and handled by models
1 parent 9e8a114 commit 326d6f5

File tree

8 files changed

+93
-10
lines changed

8 files changed

+93
-10
lines changed
 

‎python/core/processing/models/qgsprocessingmodelchildalgorithm.sip

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ class QgsProcessingModelChildAlgorithm : QgsProcessingModelComponent
7070
should be set to an existing QgsProcessingAlgorithm algorithm ID.
7171
.. seealso:: algorithm()
7272
.. seealso:: algorithmId()
73+
%End
74+
75+
QVariantMap configuration() const;
76+
%Docstring
77+
Returns the child algorithm's configuration map.
78+
79+
This map specifies configuration settings which are passed
80+
to the algorithm, allowing it to dynamically adjust its initialized parameters
81+
and outputs according to this configuration. This allows child algorithms in the model
82+
to adjust their behavior at run time according to some user configuration.
83+
84+
.. seealso:: setConfiguration()
85+
:rtype: QVariantMap
86+
%End
87+
88+
void setConfiguration( const QVariantMap &configuration );
89+
%Docstring
90+
Sets the child algorithm's ``configuration`` map.
91+
92+
This map specifies configuration settings which are passed
93+
to the algorithm, allowing it to dynamically adjust its initialized parameters
94+
and outputs according to this configuration. This allows child algorithms in the model
95+
to adjust their behavior at run time according to some user configuration.
96+
97+
.. seealso:: configuration()
7398
%End
7499

75100
const QgsProcessingAlgorithm *algorithm() const;

‎python/core/processing/qgsprocessingregistry.sip

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,17 @@ class QgsProcessingRegistry : QObject
8989
:rtype: QgsProcessingAlgorithm
9090
%End
9191

92-
QgsProcessingAlgorithm *createAlgorithmById( const QString &id ) const /Factory/;
92+
QgsProcessingAlgorithm *createAlgorithmById( const QString &id, const QVariantMap &configuration = QVariantMap() ) const /Factory/;
9393
%Docstring
9494
Creates a new instance of an algorithm by its ID. If no matching algorithm is found, a None
95-
is returned. Callers take responsibility for deleting the returned object.`
95+
is returned. Callers take responsibility for deleting the returned object.
96+
97+
The ``configuration`` argument allows passing of a map of configuration settings
98+
to the algorithm, allowing it to dynamically adjust its initialized parameters
99+
and outputs according to this configuration. This is generally used only for
100+
algorithms in a model, allowing them to adjust their behavior at run time
101+
according to some user configuration.
102+
96103
.. seealso:: algorithms()
97104
.. seealso:: algorithmById()
98105
:rtype: QgsProcessingAlgorithm

‎src/core/processing/models/qgsprocessingmodelalgorithm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
259259
childTime.start();
260260

261261
bool ok = false;
262-
std::unique_ptr< QgsProcessingAlgorithm > childAlg( child.algorithm()->create() );
262+
std::unique_ptr< QgsProcessingAlgorithm > childAlg( child.algorithm()->create( child.configuration() ) );
263263
QVariantMap results = childAlg->run( childParams, context, feedback, &ok );
264264
childAlg.reset( nullptr );
265265
if ( !ok )

‎src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ QgsProcessingModelChildAlgorithm::QgsProcessingModelChildAlgorithm( const QStrin
3030
QgsProcessingModelChildAlgorithm::QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other )
3131
: QgsProcessingModelComponent( other )
3232
, mId( other.mId )
33+
, mConfiguration( other.mConfiguration )
3334
, mParams( other.mParams )
3435
, mModelOutputs( other.mModelOutputs )
3536
, mActive( other.mActive )
@@ -44,6 +45,7 @@ QgsProcessingModelChildAlgorithm &QgsProcessingModelChildAlgorithm::operator=( c
4445
{
4546
QgsProcessingModelComponent::operator =( other );
4647
mId = other.mId;
48+
mConfiguration = other.mConfiguration;
4749
setAlgorithmId( other.algorithmId() );
4850
mParams = other.mParams;
4951
mModelOutputs = other.mModelOutputs;
@@ -76,6 +78,7 @@ QVariant QgsProcessingModelChildAlgorithm::toVariant() const
7678
QVariantMap map;
7779
map.insert( QStringLiteral( "id" ), mId );
7880
map.insert( QStringLiteral( "alg_id" ), mAlgorithmId );
81+
map.insert( QStringLiteral( "alg_config" ), mConfiguration );
7982
map.insert( QStringLiteral( "active" ), mActive );
8083
map.insert( QStringLiteral( "dependencies" ), mDependencies );
8184
map.insert( QStringLiteral( "parameters_collapsed" ), mParametersCollapsed );
@@ -112,6 +115,7 @@ bool QgsProcessingModelChildAlgorithm::loadVariant( const QVariant &child )
112115
QVariantMap map = child.toMap();
113116

114117
mId = map.value( QStringLiteral( "id" ) ).toString();
118+
mConfiguration = map.value( QStringLiteral( "alg_config" ) ).toMap();
115119
setAlgorithmId( map.value( QStringLiteral( "alg_id" ) ).toString() );
116120
mActive = map.value( QStringLiteral( "active" ) ).toBool();
117121
mDependencies = map.value( QStringLiteral( "dependencies" ) ).toStringList();
@@ -186,9 +190,16 @@ QString QgsProcessingModelChildAlgorithm::asPythonCode() const
186190
return lines.join( '\n' );
187191
}
188192

193+
QVariantMap QgsProcessingModelChildAlgorithm::configuration() const
194+
{
195+
return mConfiguration;
196+
}
189197

190-
191-
198+
void QgsProcessingModelChildAlgorithm::setConfiguration( const QVariantMap &configuration )
199+
{
200+
mConfiguration = configuration;
201+
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId, mConfiguration ) );
202+
}
192203

193204
void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModelAlgorithm &model )
194205
{
@@ -207,7 +218,7 @@ void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModel
207218
void QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId )
208219
{
209220
mAlgorithmId = algorithmId;
210-
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId ) );
221+
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId, mConfiguration ) );
211222
}
212223

213224
///@endcond

‎src/core/processing/models/qgsprocessingmodelchildalgorithm.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
8787
*/
8888
void setAlgorithmId( const QString &algorithmId );
8989

90+
/**
91+
* Returns the child algorithm's configuration map.
92+
*
93+
* This map specifies configuration settings which are passed
94+
* to the algorithm, allowing it to dynamically adjust its initialized parameters
95+
* and outputs according to this configuration. This allows child algorithms in the model
96+
* to adjust their behavior at run time according to some user configuration.
97+
*
98+
* \see setConfiguration()
99+
*/
100+
QVariantMap configuration() const;
101+
102+
/**
103+
* Sets the child algorithm's \a configuration map.
104+
*
105+
* This map specifies configuration settings which are passed
106+
* to the algorithm, allowing it to dynamically adjust its initialized parameters
107+
* and outputs according to this configuration. This allows child algorithms in the model
108+
* to adjust their behavior at run time according to some user configuration.
109+
*
110+
* \see configuration()
111+
*/
112+
void setConfiguration( const QVariantMap &configuration );
113+
90114
/**
91115
* Returns the underlying child algorithm, or a nullptr
92116
* if a matching algorithm is not available.
@@ -237,6 +261,8 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
237261
QString mAlgorithmId;
238262
std::unique_ptr< QgsProcessingAlgorithm > mAlgorithm;
239263

264+
QVariantMap mConfiguration;
265+
240266
//! A map of parameter sources. Keys are algorithm parameter names.
241267
QMap< QString, QgsProcessingModelChildParameterSources > mParams;
242268

‎src/core/processing/qgsprocessingregistry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QStrin
9898
return nullptr;
9999
}
100100

101-
QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id ) const
101+
QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
102102
{
103103
const QgsProcessingAlgorithm *alg = algorithmById( id );
104104
if ( !alg )
105105
return nullptr;
106106

107-
std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create() );
107+
std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
108108
return creation.release();
109109
}
110110

‎src/core/processing/qgsprocessingregistry.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,18 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject
102102

103103
/**
104104
* Creates a new instance of an algorithm by its ID. If no matching algorithm is found, a nullptr
105-
* is returned. Callers take responsibility for deleting the returned object.`
105+
* is returned. Callers take responsibility for deleting the returned object.
106+
*
107+
* The \a configuration argument allows passing of a map of configuration settings
108+
* to the algorithm, allowing it to dynamically adjust its initialized parameters
109+
* and outputs according to this configuration. This is generally used only for
110+
* algorithms in a model, allowing them to adjust their behavior at run time
111+
* according to some user configuration.
112+
*
106113
* \see algorithms()
107114
* \see algorithmById()
108115
*/
109-
QgsProcessingAlgorithm *createAlgorithmById( const QString &id ) const SIP_FACTORY;
116+
QgsProcessingAlgorithm *createAlgorithmById( const QString &id, const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
110117

111118
signals:
112119

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,6 +4284,11 @@ void TestQgsProcessing::modelerAlgorithm()
42844284
child.setAlgorithmId( QStringLiteral( "native:centroids" ) );
42854285
QVERIFY( child.algorithm() );
42864286
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );
4287+
QVariantMap myConfig;
4288+
myConfig.insert( QStringLiteral( "some_key" ), 11 );
4289+
child.setConfiguration( myConfig );
4290+
QCOMPARE( child.configuration(), myConfig );
4291+
42874292
child.setDescription( QStringLiteral( "desc" ) );
42884293
QCOMPARE( child.description(), QStringLiteral( "desc" ) );
42894294
QVERIFY( child.isActive() );
@@ -4593,6 +4598,7 @@ void TestQgsProcessing::modelerAlgorithm()
45934598
QgsProcessingModelChildAlgorithm alg5c1;
45944599
alg5c1.setChildId( "cx1" );
45954600
alg5c1.setAlgorithmId( "buffer" );
4601+
alg5c1.setConfiguration( myConfig );
45964602
alg5c1.addParameterSources( "x", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromModelParameter( "p1" ) );
45974603
alg5c1.addParameterSources( "y", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromChildOutput( "cx2", "out3" ) );
45984604
alg5c1.addParameterSources( "z", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromStaticValue( 5 ) );
@@ -4639,6 +4645,7 @@ void TestQgsProcessing::modelerAlgorithm()
46394645
QgsProcessingModelChildAlgorithm alg6c1 = alg6.childAlgorithm( "cx1" );
46404646
QCOMPARE( alg6c1.childId(), QStringLiteral( "cx1" ) );
46414647
QCOMPARE( alg6c1.algorithmId(), QStringLiteral( "buffer" ) );
4648+
QCOMPARE( alg6c1.configuration(), myConfig );
46424649
QVERIFY( alg6c1.isActive() );
46434650
QVERIFY( alg6c1.outputsCollapsed() );
46444651
QVERIFY( alg6c1.parametersCollapsed() );

0 commit comments

Comments
 (0)
Please sign in to comment.