2019-03-24 22:14:58 +01:00
|
|
|
from DesignEntity import DesignEntity
|
2019-04-05 11:37:51 +02:00
|
|
|
from Assets import defaultfontsize, colors
|
2019-03-24 22:14:58 +01:00
|
|
|
from datetime import datetime, date, timedelta
|
2019-03-26 22:30:01 +01:00
|
|
|
from TableTextDesign import TableTextDesign
|
|
|
|
from PIL import ImageDraw
|
2019-04-05 22:07:47 +02:00
|
|
|
from TextFormatter import date_summary_str, event_prefix_str
|
2019-03-26 22:30:01 +01:00
|
|
|
|
|
|
|
line_width = 1
|
2019-03-24 22:14:58 +01:00
|
|
|
|
|
|
|
class AgendaListDesign (DesignEntity):
|
|
|
|
'''Lists upcoming events in chronological order and groups them by days'''
|
2019-04-14 10:26:35 +02:00
|
|
|
def __init__ (self, size, calendar, line_spacing = 3, col_spacing = 8, text_size = defaultfontsize, start_date = date.today(), always_add_start_row = True):
|
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.start_dt = datetime(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
|
|
|
|
|
|
|
def __finish_image__ (self):
|
|
|
|
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
|
|
|
|
|
|
|
def __calculate_parameter__ (self):
|
|
|
|
self.__line_height__ = self.line_spacing + int(self.text_size)
|
|
|
|
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-03-27 18:26:54 +01:00
|
|
|
def __create_infos_events__ (self):
|
|
|
|
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
|
2019-03-27 18:26:54 +01:00
|
|
|
while len(self.infos) < self.__event_number__:
|
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:
|
|
|
|
row = [ "" ]
|
|
|
|
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-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)
|
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-03-24 22:14:58 +01:00
|
|
|
|
2019-03-27 18:26:54 +01:00
|
|
|
def __draw_infos__ (self):
|
2019-04-07 11:16:40 +02:00
|
|
|
table = TableTextDesign(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-03-24 22:14:58 +01: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-03-27 18:26:54 +01: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-03-27 18:26:54 +01:00
|
|
|
positions = [ pos, (self.size[0], ypos) ]
|
2019-03-26 22:30:01 +01:00
|
|
|
|
2019-04-05 22:28:16 +02:00
|
|
|
ImageDraw.Draw(self.__image__).line(positions, fill=colors["fg"], width=line_width)
|
|
|
|
|
|
|
|
def __get_row_props__ (self, event = None):
|
|
|
|
color = colors["fg"]
|
|
|
|
bg_color = colors["bg"]
|
|
|
|
default_cell = {
|
|
|
|
"color" : color,
|
|
|
|
"background_color" : bg_color
|
|
|
|
}
|
|
|
|
if event is not None and event.highlight:
|
|
|
|
color = colors["hl"]
|
|
|
|
cell = {
|
|
|
|
"color" : color,
|
|
|
|
"background_color" : bg_color
|
|
|
|
}
|
|
|
|
return [default_cell, default_cell, cell, cell ]
|