Skip to content
Permalink
Browse files

Improvements to QgsProcessingAlgRunnerTask

- take a clone of algs before running them. This avoids issues
if the algorithm is removed or edited while a background task
is running

- accept an optional existing feedback object
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 6d7b0a3 commit 0c6b19cd853f60f629140d9e529a304275423781
@@ -24,7 +24,8 @@ class QgsProcessingAlgRunnerTask : QgsTask

QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm,
const QVariantMap &parameters,
QgsProcessingContext &context );
QgsProcessingContext &context,
QgsProcessingFeedback *feedback = 0 );
%Docstring
Constructor for QgsProcessingAlgRunnerTask. Takes an ``algorithm``, algorithm ``parameters``
and processing ``context``.
@@ -22,13 +22,18 @@
#include "qgsprocessingutils.h"
#include "qgsvectorlayer.h"

QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context )
QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
: QgsTask( tr( "Running %1" ).arg( algorithm->name() ), QgsTask::CanCancel )
, mAlgorithm( algorithm )
, mParameters( parameters )
, mContext( context )
, mFeedback( feedback )
, mAlgorithm( algorithm->clone() )
{
mFeedback.reset( new QgsProcessingFeedback() );
if ( !mFeedback )
{
mOwnedFeedback.reset( new QgsProcessingFeedback() );
mFeedback = mOwnedFeedback.get();
}
}

void QgsProcessingAlgRunnerTask::cancel()
@@ -38,7 +43,7 @@ void QgsProcessingAlgRunnerTask::cancel()

bool QgsProcessingAlgRunnerTask::run()
{
connect( mFeedback.get(), &QgsFeedback::progressChanged, this, &QgsProcessingAlgRunnerTask::setProgress );
connect( mFeedback, &QgsFeedback::progressChanged, this, &QgsProcessingAlgRunnerTask::setProgress );
bool ok = false;
try
{
@@ -48,7 +53,7 @@ bool QgsProcessingAlgRunnerTask::run()
{
return false;
}
return !mFeedback->isCanceled();
return ok && !mFeedback->isCanceled();
}

void QgsProcessingAlgRunnerTask::finished( bool result )
@@ -22,8 +22,8 @@
#include "qgis.h"
#include "qgstaskmanager.h"
#include "qgsprocessingfeedback.h"
#include "qgsprocessingalgorithm.h"

class QgsProcessingAlgorithm;
class QgsProcessingContext;

/**
@@ -44,7 +44,8 @@ class CORE_EXPORT QgsProcessingAlgRunnerTask : public QgsTask
*/
QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm,
const QVariantMap &parameters,
QgsProcessingContext &context );
QgsProcessingContext &context,
QgsProcessingFeedback *feedback = nullptr );

virtual void cancel() override;

@@ -55,11 +56,12 @@ class CORE_EXPORT QgsProcessingAlgRunnerTask : public QgsTask

private:

const QgsProcessingAlgorithm *mAlgorithm = nullptr;
QVariantMap mParameters;
QVariantMap mResults;
QgsProcessingContext &mContext;
std::unique_ptr< QgsProcessingFeedback > mFeedback;
QgsProcessingFeedback *mFeedback = nullptr;
std::unique_ptr< QgsProcessingFeedback > mOwnedFeedback;
std::unique_ptr< const QgsProcessingAlgorithm > mAlgorithm;

};

0 comments on commit 0c6b19c

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