23#include <nlohmann/json.hpp>
26QgsProcessingBatchAlgorithmDialogBase::QgsProcessingBatchAlgorithmDialogBase( QWidget *parent, Qt::WindowFlags flags )
27 : QgsProcessingAlgorithmDialogBase( parent, flags, QgsProcessingAlgorithmDialogBase::DialogMode::Batch )
29 mButtonRunSingle =
new QPushButton( tr(
"Run as Single Process…" ) );
30 connect( mButtonRunSingle, &QPushButton::clicked,
this, &QgsProcessingBatchAlgorithmDialogBase::runAsSingle );
31 buttonBox()->addButton( mButtonRunSingle, QDialogButtonBox::ResetRole );
35 updateRunButtonVisibility();
38QgsProcessingBatchAlgorithmDialogBase::~QgsProcessingBatchAlgorithmDialogBase() =
default;
40void QgsProcessingBatchAlgorithmDialogBase::resetAdditionalGui()
42 mButtonRunSingle->setEnabled(
true );
45void QgsProcessingBatchAlgorithmDialogBase::blockAdditionalControlsWhileRunning()
47 mButtonRunSingle->setEnabled(
false );
50void QgsProcessingBatchAlgorithmDialogBase::execute(
const QList<QVariantMap> ¶meters )
52 mQueuedParameters = parameters;
54 mTotalSteps = mQueuedParameters.size();
58 mFeedback.reset( createFeedback() );
60 mBatchFeedback = std::make_unique< QgsProcessingBatchFeedback >( mTotalSteps, mFeedback.get() );
67 blockControlsWhileRunning();
68 setExecutedAnyResult(
true );
69 cancelButton()->setEnabled(
true );
75 mTotalTimer.restart();
76 if ( mTotalSteps > 0 )
80bool QgsProcessingBatchAlgorithmDialogBase::isFinalized()
82 return mQueuedParameters.empty();
85void QgsProcessingBatchAlgorithmDialogBase::executeNext()
87 if ( mQueuedParameters.empty() || mFeedback->isCanceled() )
89 allTasksComplete(
false );
93 mBatchFeedback->setCurrentStep( mCurrentStep++ );
94 setProgressText( QStringLiteral(
"\n" ) + tr(
"Processing algorithm %1/%2…" ).arg( mCurrentStep ).arg( mTotalSteps ) );
95 setInfo( tr(
"<b>Algorithm %1 starting…</b>" ).arg(
algorithm()->displayName() ),
false,
false );
97 pushInfo( tr(
"Input parameters:" ) );
103 mTaskContext.reset( createContext( mBatchFeedback.get() ) );
105 const QVariantMap paramsJson =
algorithm()->
asMap( mQueuedParameters.constFirst(), *mTaskContext ).value( QStringLiteral(
"inputs" ) ).toMap();
107 pushInfo( QString() );
110 mQueuedParameters.pop_front();
112 mCurrentStepTimer.restart();
117 onTaskComplete(
false, {} );
120 setCurrentTask( task );
127 const QVariantMap results =
algorithm()->
run( mCurrentParameters, *mTaskContext, mBatchFeedback.get(), &ok );
128 onTaskComplete( ok, results );
132void QgsProcessingBatchAlgorithmDialogBase::algExecuted(
bool successful,
const QVariantMap &results )
135 QgsProcessingAlgorithmDialogBase::algExecuted( successful, results );
136 onTaskComplete( successful, results );
139void QgsProcessingBatchAlgorithmDialogBase::onTaskComplete(
bool ok,
const QVariantMap &results )
143 setInfo( tr(
"Algorithm %1 correctly executed…" ).arg(
algorithm()->displayName() ),
false,
false );
144 pushInfo( tr(
"Execution completed in %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ) );
145 pushInfo( tr(
"Results:" ) );
148 pushInfo( QString() );
150 mResults.append( QVariantMap(
152 { QStringLiteral(
"parameters" ), mCurrentParameters },
153 { QStringLiteral(
"results" ), results }
156 handleAlgorithmResults(
algorithm(), *mTaskContext, mBatchFeedback.get(), mCurrentParameters );
159 else if ( mBatchFeedback->isCanceled() )
161 setInfo( tr(
"Algorithm %1 canceled…" ).arg(
algorithm()->displayName() ),
false,
false );
162 pushInfo( tr(
"Execution canceled after %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ) );
163 allTasksComplete(
true );
167 const QStringList taskErrors = mBatchFeedback->popErrors();
168 setInfo( tr(
"Algorithm %1 failed…" ).arg(
algorithm()->displayName() ),
false,
false );
169 reportError( tr(
"Execution failed after %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ),
false );
171 mErrors.append( QVariantMap(
173 { QStringLiteral(
"parameters" ), mCurrentParameters },
174 { QStringLiteral(
"errors" ), taskErrors }
180void QgsProcessingBatchAlgorithmDialogBase::taskTriggered(
QgsTask *task )
182 if ( task == mProxyTask )
186 setWindowState( ( windowState() & ~Qt::WindowMinimized ) | Qt::WindowActive );
192void QgsProcessingBatchAlgorithmDialogBase::allTasksComplete(
bool canceled )
194 mBatchFeedback.reset();
196 mTaskContext.reset();
197 mQueuedParameters.clear();
200 mProxyTask->finalize(
true );
201 mProxyTask =
nullptr;
206 pushInfo( tr(
"Batch execution completed in %1 seconds" ).arg( mTotalTimer.elapsed() / 1000.0, 2 ) );
207 if ( !mErrors.empty() )
209 reportError( tr(
"%1 executions failed. See log for further details." ).arg( mErrors.size() ),
true );
212 for (
int i = 0; i < mResults.size(); ++i )
214 loadHtmlResults( mResults.at( i ).value( QStringLiteral(
"results" ) ).toMap(), i );
217 createSummaryTable( mResults, mErrors );
221 cancelButton()->setEnabled(
false );
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
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 json jsonFromVariant(const QVariant &v)
Converts a QVariant v to a json object.
QgsTask task which runs a QgsProcessingAlgorithm in a background task.
bool algorithmCanceled()
Returns true if the algorithm was canceled.
virtual QVariantMap preprocessParameters(const QVariantMap ¶meters)
Pre-processes a set of parameters, allowing the algorithm to clean their values.
QVariantMap run(const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok=nullptr, const QVariantMap &configuration=QVariantMap(), bool catchExceptions=true) const
Executes the algorithm using the specified parameters.
virtual QVariantMap asMap(const QVariantMap ¶meters, QgsProcessingContext &context) const
Returns a JSON serializable variant map containing the specified parameters and context settings.
A QgsTask shell which proxies progress reports.
void setProxyProgress(double progress)
Sets the progress (from 0 to 100) for the proxied operation.
void canceled()
Emitted when the task is canceled.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskTriggered(QgsTask *task)
Emitted when a task is triggered.
Abstract base class for long running background tasks.
@ Hidden
Hide task from GUI (since QGIS 3.26)
@ CanCancel
Task can be canceled.
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