Skip to content

Commit

Permalink
[needs-docs][processing] Add methods to show algorithm dialog
Browse files Browse the repository at this point in the history
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 5f7aa45
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions python/plugins/processing/tools/general.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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.