QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #include "qgsprocessingfeedback.h"
20 #include "qgsprocessingcontext.h"
21 #include "qgsprocessingalgorithm.h"
22 #include "qgsprocessingutils.h"
23 #include "qgsvectorlayer.h"
24 
26  : QgsTask( tr( "Executing “%1”" ).arg( algorithm->displayName() ), algorithm->flags() & QgsProcessingAlgorithm::FlagCanCancel ? QgsTask::CanCancel : QgsTask::Flag() )
27  , mParameters( parameters )
28  , mContext( context )
29  , mFeedback( feedback )
30 {
31  if ( !mFeedback )
32  {
33  mOwnedFeedback.reset( new QgsProcessingFeedback() );
34  mFeedback = mOwnedFeedback.get();
35  }
36  try
37  {
38  mAlgorithm.reset( algorithm->create() );
39  if ( !( mAlgorithm && mAlgorithm->prepare( mParameters, context, mFeedback ) ) )
40  cancel();
41  }
42  catch ( QgsProcessingException &e )
43  {
44  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
45  mFeedback->reportError( e.what() );
46  cancel();
47  }
48 }
49 
51 {
52  if ( mFeedback )
53  mFeedback->cancel();
55 }
56 
58 {
59  if ( isCanceled() )
60  return false;
61 
63  bool ok = false;
64  try
65  {
66  mResults = mAlgorithm->runPrepared( mParameters, mContext, mFeedback );
67  ok = true;
68  }
69  catch ( QgsProcessingException &e )
70  {
71  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
72  mFeedback->reportError( e.what() );
73  return false;
74  }
75  return ok && !mFeedback->isCanceled();
76 }
77 
79 {
80  Q_UNUSED( result )
81  QVariantMap ppResults;
82  if ( result )
83  {
84  ppResults = mAlgorithm->postProcess( mContext, mFeedback );
85  }
86  emit executed( result, !ppResults.isEmpty() ? ppResults : mResults );
87 }
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
algorithm
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
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:39
QgsProcessingAlgRunnerTask::run
bool run() override
Performs the task's operation.
Definition: qgsprocessingalgrunnertask.cpp:57
QgsFeedback::cancel
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:97
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsProcessingAlgRunnerTask::cancel
void cancel() override
Notifies the task that it should terminate.
Definition: qgsprocessingalgrunnertask.cpp:50
QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask
QgsProcessingAlgRunnerTask(const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback=nullptr)
Constructor for QgsProcessingAlgRunnerTask.
Definition: qgsprocessingalgrunnertask.cpp:25
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
qgsprocessingalgorithm.h
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsProcessingAlgRunnerTask::executed
void executed(bool successful, const QVariantMap &results)
Emitted when the algorithm has finished execution.
QgsTask::setProgress
void setProgress(double progress)
Sets the task's current progress.
Definition: qgstaskmanager.cpp:232
qgsprocessingfeedback.h
QgsFeedback::progressChanged
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
qgsvectorlayer.h
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
qgsprocessingutils.h
qgsprocessingalgrunnertask.h
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
QgsProcessingAlgorithm::create
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const SIP_THROW(QgsProcessingException)
Creates a copy of the algorithm, ready for execution.
Definition: qgsprocessingalgorithm.cpp:40
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
qgsprocessingcontext.h
QgsProcessingAlgRunnerTask::finished
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
Definition: qgsprocessingalgrunnertask.cpp:78
Qgis::Critical
@ Critical
Definition: qgis.h:105
QgsProcessingException
Definition: qgsexception.h:82
QgsTask::Flag
Flag
Task flags.
Definition: qgstaskmanager.h:70
QgsTask::isCanceled
bool isCanceled() const
Will return true if task should terminate ASAP.
Definition: qgstaskmanager.cpp:118
QgsTask
Abstract base class for long running background tasks. Tasks can be controlled directly,...
Definition: qgstaskmanager.h:52