E-Paper-Calendar/Calendar/AgendaListDesign.py

102 lines
3.9 KiB
Python
Raw Permalink Normal View History

2019-03-24 22:14:58 +01:00
from DesignEntity import DesignEntity
2019-05-11 21:37:16 +02:00
from Assets import defaultfontsize, colors, defaultfont, path
2019-03-24 22:14:58 +01:00
from datetime import datetime, date, timedelta
from TableDesign import TableDesign
2019-05-11 21:37:16 +02:00
from PIL import ImageDraw, ImageFont
2019-04-05 22:07:47 +02:00
from TextFormatter import date_summary_str, event_prefix_str
from settings import line_thickness
2019-03-26 22:30:01 +01:00
separator_width = line_thickness
2019-03-24 22:14:58 +01:00
2019-07-13 08:05:35 +02:00
2019-03-24 22:14:58 +01:00
class AgendaListDesign (DesignEntity):
'''Lists upcoming events in chronological order and groups them by days'''
2019-07-13 08:05:35 +02:00
def __init__(self, size, calendar, line_spacing=0, col_spacing=8, text_size=defaultfontsize, start_date=date.today(), always_add_start_row=True, day_limit_foresight=91):
2019-03-24 22:14:58 +01:00
super(AgendaListDesign, self).__init__(size)
self.calendar = calendar
self.line_spacing = line_spacing
2019-03-27 18:26:54 +01:00
self.col_spacing = col_spacing
2019-03-24 22:14:58 +01:00
self.text_size = text_size
self.day_limit_foresight = day_limit_foresight
2019-04-29 18:14:10 +02:00
self.start_dt = date(start_date.year, start_date.month, start_date.day)
2019-04-14 10:26:35 +02:00
self.always_add_start_row = always_add_start_row
2019-03-24 22:14:58 +01:00
2019-07-13 08:05:35 +02:00
def __finish_image__(self):
2019-03-24 22:14:58 +01:00
self.__calculate_parameter__()
2019-03-27 18:26:54 +01:00
self.__create_infos_events__()
self.__draw_infos__()
2019-03-26 22:30:01 +01:00
self.__draw_lines__()
2019-03-24 22:14:58 +01:00
2019-07-13 08:05:35 +02:00
def __calculate_parameter__(self):
2019-05-11 21:37:16 +02:00
self.__line_height__ = self.line_spacing + self.__get_text_height__()
2019-03-24 22:14:58 +01:00
self.__event_number__ = int(int(self.size[1]) // self.__line_height__)
2019-03-26 22:30:01 +01:00
self.__date_fontsize__ = self.text_size
self.__date_linespace__ = self.line_spacing
2019-03-24 22:14:58 +01:00
2019-07-13 08:05:35 +02:00
def __create_infos_events__(self):
2019-03-27 18:26:54 +01:00
self.infos = []
2019-04-05 22:28:16 +02:00
self.cell_props = []
2019-03-24 22:14:58 +01:00
fetch_day = self.start_dt
days_foresight = 0
while len(self.infos) < self.__event_number__ and days_foresight < self.day_limit_foresight:
2019-03-26 22:30:01 +01:00
day_events = self.calendar.get_day_events(fetch_day)
2019-03-27 18:26:54 +01:00
fetch_day_added_once = False
for event in day_events:
2019-07-13 08:05:35 +02:00
row = [""]
2019-03-27 18:26:54 +01:00
if fetch_day_added_once is False:
row.append(date_summary_str(fetch_day))
fetch_day_added_once = True
else:
row.append("")
2019-04-05 22:07:47 +02:00
row.append(event_prefix_str(event, fetch_day))
2019-03-27 18:26:54 +01:00
row.append(event.title)
2019-04-05 22:28:16 +02:00
self.cell_props.append(self.__get_row_props__(event))
2019-07-13 08:05:35 +02:00
2019-03-27 18:26:54 +01:00
self.infos.append(row)
2019-03-24 22:14:58 +01:00
fetch_day = fetch_day + timedelta(1)
days_foresight = days_foresight + 1
2019-07-13 08:05:35 +02:00
2019-04-14 10:26:35 +02:00
if self.infos[0][1] != date_summary_str(self.start_dt) and self.always_add_start_row:
row = ["", date_summary_str(self.start_dt), "", ""]
props = self.__get_row_props__()
self.infos.insert(0, row)
self.cell_props.insert(0, props)
2019-07-13 08:05:35 +02:00
def __draw_infos__(self):
table = TableDesign(self.size, self.infos, fontsize=self.__date_fontsize__,
line_spacing=self.__date_linespace__, col_spacing=self.col_spacing, cell_properties=self.cell_props)
2019-03-26 22:30:01 +01:00
self.draw_design(table)
2019-07-13 08:05:35 +02:00
def __draw_lines__(self):
2019-03-27 18:26:54 +01:00
for i, (_, date, _, _) in enumerate(self.infos[1:]):
if date is not "":
2019-03-26 22:30:01 +01:00
self.__draw_line__(i + 1)
2019-07-13 08:05:35 +02:00
def __draw_line__(self, index):
2019-03-26 22:30:01 +01:00
ypos = index * self.__line_height__ - self.line_spacing / 2
pos = (0, ypos)
2019-07-13 08:05:35 +02:00
positions = [pos, (self.size[0], ypos)]
2019-03-26 22:30:01 +01:00
2019-07-13 08:05:35 +02:00
ImageDraw.Draw(self.__image__).line(
positions, fill=colors["fg"], width=separator_width)
2019-04-05 22:28:16 +02:00
2019-07-13 08:05:35 +02:00
def __get_row_props__(self, event=None):
2019-04-05 22:28:16 +02:00
color = colors["fg"]
bg_color = colors["bg"]
default_cell = {
2019-07-13 08:05:35 +02:00
"color": color,
"background_color": bg_color
2019-04-05 22:28:16 +02:00
}
if event is not None and event.highlight:
color = colors["hl"]
cell = {
2019-07-13 08:05:35 +02:00
"color": color,
"background_color": bg_color
2019-04-05 22:28:16 +02:00
}
2019-07-13 08:05:35 +02:00
return [default_cell, default_cell, cell, cell]
2019-05-11 21:37:16 +02:00
def __get_text_height__(self):
2019-07-13 08:05:35 +02:00
return ImageFont.truetype(path + defaultfont, self.text_size).font.height