Skip to content
Permalink
Browse files

[processing] Add method to attempt to reattach model children to link…

…ed algorithms

Refs #19857
  • Loading branch information
nyalldawson committed Sep 18, 2018
1 parent 6b8cd4b commit 17326541cdcbd8d741d22620daac07b2d241cbd2
@@ -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;
@@ -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

@@ -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
@@ -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.
@@ -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;
@@ -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
@@ -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 );

0 comments on commit 1732654

Please sign in to comment.
You can’t perform that action at this time.