mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
Add simple python method QgsTask.fromFunction for creation of tasks
from a function without having to create a QgsTask subclass
This commit is contained in:
parent
e29dd79432
commit
5d4689294d
@ -183,3 +183,56 @@ class edit(object):
|
||||
else:
|
||||
self.layer.rollBack()
|
||||
return False
|
||||
|
||||
|
||||
class QgsTaskException(Exception):
|
||||
|
||||
def __init__(self, msg):
|
||||
self.msg = msg
|
||||
|
||||
def __str__(self):
|
||||
return self.msg
|
||||
|
||||
|
||||
class QgsTaskResult(Exception):
|
||||
|
||||
def __init__(self, r):
|
||||
self.r = r
|
||||
|
||||
def result(self):
|
||||
return self.r
|
||||
|
||||
|
||||
class QgsTaskWrapper(QgsTask):
|
||||
|
||||
def __init__(self, description, function, *extraArgs):
|
||||
QgsTask.__init__(self, description, QgsTask.ProgressReport)
|
||||
self.extraArgs = extraArgs
|
||||
self.function = function
|
||||
self.task_result = None
|
||||
self.task_error = None
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
for status in self.function(*self.extraArgs):
|
||||
self.setProgress(status)
|
||||
except QgsTaskException as e:
|
||||
self.task_error = e.msg
|
||||
self.stopped()
|
||||
except QgsTaskResult as r:
|
||||
self.task_result = r.result()
|
||||
self.completed()
|
||||
else:
|
||||
self.completed()
|
||||
|
||||
def result(self):
|
||||
return self.task_result
|
||||
|
||||
def error(self):
|
||||
return self.task_error
|
||||
|
||||
|
||||
def fromFunction(cls, description, function, extraArgs):
|
||||
return QgsTaskWrapper(description, function, extraArgs)
|
||||
|
||||
QgsTask.fromFunction = classmethod(fromFunction)
|
||||
|
Loading…
x
Reference in New Issue
Block a user