Skip to content

Commit

Permalink
[processing] Fix use of models as child algorithms in other models
Browse files Browse the repository at this point in the history
Fixes #19857
  • Loading branch information
nyalldawson committed Sep 18, 2018
1 parent 1732654 commit cdd72e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -930,6 +930,16 @@ bool QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( const QList<int>
outputType == QgsProcessing::TypeVectorPolygon ) ) );
}

void QgsProcessingModelAlgorithm::reattachAlgorithms() const
{
QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
{
if ( !childIt->algorithm() )
childIt->reattach();
}
}

bool QgsProcessingModelAlgorithm::toFile( const QString &path ) const
{
QDomDocument doc = QDomDocument( QStringLiteral( "model" ) );
Expand Down Expand Up @@ -1182,6 +1192,7 @@ QSet< QString > QgsProcessingModelAlgorithm::dependsOnChildAlgorithms( const QSt

bool QgsProcessingModelAlgorithm::canExecute( QString *errorMessage ) const
{
reattachAlgorithms();
QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
{
Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Expand Up @@ -416,6 +416,11 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
static bool vectorOutputIsCompatibleType( const QList<int> &acceptableDataTypes, QgsProcessing::SourceType outputType );

/**
* Tries to reattach all child algorithms to their linked algorithms.
*/
void reattachAlgorithms() const;

friend class TestQgsProcessing;
};

Expand Down
9 changes: 8 additions & 1 deletion tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -5750,7 +5750,14 @@ void TestQgsProcessing::modelerAlgorithm()
alg2.addChildAlgorithm( c6 );
QVERIFY( !alg2.canExecute() );


// test that children are re-attached before testing for canExecute
QgsProcessingModelAlgorithm alg2a( "test", "testGroup" );
QgsProcessingModelChildAlgorithm c5a;
c5a.setAlgorithmId( "native:centroids" );
alg2a.addChildAlgorithm( c5a );
// simulate initially missing provider or algorithm (e.g. another model as a child algorithm)
alg2a.mChildAlgorithms.begin().value().mAlgorithm.reset();
QVERIFY( alg2a.canExecute() );

// dependencies
QgsProcessingModelAlgorithm alg3( "test", "testGroup" );
Expand Down

0 comments on commit cdd72e5

Please sign in to comment.