diff --git a/python/core/core.sip b/python/core/core.sip index 6d04a435474..d6fee0b4888 100644 --- a/python/core/core.sip +++ b/python/core/core.sip @@ -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 diff --git a/python/core/processing/qgsprocessingalgorithm.sip b/python/core/processing/qgsprocessingalgorithm.sip index c38ea966261..4dca51edf57 100644 --- a/python/core/processing/qgsprocessingalgorithm.sip +++ b/python/core/processing/qgsprocessingalgorithm.sip @@ -383,6 +383,8 @@ QFlags operator|(QgsProcessingAlgorithm::Flag f1, + + /************************************************************************ * This file has been generated automatically from * * * diff --git a/python/core/processing/qgsprocessingalgrunnertask.sip b/python/core/processing/qgsprocessingalgrunnertask.sip new file mode 100644 index 00000000000..bd05981d9b8 --- /dev/null +++ b/python/core/processing/qgsprocessingalgrunnertask.sip @@ -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 ¶meters, + 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 * + ************************************************************************/ diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 035234ffe1e..fcc6bb34007 100755 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -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 diff --git a/src/core/processing/qgsprocessingalgorithm.cpp b/src/core/processing/qgsprocessingalgorithm.cpp index 7f585a117f1..b0b07a5852a 100644 --- a/src/core/processing/qgsprocessingalgorithm.cpp +++ b/src/core/processing/qgsprocessingalgorithm.cpp @@ -250,3 +250,5 @@ QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap ¶me { return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, name, context ); } + + diff --git a/src/core/processing/qgsprocessingalgorithm.h b/src/core/processing/qgsprocessingalgorithm.h index fde2cf3f525..4cc21c777e2 100644 --- a/src/core/processing/qgsprocessingalgorithm.h +++ b/src/core/processing/qgsprocessingalgorithm.h @@ -378,6 +378,8 @@ class CORE_EXPORT QgsProcessingAlgorithm }; Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags ) + + #endif // QGSPROCESSINGALGORITHM_H diff --git a/src/core/processing/qgsprocessingalgrunnertask.cpp b/src/core/processing/qgsprocessingalgrunnertask.cpp new file mode 100644 index 00000000000..c914fa3e88f --- /dev/null +++ b/src/core/processing/qgsprocessingalgrunnertask.cpp @@ -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 ¶meters, 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 ) ); + } + } +} diff --git a/src/core/processing/qgsprocessingalgrunnertask.h b/src/core/processing/qgsprocessingalgrunnertask.h new file mode 100644 index 00000000000..17fb88b8315 --- /dev/null +++ b/src/core/processing/qgsprocessingalgrunnertask.h @@ -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 ¶meters, + 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 + +