E-Paper-Calendar/Calendar/DayHeaderDesign.py

155 lines
5.8 KiB
Python
Raw Normal View History

2019-03-07 21:28:11 +01:00
from DesignEntity import DesignEntity
from PIL import ImageDraw
from TextDesign import TextDesign
2019-03-09 21:28:02 +01:00
from WeatherColumnDesign import WeatherColumnDesign
from datetime import date, timedelta, datetime, timezone
from SingelDayEventListDesign import SingelDayEventListDesign
2019-04-08 15:42:30 +02:00
from Assets import fonts, colors, defaultfontsize
2019-04-07 16:48:07 +02:00
from settings import general_settings
2019-04-13 20:46:00 +02:00
from BoxDesign import BoxDesign
2019-03-07 21:28:11 +01:00
numberbox_ypos = 0.15
numberbox_height = 1 - 2 * numberbox_ypos
number_height = numberbox_height * 0.83
2019-05-11 21:37:16 +02:00
number_boxypos = 0.17
2019-03-07 21:31:54 +01:00
month_height = numberbox_height / 4
2019-03-07 21:28:11 +01:00
monthbox_xpadding = 0.013
2019-05-11 21:37:16 +02:00
monthbox_ypadding = -0.05
2019-03-07 21:28:11 +01:00
monthbox_width = 1 - numberbox_ypos - monthbox_xpadding
2019-03-10 13:27:15 +01:00
weathercolumn_y_size = (0.4, 1)
2019-05-11 21:37:16 +02:00
weekday_height = numberbox_height * 0.19
weekdaybox_height = (weekday_height / numberbox_height) * 1.5
2019-04-08 15:42:30 +02:00
eventlist_static_fontsize = defaultfontsize
2019-05-11 21:37:16 +02:00
eventlist_xpadding = monthbox_xpadding
eventlist_ypadding = 0.01
2019-03-07 21:28:11 +01:00
2019-04-05 11:37:51 +02:00
numberbox_font_color = colors["bg"]
numberbox_background_color = colors["hl"]
2019-03-13 22:14:39 +01:00
weekday_font = fonts["bold"]
2019-03-07 21:28:11 +01:00
2019-07-13 08:05:35 +02:00
2019-03-07 21:28:11 +01:00
class DayHeaderDesign (DesignEntity):
"""Detailed and big view of a given date."""
2019-07-13 08:05:35 +02:00
def __init__(self, size, date):
2019-03-07 21:28:11 +01:00
super(DayHeaderDesign, self).__init__(size)
2019-04-07 16:48:07 +02:00
self.weather_column_width = 0
2019-03-07 21:28:11 +01:00
self.date = date
2019-07-13 08:05:35 +02:00
def add_weather(self, weather):
2019-04-07 16:48:07 +02:00
if general_settings["weather-info"] == False:
return
2019-07-13 08:05:35 +02:00
forecast = weather.get_forecast_in_days(
self.date.day - date.today().day)
2019-04-07 16:48:07 +02:00
self.weather_column_width = weathercolumn_y_size[0] * self.size[1]
2019-07-13 08:05:35 +02:00
size = (self.weather_column_width,
weathercolumn_y_size[1] * self.size[1])
2019-03-09 21:28:02 +01:00
pos = (self.size[0] - size[0], 0)
design = WeatherColumnDesign(size, forecast)
design.pos = pos
self.draw_design(design)
2019-07-13 08:05:35 +02:00
def add_calendar(self, calendar):
local_tzinfo = datetime.now(timezone.utc).astimezone().tzinfo
now = datetime.now(local_tzinfo)
2019-07-13 08:05:35 +02:00
time_until_tomorrow = (datetime(
now.year, now.month, now.day, 0, 0, 0, 0, local_tzinfo) + timedelta(1)) - now
self.__draw_event_list__(
calendar.get_upcoming_events(time_until_tomorrow, now))
2019-07-13 08:05:35 +02:00
def add_events(self, events):
self.__draw_event_list__(events)
2019-07-13 08:05:35 +02:00
def add_rssfeed(self, rss):
pass
2019-07-13 08:05:35 +02:00
def add_crypto(self, crypto):
2019-05-18 09:28:29 +02:00
pass
2019-07-13 08:05:35 +02:00
def __finish_image__(self):
2019-03-07 21:28:11 +01:00
self.__draw_number_square__()
self.__draw_month__()
2019-07-13 08:05:35 +02:00
def __draw_event_list__(self, events):
2019-03-10 13:27:15 +01:00
box_ypos = numberbox_ypos * self.size[1]
box_xpos = numberbox_ypos * self.size[1]
box_height = numberbox_height * self.size[1]
2019-05-11 21:37:16 +02:00
xpadding = eventlist_xpadding * self.size[0]
ypadding = eventlist_ypadding * self.size[1]
monthbox_height = (monthbox_ypadding + month_height) * self.size[1]
2019-07-13 08:05:35 +02:00
pos = (box_xpos + box_height + xpadding,
box_ypos + monthbox_height + ypadding)
size = (self.size[0] - pos[0] - self.weather_column_width,
self.size[1] - pos[1] - box_ypos)
2019-04-08 15:42:30 +02:00
fontsize = eventlist_static_fontsize
2019-03-10 13:27:15 +01:00
2019-04-05 22:07:47 +02:00
rel_dates = [self.date for _ in range(len(events))]
2019-07-13 08:05:35 +02:00
event_list = SingelDayEventListDesign(
size, events, fontsize, event_prefix_rel_dates=rel_dates)
2019-03-10 13:27:15 +01:00
event_list.pos = pos
self.draw_design(event_list)
2019-07-13 08:05:35 +02:00
def __draw_month__(self):
2019-03-07 21:28:11 +01:00
font_size = int(month_height * self.size[1])
2019-05-11 21:37:16 +02:00
xpadding = int(monthbox_xpadding * self.size[0])
ypadding = int(monthbox_ypadding * self.size[1])
2019-03-07 21:28:11 +01:00
box_ypos = int(numberbox_ypos * self.size[1])
box_height = int(numberbox_height * self.size[1])
2019-05-11 21:37:16 +02:00
box_pos = (box_ypos + box_height + xpadding, box_ypos + ypadding)
2019-03-07 21:28:11 +01:00
box_size = (int(monthbox_width * self.size[0]), box_height)
2019-05-18 09:28:29 +02:00
2019-03-07 21:28:11 +01:00
month_name = self.date.strftime("%B")
2019-04-05 11:37:51 +02:00
month = TextDesign(box_size, text=month_name, fontsize=font_size)
2019-03-07 21:28:11 +01:00
month.pos = box_pos
self.draw_design(month)
2019-07-13 08:05:35 +02:00
def __draw_number_square__(self):
2019-04-13 20:46:00 +02:00
box_height = numberbox_height * self.size[1]
box_ypos = numberbox_ypos * self.size[1]
box_pos = (box_ypos, box_ypos)
2019-03-07 21:28:11 +01:00
box_size = (box_height, box_height)
2019-04-13 20:46:00 +02:00
2019-07-13 08:05:35 +02:00
box = BoxDesign(box_size, fill=numberbox_background_color)
2019-04-13 20:46:00 +02:00
box.pos = box_pos
self.draw_design(box)
self.__draw_today_number__()
self.__draw_weekday__()
2019-07-13 08:05:35 +02:00
def __draw_today_number__(self):
2019-04-13 20:46:00 +02:00
font_size = number_height * self.size[1]
box_height = numberbox_height * self.size[1]
box_ypos = numberbox_ypos * self.size[1]
2019-05-11 21:37:16 +02:00
ypadding = number_boxypos * box_height
size = (box_height, box_height - ypadding)
pos = (box_ypos, box_ypos + ypadding)
2019-04-13 20:46:00 +02:00
2019-03-07 21:44:10 +01:00
day_text = self.__get_day_text__()
2019-07-13 08:05:35 +02:00
number = TextDesign(size, text=day_text, background_color=numberbox_background_color,
color=numberbox_font_color, fontsize=font_size, horizontalalignment="center", verticalalignment="center")
2019-04-13 20:46:00 +02:00
number.pos = pos
number.mask = False
2019-03-07 21:28:11 +01:00
self.draw_design(number)
2019-07-13 08:05:35 +02:00
def __draw_weekday__(self):
2019-04-13 20:46:00 +02:00
font_size = weekday_height * self.size[1]
box_height = numberbox_height * self.size[1]
size = (box_height, weekdaybox_height * box_height)
box_ypos = numberbox_ypos * self.size[1]
pos = (box_ypos, box_ypos)
2019-05-18 09:28:29 +02:00
2019-04-13 20:46:00 +02:00
week_day_name = self.date.strftime("%A")
2019-07-13 08:05:35 +02:00
week_day = TextDesign(size, text=week_day_name, background_color=numberbox_background_color, color=numberbox_font_color,
fontsize=font_size, horizontalalignment="center", verticalalignment="center", font=weekday_font)
2019-04-13 20:46:00 +02:00
week_day.pos = pos
week_day.mask = False
self.draw_design(week_day)
2019-07-13 08:05:35 +02:00
def __abs_co__(self, coordinates):
return (int(coordinates[0] * self.size[0]), int(coordinates[1] * self.size[1]))
2019-03-07 21:44:10 +01:00
2019-07-13 08:05:35 +02:00
def __get_day_text__(self):
2019-05-18 09:28:29 +02:00
return str(self.date.day)