From 08d30c3b9fadccbeda2ac9c6b113fd546d243b9a Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 21 Mar 2018 19:22:14 +1000 Subject: [PATCH] [processing] Add method for algorithms to preprocess parameter values Allows algorithms to pre-processes a set of parameters, allowing the algorithm to clean their values. This method is automatically called after users enter parameters, e.g. via the algorithm dialog. This method should NOT be called manually by algorithms. --- python/core/processing/qgsprocessingalgorithm.sip.in | 9 +++++++++ python/plugins/processing/gui/AlgorithmDialog.py | 2 +- python/plugins/processing/gui/BatchAlgorithmDialog.py | 2 ++ src/core/processing/qgsprocessingalgorithm.cpp | 5 +++++ src/core/processing/qgsprocessingalgorithm.h | 9 +++++++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/python/core/processing/qgsprocessingalgorithm.sip.in b/python/core/processing/qgsprocessingalgorithm.sip.in index b36cdaad529..0743c780d92 100644 --- a/python/core/processing/qgsprocessingalgorithm.sip.in +++ b/python/core/processing/qgsprocessingalgorithm.sip.in @@ -203,6 +203,15 @@ filled with explanatory text if validation fails. Overridden implementations should also check this base class implementation. :return: true if parameters are acceptable for the algorithm. +%End + + virtual QVariantMap preprocessParameters( const QVariantMap ¶meters ); +%Docstring +Pre-processes a set of ``parameters``, allowing the algorithm to clean their +values. + +This method is automatically called after users enter parameters, e.g. via the algorithm +dialog. This method should NOT be called manually by algorithms. %End QgsProcessingProvider *provider() const; diff --git a/python/plugins/processing/gui/AlgorithmDialog.py b/python/plugins/processing/gui/AlgorithmDialog.py index 632ec448f88..bc52d8477f1 100644 --- a/python/plugins/processing/gui/AlgorithmDialog.py +++ b/python/plugins/processing/gui/AlgorithmDialog.py @@ -116,7 +116,7 @@ class AlgorithmDialog(QgsProcessingAlgorithmDialogBase): if value: parameters[param.name()] = value - return parameters + return self.algorithm().preprocessParameters(parameters) def checkExtentCRS(self): unmatchingCRS = False diff --git a/python/plugins/processing/gui/BatchAlgorithmDialog.py b/python/plugins/processing/gui/BatchAlgorithmDialog.py index d6a59791b7f..23d731fa5c7 100644 --- a/python/plugins/processing/gui/BatchAlgorithmDialog.py +++ b/python/plugins/processing/gui/BatchAlgorithmDialog.py @@ -134,6 +134,8 @@ class BatchAlgorithmDialog(QgsProcessingAlgorithmDialogBase): self.setProgressText(QCoreApplication.translate('BatchAlgorithmDialog', '\nProcessing algorithm {0}/{1}…').format(count + 1, len(alg_parameters))) self.setInfo(self.tr('Algorithm {0} starting…').format(self.algorithm().displayName()), escapeHtml=False) + parameters = self.algorithm().preprocessParameters(parameters) + feedback.pushInfo(self.tr('Input parameters:')) feedback.pushCommandInfo(pformat(parameters)) feedback.pushInfo('') diff --git a/src/core/processing/qgsprocessingalgorithm.cpp b/src/core/processing/qgsprocessingalgorithm.cpp index bce86850e1c..d2810d16653 100644 --- a/src/core/processing/qgsprocessingalgorithm.cpp +++ b/src/core/processing/qgsprocessingalgorithm.cpp @@ -98,6 +98,11 @@ bool QgsProcessingAlgorithm::checkParameterValues( const QVariantMap ¶meters return true; } +QVariantMap QgsProcessingAlgorithm::preprocessParameters( const QVariantMap ¶meters ) +{ + return parameters; +} + QgsProcessingProvider *QgsProcessingAlgorithm::provider() const { return mProvider; diff --git a/src/core/processing/qgsprocessingalgorithm.h b/src/core/processing/qgsprocessingalgorithm.h index 853f4ee3aaa..98777f30c96 100644 --- a/src/core/processing/qgsprocessingalgorithm.h +++ b/src/core/processing/qgsprocessingalgorithm.h @@ -232,6 +232,15 @@ class CORE_EXPORT QgsProcessingAlgorithm virtual bool checkParameterValues( const QVariantMap ¶meters, QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const; + /** + * Pre-processes a set of \a parameters, allowing the algorithm to clean their + * values. + * + * This method is automatically called after users enter parameters, e.g. via the algorithm + * dialog. This method should NOT be called manually by algorithms. + */ + virtual QVariantMap preprocessParameters( const QVariantMap ¶meters ); + /** * Returns the provider to which this algorithm belongs. */