64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
import cairo
|
|
|
|
from hexagram.box import Align
|
|
|
|
class Gauge():
|
|
__slots__ = 'min_value', 'max_value', 'value',
|
|
|
|
def __init__(self, min_value: float, max_value: float):
|
|
self.min_value: float = min_value
|
|
self.max_value: float = max_value
|
|
self.value: float = min_value
|
|
|
|
def set_value(self, value):
|
|
self.value = value
|
|
|
|
def draw_bg(self, cr: cairo.Context):
|
|
raise NotImplementedError
|
|
|
|
def draw_fg(self, cr: cairo.Context):
|
|
raise NotImplementedError
|
|
|
|
class TextGauge(Gauge):
|
|
__slots__ = 'x', 'y', 'align',
|
|
|
|
FONT_FACE = "Muli"
|
|
FONT_SLANT = cairo.FontSlant.NORMAL
|
|
FONT_WEIGHT = cairo.FontWeight.BOLD
|
|
FONT_SIZE = 24
|
|
|
|
def __init__(self, x: float, y: float, min_value: float, max_value: float, align: Align=Align.LEFT):
|
|
super().__init__(min_value, max_value)
|
|
|
|
self.x = x
|
|
self.y = y
|
|
self.align = align
|
|
|
|
def format_text(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
def draw_bg(self, cr: cairo.Context):
|
|
pass
|
|
|
|
def draw_fg(self, cr: cairo.Context):
|
|
cr.set_source_rgb(1, 1, 1)
|
|
|
|
cr.select_font_face(self.FONT_FACE,
|
|
self.FONT_SLANT,
|
|
self.FONT_WEIGHT)
|
|
|
|
text = self.format_text()
|
|
|
|
cr.set_font_size(self.FONT_SIZE)
|
|
|
|
extents = cr.text_extents(text)
|
|
width = extents[2] - extents[0]
|
|
|
|
if self.align is Align.LEFT:
|
|
cr.move_to(self.x, self.y)
|
|
elif self.align is Align.MIDDLE:
|
|
cr.move_to(self.x - width / 2, self.y)
|
|
elif self.align is Align.RIGHT:
|
|
cr.move_to(self.x - width, self.y)
|
|
|
|
cr.show_text(text)
|