Source

ttgui / tcore.py

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


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

    
    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")
    
    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")


    def __init__(self, point, size):
        self._topLeft = point
        self._size = size


    @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