From faf052b01502420df74568dce4e5007c4ee91511 Mon Sep 17 00:00:00 2001 From: June Tate-Gans Date: Sat, 8 May 2021 23:00:48 -0500 Subject: [PATCH] bitwidgets: Split classes out of button There's a bunch of unrelated stuff in button, like Glyphs and ButtonBar, which should be in their own modules. --- g13gui/applet/switcher.py | 2 +- g13gui/bitwidgets/button.py | 119 +-------------------------------- g13gui/bitwidgets/buttonbar.py | 74 ++++++++++++++++++++ g13gui/bitwidgets/glyph.py | 44 ++++++++++++ g13gui/bitwidgets/listview.py | 6 +- g13gui/bitwidgets/screen.py | 2 +- 6 files changed, 125 insertions(+), 122 deletions(-) create mode 100644 g13gui/bitwidgets/buttonbar.py create mode 100644 g13gui/bitwidgets/glyph.py diff --git a/g13gui/applet/switcher.py b/g13gui/applet/switcher.py index 3882557..ec5c934 100644 --- a/g13gui/applet/switcher.py +++ b/g13gui/applet/switcher.py @@ -12,7 +12,7 @@ from g13gui.bitwidgets.display import Display from g13gui.bitwidgets.screen import Screen from g13gui.bitwidgets.label import Label from g13gui.bitwidgets.button import Button -from g13gui.bitwidgets.button import Glyphs +from g13gui.bitwidgets.glyph import Glyphs from g13gui.bitwidgets.listview import ListView gi.require_version('GLib', '2.0') diff --git a/g13gui/bitwidgets/button.py b/g13gui/bitwidgets/button.py index 6612dd3..b5ab673 100644 --- a/g13gui/bitwidgets/button.py +++ b/g13gui/bitwidgets/button.py @@ -1,126 +1,11 @@ -import enum - from builtins import property -from g13gui.bitwidgets import DISPLAY_WIDTH -from g13gui.bitwidgets import DISPLAY_HEIGHT + from g13gui.bitwidgets.widget import Widget +from g13gui.bitwidgets.glyph import Glyph from g13gui.bitwidgets.rectangle import Rectangle from g13gui.observer.subject import ChangeType -GLYPH_WIDTH = 5 -GLYPH_HEIGHT = 5 - - -class Glyphs(enum.Enum): - DOWN_ARROW = [(2, 0), (2, 4), (0, 2), (4, 2), (2, 4)] - UP_ARROW = [(2, 4), (2, 0), (4, 2), (0, 2), (2, 0)] - CHECKMARK = [(0, 3), (1, 4), (4, 1), (1, 4)] - XMARK = [(0, 0), (4, 4), (2, 2), (4, 0), (0, 4)] - BLANK = [] - - BOUNDS = (5, 5) - - def transformTo(self, offsetx, offsety): - return [(offsetx + x, offsety + y) for (x, y) in self.value] - - -class ButtonBar(Widget): - MAX_BUTTONS = 4 - TOP_LINE = 33 - - def __init__(self): - Widget.__init__(self) - self._children = [None] * ButtonBar.MAX_BUTTONS - self.position = (0, ButtonBar.TOP_LINE) - self.bounds = (DISPLAY_WIDTH, DISPLAY_HEIGHT - ButtonBar.TOP_LINE) - - def _buttonBounds(self): - width = (DISPLAY_WIDTH // ButtonBar.MAX_BUTTONS) - 2 - height = DISPLAY_HEIGHT - ButtonBar.TOP_LINE - return (width, height) - - def _positionForSlot(self, buttonNum): - slotWidth = DISPLAY_WIDTH / ButtonBar.MAX_BUTTONS - slotX = (buttonNum * slotWidth) - slotY = ButtonBar.TOP_LINE + 1 - return (int(slotX), int(slotY)) - - def button(self, buttonNum): - return self._children[buttonNum] - - def setButton(self, buttonNum, button): - if self._children[buttonNum]: - self.removeChild(self._children[buttonNum]) - - self._children[buttonNum] = button - button.position = self._positionForSlot(buttonNum) - button.bounds = self._buttonBounds() - button.parent = self - - self.addChange(ChangeType.ADD, 'child', button) - self.notifyChanged() - - def addChild(self, button): - buttonNum = self._children.index(None) - if buttonNum > ButtonBar.MAX_BUTTONS: - raise ValueError('Can\'t store another button!') - self.setButton(buttonNum, button) - - def removeChild(self, button): - buttonNum = self._children.index(button) - button = self._children[buttonNum] - self._children[buttonNum] = None - button.parent = None - - self.addChange(ChangeType.REMOVE, 'child', button) - self.notifyChanged() - - def draw(self, ctx): - if self.visible: - for child in self._children: - if child and child.visible: - child.draw(ctx) - - # Top line - ctx.line(self.position + (DISPLAY_WIDTH, - ButtonBar.TOP_LINE), - fill=1) - - # Dividing lines - for slot in range(0, ButtonBar.MAX_BUTTONS): - (x, y) = self._positionForSlot(slot) - x -= 1 - ctx.line((x, y) + (x, DISPLAY_HEIGHT), - fill=1) - - -class Glyph(Widget): - def __init__(self, x, y, glyph=Glyphs.BLANK, fill=True): - Widget.__init__(self) - self.glyph = glyph - self.position = (x, y) - self.fill = fill - - @property - def glyph(self): - return self._glyph - - @glyph.setter - def glyph(self, glyph): - self.setProperty('glyph', glyph) - - @property - def bounds(self): - return Glyphs.BOUNDS.value - - def draw(self, ctx): - if self.visible: - (x, y) = self.position - xformedGlyph = self.glyph.transformTo(x, y) - ctx.line(xformedGlyph, fill=self._fill) - - class Button(Widget): def __init__(self, glyph, fill=True): Widget.__init__(self) diff --git a/g13gui/bitwidgets/buttonbar.py b/g13gui/bitwidgets/buttonbar.py new file mode 100644 index 0000000..8624d8a --- /dev/null +++ b/g13gui/bitwidgets/buttonbar.py @@ -0,0 +1,74 @@ +from g13gui.observer.subject import ChangeType +from g13gui.bitwidgets import DISPLAY_WIDTH +from g13gui.bitwidgets import DISPLAY_HEIGHT +from g13gui.bitwidgets.widget import Widget + + +class ButtonBar(Widget): + MAX_BUTTONS = 4 + TOP_LINE = 33 + + def __init__(self): + Widget.__init__(self) + self._children = [None] * ButtonBar.MAX_BUTTONS + self.position = (0, ButtonBar.TOP_LINE) + self.bounds = (DISPLAY_WIDTH, DISPLAY_HEIGHT - ButtonBar.TOP_LINE) + + def _buttonBounds(self): + width = (DISPLAY_WIDTH // ButtonBar.MAX_BUTTONS) - 2 + height = DISPLAY_HEIGHT - ButtonBar.TOP_LINE + return (width, height) + + def _positionForSlot(self, buttonNum): + slotWidth = DISPLAY_WIDTH / ButtonBar.MAX_BUTTONS + slotX = (buttonNum * slotWidth) + slotY = ButtonBar.TOP_LINE + 1 + return (int(slotX), int(slotY)) + + def button(self, buttonNum): + return self._children[buttonNum] + + def setButton(self, buttonNum, button): + if self._children[buttonNum]: + self.removeChild(self._children[buttonNum]) + + self._children[buttonNum] = button + button.position = self._positionForSlot(buttonNum) + button.bounds = self._buttonBounds() + button.parent = self + + self.addChange(ChangeType.ADD, 'child', button) + self.notifyChanged() + + def addChild(self, button): + buttonNum = self._children.index(None) + if buttonNum > ButtonBar.MAX_BUTTONS: + raise ValueError('Can\'t store another button!') + self.setButton(buttonNum, button) + + def removeChild(self, button): + buttonNum = self._children.index(button) + button = self._children[buttonNum] + self._children[buttonNum] = None + button.parent = None + + self.addChange(ChangeType.REMOVE, 'child', button) + self.notifyChanged() + + def draw(self, ctx): + if self.visible: + for child in self._children: + if child and child.visible: + child.draw(ctx) + + # Top line + ctx.line(self.position + (DISPLAY_WIDTH, + ButtonBar.TOP_LINE), + fill=1) + + # Dividing lines + for slot in range(0, ButtonBar.MAX_BUTTONS): + (x, y) = self._positionForSlot(slot) + x -= 1 + ctx.line((x, y) + (x, DISPLAY_HEIGHT), + fill=1) diff --git a/g13gui/bitwidgets/glyph.py b/g13gui/bitwidgets/glyph.py new file mode 100644 index 0000000..bdc3e96 --- /dev/null +++ b/g13gui/bitwidgets/glyph.py @@ -0,0 +1,44 @@ +import enum + +from builtins import property + +from g13gui.bitwidgets.widget import Widget + + +class Glyphs(enum.Enum): + DOWN_ARROW = [(2, 0), (2, 4), (0, 2), (4, 2), (2, 4)] + UP_ARROW = [(2, 4), (2, 0), (4, 2), (0, 2), (2, 0)] + CHECKMARK = [(0, 3), (1, 4), (4, 1), (1, 4)] + XMARK = [(0, 0), (4, 4), (2, 2), (4, 0), (0, 4)] + BLANK = [] + + BOUNDS = (5, 5) + + def transformTo(self, offsetx, offsety): + return [(offsetx + x, offsety + y) for (x, y) in self.value] + + +class Glyph(Widget): + def __init__(self, x, y, glyph=Glyphs.BLANK, fill=True): + Widget.__init__(self) + self.glyph = glyph + self.position = (x, y) + self.fill = fill + + @property + def glyph(self): + return self._glyph + + @glyph.setter + def glyph(self, glyph): + self.setProperty('glyph', glyph) + + @property + def bounds(self): + return Glyphs.BOUNDS.value + + def draw(self, ctx): + if self.visible: + (x, y) = self.position + xformedGlyph = self.glyph.transformTo(x, y) + ctx.line(xformedGlyph, fill=self._fill) diff --git a/g13gui/bitwidgets/listview.py b/g13gui/bitwidgets/listview.py index f014b52..3f4fa94 100644 --- a/g13gui/bitwidgets/listview.py +++ b/g13gui/bitwidgets/listview.py @@ -1,7 +1,7 @@ from g13gui.bitwidgets.widget import Widget -from g13gui.bitwidgets.button import ButtonBar -from g13gui.bitwidgets.button import Glyph -from g13gui.bitwidgets.button import Glyphs +from g13gui.bitwidgets.buttonbar import ButtonBar +from g13gui.bitwidgets.glyph import Glyph +from g13gui.bitwidgets.glyph import Glyphs from g13gui.bitwidgets.rectangle import Rectangle from g13gui.bitwidgets.label import Label from g13gui.bitwidgets.fonts import Fonts diff --git a/g13gui/bitwidgets/screen.py b/g13gui/bitwidgets/screen.py index c8570aa..f3c43ae 100644 --- a/g13gui/bitwidgets/screen.py +++ b/g13gui/bitwidgets/screen.py @@ -2,7 +2,7 @@ import time from builtins import property from g13gui.bitwidgets.widget import Widget -from g13gui.bitwidgets.button import ButtonBar +from g13gui.bitwidgets.buttonbar import ButtonBar MIN_NSECS_BETWEEN_FRAMES = 125000