util.py 1.03 KB
Newer Older
1 2 3
# Copyright (C) 2010 AG Projects. See LICENSE for details.
#

4
__all__ = ['QSingleton', 'call_in_gui_thread', 'call_later', 'run_in_gui_thread']
5

6
from PyQt4.QtCore import QObject, QTimer
7
from application.python.decorator import decorator, preserve_signature
8 9 10 11 12 13 14
from application.python.util import Singleton


class QSingleton(Singleton, type(QObject)):
    """A metaclass for making Qt objects singletons"""


15 16 17 18 19 20 21
def call_in_gui_thread(function, *args, **kw):
    from blink import Blink
    from blink.event import CallFunctionEvent
    blink = Blink()
    blink.postEvent(blink, CallFunctionEvent(function, args, kw))


22 23 24 25 26
def call_later(interval, function, *args, **kw):
    interval = int(interval*1000)
    QTimer.singleShot(interval, lambda: function(*args, **kw))


27 28 29 30 31 32 33 34 35 36 37
@decorator
def run_in_gui_thread(func):
    @preserve_signature(func)
    def wrapper(*args, **kw):
        from blink import Blink
        from blink.event import CallFunctionEvent
        blink = Blink()
        blink.postEvent(blink, CallFunctionEvent(func, args, kw))
    return wrapper