1. Henning Schröder
  2. ttgui

Commits

Henning Schröder  committed 6cbf198

slots, colors and dedicated draw methods in paint engine

  • Participants
  • Parent commits 54e3e82
  • Branches default

Comments (0)

Files changed (8)

File tcore/__init__.py

View file
     def __cmp__(self, other):
         return cmp((self.width, self.height), (other.width, other.height))
 
+    def __repr__(self):
+        return "<%s %sx%s>" % (self.__class__.__name__, self.width, self.height)
+
 
 class TPoint(object):
     __slots__ = ("x", "y")
         self.x = x
         self.y = y
 
+    def __repr__(self):
+        return "<%s %s,%s>" % (self.__class__.__name__, self.x, self.y)
+
     def __cmp__(self, other):
         return cmp((self.x, self.y), (other.x, other.y))
 
     def __cmp__(self, other):
         return cmp((self._topLeft, self._size), (other._topLeft, other._size))
 
+    def __rect__(self):
+        return "<%s topLeft=%r size=%r>" % (self.__class__.__name__,
+                                            self._topLeft, self._size)
+
     @property
     def size(self):
         return self._size
 
 
 class TEvent(object):
+    __slots__ = ("kind", "accepted", "ignored")
 
     def __init__(self, kind):
         self.kind = kind
 
 class TInvokeEvent(TEvent):
 
+    __slots__ = TEvent.__slots__ + ("slot", "args")
+
+
     def __init__(self, slot, args):
         super(TInvokeEvent, self).__init__(-1)
         self.slot = slot
 
 class Property(object):
 
+    __slots__ = ("getter", "setter", "doc", "_name")
+
     def __init__(self, getter=None, setter=None, doc=None):
         self.getter = getter
         self.setter = setter
         self.doc = doc
+        self._name = None
 
     def __get__(self, obj, cls):
         if obj is None:
 
     def __set__(self, obj, value):
         self.setter(obj, value)
+        # TODO: better put this into metaclass of TObject
+        name = self._name
+        if name is None:
+            for (cls_key, cls_value) in obj.__class__.__dict__.iteritems():
+                if cls_value is self:
+                    name = cls_key
+                    self._name = name
+                    break
+        obj.propertyChanged.emit(name)
+
+    def make_setter(self, func):
+        self.setter = func
+        return func
+
+    def make_getter(self, func):
+        self.getter = func
 
 
 class BoundSignal(object):
 
+    __slots__ = ("sender", "slots")
+
     def __init__(self, sender):
         self.sender = sender
         self.slots = WeakSet()
 
 
 class Signal(object):
+    # TODO: specify args in constructor signature
 
     def __get__(self, obj, cls):
         signals = obj.__dict__.setdefault("__signals__", {})
-        objsig = signals.get(self)
-        if not objsig:
-            objsig = BoundSignal(obj)
-            signals[self] = objsig
-        return objsig
+        bound_signal = signals.get(self)
+        if not bound_signal:
+            bound_signal = BoundSignal(obj)
+            signals[self] = bound_signal
+        return bound_signal
 
 
 
     _sender = None
 
     destroyed = Signal()
+    propertyChanged = Signal()
 
     def __init__(self, parent=None, **kwargs):
         TObject._object_instances.add(self)

File test.py

View file
-from tcore import TPoint, TSize
 from tgui import TApplication, TAction
 from tgui import TCheckBox, TPushButton
 from tgui import TLabel, TLineEdit, TMenu, TMenuBar

File tgui/base.py

View file
 TColor.RED = TColor("dark red")
 TColor.GREEN = TColor("dark green")
 TColor.BLUE = TColor("blue")
+TColor.DARKGREY = TColor("dark gray")
 
 
 class TPalette(object):
     def disabled(self):
         return TColorScheme(TColor.WHITE.value, TColor.GRAY.value)
 
+    def button(self):
+        return TColorScheme(TColor.BLACK.value, TColor.GREEN.value)
+
+    def desktop(self):
+        return TColorScheme(TColor.WHITE.value, TColor.DARKGREY.value)
 
 
 class TCursor(object):

File tgui/buttons.py

View file
 
     def __init__(self, parent, **kwargs):
         super(TCheckBox, self).__init__(parent, checkable=True, **kwargs)
+        self.setCursorPosition((1, 0))
 
     def setChecked(self, flag):
         super(TCheckBox, self).setChecked(flag)
 
     def paintEvent(self, event):
         painter = TPainter(self)
-        checked = self._checked and "x" or " "
-        painter.drawText(self.pos(), "[%s] %s" % (checked, self.text()))
+        pos = self.mapToGlobal(self.pos())
+        size = self.size()
+        size.height = 1
+        painter.engine.draw_checkbox(pos.x, pos.y, size.width, size.height, self.text(),
+                                     self.isChecked(), pal=self.palette())
 
 
 class TPushButton(TAbstractButton):
 
     def paintEvent(self, event):
         painter = TPainter(self)
-        painter.drawText(self.pos(), "< %s >" % self.text())
+        pos = self.mapToGlobal(self.pos())
+        size = self.size()
+        size.height = 1
+        painter.engine.draw_button(pos.x, pos.y, size.width, size.height, self.text(), pal=self.palette())
 
 
 class TRadioButton(TAbstractButton):
 
     def __init__(self, parent):
         super(TRadioButton, self).__init__(self, parent, checkable=True)
+        self.setCursorPosition((1, 0))
 
     def paintEvent(self, event):
         painter = TPainter(self)
-        checked = self._checked and "x" or " "
-        painter.drawText(self.pos(), "(%s) %s" % (checked, self.text()))
+        pos = self.mapToGlobal(self.pos())
+        size = self.size()
+        size.height = 1
+        painter.engine.draw_radiobutton(pos.x, pos.y, size.width, size.height, self.text(), pal=self.palette())
 
 
 class TToolButton(TAbstractButton):

File tgui/cursesengine.py

View file
         self._x = x
         self._y = y
         #self.window.move(y, x)
-        self.text(x, y, "")
+        self.text(x, y, u"")
 
     def show_cursor(self, block=True):
         if block:
         self.window.addch(lry, lrx, curses.ACS_LRCORNER, style)
         self.window.addch(lry, ulx, curses.ACS_LLCORNER, style)
 
+    def palette(self):
+        from tgui import TPalette
+        return TPalette()
+
+    def display_text(self, text, width):
+        from tgui import elide
+        if len(text) > width:
+            text = elide(text, width)
+        else:
+            text = text.ljust(width)
+        return text
+
+    def draw_window(self, x, y, width, height, pal=None):
+        from tgui import TPalette
+        if pal is None:
+            pal = TPalette()
+        self.rectangle(x, y, x + width, self.y + height, pal.window())
+
+    def draw_button(self, x, y, width, height, text,
+                    current=True, enabled=True, pal=None):
+        assert height == 1
+        pal = pal or self.palette()
+        text_width = width - 4
+        color = (pal or self.palette()).button()
+        self.text(x, y, u"< %s >" % self.display_text(text, text_width), color)
+
+    def draw_radiobutton(self, x, y, width, height, text, checked=True,
+                         current=True, enabled=True, pal=None):
+        assert height == 1
+        pal = pal or self.palette()
+        text_width = width - 4
+        color = (pal or self.palette()).text()
+        self.text(x, y, u"(%s) %s" % (checked and "*" or " ",
+                                     self.display_text(text, text_width)), color)
+
+    def draw_checkbox(self, x, y, width, height, text, checked=True,
+                         current=True, enabled=True, pal=None):
+        assert height == 1, "checkbox height is %s" % height
+        pal = pal or self.palette()
+        text_width = width - 4
+        color = (pal or self.palette()).text()
+        self.text(x, y, u"[%s] %s" % (checked and "x" or " ",
+                                     self.display_text(text, text_width)), color)
+
+    def draw_label(self, x, y, width, height, text, pal=None):
+        assert height == 1
+        pal = pal or self.palette()
+        color = pal.text()
+        self.text(x, y, self.display_text(text, width), color)
+
+    def fill_rect(self, x1, y1, width, height, char, color):
+        #x2 = x1 + width - 1
+        y2 = y1 + height - 1
+        for y in range(y1, y2):
+            self.text(x1, y, char * width, color)
+
+    def draw_desktop(self, x, y, width, height, pal=None):
+        pal = pal or self.palette()
+        self.fill_rect(x, y, width, height, " ", pal.desktop())
+
 
 if __name__ == '__main__':
     paint = TPaintEngine()

File tgui/tapplication.py

View file
 
     def paintEvent(self, event):
         painter = TPainter(self)
-        painter.fillRect(self.geometry(), " ")
+        pos = self.pos()
+        size = self.size()
+        painter.engine.draw_desktop(pos.x, pos.y, size.width, size.height)

File tgui/twidget.py

View file
 class TWidget(TObject):
 
     widgetCreated = Signal()
+    cursorPositionChanged = Signal()
 
     def __init__(self, parent=None, **kwargs):
         self._contents_margins = TMargins(0, 0, 0, 0)
         self._palette = kwargs.pop("Palette", None) or TPalette()
         self._windowTitle = u""
         self._actions = []
+        self._enabled = True
         self._cursor = TCursor.NORMAL
         self._cursorPosition = TPoint(0, 0)
         super(TWidget, self).__init__(parent, **kwargs)
     def setupUi(self):
         pass
 
+    def isEnabled(self):
+        return self._enabled
+
+    def setEnabled(self, flag=True):
+        self._enabled = flag
+
+    Enabled = Property(isEnabled, setEnabled)
+
     def cursor(self):
         return self._cursor
 
     def cursorPosition(self):
         return self._cursorPosition
 
+    @dispatch(tuple)
+    def setCursorPosition(self, x_y):
+        assert len(x_y) == 2, "got no row/col tuple"
+        self.setCursorPosition(TPoint(x_y[0], x_y[1]))
+
+
     @dispatch(TPoint)
     def setCursorPosition(self, x, y):
         self.setCursorPosition(TPoint(x, y))
     Visible = Property(isVisible, setVisible)
 
     def _update_geom(self, pos, size, silent=False):
+        assert isinstance(pos, TPoint)
+        assert isinstance(size, TSize)
         x1 = pos.x
         y1 = pos.y
         x2 = x1 + size.width

File tgui/widgets.py

View file
 
     def paintEvent(self, event):
         painter = TPainter(self)
-        painter.drawText(self.pos(), elide(self.text(), self.width()))
+        pos = self.pos()
+        size = self.size()
+        painter.engine.draw_label(pos.x, pos.y, size.width, size.height,
+                                  self.text(), pal=self.palette())
 
 
 class TFrame(TWidget):