E-Paper-Calendar/Calendar/DayRowDesign.py

124 lines
4.3 KiB
Python
Raw Normal View History

from PIL import ImageDraw, Image
2019-03-08 21:37:54 +01:00
from TextDesign import TextDesign
2019-05-06 16:12:18 +02:00
from settings import week_starts_on, owm_paid_subscription, general_settings
2019-03-08 21:37:54 +01:00
from DesignEntity import DesignEntity
from datetime import datetime
2019-04-17 17:23:41 +02:00
from Assets import weathericons, wpath, fonts, colors, defaultfontsize
2019-03-10 15:19:27 +01:00
from SingelDayEventListDesign import SingelDayEventListDesign
2019-03-08 21:37:54 +01:00
2019-05-11 21:37:16 +02:00
daynumber_y_size = (1, 0.60)
2019-03-08 21:37:54 +01:00
weekday_y_size = (daynumber_y_size[0], 1 - daynumber_y_size[1])
weekday_ypos = daynumber_y_size[1]
2019-05-11 21:37:16 +02:00
daynumber_fontsize = daynumber_y_size[1] * 0.85
daynumber_ypadding = 0.1
weekday_fontsize = weekday_y_size[1] * 0.65
weathericon_ypos = 0.1
weathericon_height = 1 - 2 * weathericon_ypos
2019-03-10 15:19:27 +01:00
eventlist_xpadding = 5
2019-05-11 21:37:16 +02:00
eventlist_ypos = 0.02
eventlist_y_fontsize = 0.2
2019-03-08 21:37:54 +01:00
2019-04-12 08:15:28 +02:00
font = fonts["light"]
2019-03-08 21:37:54 +01:00
2019-07-13 08:05:35 +02:00
2019-03-08 21:37:54 +01:00
class DayRowDesign (DesignEntity):
"""Detailed view of a given date."""
2019-07-13 08:05:35 +02:00
def __init__(self, size, date):
2019-03-08 21:37:54 +01:00
super(DayRowDesign, self).__init__(size)
2019-04-05 11:37:51 +02:00
self.__init_image__()
2019-03-08 21:37:54 +01:00
self.date = date
2019-07-13 08:05:35 +02:00
def add_weather(self, weather):
if weather.is_available is False:
return
self.__draw_forecast__(weather)
2019-07-13 08:05:35 +02:00
def add_calendar(self, calendar):
2019-03-10 15:19:27 +01:00
self.__draw_event_list__(calendar)
2019-07-13 08:05:35 +02:00
def add_rssfeed(self, rss):
pass
2019-07-13 08:05:35 +02:00
def __draw_event_list__(self, calendar):
2019-03-10 15:19:27 +01:00
number_width = daynumber_y_size[0] * self.size[1]
ypos = eventlist_ypos * self.size[1]
weather_width = 0
2019-05-06 16:12:18 +02:00
if owm_paid_subscription and general_settings["weather-info"]:
2019-03-10 15:19:27 +01:00
weather_width = weathericon_height * self.size[1]
pos = (number_width + eventlist_xpadding, ypos)
size = (self.size[0] - pos[0] - weather_width, self.size[1] - pos[1])
fontsize = eventlist_y_fontsize * self.size[1]
2019-05-18 09:28:29 +02:00
events = calendar.get_day_events(self.date)
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 15:19:27 +01:00
event_list.pos = pos
self.draw_design(event_list)
2019-07-13 08:05:35 +02:00
def __draw_forecast__(self, weather):
forecast = weather.get_forecast_in_days(
self.date.day - datetime.today().day)
2019-05-18 09:28:29 +02:00
if forecast is None:
return
height = int(weathericon_height * self.size[1])
size = (height, height)
ypos = weathericon_ypos * self.size[1]
pos = (self.size[0] - ypos - size[0], ypos)
icon = Image.open(wpath + weathericons[forecast.icon] + ".jpeg")
resized_icon = icon.resize(size, resample=Image.LANCZOS)
self.draw(resized_icon, pos)
2019-07-13 08:05:35 +02:00
def __finish_image__(self):
2019-03-08 21:37:54 +01:00
self.__draw_weekday__()
2019-05-11 21:37:16 +02:00
self.__draw_day_number__()
2019-03-08 21:37:54 +01:00
2019-07-13 08:05:35 +02:00
def __draw_weekday__(self):
2019-03-08 21:37:54 +01:00
font_size = int(weekday_fontsize * self.size[1])
2019-07-13 08:05:35 +02:00
size = (weekday_y_size[0] * self.size[1],
weekday_y_size[1] * self.size[1])
2019-03-08 21:37:54 +01:00
ypos = weekday_ypos * self.size[1]
pos = (0, ypos)
color = self.__get_day_color__()
week_day_name = self.date.strftime("%a")
2019-05-18 09:28:29 +02:00
2019-07-13 08:05:35 +02:00
week_day = TextDesign(size, text=week_day_name, font=font, color=color,
fontsize=font_size, horizontalalignment="center", verticalalignment="top")
2019-03-08 21:37:54 +01:00
week_day.pos = pos
2019-04-10 12:12:54 +02:00
week_day.mask = False
2019-03-08 21:37:54 +01:00
self.draw_design(week_day)
2019-07-13 08:05:35 +02:00
def __draw_day_number__(self):
2019-03-08 21:37:54 +01:00
font_size = int(daynumber_fontsize * self.size[1])
2019-05-11 21:37:16 +02:00
ypadding = daynumber_ypadding * self.size[1]
2019-07-13 08:05:35 +02:00
size = (daynumber_y_size[0] * self.size[1],
daynumber_y_size[1] * self.size[1])
2019-05-11 21:37:16 +02:00
pos = (0, ypadding)
2019-03-08 21:37:54 +01:00
day_text = self.__get_day_text__()
color = self.__get_day_color__()
2019-07-13 08:05:35 +02:00
number = TextDesign(size, text=day_text, font=font, color=color,
fontsize=font_size, horizontalalignment="center", verticalalignment="bottom")
2019-03-08 21:37:54 +01:00
number.pos = pos
self.draw_design(number)
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-08 21:37:54 +01:00
2019-07-13 08:05:35 +02:00
def __get_day_text__(self):
2019-03-08 21:37:54 +01:00
return str(self.date.day)
2019-07-13 08:05:35 +02:00
def __get_day_color__(self):
2019-03-08 21:37:54 +01:00
"""Depending on week_starts_on"""
if week_starts_on == "Monday" and self.date.strftime("%w") == "0":
2019-04-05 11:37:51 +02:00
return colors["hl"]
2019-03-08 21:37:54 +01:00
elif week_starts_on == "Sunday" and self.date.strftime("%w") == "6":
2019-04-05 11:37:51 +02:00
return colors["hl"]
2019-03-08 21:37:54 +01:00
else:
2019-05-18 09:28:29 +02:00
return colors["fg"]