Skip to content

Commit a0d0145

Browse files
author
cpolymeris@gmail.com
committedJul 13, 2012
Dedup Sextante.runalg and Sextante.runandload
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@300 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent d40660c commit a0d0145

File tree

1 file changed

+29
-69
lines changed

1 file changed

+29
-69
lines changed
 

‎src/sextante/core/Sextante.py

Lines changed: 29 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,8 @@ def alghelp(name):
245245
else:
246246
print "Algorithm not found"
247247

248-
249248
@staticmethod
250-
def runalg(algOrName, *args):
249+
def runAlgorithm(algOrName, onFinish, *args):
251250
if isinstance(algOrName, GeoAlgorithm):
252251
alg = algOrName
253252
else:
@@ -287,27 +286,50 @@ def runalg(algOrName, *args):
287286

288287
msg = alg.checkParameterValuesBeforeExecuting()
289288
if msg:
290-
print ("Unable to execute algorithm\n" + msg)
291-
return
292-
289+
try:
290+
QMessageBox.critical(None, "Unable to execute algorithm", msg)
291+
return
292+
except:
293+
print ("Unable to execute algorithm\n" + msg)
294+
return
295+
293296
SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, alg.getAsCommand())
294297

295298
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
296-
if SextanteConfig.getSetting(SextanteConfig.USE_THREADS):
299+
if SextanteConfig.getSetting(SextanteConfig.USE_THREADS) and onFinish:
297300
algEx = AlgorithmExecutor(alg)
301+
progress = QProgressDialog()
302+
progress.setWindowTitle(alg.name)
303+
progress.setLabelText("Executing %s..." % alg.name)
298304
def finish():
299305
QApplication.restoreOverrideCursor()
306+
onFinish(alg)
307+
progress.close()
300308
def error(msg):
301309
QApplication.restoreOverrideCursor()
302310
print msg
303311
SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg)
312+
def cancel():
313+
try:
314+
algEx.finished.disconnect()
315+
algEx.terminate()
316+
QApplication.restoreOverrideCursor()
317+
progress.close()
318+
except:
319+
pass
304320
algEx.error.connect(error)
305321
algEx.finished.connect(finish)
306322
algEx.start()
307323
algEx.wait()
308324
else:
309325
UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress())
326+
if onFinish:
327+
onFinish(alg)
328+
return alg
310329

330+
@staticmethod
331+
def runalg(algOrName, *args):
332+
alg = Sextante.runAlgorithm(algOrName, None, *args)
311333
return alg.getOutputValuesAsDictionary()
312334

313335

@@ -329,66 +351,4 @@ def getObject(uri):
329351

330352
@staticmethod
331353
def runandload(name, *args):
332-
#a quick fix to call algorithms from the history dialog
333-
alg = Sextante.getAlgorithm(name)
334-
if alg == None:
335-
#in theory, this could not happen. Maybe we should show a message box?
336-
QMessageBox.critical(None,"Error", "Error: Algorithm not found\n")
337-
return
338-
if len(args) != len(alg.parameters) + alg.getVisibleOutputsCount():
339-
QMessageBox.critical(None,"Error", "Error: Wrong number of parameters")
340-
Sextante.alghelp(name)
341-
return
342-
343-
alg = alg.getCopy()
344-
i = 0
345-
for param in alg.parameters:
346-
if not param.setValue(args[i]):
347-
QMessageBox.critical(None, "Error", "Error: Wrong parameter value: " + args[i])
348-
return
349-
i = i +1
350-
351-
for output in alg.outputs:
352-
if not output.hidden:
353-
if not output.setValue(args[i]):
354-
QMessageBox.critical(None, "Error", "Error: Wrong output value: " + args[i])
355-
return
356-
i = i +1
357-
358-
msg = alg.checkParameterValuesBeforeExecuting()
359-
if msg:
360-
QMessageBox.critical(None, "Unable to execute algorithm", msg)
361-
return
362-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
363-
if SextanteConfig.getSetting(SextanteConfig.USE_THREADS):
364-
algEx = AlgorithmExecutor(alg)
365-
progress = QProgressDialog()
366-
progress.setWindowTitle(alg.name)
367-
progress.setLabelText("Executing %s..." % alg.name)
368-
def finish():
369-
SextantePostprocessing.handleAlgorithmResults(alg)
370-
QApplication.restoreOverrideCursor()
371-
progress.close()
372-
def error(msg):
373-
QApplication.restoreOverrideCursor()
374-
QMessageBox.critical(None, "Error", msg)
375-
SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg)
376-
cancel();
377-
def cancel():
378-
try:
379-
algEx.finished.disconnect()
380-
algEx.terminate()
381-
QApplication.restoreOverrideCursor()
382-
progress.close()
383-
except:
384-
pass
385-
algEx.error.connect(error)
386-
algEx.finished.connect(finish)
387-
algEx.textChanged.connect(lambda t: progress.setLabelText(t))
388-
algEx.percentageChanged.connect(lambda x: progress.setValue(x))
389-
progress.canceled.connect(cancel)
390-
algEx.start()
391-
progress.show()
392-
else:
393-
if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()):
394-
SextantePostprocessing.handleAlgorithmResults(alg)
354+
Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)

0 commit comments

Comments
 (0)
Please sign in to comment.