Skip to content

Commit

Permalink
[Processing] Allow "progress" to be specified when calling processing…
Browse files Browse the repository at this point in the history
….runalg

This is mostly so that algorithms executed in Processing scripts can
display messages in the same way as if they were executed directly from
Processing toolbox.

Also fixes a small issue with busy cursor being reset too early when
algorithms were executed from Processing scripts.
  • Loading branch information
radosuav authored and volaya committed Nov 4, 2015
1 parent e497e8b commit 507aeb0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
24 changes: 20 additions & 4 deletions python/plugins/processing/core/Processing.py
Expand Up @@ -265,12 +265,13 @@ def runandload(name, *args):
Processing.runAlgorithm(name, handleAlgorithmResults, *args)

@staticmethod
def runAlgorithm(algOrName, onFinish, *args):
def runAlgorithm(algOrName, onFinish, *args, **kwargs):
if isinstance(algOrName, GeoAlgorithm):
alg = algOrName
else:
alg = Processing.getAlgorithm(algOrName)
if alg is None:
print 'Error: Algorithm not found\n'
QgsMessageLog.logMessage(Processing.tr('Error: Algorithm {0} not found\n').format(algOrName), Processing.tr("Processing"))
return
alg = alg.getCopy()
Expand All @@ -287,6 +288,7 @@ def runAlgorithm(algOrName, onFinish, *args):
output = alg.getOutputFromName(name)
if output and output.setValue(value):
continue
print 'Error: Wrong parameter value %s for parameter %s.' % (value, name)
QgsMessageLog.logMessage(Processing.tr('Error: Wrong parameter value {0} for parameter {1}.').format(value, name), Processing.tr("Processing"))
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Expand All @@ -298,6 +300,7 @@ def runAlgorithm(algOrName, onFinish, *args):
for param in alg.parameters:
if param.name not in setParams:
if not param.setValue(None):
print ('Error: Missing parameter value for parameter %s.' % (param.name))
QgsMessageLog.logMessage(Processing.tr('Error: Missing parameter value for parameter {0}.').format(param.name), Processing.tr("Processing"))
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Expand All @@ -307,53 +310,66 @@ def runAlgorithm(algOrName, onFinish, *args):
return
else:
if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount():
print 'Error: Wrong number of parameters'
QgsMessageLog.logMessage(Processing.tr('Error: Wrong number of parameters'), Processing.tr("Processing"))
processing.alghelp(algOrName)
return
i = 0
for param in alg.parameters:
if not param.hidden:
if not param.setValue(args[i]):
print 'Error: Wrong parameter value: ' \
+ unicode(args[i])
QgsMessageLog.logMessage(Processing.tr('Error: Wrong parameter value: ') + unicode(args[i]), Processing.tr("Processing"))
return
i = i + 1

for output in alg.outputs:
if not output.hidden:
if not output.setValue(args[i]):
print 'Error: Wrong output value: ' + unicode(args[i])
QgsMessageLog.logMessage(Processing.tr('Error: Wrong output value: ') + unicode(args[i]), Processing.tr("Processing"))
return
i = i + 1

msg = alg._checkParameterValuesBeforeExecuting()
if msg:
print 'Unable to execute algorithm\n' + msg
QgsMessageLog.logMessage(Processing.tr('Unable to execute algorithm\n{0}').format(msg), Processing.tr("Processing"))
return

if not alg.checkInputCRS():
print 'Warning: Not all input layers use the same CRS.\n' \
+ 'This can cause unexpected results.'
QgsMessageLog.logMessage(Processing.tr('Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.'), Processing.tr("Processing"))

if iface is not None:
# Don't set the wait cursor twice, because then when you
# restore it, it will still be a wait cursor.
overrideCursor = False
cursor = QApplication.overrideCursor()
if cursor is None or cursor == 0:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
overrideCursor = True
elif cursor.shape() != Qt.WaitCursor:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
overrideCursor = True

progress = None
if iface is not None:
progress = MessageBarProgress(alg.name)
if kwargs is not None and "progress" in kwargs.keys():
progress = kwargs["progress"]
elif iface is not None :
progress = MessageBarProgress()
ret = runalg(alg, progress)
if ret:
if onFinish is not None:
onFinish(alg, progress)
else:
QgsMessageLog.logMessage(Processing.tr("There were errors executing the algorithm."), Processing.tr("Processing"))

if iface is not None:
if iface is not None and overrideCursor:
QApplication.restoreOverrideCursor()
if isinstance(progress, MessageBarProgress):
progress.close()
return alg

Expand Down
8 changes: 4 additions & 4 deletions python/plugins/processing/tools/general.py
Expand Up @@ -67,11 +67,11 @@ def alghelp(name):
print 'Algorithm not found'


def runalg(algOrName, *args):
alg = Processing.runAlgorithm(algOrName, None, *args)
def runalg(algOrName, *args, **kwargs):
alg = Processing.runAlgorithm(algOrName, None, *args, **kwargs)
if alg is not None:
return alg.getOutputValuesAsDictionary()


def runandload(name, *args):
return Processing.runAlgorithm(name, handleAlgorithmResults, *args)
def runandload(name, *args, **kwargs):
return Processing.runAlgorithm(name, handleAlgorithmResults, *args, **kwargs)

0 comments on commit 507aeb0

Please sign in to comment.