Create a QgsProcessingAlgRunnerTask task

Allows background execution of processing algorithms.

Not exposed anywhere in GUI (yet)
This commit is contained in:
Nyall Dawson 2017-05-16 12:48:56 +10:00
parent c1d9d57dd2
commit a23a6ac631
8 changed files with 183 additions and 0 deletions

View File

@ -285,6 +285,7 @@
%Include metadata/qgslayermetadatavalidator.sip
%Include processing/qgsprocessingalgorithm.sip
%Include processing/qgsprocessingalgrunnertask.sip
%Include processing/qgsprocessingcontext.sip
%Include processing/qgsprocessingfeedback.sip
%Include processing/qgsprocessingoutputs.sip

View File

@ -383,6 +383,8 @@ QFlags<QgsProcessingAlgorithm::Flag> operator|(QgsProcessingAlgorithm::Flag f1,
/************************************************************************
* This file has been generated automatically from *
* *

View File

@ -0,0 +1,50 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingalgrunnertask.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
class QgsProcessingAlgRunnerTask : QgsTask
{
%Docstring
QgsTask task which runs a QgsProcessingAlgorithm in a background task.
.. versionadded:: 3.0
%End
%TypeHeaderCode
#include "qgsprocessingalgrunnertask.h"
%End
public:
QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm,
const QVariantMap &parameters,
QgsProcessingContext &context );
%Docstring
Constructor for QgsProcessingAlgRunnerTask. Takes an ``algorithm``, algorithm ``parameters``
and processing ``context``.
%End
virtual void cancel();
protected:
virtual bool run();
virtual void finished( bool result );
};
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingalgrunnertask.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

View File

@ -95,6 +95,7 @@ SET(QGIS_CORE_SRCS
processing/qgsnativealgorithms.cpp
processing/qgsprocessingalgorithm.cpp
processing/qgsprocessingalgrunnertask.cpp
processing/qgsprocessingoutputs.cpp
processing/qgsprocessingparameters.cpp
processing/qgsprocessingprovider.cpp
@ -633,6 +634,7 @@ SET(QGIS_CORE_MOC_HDRS
composer/qgslayoutmanager.h
composer/qgspaperitem.h
processing/qgsprocessingalgrunnertask.h
processing/qgsprocessingfeedback.h
processing/qgsprocessingprovider.h
processing/qgsprocessingregistry.h

View File

@ -250,3 +250,5 @@ QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap &parame
{
return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, name, context );
}

View File

@ -378,6 +378,8 @@ class CORE_EXPORT QgsProcessingAlgorithm
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
#endif // QGSPROCESSINGALGORITHM_H

View File

@ -0,0 +1,56 @@
/***************************************************************************
qgsprocessingalgrunnertask.cpp
------------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsprocessingalgrunnertask.h"
#include "qgsprocessingfeedback.h"
#include "qgsprocessingcontext.h"
#include "qgsprocessingalgorithm.h"
#include "qgsprocessingutils.h"
#include "qgsvectorlayer.h"
QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context )
: mAlgorithm( algorithm )
, mParameters( parameters )
, mContext( context )
{
mFeedback.reset( new QgsProcessingFeedback() );
}
void QgsProcessingAlgRunnerTask::cancel()
{
mFeedback->cancel();
}
bool QgsProcessingAlgRunnerTask::run()
{
connect( mFeedback.get(), &QgsFeedback::progressChanged, this, &QgsProcessingAlgRunnerTask::setProgress );
mResults = mAlgorithm->run( mParameters, mContext, mFeedback.get() );
return !mFeedback->isCanceled();
}
void QgsProcessingAlgRunnerTask::finished( bool result )
{
Q_UNUSED( result );
if ( !mResults.isEmpty() )
{
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( mResults.value( "OUTPUT_LAYER" ).toString(), mContext );
if ( layer )
{
mContext.project()->addMapLayer( mContext.temporaryLayerStore()->takeMapLayer( layer ) );
}
}
}

View File

@ -0,0 +1,68 @@
/***************************************************************************
qgsprocessingalgrunnertask.h
------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROCESSINGALGRUNNERTASK_H
#define QGSPROCESSINGALGRUNNERTASK_H
#include "qgis_core.h"
#include "qgis.h"
#include "qgstaskmanager.h"
#include "qgsprocessingfeedback.h"
class QgsProcessingAlgorithm;
class QgsProcessingContext;
/**
* \class QgsProcessingAlgRunnerTask
* \ingroup core
* QgsTask task which runs a QgsProcessingAlgorithm in a background task.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingAlgRunnerTask : public QgsTask
{
Q_OBJECT
public:
/**
* Constructor for QgsProcessingAlgRunnerTask. Takes an \a algorithm, algorithm \a parameters
* and processing \a context.
*/
QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm,
const QVariantMap &parameters,
QgsProcessingContext &context );
virtual void cancel() override;
protected:
virtual bool run() override;
virtual void finished( bool result ) override;
private:
const QgsProcessingAlgorithm *mAlgorithm = nullptr;
QVariantMap mParameters;
QVariantMap mResults;
QgsProcessingContext &mContext;
std::unique_ptr< QgsProcessingFeedback > mFeedback;
};
#endif // QGSPROCESSINGALGRUNNERTASK_H