Coverage for C:\Program Files\QGIS 3.10\apps\qgis-ltr\python\qgis\core\additions\fromfunction.py: 71%
7 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-10 14:40 +0900
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-10 14:40 +0900
1# -*- coding: utf-8 -*-
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"""
20from .qgstaskwrapper import QgsTaskWrapper
21from qgis._core import QgsTask
24@staticmethod
25def fromFunction(description, function, *args, on_finished=None, flags=QgsTask.AllFlags, **kwargs):
26 """
27 Creates a new QgsTask task from a python function.
29 Example:
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
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))
44 task = QgsTask.fromFunction('my task', calculate,
45 on_finished=calculation_finished)
46 QgsApplication.taskManager().addTask(task)
48 """
50 assert function
51 return QgsTaskWrapper(description, flags, function, on_finished, *args, **kwargs)