Skip to content

Commit

Permalink
[processing] Add method to attempt to reattach model children to link…
Browse files Browse the repository at this point in the history
…ed algorithms

Refs #19857
  • Loading branch information
nyalldawson committed Sep 18, 2018
1 parent 6b8cd4b commit 1732654
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
Expand Up @@ -71,14 +71,32 @@ Returns the underlying child algorithm's ID.
.. seealso:: :py:func:`setAlgorithmId`
%End

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

Returns true if the algorithm was successfully set.

.. seealso:: :py:func:`reattach`

.. seealso:: :py:func:`algorithm`

.. seealso:: :py:func:`algorithmId`
%End

bool reattach() const;
%Docstring
Attempts to re-attach the child to the algorithm specified by ``algorithmId``().

This can be run to relink the child to algorithms from providers which were not
originally available for the model to link to.

Returns true if the algorithm was successfully reattached.

.. seealso:: :py:func:`algorithm`

.. seealso:: :py:func:`setAlgorithmId`
%End

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

.. seealso:: :py:func:`reattach`

.. seealso:: :py:func:`algorithmId`
%End

Expand Down
Expand Up @@ -222,10 +222,16 @@ void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModel
mId = id;
}

void QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId )
bool QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId )
{
mAlgorithmId = algorithmId;
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId, mConfiguration ) );
return static_cast< bool >( mAlgorithm.get() );
}

bool QgsProcessingModelChildAlgorithm::reattach() const
{
return const_cast< QgsProcessingModelChildAlgorithm * >( this )->setAlgorithmId( mAlgorithmId );
}

///@endcond
22 changes: 21 additions & 1 deletion src/core/processing/models/qgsprocessingmodelchildalgorithm.h
Expand Up @@ -82,10 +82,27 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
/**
* Sets the underlying child algorithm's ID. This
* should be set to an existing QgsProcessingAlgorithm algorithm ID.
*
* Returns true if the algorithm was successfully set.
*
* \see reattach()
* \see algorithm()
* \see algorithmId()
*/
void setAlgorithmId( const QString &algorithmId );
bool setAlgorithmId( const QString &algorithmId );

/**
* Attempts to re-attach the child to the algorithm specified by \a algorithmId().
*
* This can be run to relink the child to algorithms from providers which were not
* originally available for the model to link to.
*
* Returns true if the algorithm was successfully reattached.
*
* \see algorithm()
* \see setAlgorithmId()
*/
bool reattach() const;

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

friend class TestQgsProcessing;

};

///@endcond
Expand Down
14 changes: 10 additions & 4 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -5539,15 +5539,21 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelChildParameterSource::fromStaticValue( QStringLiteral( "b" ) ) );





QgsProcessingModelChildAlgorithm child( QStringLiteral( "some_id" ) );
QCOMPARE( child.algorithmId(), QStringLiteral( "some_id" ) );
QVERIFY( !child.algorithm() );
child.setAlgorithmId( QStringLiteral( "native:centroids" ) );
QVERIFY( !child.setAlgorithmId( QStringLiteral( "blah" ) ) );
QVERIFY( !child.reattach() );
QVERIFY( child.setAlgorithmId( QStringLiteral( "native:centroids" ) ) );
QVERIFY( child.algorithm() );
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );
// bit of a hack -- but try to simulate an algorithm not originally available!
child.mAlgorithm.reset();
QVERIFY( !child.algorithm() );
QVERIFY( child.reattach() );
QVERIFY( child.algorithm() );
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );

QVariantMap myConfig;
myConfig.insert( QStringLiteral( "some_key" ), 11 );
child.setConfiguration( myConfig );
Expand Down

0 comments on commit 1732654

Please sign in to comment.