Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create a QgsProcessingAlgRunnerTask task
Allows background execution of processing algorithms. Not exposed anywhere in GUI (yet)
- Loading branch information
1 parent
c1d9d57
commit a23a6ac
Showing
8 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|