Refactor to generate the Widgets
This commit is contained in:
parent
381b02a15c
commit
0644bdc68f
2 changed files with 125 additions and 65 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import logging
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
|
@ -90,6 +91,43 @@ class AlertsModule:
|
|||
self.icon_size = 50
|
||||
self.alerts = alerts
|
||||
|
||||
def draw_alert(self, alert, width, image, draw, fnt_R, fnt_B, align, font_size):
|
||||
if "icon" in alert and alert["icon"] is not None:
|
||||
if callable(alert["icon"]):
|
||||
icon_img = alert["icon"](size=int(font_size*self.icon_size/16))
|
||||
image.paste(icon_img, (int(self.icon_size / 2 - font_size*self.icon_size/32), align))
|
||||
else:
|
||||
color_img = Image.new('RGB', (self.icon_size, self.icon_size), "#fff")
|
||||
icon_img = Image.open("icons/" + alert["icon"]).resize((self.icon_size, self.icon_size))
|
||||
image.paste(color_img, (0, align - 5), icon_img)
|
||||
|
||||
if "title" in alert:
|
||||
draw.text(
|
||||
((self.icon_size if alert["icon"] is not None else 0) - 5, align),
|
||||
alert["title"],
|
||||
fill="white", anchor="lt", font=fnt_B
|
||||
)
|
||||
if "subtitle" in alert and alert["subtitle"]:
|
||||
draw.text(
|
||||
((self.icon_size if alert["icon"] is not None else 0) + (fnt_B.getsize(alert["title"])[0] if "title" in alert else 0), align + 3),
|
||||
alert["subtitle"],
|
||||
fill="white", anchor="lt", font=fnt_R
|
||||
)
|
||||
|
||||
if "title" in alert:
|
||||
align += fnt_B.getsize(alert["title"])[1]
|
||||
|
||||
align += display_longtext(
|
||||
draw,
|
||||
(self.icon_size - 5, align),
|
||||
alert["description"],
|
||||
fill="white", font=fnt_R,
|
||||
maxwidth=width-self.icon_size-5
|
||||
)
|
||||
|
||||
return align + 7
|
||||
|
||||
|
||||
def draw_module(self, config, width, height, font_size=16):
|
||||
image = Image.new('RGBA', (width, height), "#000")
|
||||
draw = ImageDraw.Draw(image)
|
||||
|
|
@ -97,46 +135,32 @@ class AlertsModule:
|
|||
|
||||
more_alerts = 0
|
||||
if len(self.alerts) > 0:
|
||||
fnt_R = ImageFont.truetype(config.fnt_R_path, font_size)
|
||||
fnt_B = ImageFont.truetype(config.fnt_RB_path, font_size)
|
||||
fnt_R = ImageFont.truetype(config.fnt_R_path, font_size)
|
||||
fnt_B = ImageFont.truetype(config.fnt_RB_path, font_size)
|
||||
|
||||
align = 9
|
||||
for alert in self.alerts:
|
||||
if alert["icon"] is not None:
|
||||
if callable(alert["icon"]):
|
||||
icon_img = alert["icon"](size=int(font_size*self.icon_size/16))
|
||||
image.paste(icon_img, (int(self.icon_size / 2 - font_size*self.icon_size/32), align))
|
||||
else:
|
||||
color_img = Image.new('RGB', (self.icon_size, self.icon_size), "#fff")
|
||||
icon_img = Image.open("icons/" + alert["icon"]).resize((self.icon_size, self.icon_size))
|
||||
image.paste(color_img, (0, align), icon_img)
|
||||
align = 7
|
||||
for alert in self.alerts:
|
||||
args = []
|
||||
if isinstance(alert, dict) and "module" in alert:
|
||||
args = alert["args"] if "args" in alert else []
|
||||
alert = alert["module"]
|
||||
|
||||
if "title" in alert:
|
||||
draw.text(
|
||||
((self.icon_size if alert["icon"] is not None else 0) - 5, align),
|
||||
alert["title"],
|
||||
fill="white", anchor="lt", font=fnt_B
|
||||
)
|
||||
if "subtitle" in alert and alert["subtitle"]:
|
||||
draw.text(
|
||||
((self.icon_size if alert["icon"] is not None else 0) + (fnt_B.getsize(alert["title"])[0] if "title" in alert else 0), align + 3),
|
||||
alert["subtitle"],
|
||||
fill="white", anchor="lt", font=fnt_R
|
||||
)
|
||||
if isinstance(alert, type):
|
||||
try:
|
||||
for alert in alert(*args).gen_alerts():
|
||||
align = self.draw_alert(alert, width, image, draw, fnt_R, fnt_B, align, font_size)
|
||||
except BaseException as e:
|
||||
logging.exception(e)
|
||||
self.alerts.append({
|
||||
"title": "Impossible de générer les alertes de " + alert.__name__,
|
||||
"description": type(e).__name__ + ": " + (e.message if hasattr(e, 'message') else str(e)),
|
||||
"icon": "wi-earthquake.png",
|
||||
})
|
||||
else:
|
||||
align = self.draw_alert(alert, width, image, draw, fnt_R, fnt_B, align, font_size)
|
||||
|
||||
if "title" in alert:
|
||||
align += fnt_B.getsize(alert["title"])[1]
|
||||
|
||||
align += display_longtext(
|
||||
draw,
|
||||
(self.icon_size - 5, align),
|
||||
alert["description"],
|
||||
fill="white", font=fnt_R,
|
||||
maxwidth=width-self.icon_size-5
|
||||
)
|
||||
align += 7
|
||||
if align > height:
|
||||
more_alerts += 1
|
||||
if align > height:
|
||||
more_alerts += 1
|
||||
|
||||
image = image.crop((0,0,width, min(align, height)))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue