Skip to content

Commit a23a6ac

Browse files
committed
Create a QgsProcessingAlgRunnerTask task
Allows background execution of processing algorithms. Not exposed anywhere in GUI (yet)
1 parent c1d9d57 commit a23a6ac

File tree

8 files changed

+183
-0
lines changed

8 files changed

+183
-0
lines changed

python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@
285285
%Include metadata/qgslayermetadatavalidator.sip
286286

287287
%Include processing/qgsprocessingalgorithm.sip
288+
%Include processing/qgsprocessingalgrunnertask.sip
288289
%Include processing/qgsprocessingcontext.sip
289290
%Include processing/qgsprocessingfeedback.sip
290291
%Include processing/qgsprocessingoutputs.sip

python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ QFlags<QgsProcessingAlgorithm::Flag> operator|(QgsProcessingAlgorithm::Flag f1,
383383

384384

385385

386+
387+
386388
/************************************************************************
387389
* This file has been generated automatically from *
388390
* *
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/processing/qgsprocessingalgrunnertask.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
class QgsProcessingAlgRunnerTask : QgsTask
14+
{
15+
%Docstring
16+
QgsTask task which runs a QgsProcessingAlgorithm in a background task.
17+
.. versionadded:: 3.0
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgsprocessingalgrunnertask.h"
22+
%End
23+
public:
24+
25+
QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm,
26+
const QVariantMap &parameters,
27+
QgsProcessingContext &context );
28+
%Docstring
29+
Constructor for QgsProcessingAlgRunnerTask. Takes an ``algorithm``, algorithm ``parameters``
30+
and processing ``context``.
31+
%End
32+
33+
virtual void cancel();
34+
35+
protected:
36+
37+
virtual bool run();
38+
virtual void finished( bool result );
39+
40+
};
41+
42+
43+
44+
/************************************************************************
45+
* This file has been generated automatically from *
46+
* *
47+
* src/core/processing/qgsprocessingalgrunnertask.h *
48+
* *
49+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
50+
************************************************************************/

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ SET(QGIS_CORE_SRCS
9595

9696
processing/qgsnativealgorithms.cpp
9797
processing/qgsprocessingalgorithm.cpp
98+
processing/qgsprocessingalgrunnertask.cpp
9899
processing/qgsprocessingoutputs.cpp
99100
processing/qgsprocessingparameters.cpp
100101
processing/qgsprocessingprovider.cpp
@@ -633,6 +634,7 @@ SET(QGIS_CORE_MOC_HDRS
633634
composer/qgslayoutmanager.h
634635
composer/qgspaperitem.h
635636

637+
processing/qgsprocessingalgrunnertask.h
636638
processing/qgsprocessingfeedback.h
637639
processing/qgsprocessingprovider.h
638640
processing/qgsprocessingregistry.h

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,5 @@ QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap &parame
250250
{
251251
return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, name, context );
252252
}
253+
254+

src/core/processing/qgsprocessingalgorithm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ class CORE_EXPORT QgsProcessingAlgorithm
378378
};
379379
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
380380

381+
382+
381383
#endif // QGSPROCESSINGALGORITHM_H
382384

383385

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***************************************************************************
2+
qgsprocessingalgrunnertask.cpp
3+
------------------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsprocessingalgrunnertask.h"
19+
#include "qgsprocessingfeedback.h"
20+
#include "qgsprocessingcontext.h"
21+
#include "qgsprocessingalgorithm.h"
22+
#include "qgsprocessingutils.h"
23+
#include "qgsvectorlayer.h"
24+
25+
QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context )
26+
: mAlgorithm( algorithm )
27+
, mParameters( parameters )
28+
, mContext( context )
29+
{
30+
mFeedback.reset( new QgsProcessingFeedback() );
31+
}
32+
33+
void QgsProcessingAlgRunnerTask::cancel()
34+
{
35+
mFeedback->cancel();
36+
}
37+
38+
bool QgsProcessingAlgRunnerTask::run()
39+
{
40+
connect( mFeedback.get(), &QgsFeedback::progressChanged, this, &QgsProcessingAlgRunnerTask::setProgress );
41+
mResults = mAlgorithm->run( mParameters, mContext, mFeedback.get() );
42+
return !mFeedback->isCanceled();
43+
}
44+
45+
void QgsProcessingAlgRunnerTask::finished( bool result )
46+
{
47+
Q_UNUSED( result );
48+
if ( !mResults.isEmpty() )
49+
{
50+
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( mResults.value( "OUTPUT_LAYER" ).toString(), mContext );
51+
if ( layer )
52+
{
53+
mContext.project()->addMapLayer( mContext.temporaryLayerStore()->takeMapLayer( layer ) );
54+
}
55+
}
56+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/***************************************************************************
2+
qgsprocessingalgrunnertask.h
3+
------------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSPROCESSINGALGRUNNERTASK_H
19+
#define QGSPROCESSINGALGRUNNERTASK_H
20+
21+
#include "qgis_core.h"
22+
#include "qgis.h"
23+
#include "qgstaskmanager.h"
24+
#include "qgsprocessingfeedback.h"
25+
26+
class QgsProcessingAlgorithm;
27+
class QgsProcessingContext;
28+
29+
/**
30+
* \class QgsProcessingAlgRunnerTask
31+
* \ingroup core
32+
* QgsTask task which runs a QgsProcessingAlgorithm in a background task.
33+
* \since QGIS 3.0
34+
*/
35+
class CORE_EXPORT QgsProcessingAlgRunnerTask : public QgsTask
36+
{
37+
Q_OBJECT
38+
39+
public:
40+
41+
/**
42+
* Constructor for QgsProcessingAlgRunnerTask. Takes an \a algorithm, algorithm \a parameters
43+
* and processing \a context.
44+
*/
45+
QgsProcessingAlgRunnerTask( const QgsProcessingAlgorithm *algorithm,
46+
const QVariantMap &parameters,
47+
QgsProcessingContext &context );
48+
49+
virtual void cancel() override;
50+
51+
protected:
52+
53+
virtual bool run() override;
54+
virtual void finished( bool result ) override;
55+
56+
private:
57+
58+
const QgsProcessingAlgorithm *mAlgorithm = nullptr;
59+
QVariantMap mParameters;
60+
QVariantMap mResults;
61+
QgsProcessingContext &mContext;
62+
std::unique_ptr< QgsProcessingFeedback > mFeedback;
63+
64+
};
65+
66+
#endif // QGSPROCESSINGALGRUNNERTASK_H
67+
68+

0 commit comments

Comments
 (0)