From 5f7aa45aabf4acd2240563a5ac1ef23215bc662a Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 21 Dec 2017 10:44:00 +1000 Subject: [PATCH] [needs-docs][processing] Add methods to show algorithm dialog Adds processing.createAlgorithmDialog and processing.execAlgorithmDialog. These methods can be used to create and execute algorithm dialogs for a specified algorithm, optionally pre-populated with a given set of (non-default) parameter values. --- python/plugins/processing/tools/general.py | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/python/plugins/processing/tools/general.py b/python/plugins/processing/tools/general.py index 7ff4388b9b44..366ccadd230b 100644 --- a/python/plugins/processing/tools/general.py +++ b/python/plugins/processing/tools/general.py @@ -38,6 +38,8 @@ QgsProject) from processing.core.Processing import Processing from processing.gui.Postprocessing import handleAlgorithmResults +from processing.gui.AlgorithmDialog import AlgorithmDialog +from qgis.utils import iface def algorithmHelp(id): @@ -105,3 +107,54 @@ def runAndLoadResults(algOrName, parameters, feedback=None, context=None): parameters[param.name()] = p return Processing.runAlgorithm(alg, parameters=parameters, onFinish=handleAlgorithmResults, feedback=feedback, context=context) + + +def createAlgorithmDialog(algOrName, parameters={}): + """Creates and returns an algorithm dialog for the specified algorithm, prepopulated + with a given set of parameters. It is the caller's responsibility to execute + and delete this dialog. + """ + if isinstance(algOrName, QgsProcessingAlgorithm): + alg = algOrName + else: + alg = QgsApplication.processingRegistry().createAlgorithmById(algOrName) + + if alg is None: + return False + + dlg = alg.createCustomParametersWidget(iface.mainWindow()) + + if not dlg: + dlg = AlgorithmDialog(alg) + + dlg.setParameters(parameters) + + return dlg + + +def execAlgorithmDialog(algOrName, parameters={}): + """Executes an algorithm dialog for the specified algorithm, prepopulated + with a given set of parameters. + + Returns the algorithm's results. + """ + dlg = createAlgorithmDialog(algOrName, parameters) + if dlg is None: + return {} + + canvas = iface.mapCanvas() + prevMapTool = canvas.mapTool() + dlg.show() + dlg.exec_() + if canvas.mapTool() != prevMapTool: + try: + canvas.mapTool().reset() + except: + pass + canvas.setMapTool(prevMapTool) + + results = dlg.results() + # have to manually delete the dialog - otherwise it's owned by the + # iface mainWindow and never deleted + dlg.deleteLater() + return results