Initial implementation of icons.py
This commit is contained in:
parent
4c7f997deb
commit
1bb12ea4fa
1 changed files with 90 additions and 0 deletions
90
py/hexagram/icons.py
Normal file
90
py/hexagram/icons.py
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue