Skip to content

Commit 1732654

Browse files
committed
[processing] Add method to attempt to reattach model children to linked algorithms
Refs #19857
1 parent 6b8cd4b commit 1732654

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

python/core/auto_generated/processing/models/qgsprocessingmodelchildalgorithm.sip.in

+21-1
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,32 @@ Returns the underlying child algorithm's ID.
7171
.. seealso:: :py:func:`setAlgorithmId`
7272
%End
7373

74-
void setAlgorithmId( const QString &algorithmId );
74+
bool setAlgorithmId( const QString &algorithmId );
7575
%Docstring
7676
Sets the underlying child algorithm's ID. This
7777
should be set to an existing QgsProcessingAlgorithm algorithm ID.
7878

79+
Returns true if the algorithm was successfully set.
80+
81+
.. seealso:: :py:func:`reattach`
82+
7983
.. seealso:: :py:func:`algorithm`
8084

8185
.. seealso:: :py:func:`algorithmId`
86+
%End
87+
88+
bool reattach() const;
89+
%Docstring
90+
Attempts to re-attach the child to the algorithm specified by ``algorithmId``().
91+
92+
This can be run to relink the child to algorithms from providers which were not
93+
originally available for the model to link to.
94+
95+
Returns true if the algorithm was successfully reattached.
96+
97+
.. seealso:: :py:func:`algorithm`
98+
99+
.. seealso:: :py:func:`setAlgorithmId`
82100
%End
83101

84102
QVariantMap configuration() const;
@@ -110,6 +128,8 @@ to adjust their behavior at run time according to some user configuration.
110128
Returns the underlying child algorithm, or a None
111129
if a matching algorithm is not available.
112130

131+
.. seealso:: :py:func:`reattach`
132+
113133
.. seealso:: :py:func:`algorithmId`
114134
%End
115135

src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,16 @@ void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModel
222222
mId = id;
223223
}
224224

225-
void QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId )
225+
bool QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId )
226226
{
227227
mAlgorithmId = algorithmId;
228228
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId, mConfiguration ) );
229+
return static_cast< bool >( mAlgorithm.get() );
230+
}
231+
232+
bool QgsProcessingModelChildAlgorithm::reattach() const
233+
{
234+
return const_cast< QgsProcessingModelChildAlgorithm * >( this )->setAlgorithmId( mAlgorithmId );
229235
}
230236

231237
///@endcond

src/core/processing/models/qgsprocessingmodelchildalgorithm.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,27 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
8282
/**
8383
* Sets the underlying child algorithm's ID. This
8484
* should be set to an existing QgsProcessingAlgorithm algorithm ID.
85+
*
86+
* Returns true if the algorithm was successfully set.
87+
*
88+
* \see reattach()
8589
* \see algorithm()
8690
* \see algorithmId()
8791
*/
88-
void setAlgorithmId( const QString &algorithmId );
92+
bool setAlgorithmId( const QString &algorithmId );
93+
94+
/**
95+
* Attempts to re-attach the child to the algorithm specified by \a algorithmId().
96+
*
97+
* This can be run to relink the child to algorithms from providers which were not
98+
* originally available for the model to link to.
99+
*
100+
* Returns true if the algorithm was successfully reattached.
101+
*
102+
* \see algorithm()
103+
* \see setAlgorithmId()
104+
*/
105+
bool reattach() const;
89106

90107
/**
91108
* Returns the child algorithm's configuration map.
@@ -114,6 +131,7 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
114131
/**
115132
* Returns the underlying child algorithm, or a nullptr
116133
* if a matching algorithm is not available.
134+
* \see reattach()
117135
* \see algorithmId()
118136
*/
119137
const QgsProcessingAlgorithm *algorithm() const;
@@ -289,6 +307,8 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
289307
//! Whether list of outputs should be collapsed in the graphical modeler
290308
bool mOutputsCollapsed = true;
291309

310+
friend class TestQgsProcessing;
311+
292312
};
293313

294314
///@endcond

tests/src/analysis/testqgsprocessing.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -5539,15 +5539,21 @@ void TestQgsProcessing::modelerAlgorithm()
55395539
QgsProcessingModelChildParameterSource::fromStaticValue( QStringLiteral( "b" ) ) );
55405540

55415541

5542-
5543-
5544-
55455542
QgsProcessingModelChildAlgorithm child( QStringLiteral( "some_id" ) );
55465543
QCOMPARE( child.algorithmId(), QStringLiteral( "some_id" ) );
55475544
QVERIFY( !child.algorithm() );
5548-
child.setAlgorithmId( QStringLiteral( "native:centroids" ) );
5545+
QVERIFY( !child.setAlgorithmId( QStringLiteral( "blah" ) ) );
5546+
QVERIFY( !child.reattach() );
5547+
QVERIFY( child.setAlgorithmId( QStringLiteral( "native:centroids" ) ) );
55495548
QVERIFY( child.algorithm() );
55505549
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );
5550+
// bit of a hack -- but try to simulate an algorithm not originally available!
5551+
child.mAlgorithm.reset();
5552+
QVERIFY( !child.algorithm() );
5553+
QVERIFY( child.reattach() );
5554+
QVERIFY( child.algorithm() );
5555+
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );
5556+
55515557
QVariantMap myConfig;
55525558
myConfig.insert( QStringLiteral( "some_key" ), 11 );
55535559
child.setConfiguration( myConfig );

0 commit comments

Comments
 (0)