@@ -373,12 +373,21 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
373
373
374
374
const QString algorithmClassName = safeName ( name () );
375
375
376
+ QSet< QString > toExecute;
377
+ for ( auto childIt = mChildAlgorithms .constBegin (); childIt != mChildAlgorithms .constEnd (); ++childIt )
378
+ {
379
+ if ( childIt->isActive () && childIt->algorithm () )
380
+ toExecute.insert ( childIt->childId () );
381
+ }
382
+ const int totalSteps = toExecute.count ();
383
+
376
384
switch ( outputType )
377
385
{
378
386
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
379
387
{
380
388
lines << QStringLiteral ( " from qgis.core import QgsProcessing" );
381
389
lines << QStringLiteral ( " from qgis.core import QgsProcessingAlgorithm" );
390
+ lines << QStringLiteral ( " from qgis.core import QgsProcessingMultiStepFeedback" );
382
391
// add specific parameter type imports
383
392
const auto params = parameterDefinitions ();
384
393
QStringList importLines; // not a set - we need regular ordering
@@ -405,8 +414,12 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
405
414
}
406
415
407
416
lines << QString ();
408
- lines << indent + QStringLiteral ( " def processAlgorithm(self, parameters, context, feedback ):" );
417
+ lines << indent + QStringLiteral ( " def processAlgorithm(self, parameters, context, model_feedback ):" );
409
418
currentIndent = indent + indent;
419
+
420
+ lines << currentIndent + QStringLiteral ( " # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the" );
421
+ lines << currentIndent + QStringLiteral ( " # overall progress through the model" );
422
+ lines << currentIndent + QStringLiteral ( " feedback = QgsProcessingMultiStepFeedback(%1, model_feedback)" ).arg ( totalSteps );
410
423
break ;
411
424
}
412
425
#if 0
@@ -450,15 +463,9 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
450
463
lines << currentIndent + QStringLiteral ( " results = {}" );
451
464
lines << currentIndent + QStringLiteral ( " outputs = {}" );
452
465
453
- QSet< QString > toExecute;
454
- for ( auto childIt = mChildAlgorithms .constBegin (); childIt != mChildAlgorithms .constEnd (); ++childIt )
455
- {
456
- if ( childIt->isActive () && childIt->algorithm () )
457
- toExecute.insert ( childIt->childId () );
458
- }
459
-
460
466
QSet< QString > executed;
461
467
bool executedAlg = true ;
468
+ int currentStep = 0 ;
462
469
while ( executedAlg && executed.count () < toExecute.count () )
463
470
{
464
471
executedAlg = false ;
@@ -530,7 +537,15 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
530
537
}
531
538
532
539
lines << child.asPythonCode ( outputType, childParams, currentIndent.size (), indentSize );
533
-
540
+ currentStep++;
541
+ if ( currentStep < totalSteps )
542
+ {
543
+ lines << QString ();
544
+ lines << currentIndent + QStringLiteral ( " feedback.setCurrentStep(%1)" ).arg ( currentStep );
545
+ lines << currentIndent + QStringLiteral ( " if feedback.isCanceled():" );
546
+ lines << currentIndent + indent + QStringLiteral ( " return {}" );
547
+ lines << QString ();
548
+ }
534
549
executed.insert ( childId );
535
550
}
536
551
}
0 commit comments