Skip to content

Commit e1184cd

Browse files
committedJul 6, 2017
Make QgsProcessingAlgRunnerTask work correctly
It now safely can execute algorithms in background threads without issues
1 parent e5b156b commit e1184cd

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed
 

‎python/core/processing/qgsprocessingalgrunnertask.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class QgsProcessingAlgRunnerTask : QgsTask
3333

3434
virtual void cancel();
3535

36+
signals:
37+
38+
void executed( bool successful, const QVariantMap &results );
39+
%Docstring
40+
Emitted when the algorithm has finished execution. If the algorithm completed
41+
execution without errors then ``successful`` will be true. The ``results`` argument
42+
contains the results reported by the algorithm.
43+
%End
44+
3645
protected:
3746

3847
virtual bool run();

‎src/core/processing/qgsprocessingalgrunnertask.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
2626
: QgsTask( tr( "Running %1" ).arg( algorithm->name() ), QgsTask::CanCancel )
27-
, mParameters( parameters )
2827
, mContext( context )
2928
, mFeedback( feedback )
3029
, mAlgorithm( algorithm->create() )
@@ -34,6 +33,8 @@ QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgor
3433
mOwnedFeedback.reset( new QgsProcessingFeedback() );
3534
mFeedback = mOwnedFeedback.get();
3635
}
36+
if ( !mAlgorithm->prepare( parameters, context, mFeedback ) )
37+
cancel();
3738
}
3839

3940
void QgsProcessingAlgRunnerTask::cancel()
@@ -47,7 +48,7 @@ bool QgsProcessingAlgRunnerTask::run()
4748
bool ok = false;
4849
try
4950
{
50-
mResults = mAlgorithm->run( mParameters, mContext, mFeedback, &ok );
51+
ok = mAlgorithm->runPrepared( mContext, mFeedback );
5152
}
5253
catch ( QgsProcessingException & )
5354
{
@@ -59,12 +60,9 @@ bool QgsProcessingAlgRunnerTask::run()
5960
void QgsProcessingAlgRunnerTask::finished( bool result )
6061
{
6162
Q_UNUSED( result );
62-
if ( !mResults.isEmpty() )
63+
if ( result )
6364
{
64-
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( mResults.value( "OUTPUT_LAYER" ).toString(), mContext );
65-
if ( layer )
66-
{
67-
mContext.project()->addMapLayer( mContext.temporaryLayerStore()->takeMapLayer( layer ) );
68-
}
65+
mResults = mAlgorithm->postProcess( mContext, mFeedback );
6966
}
67+
emit executed( result, mResults );
7068
}

‎src/core/processing/qgsprocessingalgrunnertask.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,22 @@ class CORE_EXPORT QgsProcessingAlgRunnerTask : public QgsTask
4949

5050
virtual void cancel() override;
5151

52+
signals:
53+
54+
/**
55+
* Emitted when the algorithm has finished execution. If the algorithm completed
56+
* execution without errors then \a successful will be true. The \a results argument
57+
* contains the results reported by the algorithm.
58+
*/
59+
void executed( bool successful, const QVariantMap &results );
60+
5261
protected:
5362

5463
virtual bool run() override;
5564
virtual void finished( bool result ) override;
5665

5766
private:
5867

59-
QVariantMap mParameters;
6068
QVariantMap mResults;
6169
QgsProcessingContext &mContext;
6270
QgsProcessingFeedback *mFeedback = nullptr;

0 commit comments

Comments
 (0)