QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsprocessingalgrunnertask.cpp
Go to the documentation of this file.
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
22#include "qgsmessagelog.h"
23
25 : QgsTask(
26 tr( "Executing “%1”" ).arg( algorithm->displayName() ),
27 flags & ( !( algorithm->flags() & Qgis::ProcessingAlgorithmFlag::CanCancel ) ? ( ~QgsTask::CanCancel ) : ( ~QgsTask::Flags() ) )
28 )
29 , mParameters( parameters )
30 , mContext( context )
31 , mFeedback( feedback )
32{
33 if ( !mFeedback )
34 {
35 mOwnedFeedback.reset( new QgsProcessingFeedback() );
36 mFeedback = mOwnedFeedback.get();
37 }
38 try
39 {
40 mAlgorithm.reset( algorithm->create() );
41 if ( !( mAlgorithm && mAlgorithm->prepare( mParameters, context, mFeedback ) ) )
42 cancel();
43 }
44 catch ( QgsProcessingException &e )
45 {
46 QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::MessageLevel::Critical );
47 mFeedback->reportError( e.what() );
48 cancel();
49 }
50}
51
53{
54 if ( mFeedback )
55 mFeedback->cancel();
57}
58
60{
61 if ( isCanceled() )
62 return false;
63
65 bool ok = false;
66 try
67 {
68 mResults = mAlgorithm->runPrepared( mParameters, mContext, mFeedback );
69 ok = true;
70 }
71 catch ( QgsProcessingException &e )
72 {
73 QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::MessageLevel::Critical );
74 mFeedback->reportError( e.what() );
75 return false;
76 }
77 return ok && !mFeedback->isCanceled();
78}
79
81{
82 Q_UNUSED( result )
83 const QVariantMap ppResults = mAlgorithm->postProcess( mContext, mFeedback, result );
84 emit executed( result, !ppResults.isEmpty() ? ppResults : mResults );
85}
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ Critical
Critical/error message.
Definition qgis.h:157
QString what() const
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void cancel() override
Notifies the task that it should terminate.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
bool run() override
Performs the task's operation.
void executed(bool successful, const QVariantMap &results)
Emitted when the algorithm has finished execution.
QgsProcessingAlgRunnerTask(const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback=nullptr, QgsTask::Flags flags=QgsTask::CanCancel)
Constructor for QgsProcessingAlgRunnerTask.
Abstract base class for processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Abstract base class for long running background tasks.
QFlags< Flag > Flags
virtual void cancel()
Notifies the task that it should terminate.
bool isCanceled() const
Will return true if task should terminate ASAP.
void setProgress(double progress)
Sets the task's current progress.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call