1# -*- coding: utf-8 -*-

2

3"""

4***************************************************************************

5 fromfunction.py

6 ---------------------

7 Date : May 2018

8 Copyright : (C) 2018 by Denis Rouzaud

9 Email : denis@opengis.ch

10***************************************************************************

11* *

12* This program is free software; you can redistribute it and/or modify *

13* it under the terms of the GNU General Public License as published by *

14* the Free Software Foundation; either version 2 of the License, or *

15* (at your option) any later version. *

16* *

17***************************************************************************

18"""

19

20 from .qgstaskwrapper import QgsTaskWrapper

21 from qgis._core import QgsTask

22

23

24@staticmethod

25 def fromFunction(description, function, *args, on_finished=None, flags=QgsTask.AllFlags, **kwargs):

26 """

27 Creates a new QgsTask task from a python function.

28

29 Example:

30

31 def calculate(task):

32 # pretend this is some complex maths and stuff we want

33 # to run in the background

34 return 5*6

35

36 def calculation_finished(exception, value=None):

37 if not exception:

38 iface.messageBar().pushMessage(

39 'the magic number is {}'.format(value))

40 else:

41 iface.messageBar().pushMessage(

42 str(exception))

43

44 task = QgsTask.fromFunction('my task', calculate,

45 on_finished=calculation_finished)

46 QgsApplication.taskManager().addTask(task)

47

48 """

49

50 assert function

51 return QgsTaskWrapper(description, flags, function, on_finished, *args, **kwargs)