QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsprocessingbatchalgorithmdialogbase.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingbatchalgorithmdialogbase.cpp
3 ------------------------------------
4 Date : March 2022
5 Copyright : (C) 2022 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <nlohmann/json.hpp>
19
20#include "qgsapplication.h"
21#include "qgsjsonutils.h"
24#include "qgsprocessingbatch.h"
26
27#include "moc_qgsprocessingbatchalgorithmdialogbase.cpp"
28
30QgsProcessingBatchAlgorithmDialogBase::QgsProcessingBatchAlgorithmDialogBase( QWidget *parent, Qt::WindowFlags flags )
31 : QgsProcessingAlgorithmDialogBase( parent, flags, QgsProcessingAlgorithmDialogBase::DialogMode::Batch )
32{
33 mButtonRunSingle = new QPushButton( tr( "Run as Single Process…" ) );
34 connect( mButtonRunSingle, &QPushButton::clicked, this, &QgsProcessingBatchAlgorithmDialogBase::runAsSingle );
35 buttonBox()->addButton( mButtonRunSingle, QDialogButtonBox::ResetRole ); // reset role to ensure left alignment
36
37 connect( QgsApplication::taskManager(), &QgsTaskManager::taskTriggered, this, &QgsProcessingBatchAlgorithmDialogBase::taskTriggered );
38
39 updateRunButtonVisibility();
40}
41
42QgsProcessingBatchAlgorithmDialogBase::~QgsProcessingBatchAlgorithmDialogBase() = default;
43
44void QgsProcessingBatchAlgorithmDialogBase::resetAdditionalGui()
45{
46 mButtonRunSingle->setEnabled( true );
47}
48
49void QgsProcessingBatchAlgorithmDialogBase::blockAdditionalControlsWhileRunning()
50{
51 mButtonRunSingle->setEnabled( false );
52}
53
54void QgsProcessingBatchAlgorithmDialogBase::execute( const QList<QVariantMap> &parameters )
55{
56 mQueuedParameters = parameters;
57 mCurrentStep = 0;
58 mTotalSteps = mQueuedParameters.size();
59 mResults.clear();
60 mErrors.clear();
61
62 mFeedback.reset( createFeedback() );
63
64 mBatchFeedback = std::make_unique<QgsProcessingBatchFeedback>( mTotalSteps, mFeedback.get() );
65
66 mProxyTask = new QgsProxyProgressTask( tr( "Batch Processing - %1" ).arg( algorithm()->displayName() ), true );
67 connect( mProxyTask, &QgsProxyProgressTask::canceled, mBatchFeedback.get(), &QgsFeedback::cancel );
68 connect( mFeedback.get(), &QgsFeedback::progressChanged, mProxyTask, &QgsProxyProgressTask::setProxyProgress );
69 QgsApplication::taskManager()->addTask( mProxyTask );
70
71 blockControlsWhileRunning();
72 setExecutedAnyResult( true );
73 cancelButton()->setEnabled( true );
74
75 // Make sure the Log tab is visible before executing the algorithm
76 showLog();
77 repaint();
78
79 mTotalTimer.restart();
80 if ( mTotalSteps > 0 )
81 executeNext();
82}
83
84bool QgsProcessingBatchAlgorithmDialogBase::isFinalized()
85{
86 return mQueuedParameters.empty();
87}
88
89void QgsProcessingBatchAlgorithmDialogBase::executeNext()
90{
91 if ( mQueuedParameters.empty() || mFeedback->isCanceled() )
92 {
93 allTasksComplete( false );
94 return;
95 }
96
97 mBatchFeedback->setCurrentStep( mCurrentStep++ );
98 setProgressText( QStringLiteral( "\n" ) + tr( "Processing algorithm %1/%2…" ).arg( mCurrentStep ).arg( mTotalSteps ) );
99 setInfo( tr( "<b>Algorithm %1 starting&hellip;</b>" ).arg( algorithm()->displayName() ), false, false );
100
101 pushInfo( tr( "Input parameters:" ) );
102
103 // important - we create a new context for each iteration
104 // this avoids holding onto resources and layers from earlier iterations,
105 // and allows batch processing of many more items then is possible
106 // if we hold on to these layers
107 mTaskContext.reset( createContext( mBatchFeedback.get() ) );
108
109 const QVariantMap paramsJson = algorithm()->asMap( mQueuedParameters.constFirst(), *mTaskContext ).value( QStringLiteral( "inputs" ) ).toMap();
110 pushCommandInfo( QString::fromStdString( QgsJsonUtils::jsonFromVariant( paramsJson ).dump() ) );
111 pushInfo( QString() );
112
113 mCurrentParameters = algorithm()->preprocessParameters( mQueuedParameters.constFirst() );
114 mQueuedParameters.pop_front();
115
116 mCurrentStepTimer.restart();
118 {
119 QgsProcessingAlgRunnerTask *task = new QgsProcessingAlgRunnerTask( algorithm(), mCurrentParameters, *mTaskContext, mBatchFeedback.get(), QgsTask::CanCancel | QgsTask::Hidden );
120 if ( task->algorithmCanceled() )
121 onTaskComplete( false, {} );
122 else
123 {
124 setCurrentTask( task );
125 }
126 }
127 else
128 {
129 // have to execute in main thread, no tasks allowed
130 bool ok = false;
131 const QVariantMap results = algorithm()->run( mCurrentParameters, *mTaskContext, mBatchFeedback.get(), &ok );
132 onTaskComplete( ok, results );
133 }
134}
135
136void QgsProcessingBatchAlgorithmDialogBase::algExecuted( bool successful, const QVariantMap &results )
137{
138 // parent class cleanup first!
139 QgsProcessingAlgorithmDialogBase::algExecuted( successful, results );
140 onTaskComplete( successful, results );
141}
142
143void QgsProcessingBatchAlgorithmDialogBase::onTaskComplete( bool ok, const QVariantMap &results )
144{
145 if ( ok )
146 {
147 setInfo( tr( "Algorithm %1 correctly executed…" ).arg( algorithm()->displayName() ), false, false );
148 pushInfo( tr( "Execution completed in %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ) );
149 pushInfo( tr( "Results:" ) );
150
151 pushCommandInfo( QString::fromStdString( QgsJsonUtils::jsonFromVariant( results ).dump() ) );
152 pushInfo( QString() );
153
154 mResults.append( QVariantMap(
155 { { QStringLiteral( "parameters" ), mCurrentParameters },
156 { QStringLiteral( "results" ), results }
157 }
158 ) );
159
160 handleAlgorithmResults( algorithm(), *mTaskContext, mBatchFeedback.get(), mCurrentParameters );
161 executeNext();
162 }
163 else if ( mBatchFeedback->isCanceled() )
164 {
165 setInfo( tr( "Algorithm %1 canceled…" ).arg( algorithm()->displayName() ), false, false );
166 pushInfo( tr( "Execution canceled after %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ) );
167 allTasksComplete( true );
168 }
169 else
170 {
171 const QStringList taskErrors = mBatchFeedback->popErrors();
172 setInfo( tr( "Algorithm %1 failed…" ).arg( algorithm()->displayName() ), false, false );
173 reportError( tr( "Execution failed after %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ), false );
174
175 mErrors.append( QVariantMap(
176 { { QStringLiteral( "parameters" ), mCurrentParameters },
177 { QStringLiteral( "errors" ), taskErrors }
178 }
179 ) );
180 executeNext();
181 }
182}
183
184void QgsProcessingBatchAlgorithmDialogBase::taskTriggered( QgsTask *task )
185{
186 if ( task == mProxyTask )
187 {
188 show();
189 raise();
190 setWindowState( ( windowState() & ~Qt::WindowMinimized ) | Qt::WindowActive );
191 activateWindow();
192 showLog();
193 }
194}
195
196void QgsProcessingBatchAlgorithmDialogBase::allTasksComplete( bool canceled )
197{
198 mBatchFeedback.reset();
199 mFeedback.reset();
200 mTaskContext.reset();
201 mQueuedParameters.clear();
202 if ( mProxyTask )
203 {
204 mProxyTask->finalize( true );
205 mProxyTask = nullptr;
206 }
207
208 if ( !canceled )
209 {
210 pushInfo( tr( "Batch execution completed in %1 seconds" ).arg( mTotalTimer.elapsed() / 1000.0, 2 ) );
211 if ( !mErrors.empty() )
212 {
213 reportError( tr( "%1 executions failed. See log for further details." ).arg( mErrors.size() ), true );
214 }
215
216 for ( int i = 0; i < mResults.size(); ++i )
217 {
218 loadHtmlResults( mResults.at( i ).value( QStringLiteral( "results" ) ).toMap(), i );
219 }
220
221 createSummaryTable( mResults, mErrors );
222 }
223
224 resetGui();
225 cancelButton()->setEnabled( false );
226}
227
228
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition qgis.h:3588
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 &parameters)
Pre-processes a set of parameters, allowing the algorithm to clean their values.
QVariantMap run(const QVariantMap &parameters, 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 &parameters, 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.
@ 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