Skip to content
Permalink
Browse files

Make QgsProcessingAlgRunnerTask work correctly

It now safely can execute algorithms in background threads
without issues
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent e5b156b commit e1184cd69a74535942d24d619a6fdfa100df38c3
@@ -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();
@@ -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() )
@@ -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()
@@ -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 & )
{
@@ -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 );
}
@@ -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;

0 comments on commit e1184cd

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