Skip to content

Commit

Permalink
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
nyalldawson committed Jun 5, 2017
1 parent c1d9d57 commit a23a6ac
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -383,6 +383,8 @@ QFlags<QgsProcessingAlgorithm::Flag> operator|(QgsProcessingAlgorithm::Flag f1,





/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
50 changes: 50 additions & 0 deletions 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 &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 *
************************************************************************/
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -250,3 +250,5 @@ QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap &parame
{
return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, name, context );
}


2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -378,6 +378,8 @@ class CORE_EXPORT QgsProcessingAlgorithm
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )



#endif // QGSPROCESSINGALGORITHM_H


56 changes: 56 additions & 0 deletions 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 &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 ) );
}
}
}
68 changes: 68 additions & 0 deletions 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 &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


0 comments on commit a23a6ac

Please sign in to comment.