From 1bb12ea4fa92e7b1d6053288c574143ccd2f57fa Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 4 Jan 2024 16:04:34 -0500 Subject: [PATCH] Initial implementation of icons.py --- py/hexagram/icons.py | 90 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 py/hexagram/icons.py diff --git a/py/hexagram/icons.py b/py/hexagram/icons.py new file mode 100644 index 0000000..6c3af64 --- /dev/null +++ b/py/hexagram/icons.py @@ -0,0 +1,90 @@ +import os +import cairo + +from hexagram.status import VehicleStatus +from hexagram.svg import render_to_image + +class ISO7000Icon(): + __slots__ = 'name', 'status', 'color', + + def __init__(self, name: str, status: VehicleStatus, color: str): + self.name = name + self.status = status + self.color = color + +class ISO7000(): + __slots__ = 'icons', 'width', 'height', + + __dirs__ = ('./icons', + '/usr/local/share/hexagram/icons' + '/usr/share/hexagram/icons') + + __icons__ = ( + ('abs', VehicleStatus.ABS_FAULT, '#fa0'), + ('airbag', VehicleStatus.AIRBAG_FAULT, '#fa0'), + ('beams-fog', VehicleStatus.BATTERY_FAULT, '#f00'), + ('beams-fog', VehicleStatus.BEAMS_FOG, '#0f0'), + ('beams-high', VehicleStatus.BEAMS_HIGH, '#00f'), + ('beams-low', VehicleStatus.BEAMS_LOW, '#0f0'), + ('beams-parking', VehicleStatus.BEAMS_PARKING, '#ff0'), + ('belt', VehicleStatus.BELT, '#f00'), + ('caution', VehicleStatus.CAUTION, '#fa0'), + ('cold', VehicleStatus.COLD, '#eef'), + ('coolant', VehicleStatus.COLLISION, '#f00'), + ('coolant', VehicleStatus.COOLANT_LOW, '#fa0'), + ('coolant', VehicleStatus.COOLANT_OVERHEAT, '#f00'), + ('cruise', VehicleStatus.CRUISE, '#0f0'), + ('fuel', VehicleStatus.FUEL_LOW, '#fa0'), + ('lane-departure', VehicleStatus.LANEKEEP_OFF, '#fa0'), + ('mil', VehicleStatus.ENGINE_FAULT, '#fa0'), + ('oil', VehicleStatus.OIL_LOW, '#fa0'), + ('oil', VehicleStatus.OIL_OVERHEAT, '#f00'), + ('stability', VehicleStatus.STABILITY_OFF, '#fa0'), + ('tpms', VehicleStatus.TPMS_WARNING, '#f00'), + ('traction', VehicleStatus.TRACTION_OFF, '#fa0'), + ('traction', VehicleStatus.TRACTION_FAULT, '#f00'), + ('warning', VehicleStatus.WARNING, '#f00'), + ('wiper-washer', VehicleStatus.WIPER_WASHER_LOW, '#fa0'), + ) + + @staticmethod + def _locate(name: str): + for dir in ISO7000.__dirs__: + path = "%s/%s.svg" % (dir, name) + + if os.path.exists(path): + return path + + raise FileNotFoundError(name + '.svg') + + def __init__(self, width: float, height: float): + self.icons = dict() + self.width = width + self.height = height + + for item in self.__icons__: + icon = ISO7000Icon(*item) + style = 'rect,path,circle{stroke:%(s)s;fill:%(s)s}' % {'s': icon.color} + path = ISO7000._locate(icon.name) + + self.icons[icon.name] = render_to_image(path, width, height, style) + + def __del__(self): + for name in self.icons: + self.icons[name].finish() + + def draw(self, cr: cairo.Context, name: str, x: float, y: float): + icon = self.icons[name] + + cr.set_source_surface(icon, 0, 0) + cr.rectangle(x, y, self.width, self.height) + cr.fill() + +class StatusIconBox(): + __slots__ = 'x', 'y', 'width', 'height', + + def __init__(self, x: float, y: float, width: float, height: float): + self.x = x + self.y = y + self.width = width + self.height = height