18#include <nlohmann/json.hpp>
27#include "moc_qgsprocessingbatchalgorithmdialogbase.cpp"
30QgsProcessingBatchAlgorithmDialogBase::QgsProcessingBatchAlgorithmDialogBase( QWidget *parent, Qt::WindowFlags flags )
31 : QgsProcessingAlgorithmDialogBase( parent, flags, QgsProcessingAlgorithmDialogBase::DialogMode::Batch )
33 mButtonRunSingle =
new QPushButton( tr(
"Run as Single Process…" ) );
34 connect( mButtonRunSingle, &QPushButton::clicked,
this, &QgsProcessingBatchAlgorithmDialogBase::runAsSingle );
35 buttonBox()->addButton( mButtonRunSingle, QDialogButtonBox::ResetRole );
39 updateRunButtonVisibility();
42QgsProcessingBatchAlgorithmDialogBase::~QgsProcessingBatchAlgorithmDialogBase() =
default;
44void QgsProcessingBatchAlgorithmDialogBase::resetAdditionalGui()
46 mButtonRunSingle->setEnabled(
true );
49void QgsProcessingBatchAlgorithmDialogBase::blockAdditionalControlsWhileRunning()
51 mButtonRunSingle->setEnabled(
false );
54void QgsProcessingBatchAlgorithmDialogBase::execute(
const QList<QVariantMap> ¶meters )
56 mQueuedParameters = parameters;
58 mTotalSteps = mQueuedParameters.size();
62 mFeedback.reset( createFeedback() );
64 mBatchFeedback = std::make_unique<QgsProcessingBatchFeedback>( mTotalSteps, mFeedback.get() );
71 blockControlsWhileRunning();
72 setExecutedAnyResult(
true );
73 cancelButton()->setEnabled(
true );
79 mTotalTimer.restart();
80 if ( mTotalSteps > 0 )
84bool QgsProcessingBatchAlgorithmDialogBase::isFinalized()
86 return mQueuedParameters.empty();
89void QgsProcessingBatchAlgorithmDialogBase::executeNext()
91 if ( mQueuedParameters.empty() || mFeedback->isCanceled() )
93 allTasksComplete(
false );
97 mBatchFeedback->setCurrentStep( mCurrentStep++ );
98 setProgressText( QStringLiteral(
"\n" ) + tr(
"Processing algorithm %1/%2…" ).arg( mCurrentStep ).arg( mTotalSteps ) );
99 setInfo( tr(
"<b>Algorithm %1 starting…</b>" ).arg(
algorithm()->displayName() ),
false,
false );
101 pushInfo( tr(
"Input parameters:" ) );
107 mTaskContext.reset( createContext( mBatchFeedback.get() ) );
109 const QVariantMap paramsJson =
algorithm()->
asMap( mQueuedParameters.constFirst(), *mTaskContext ).value( QStringLiteral(
"inputs" ) ).toMap();
111 pushInfo( QString() );
114 mQueuedParameters.pop_front();
116 mCurrentStepTimer.restart();
121 onTaskComplete(
false, {} );
124 setCurrentTask( task );
131 const QVariantMap results =
algorithm()->
run( mCurrentParameters, *mTaskContext, mBatchFeedback.get(), &ok );
132 onTaskComplete( ok, results );
136void QgsProcessingBatchAlgorithmDialogBase::algExecuted(
bool successful,
const QVariantMap &results )
139 QgsProcessingAlgorithmDialogBase::algExecuted( successful, results );
140 onTaskComplete( successful, results );
143void QgsProcessingBatchAlgorithmDialogBase::onTaskComplete(
bool ok,
const QVariantMap &results )
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:" ) );
152 pushInfo( QString() );
154 mResults.append( QVariantMap(
155 { { QStringLiteral(
"parameters" ), mCurrentParameters },
156 { QStringLiteral(
"results" ), results }
160 handleAlgorithmResults(
algorithm(), *mTaskContext, mBatchFeedback.get(), mCurrentParameters );
163 else if ( mBatchFeedback->isCanceled() )
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 );
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 );
175 mErrors.append( QVariantMap(
176 { { QStringLiteral(
"parameters" ), mCurrentParameters },
177 { QStringLiteral(
"errors" ), taskErrors }
184void QgsProcessingBatchAlgorithmDialogBase::taskTriggered(
QgsTask *task )
186 if ( task == mProxyTask )
190 setWindowState( ( windowState() & ~Qt::WindowMinimized ) | Qt::WindowActive );
196void QgsProcessingBatchAlgorithmDialogBase::allTasksComplete(
bool canceled )
198 mBatchFeedback.reset();
200 mTaskContext.reset();
201 mQueuedParameters.clear();
204 mProxyTask->finalize(
true );
205 mProxyTask =
nullptr;
210 pushInfo( tr(
"Batch execution completed in %1 seconds" ).arg( mTotalTimer.elapsed() / 1000.0, 2 ) );
211 if ( !mErrors.empty() )
213 reportError( tr(
"%1 executions failed. See log for further details." ).arg( mErrors.size() ),
true );
216 for (
int i = 0; i < mResults.size(); ++i )
218 loadHtmlResults( mResults.at( i ).value( QStringLiteral(
"results" ) ).toMap(), i );
221 createSummaryTable( mResults, mErrors );
225 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.
@ 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