QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
19
20#include <memory>
21
22#include "qgsmessagelog.h"
26
27#include "moc_qgsprocessingalgrunnertask.cpp"
28
30 : QgsTask(
31 tr( "Executing “%1”" ).arg( algorithm->displayName() ),
32 flags & ( !( algorithm->flags() & Qgis::ProcessingAlgorithmFlag::CanCancel ) ? ( ~QgsTask::CanCancel ) : ( ~QgsTask::Flags() ) )
33 )
34 , mParameters( parameters )
35 , mContext( context )
36 , mFeedback( feedback )
37{
38 if ( !mFeedback )
39 {
40 mOwnedFeedback = std::make_unique<QgsProcessingFeedback>( );
41 mFeedback = mOwnedFeedback.get();
42 }
43 try
44 {
45 mAlgorithm.reset( algorithm->create() );
46 if ( !( mAlgorithm && mAlgorithm->prepare( mParameters, context, mFeedback ) ) )
47 cancel();
48 }
49 catch ( QgsProcessingException &e )
50 {
51 QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::MessageLevel::Critical );
52 mFeedback->reportError( e.what() );
53 cancel();
54 }
55}
56
58{
59 if ( mFeedback )
60 mFeedback->cancel();
62}
63
65{
66 if ( isCanceled() )
67 return false;
68
70 bool ok = false;
71 try
72 {
73 mResults = mAlgorithm->runPrepared( mParameters, mContext, mFeedback );
74 ok = true;
75 }
76 catch ( QgsProcessingException &e )
77 {
78 QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::MessageLevel::Critical );
79 mFeedback->reportError( e.what() );
80 return false;
81 }
82 return ok && !mFeedback->isCanceled();
83}
84
86{
87 Q_UNUSED( result )
88 const QVariantMap ppResults = mAlgorithm->postProcess( mContext, mFeedback, result );
89 emit executed( result, !ppResults.isEmpty() ? ppResults : mResults );
90}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
@ Critical
Critical/error message.
Definition qgis.h:159
QString what() const
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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.
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.
Flags flags() const
Returns the flags associated with the task.
QFlags< Flag > Flags
virtual void cancel()
Notifies the task that it should terminate.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
@ CanCancel
Task can be canceled.
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