Commit 928cc842 authored by Luci Stanescu's avatar Luci Stanescu

Added (call|run)_in_gui_thread

parent 351a3cf3
......@@ -6,6 +6,8 @@ __all__ = ['Blink']
import sys
from PyQt4.QtGui import QApplication
from application import log
from application.python.util import Null
from blink.mainwindow import MainWindow
from blink.util import QSingleton
......@@ -22,4 +24,15 @@ class Blink(QApplication):
self.main_window.show()
self.exec_()
def customEvent(self, event):
handler = getattr(self, '_EH_%s' % event.name, Null)
handler(event)
def _EH_CallFunctionEvent(self, event):
try:
event.function(*event.args, **event.kw)
except:
log.error('Exception occured while calling function %s in the GUI thread' % event.function.__name__)
log.err()
# Copyright (C) 2010 AG Projects. See LICENSE for details.
#
__all__ = ['CallFunctionEvent']
from PyQt4.QtCore import QEvent
from blink.util import classproperty
class EventMeta(type(QEvent)):
def __init__(cls, name, bases, dct):
super(EventMeta, cls).__init__(name, bases, dct)
cls.id = QEvent.registerEventType() if name != 'EventBase' else None
class EventBase(QEvent):
__metaclass__ = EventMeta
def __new__(cls, *args, **kw):
if cls is EventBase:
raise TypeError("EventBase cannot be directly instantiated")
return super(EventBase, cls).__new__(cls)
def __init__(self):
super(EventBase, self).__init__(self.id)
@classproperty
def name(cls):
return cls.__name__
class CallFunctionEvent(EventBase):
def __init__(self, function, args, kw):
super(CallFunctionEvent, self).__init__()
self.function = function
self.args = args
self.kw = kw
......@@ -8,16 +8,7 @@ __all__ = ['Resources']
import os
import sys
# TODO: replace later with classproperty from sipsimple.util
def classproperty(function):
class Descriptor(object):
def __get__(self, instance, owner):
return function(owner)
def __set__(self, instance, value):
raise AttributeError("read-only attribute cannot be set")
def __delete__(self, instance):
raise AttributeError("read-only attribute cannot be deleted")
return Descriptor()
from blink.util import classproperty
class DirectoryContextManager(str):
......
# Copyright (C) 2010 AG Projects. See LICENSE for details.
#
__all__ = ['QSingleton']
__all__ = ['QSingleton', 'classproperty', 'call_in_gui_thread', 'run_in_gui_thread']
from PyQt4.QtCore import QObject
from application.python.decorator import decorator, preserve_signature
from application.python.util import Singleton
......@@ -11,3 +12,33 @@ class QSingleton(Singleton, type(QObject)):
"""A metaclass for making Qt objects singletons"""
# TODO: replace later with classproperty from sipsimple.util
def classproperty(function):
class Descriptor(object):
def __get__(self, instance, owner):
return function(owner)
def __set__(self, instance, value):
raise AttributeError("read-only attribute cannot be set")
def __delete__(self, instance):
raise AttributeError("read-only attribute cannot be deleted")
return Descriptor()
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))
@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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment