Skip to content

Commit

Permalink
Make QgsProcessingAlgRunnerTask work correctly
Browse files Browse the repository at this point in the history
It now safely can execute algorithms in background threads
without issues
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent e5b156b commit e1184cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
9 changes: 9 additions & 0 deletions python/core/processing/qgsprocessingalgrunnertask.sip
Expand Up @@ -33,6 +33,15 @@ class QgsProcessingAlgRunnerTask : QgsTask

virtual void cancel();

signals:

void executed( bool successful, const QVariantMap &results );
%Docstring
Emitted when the algorithm has finished execution. If the algorithm completed
execution without errors then ``successful`` will be true. The ``results`` argument
contains the results reported by the algorithm.
%End

protected:

virtual bool run();
Expand Down
14 changes: 6 additions & 8 deletions src/core/processing/qgsprocessingalgrunnertask.cpp
Expand Up @@ -24,7 +24,6 @@

QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
: QgsTask( tr( "Running %1" ).arg( algorithm->name() ), QgsTask::CanCancel )
, mParameters( parameters )
, mContext( context )
, mFeedback( feedback )
, mAlgorithm( algorithm->create() )
Expand All @@ -34,6 +33,8 @@ QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgor
mOwnedFeedback.reset( new QgsProcessingFeedback() );
mFeedback = mOwnedFeedback.get();
}
if ( !mAlgorithm->prepare( parameters, context, mFeedback ) )
cancel();
}

void QgsProcessingAlgRunnerTask::cancel()
Expand All @@ -47,7 +48,7 @@ bool QgsProcessingAlgRunnerTask::run()
bool ok = false;
try
{
mResults = mAlgorithm->run( mParameters, mContext, mFeedback, &ok );
ok = mAlgorithm->runPrepared( mContext, mFeedback );
}
catch ( QgsProcessingException & )
{
Expand All @@ -59,12 +60,9 @@ bool QgsProcessingAlgRunnerTask::run()
void QgsProcessingAlgRunnerTask::finished( bool result )
{
Q_UNUSED( result );
if ( !mResults.isEmpty() )
if ( result )
{
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( mResults.value( "OUTPUT_LAYER" ).toString(), mContext );
if ( layer )
{
mContext.project()->addMapLayer( mContext.temporaryLayerStore()->takeMapLayer( layer ) );
}
mResults = mAlgorithm->postProcess( mContext, mFeedback );
}
emit executed( result, mResults );
}
10 changes: 9 additions & 1 deletion src/core/processing/qgsprocessingalgrunnertask.h
Expand Up @@ -49,14 +49,22 @@ class CORE_EXPORT QgsProcessingAlgRunnerTask : public QgsTask

virtual void cancel() override;

signals:

/**
* Emitted when the algorithm has finished execution. If the algorithm completed
* execution without errors then \a successful will be true. The \a results argument
* contains the results reported by the algorithm.
*/
void executed( bool successful, const QVariantMap &results );

protected:

virtual bool run() override;
virtual void finished( bool result ) override;

private:

QVariantMap mParameters;
QVariantMap mResults;
QgsProcessingContext &mContext;
QgsProcessingFeedback *mFeedback = nullptr;
Expand Down

0 comments on commit e1184cd

Please sign in to comment.