18#include <nlohmann/json.hpp>
29#include "moc_qgsprocessingbatchalgorithmdialogbase.cpp"
31using namespace Qt::StringLiterals;
34QgsProcessingBatchAlgorithmDialogBase::QgsProcessingBatchAlgorithmDialogBase( QWidget *parent, Qt::WindowFlags flags )
35 : QgsProcessingAlgorithmDialogBase( parent, flags, QgsProcessingAlgorithmDialogBase::DialogMode::Batch )
37 mButtonRunSingle =
new QPushButton( tr(
"Run as Single Process…" ) );
38 connect( mButtonRunSingle, &QPushButton::clicked,
this, &QgsProcessingBatchAlgorithmDialogBase::runAsSingle );
39 buttonBox()->addButton( mButtonRunSingle, QDialogButtonBox::ResetRole );
43 updateRunButtonVisibility();
46QgsProcessingBatchAlgorithmDialogBase::~QgsProcessingBatchAlgorithmDialogBase() =
default;
48void QgsProcessingBatchAlgorithmDialogBase::resetAdditionalGui()
50 mButtonRunSingle->setEnabled(
true );
53void QgsProcessingBatchAlgorithmDialogBase::blockAdditionalControlsWhileRunning()
55 mButtonRunSingle->setEnabled(
false );
58void QgsProcessingBatchAlgorithmDialogBase::execute(
const QList<QVariantMap> ¶meters )
60 mQueuedParameters = parameters;
62 mTotalSteps = mQueuedParameters.size();
66 mFeedback.reset( createFeedback() );
68 mBatchFeedback = std::make_unique<QgsProcessingBatchFeedback>( mTotalSteps, mFeedback.get() );
75 blockControlsWhileRunning();
76 setExecutedAnyResult(
true );
77 cancelButton()->setEnabled(
true );
83 mTotalTimer.restart();
84 if ( mTotalSteps > 0 )
88bool QgsProcessingBatchAlgorithmDialogBase::isFinalized()
90 return mQueuedParameters.empty();
93void QgsProcessingBatchAlgorithmDialogBase::executeNext()
95 if ( mQueuedParameters.empty() || mFeedback->isCanceled() )
97 allTasksComplete(
false );
101 mBatchFeedback->setCurrentStep( mCurrentStep++ );
102 setProgressText( u
"\n"_s + tr(
"Processing algorithm %1/%2…" ).arg( mCurrentStep ).arg( mTotalSteps ) );
103 setInfo( tr(
"<b>Algorithm %1 starting…</b>" ).arg(
algorithm()->displayName() ),
false,
false );
105 pushInfo( tr(
"Input parameters:" ) );
111 mTaskContext.reset( createContext( mBatchFeedback.get() ) );
113 const QVariantMap paramsJson =
algorithm()->
asMap( mQueuedParameters.constFirst(), *mTaskContext ).value( u
"inputs"_s ).toMap();
115 pushInfo( QString() );
118 mQueuedParameters.pop_front();
120 mCurrentStepTimer.restart();
125 onTaskComplete(
false, {} );
128 setCurrentTask( task );
135 const QVariantMap results =
algorithm()->
run( mCurrentParameters, *mTaskContext, mBatchFeedback.get(), &ok );
136 onTaskComplete( ok, results );
140void QgsProcessingBatchAlgorithmDialogBase::algExecuted(
bool successful,
const QVariantMap &results )
143 QgsProcessingAlgorithmDialogBase::algExecuted( successful, results );
144 onTaskComplete( successful, results );
147void QgsProcessingBatchAlgorithmDialogBase::onTaskComplete(
bool ok,
const QVariantMap &results )
151 setInfo( tr(
"Algorithm %1 correctly executed…" ).arg(
algorithm()->displayName() ),
false,
false );
152 pushInfo( tr(
"Execution completed in %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ) );
153 pushInfo( tr(
"Results:" ) );
156 pushInfo( QString() );
158 mResults.append( QVariantMap(
159 { { u
"parameters"_s, mCurrentParameters },
160 { u
"results"_s, results }
164 handleAlgorithmResults(
algorithm(), *mTaskContext, mBatchFeedback.get(), mCurrentParameters );
167 else if ( mBatchFeedback->isCanceled() )
169 setInfo( tr(
"Algorithm %1 canceled…" ).arg(
algorithm()->displayName() ),
false,
false );
170 pushInfo( tr(
"Execution canceled after %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ) );
171 allTasksComplete(
true );
175 const QStringList taskErrors = mBatchFeedback->popErrors();
176 setInfo( tr(
"Algorithm %1 failed…" ).arg(
algorithm()->displayName() ),
false,
false );
177 reportError( tr(
"Execution failed after %1 seconds" ).arg( mCurrentStepTimer.elapsed() / 1000.0, 2 ),
false );
179 mErrors.append( QVariantMap(
180 { { u
"parameters"_s, mCurrentParameters },
181 { u
"errors"_s, taskErrors }
188void QgsProcessingBatchAlgorithmDialogBase::taskTriggered(
QgsTask *task )
190 if ( task == mProxyTask )
194 setWindowState( ( windowState() & ~Qt::WindowMinimized ) | Qt::WindowActive );
200void QgsProcessingBatchAlgorithmDialogBase::allTasksComplete(
bool canceled )
202 mBatchFeedback.reset();
204 mTaskContext.reset();
205 mQueuedParameters.clear();
208 mProxyTask->finalize(
true );
209 mProxyTask =
nullptr;
214 pushInfo( tr(
"Batch execution completed in %1 seconds" ).arg( mTotalTimer.elapsed() / 1000.0, 2 ) );
215 if ( !mErrors.empty() )
217 reportError( tr(
"%1 executions failed. See log for further details." ).arg( mErrors.size() ),
true );
220 for (
int i = 0; i < mResults.size(); ++i )
222 loadHtmlResults( mResults.at( i ).value( u
"results"_s ).toMap(), i );
225 createSummaryTable( mResults, mErrors );
229 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