QGIS API Documentation  3.0.2-Girona (307d082)
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() ), QgsTask::CanCancel )
27  , mParameters( parameters )
28  , mContext( context )
29  , mFeedback( feedback )
30  , mAlgorithm( algorithm->create() )
31 {
32  if ( !mFeedback )
33  {
34  mOwnedFeedback.reset( new QgsProcessingFeedback() );
35  mFeedback = mOwnedFeedback.get();
36  }
37  if ( !mAlgorithm->prepare( mParameters, context, mFeedback ) )
38  cancel();
39 }
40 
42 {
43  mFeedback->cancel();
45 }
46 
48 {
49  if ( isCanceled() )
50  return false;
51 
53  bool ok = false;
54  try
55  {
56  mResults = mAlgorithm->runPrepared( mParameters, mContext, mFeedback );
57  ok = true;
58  }
59  catch ( QgsProcessingException &e )
60  {
61  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
62  mFeedback->reportError( e.what() );
63  return false;
64  }
65  return ok && !mFeedback->isCanceled();
66 }
67 
69 {
70  Q_UNUSED( result );
71  QVariantMap ppResults;
72  if ( result )
73  {
74  ppResults = mAlgorithm->postProcess( mContext, mFeedback );
75  }
76  emit executed( result, !ppResults.isEmpty() ? ppResults : mResults );
77 }
void setProgress(double progress)
Sets the task's current progress.
Base class for providing feedback from a processing algorithm.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:85
bool run() override
Performs the task's operation.
bool isCanceled() const
Will return true if task should terminate ASAP.
Abstract base class for processing algorithms.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
QString what() const
Definition: qgsexception.h:48
QgsProcessingAlgRunnerTask(const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback=nullptr)
Constructor for QgsProcessingAlgRunnerTask.
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
Abstract base class for long running background tasks.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
void cancel() override
Notifies the task that it should terminate.
virtual void cancel()
Notifies the task that it should terminate.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
Contains information about the context in which a processing algorithm is executed.
void executed(bool successful, const QVariantMap &results)
Emitted when the algorithm has finished execution.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.