Skip to content
Permalink
Browse files

[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.
  • Loading branch information
nyalldawson committed Dec 21, 2017
1 parent 54910fc commit 5f7aa45aabf4acd2240563a5ac1ef23215bc662a
Showing with 53 additions and 0 deletions.
  1. +53 −0 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

0 comments on commit 5f7aa45

Please sign in to comment.
You can’t perform that action at this time.