Minor cleanup in processing plugin python code

This commit is contained in:
Valentin Buira 2025-08-26 14:12:05 +02:00 committed by Nyall Dawson
parent 0503771bf1
commit 1ddc5ad90c
2 changed files with 125 additions and 119 deletions

View File

@ -444,74 +444,76 @@ class ProcessingPlugin(QObject):
.createAlgorithmById(alg_id, config)
)
if alg is not None:
if not alg:
return
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr("Error executing algorithm"))
dlg.setMessage(
self.tr(
"<h3>This algorithm cannot " "be run :-( </h3>\n{0}"
).format(message)
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr("Error executing algorithm"))
dlg.setMessage(
self.tr("<h3>This algorithm cannot " "be run :-( </h3>\n{0}").format(
message
)
dlg.exec()
return
)
dlg.exec()
return
if as_batch:
dlg = BatchAlgorithmDialog(alg, iface.mainWindow())
dlg.show()
dlg.exec()
else:
in_place_input_parameter_name = "INPUT"
if hasattr(alg, "inputParameterName"):
in_place_input_parameter_name = alg.inputParameterName()
if as_batch:
dlg = BatchAlgorithmDialog(alg, iface.mainWindow())
dlg.show()
dlg.exec()
return
if in_place and not [
d
for d in alg.parameterDefinitions()
if d.name() not in (in_place_input_parameter_name, "OUTPUT")
]:
parameters = {}
feedback = MessageBarProgress(algname=alg.displayName())
ok, results = execute_in_place(alg, parameters, feedback=feedback)
if ok:
iface.messageBar().pushSuccess(
"",
self.tr(
"{algname} completed. %n feature(s) processed.",
n=results["__count"],
).format(algname=alg.displayName()),
)
feedback.close()
# MessageBarProgress handles errors
return
in_place_input_parameter_name = "INPUT"
if hasattr(alg, "inputParameterName"):
in_place_input_parameter_name = alg.inputParameterName()
if alg.countVisibleParameters() > 0:
dlg = alg.createCustomParametersWidget(parent)
if in_place and not [
d
for d in alg.parameterDefinitions()
if d.name() not in (in_place_input_parameter_name, "OUTPUT")
]:
parameters = {}
feedback = MessageBarProgress(algname=alg.displayName())
ok, results = execute_in_place(alg, parameters, feedback=feedback)
if ok:
iface.messageBar().pushSuccess(
"",
self.tr(
"{algname} completed. %n feature(s) processed.",
n=results["__count"],
).format(algname=alg.displayName()),
)
feedback.close()
# MessageBarProgress handles errors
return
if not dlg:
dlg = AlgorithmDialog(alg, in_place, iface.mainWindow())
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except Exception:
pass
try:
canvas.setMapTool(prevMapTool)
except RuntimeError:
pass
else:
feedback = MessageBarProgress(algname=alg.displayName())
context = dataobjects.createContext(feedback)
parameters = {}
ret, results = execute(alg, parameters, context, feedback)
handleAlgorithmResults(alg, context, feedback)
feedback.close()
if alg.countVisibleParameters() > 0:
dlg = alg.createCustomParametersWidget(parent)
if not dlg:
dlg = AlgorithmDialog(alg, in_place, iface.mainWindow())
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except Exception:
pass
try:
canvas.setMapTool(prevMapTool)
except RuntimeError:
pass
else:
feedback = MessageBarProgress(algname=alg.displayName())
context = dataobjects.createContext(feedback)
parameters = {}
ret, results = execute(alg, parameters, context, feedback)
handleAlgorithmResults(alg, context, feedback)
feedback.close()
def sync_in_place_button_state(self, layer=None):
"""Synchronise the button state with layer state"""

View File

@ -114,30 +114,32 @@ class AlgorithmLocatorFilter(QgsLocatorFilter):
def triggerResult(self, result):
alg = QgsApplication.processingRegistry().createAlgorithmById(result.userData)
if alg:
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr("Missing dependency"))
dlg.setMessage(message)
dlg.exec()
return
dlg = alg.createCustomParametersWidget(parent=iface.mainWindow())
if not dlg:
dlg = AlgorithmDialog(alg, parent=iface.mainWindow())
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
if not alg:
return
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr("Missing dependency"))
dlg.setMessage(message)
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
try:
canvas.setMapTool(prevMapTool)
except RuntimeError:
pass
return
dlg = alg.createCustomParametersWidget(parent=iface.mainWindow())
if not dlg:
dlg = AlgorithmDialog(alg, parent=iface.mainWindow())
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
try:
canvas.setMapTool(prevMapTool)
except RuntimeError:
pass
class InPlaceAlgorithmLocatorFilter(QgsLocatorFilter):
@ -221,39 +223,41 @@ class InPlaceAlgorithmLocatorFilter(QgsLocatorFilter):
alg = QgsApplication.processingRegistry().createAlgorithmById(
result.userData, config
)
if alg:
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr("Missing dependency"))
dlg.setMessage(message)
dlg.exec()
return
if not alg:
return
in_place_input_parameter_name = "INPUT"
if hasattr(alg, "inputParameterName"):
in_place_input_parameter_name = alg.inputParameterName()
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr("Missing dependency"))
dlg.setMessage(message)
dlg.exec()
return
if [
d
for d in alg.parameterDefinitions()
if d.name() not in (in_place_input_parameter_name, "OUTPUT")
]:
dlg = alg.createCustomParametersWidget(parent=iface.mainWindow())
if not dlg:
dlg = AlgorithmDialog(alg, True, parent=iface.mainWindow())
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
else:
feedback = MessageBarProgress(algname=alg.displayName())
parameters = {}
execute_in_place(alg, parameters, feedback=feedback)
feedback.close()
in_place_input_parameter_name = "INPUT"
if hasattr(alg, "inputParameterName"):
in_place_input_parameter_name = alg.inputParameterName()
if [
d
for d in alg.parameterDefinitions()
if d.name() not in (in_place_input_parameter_name, "OUTPUT")
]:
dlg = alg.createCustomParametersWidget(parent=iface.mainWindow())
if not dlg:
dlg = AlgorithmDialog(alg, True, parent=iface.mainWindow())
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
else:
feedback = MessageBarProgress(algname=alg.displayName())
parameters = {}
execute_in_place(alg, parameters, feedback=feedback)
feedback.close()