From 2f53c639b52b64fa972d196856a0ef8a6dc9f267 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Sat, 2 Mar 2019 10:10:39 +0100 Subject: [PATCH] Encapsuled the month number overview to an extra design class --- Calendar/MonthBlockDesign.py | 77 ++++++++++++++++++++++++++++++++++++ Calendar/MonthOvPanel.py | 59 ++++++++------------------- 2 files changed, 93 insertions(+), 43 deletions(-) create mode 100644 Calendar/MonthBlockDesign.py diff --git a/Calendar/MonthBlockDesign.py b/Calendar/MonthBlockDesign.py new file mode 100644 index 0000000..a6bc0bd --- /dev/null +++ b/Calendar/MonthBlockDesign.py @@ -0,0 +1,77 @@ +from DesignEntity import DesignEntity +import calendar as callib +from datetime import datetime, timedelta +from TextDesign import TextDesign +from Assets import * +from settings import * +from BoxDesign import BoxDesign + +daynumberboxsize = (0.143, 0.286) +dayhighlightboxsize = (0.143, 0.14) +daynumbersize = 25 + +class MonthBlockDesign (DesignEntity): + """Creates a view containing one week of the month in + one row""" + def __init__(self, size, datetime_month, highlight_today = False): + super(MonthBlockDesign, self).__init__(size, mask=True) + self.month = datetime_month.month + self.year = datetime_month.year + self.highlight_today = highlight_today + self.__week_days__ = self.__get_week_days_ordered__() + + def __finish_image__(self): + self.__draw_month_overview__() + + def __draw_month_overview__ (self): + """Using the built-in calendar function, draw icons for each + number of the month (1,2,3,...28,29,30)""" + cal = callib.monthcalendar(self.year, self.month) + for week in cal: + for numbers in week: + self.__draw_day_number__(numbers, self.get_day_pos(cal.index(week), week.index(numbers))) + + if self.highlight_today: + self.__draw_highlight_box__(self.__abs_pos__(dayhighlightboxsize), self.__get_today_box_pos__(), width=3) + + def __draw_highlight_box__ (self, size, pos, color='black', width=1): + design = BoxDesign(size, outline=color, width = width) + design.pos = pos + self.draw_design(design) + + def __draw_day_number__ (self, number, pos): + if number <= 0: + return + txt = TextDesign(self.__abs_pos__(daynumberboxsize), fontsize=daynumbersize, text=str(number), verticalalignment="center", horizontalalignment="center") + txt.pos = pos + self.draw_design(txt) + + def get_day_pos (self, week_in_month, day_of_week, rel_pos=(0,0)): + maxwidth, maxheight = self.size + partialwidth = maxwidth / 7 + partialheight = maxheight / 5 + return (int(rel_pos[0] + day_of_week * partialwidth), int(rel_pos[1] + week_in_month * partialheight)) + + def __get_today_box_pos__ (self): + x, y = self.get_day_pos(int(datetime.now().day / 7), self.__get_day_of_week__(datetime.now())) + return (x, int(y + (self.__abs_pos__(daynumberboxsize)[1] - self.__abs_pos__(dayhighlightboxsize)[1]) / 2)) + + def __get_day_of_week__ (self, date): + return self.__week_days__.index(date.strftime("%a")) + + def __get_week_days_ordered__ (self): + cur_weekday = datetime.now().weekday() + correction = -cur_weekday + if week_starts_on == "Sunday": + correction -= 1 + + weekdays = [] + for i in range(7): + weekdays.append((datetime.now() + timedelta(days=(i + correction))).strftime("%a")) + + return weekdays + + def __abs_pos__ (self, pos, size = None): + if size is None: + size = self.size + return (int(pos[0] * size[0]), int(pos[1] * size[1])) \ No newline at end of file diff --git a/Calendar/MonthOvPanel.py b/Calendar/MonthOvPanel.py index ecdf651..b59921e 100644 --- a/Calendar/MonthOvPanel.py +++ b/Calendar/MonthOvPanel.py @@ -8,17 +8,15 @@ from PIL import ImageDraw from TextDesign import TextDesign from BoxDesign import BoxDesign from EllipseDesign import EllipseDesign +from MonthBlockDesign import MonthBlockDesign, daynumberboxsize +monthtextsize = 40 +monthovsize = (1, 0.5) +monthovposition = (0, 0.23) weatherheadersize = (1,0.113) seperatorplace = (0, 0.113) monthplace = (0, 0.12) monthboxsize = (1, 0.085) -daynumberboxsize = (0.143, 0.143) -dayhighlightboxsize = (0.143, 0.07) -daynumbersize = 25 -monthtextsize = 40 -monthovposition = (0, 0.23) -monthovsize = (1, 0.5) weekdayrowpos = (0, 0.209) weekrowboxsize = (1, 0.044) weekdaytextsize = 18 @@ -41,9 +39,12 @@ class MonthOvPanel (PanelDesign): self.__draw_month_name__() self.__draw_seperator__() - self.__draw_month_overview__() self.__draw_week_row__() + self.month_block = MonthBlockDesign(self.__abs_pos__(monthovsize), datetime.now(), highlight_today = True) + self.month_block.pos = self.__abs_pos__(monthovposition) + self.draw_design(self.month_block) + def add_weather (self, weather): self.draw_design(WeatherHeaderDesign(self.__abs_pos__(weatherheadersize), weather)) @@ -62,8 +63,8 @@ class MonthOvPanel (PanelDesign): side_length = int(eventcirclehorizontalsize * self.size[0]) circle_size = (side_length,side_length) - pos = self.__get_day_pos__(cur_date.isocalendar()[1] - first_month_week, self.__get_day_of_week__(cur_date)) - place_size = self.__abs_pos__(daynumberboxsize) + pos = self.month_block.get_day_pos(cur_date.isocalendar()[1] - first_month_week, self.__get_day_of_week__(cur_date), rel_pos = self.__abs_pos__(monthovposition)) + place_size = (self.month_block.size[0] * daynumberboxsize[0], self.month_block.size[1] * daynumberboxsize[1]) pos = (int(pos[0] + (place_size[0] - circle_size[0]) / 2), int(pos[1] + (place_size[1] - circle_size[1]) / 2)) self.__draw_highlight_circle__(circle_size, pos, 'red', width=2) @@ -76,13 +77,6 @@ class MonthOvPanel (PanelDesign): """Draw a line seperating the weather and Calendar section""" ImageDraw.Draw(self.__image__).line([ self.__abs_pos__(seperatorplace), self.__abs_pos__((1, seperatorplace[1])) ], fill='red', width=5) - def __draw_day_number__ (self, number, pos): - if number <= 0: - return - txt = TextDesign(self.__abs_pos__(daynumberboxsize), fontsize=daynumbersize, text=str(number), verticalalignment="center", horizontalalignment="center") - txt.pos = pos - self.draw_design(txt) - def __draw_month_name__ (self): """Draw the icon with the current month's name""" month = datetime.now().strftime("%B") @@ -90,23 +84,6 @@ class MonthOvPanel (PanelDesign): txt.pos = self.__abs_pos__(monthplace) self.draw_design(txt) - def __get_day_pos__ (self, week_in_month, day_of_week): - maxwidth, maxheight = self.__abs_pos__(monthovsize) - partialwidth = maxwidth / 7 - partialheight = maxheight / 5 - posx, posy = self.__abs_pos__(monthovposition) - return (int(posx + day_of_week * partialwidth), int(posy + week_in_month * partialheight)) - - def __draw_month_overview__ (self): - """Using the built-in calendar function, draw icons for each - number of the month (1,2,3,...28,29,30)""" - cal = callib.monthcalendar(datetime.now().year, datetime.now().month) - for week in cal: - for numbers in week: - self.__draw_day_number__(numbers, self.__get_day_pos__(cal.index(week), week.index(numbers))) - - self.__draw_highlight_box__(self.__abs_pos__(dayhighlightboxsize), self.__get_today_box_pos__(), width=3) - def __draw_week_row__ (self): for day_of_week, day in enumerate(self.__week_days__): txt = TextDesign(self.__abs_pos__(weekrownameboxsize), fontsize=weekdaytextsize, text=str(day), verticalalignment="center", horizontalalignment="center") @@ -121,19 +98,12 @@ class MonthOvPanel (PanelDesign): posx, posy = self.__abs_pos__(weekdayrowpos) return (int(posx + day_of_week * partialwidth), int(posy)) - def __get_today_box_pos__ (self): - x, y = self.__get_day_pos__(int(datetime.now().day / 7), self.__get_day_of_week__(datetime.now())) - return (x, int(y + (self.__abs_pos__(daynumberboxsize)[1] - self.__abs_pos__(dayhighlightboxsize)[1]) / 2)) - - def __get_day_of_week__ (self, date): - return self.__week_days__.index(date.strftime("%a")) - - def __draw_highlight_box__ (self, size, pos, color='black', width=1): + def __draw_highlight_box__ (self, size, pos, color = 'black', width = 1): design = BoxDesign(size, outline=color, width = width) design.pos = pos self.draw_design(design) - def __draw_highlight_circle__ (self, size, pos, color = 'black', width=1): + def __draw_highlight_circle__ (self, size, pos, color = 'black', width = 1): design = EllipseDesign(size, outline=color, width = width) design.pos = pos self.draw_design(design) @@ -148,4 +118,7 @@ class MonthOvPanel (PanelDesign): for i in range(7): weekdays.append((datetime.now() + timedelta(days=(i + correction))).strftime("%a")) - return weekdays \ No newline at end of file + return weekdays + + def __get_day_of_week__ (self, date): + return self.__week_days__.index(date.strftime("%a")) \ No newline at end of file