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