Source

ttgui / tcore.py

# -*- coding: utf-8 -*-
from console import debug
from collections import namedtuple

    
_dispatch_map = {}

def dispatch(*types):
    def wrapper(func):
        def inner(*args, **kwargs):
            arg_types = tuple([type(a) for a in args])
            key = (getattr(func, "im_self", ""), func.__name__, arg_types)
            real_func =_dispatch_map.get(key)
            return real_func(*args, **kwargs)
        key = (getattr(func, "im_self", ""), func.__name__, types)
        _dispatch_map[key] = func
        return inner
    return wrapper




class TSize(object):
    __slots__ = ("width", "height")


    @dispatch(int, int)
    def __init__(self, width, height):
        self.width = width
        self.height = height


    def __cmp__(self, other):
        return cmp((self.width, self.height), (other.width, other.height))



class TPoint(object):
    __slots__ = ("x", "y")


    @dispatch(int,int)
    def __init__(self, x, y):
        self.x = x
        self.y = y


    def __cmp__(self, other):
        return cmp((self.x, self.y), (other.x, other.y))


        
        
class TRect(object):
    __slots__ = ("_topLeft", "_size")

    
    
    @dispatch(TPoint, TSize)
    def __init__(self, point, size):
        self._topLeft = point
        self._size = size


    @dispatch(int,int,int,int)
    def __init__(self, x1, y1, width, height):
        self._topLeft = TPoint(x1,y1)
        self._size = TSize(width,height)
    
        
    @property
    def size(self):
        return self._size
    
    
    @property
    def topLeft(self):
        return self._topLeft

        
    @property
    def bottomLeft(self):
        x, y = self.topLeft.x, self.topLeft.y
        return TPoint(x + self.width, y + self.height)


    @property
    def width(self):
        return self._size.width
    
    
    @property
    def height(self):
        return self._size.height

    
    @property
    def left(seft):
        return self._topLeft.x

    
    @property
    def top(self):
        return self._topLeft.y


    def inner(self, radius=1):
        tl = self._topLeft
        sz = self._size
        return TRect(
           TPoint(tl.x+radius, tl.y+radius), 
           TSize(sz.width-1-radius, sz.height-1-radius))



class TEvent(object):

    
    def __init__(self):
        self.accepted = False
        self.ignored = False


    
    def accept(self):
        self.accepted = True
    
    
    def ignore(self):
        self.ignored = True




class TEventLoop(object):
    _instance = None

    def __init__(self):
        self.queue = []


    @classmethod
    def instance(cls):
        if not cls._instance:
            cls._instance = cls()
        return cls._instance


    def postEvent(self, obj, event):
        self.queue.insert(0, (obj, event))


    def processEvent(self):
        if self.queue:
            obj, event = self.queue.pop()
            debug("processing event", event, "for", obj)
            obj.event(event)
            return True
        return False



class TObject(object):

    
    def __init__(self, parent=None):
        self._children = set()
        self._parent = parent
        self._event_filters = set()
        if parent:
            parent._children.add(self)
            
            
    def children(self):
        return list(self._children)
    
    
    def parent(self):
        return self._parent
    
    
    def dump_object_tree(self):
        def dump(obj, level=0):
            print " " * (level*2), obj
            for child in obj.children():
                dump(child, level+1)
        dump(self)

    
    def installEventFilter(self, receiver_object):
        self._event_filters.add(receiver_object)


    def postEvent(self, event):
        TEventLoop.instance().postEvent(self, event)


    def event(self, event):
        for receiver_object in self._event_filters:
            if receiver_object.eventFilter(self, event):
                return True
        return False
            
    
    def eventFilter(self, obj, event):
        pass
    






    
if __name__ == "__main__":
    @dispatch(int)
    def test(v):
        print v, "is int"
        
    @dispatch(float)
    def test(v):
        print v, "is float"
        

    test(1)
    test(1.0)