diff --git a/layouts/_base.py b/layouts/_base.py index facb77c..36869bb 100644 --- a/layouts/_base.py +++ b/layouts/_base.py @@ -84,19 +84,20 @@ class DayCell(object): @type show_day_name: bool @ivar show_day_name: whether day name is displayed """ - def __init__(self, day, header, footer, theme, show_day_name, lightweight = False): + def __init__(self, day, header, footer, theme, show_day_name, options, lightweight = False): self.day = day self.header = header self.footer = footer self.theme = theme self.show_day_name = show_day_name + self.options = options self.lightweight = lightweight def _draw_short(self, cr, rect): """render the day cell in short mode""" S,G,L = self.theme x, y, w, h = rect - day_of_month, day_of_week, week_of_year = self.day + day_of_week, cell_date = self.day S_bg = S.bg #if day_of_week >= 5 and (week_of_year & 1): S_bg = color_scale(S.bg,0.9) draw_box(cr, rect, S.frame, S_bg, mm_to_dots(S.frame_thickness), @@ -108,8 +109,15 @@ class DayCell(object): Rdom = R valign = 0 if self.show_day_name else 2 # draw day of month (number) - draw_str(cr, text = str(day_of_month), rect = Rdom, scaling = -1, stroke_rgba = S.fg, + draw_str(cr, text = str(cell_date.day), rect = Rdom, scaling = -1, stroke_rgba = S.fg, align = (2,valign), font = S.font, measure = "88") + # draw week number + if self.options.iso_week and (day_of_week == 0 or cell_date.day == 1 and not getattr(self.options, 'phantom_days', False)): + Rweek = rect_rel_scale(rect, 0.95, G.size[1] if self.header else 0.95) + Rweek = rect_vsplit(rect_hsplit(Rweek,0.2)[0], 0.2)[0] + week_of_year = cell_date.isocalendar()[1]; + draw_str(cr, text = "%s%d" % (L.week_of_year_prefix,week_of_year), rect = Rweek, scaling = -1, stroke_rgba = S.fg, + align = (2,2), font = S.font, measure = "%s88" % (L.week_of_year_prefix,)) # draw name of day if self.show_day_name: draw_str(cr, text = L.day_name[day_of_week][0], rect = Rdow, scaling = -1, stroke_rgba = S.fg, @@ -129,7 +137,7 @@ class DayCell(object): """render the day cell in long mode""" S,G,L = self.theme x, y, w, h = rect - day_of_month, day_of_week, week_of_year = self.day + day_of_week, cell_date = self.day S_bg = S.bg #if day_of_week >= 5 and (week_of_year & 1): S_bg = tuple(reversed(S.bg)) draw_box(cr, rect, S.frame, S_bg, mm_to_dots(S.frame_thickness), @@ -142,8 +150,14 @@ class DayCell(object): Rdom = rect_rel_scale(R1, G.size[0], G.size[1]) valign = 0 if self.show_day_name else 2 # draw day of month (number) - draw_str(cr, text = str(day_of_month), rect = Rdom, scaling = -1, stroke_rgba = S.fg, + draw_str(cr, text = str(cell_date.day), rect = Rdom, scaling = -1, stroke_rgba = S.fg, align = (2,valign), font = S.font, measure = "88") + # draw week number + if self.options.iso_week and (day_of_week == 0 or cell_date.day == 1): + Rweek = rect_vsplit(rect_hsplit(rect,0.125)[0], 0.25)[0] + week_of_year = cell_date.isocalendar()[1]; + draw_str(cr, text = "%s%d" % (L.week_of_year_prefix,week_of_year), rect = Rweek, scaling = -1, stroke_rgba = S.fg, + align = (2,2), font = S.font, measure = "%s88" % (L.week_of_year_prefix,)) # draw name of day if self.show_day_name: draw_str(cr, text = L.day_name[day_of_week], rect = Rdow, scaling = -1, stroke_rgba = S.fg, @@ -158,12 +172,12 @@ class DayCell(object): draw_str(cr, text = self.footer, rect = Rf, scaling = -1, stroke_rgba = S.footer, align = (1,2), font = S.footer_font) - def draw(self, cr, rect, short_thres): - """automatically render a short or long day cell depending on threshold I{short_thres} + def draw(self, cr, rect): + """automatically render a short or long day cell depending on threshold given in options - If C{rect} ratio is less than C{short_thres} then short mode is chosen, otherwise long mode. + If C{rect} ratio is less than C{self.options.short_daycell_ratio} then short mode is chosen, otherwise long mode. """ - if rect_ratio(rect) < short_thres: + if rect_ratio(rect) < self.options.short_daycell_ratio: self._draw_short(cr, rect) else: self._draw_long(cr, rect) diff --git a/layouts/bars.py b/layouts/bars.py index 66fbc56..314ea85 100644 --- a/layouts/bars.py +++ b/layouts/bars.py @@ -27,6 +27,8 @@ from datetime import date, timedelta from . import _base parser = _base.get_parser(__name__) +parser.add_option("--iso-week", action="store_true", default=False, + help="show ISO week number (starts on Monday)") parser.set_defaults(rows=2) class CalendarRenderer(_base.CalendarRenderer): @@ -51,22 +53,19 @@ class CalendarRenderer(_base.CalendarRenderer): draw_shadow(cr, rect_from_origin(rect), shad) # draw day cells - iso_y, iso_w, iso_d = date(year,month,1).isocalendar() for dom in range(1,rows+1): R = dom_grid.item(dom-1) if dom <= span: holiday_tuple = self.holiday_provider(year, month, dom, day) day_style = holiday_tuple[2] - dcell = _base.DayCell(day = (dom, day, iso_w), header = holiday_tuple[0], footer = holiday_tuple[1], - theme = (day_style, G.dom, L), show_day_name = True) - dcell.draw(cr, R, self.options.short_daycell_ratio) + dcell = _base.DayCell(day = (day, date(year, month, dom)), header = holiday_tuple[0], footer = holiday_tuple[1], + theme = (day_style, G.dom, L), show_day_name = True, options = self.options) + dcell.draw(cr, R) else: day_style = S.dom draw_box(cr, rect = R, stroke_rgba = day_style.frame, fill_rgba = day_style.bg, stroke_width = mm_to_dots(day_style.frame_thickness)) day = (day + 1) % 7 - iso_w += (iso_d == 7) - iso_d = (iso_d + 1) if iso_d < 7 else 1 # draw month title (name) mcolor = S.month.color_map_bg[year%2][month] diff --git a/layouts/classic.py b/layouts/classic.py index 3fd286f..c9b1d40 100644 --- a/layouts/classic.py +++ b/layouts/classic.py @@ -32,6 +32,9 @@ parser.add_option("--lightweight", action="store_true", default=False, help="lightweight cell rendering (lighter/no borders)") parser.add_option("--lw-inner-padding", type="float", default=1.5, help="inner padding for month box in lightweight mode") +parser.add_option("--iso-week", action="store_true", default=False, + help="show ISO week number (starts on Monday)") + def _weekrows_of_month(year, month): """returns the number of Monday-Sunday ranges (or subsets of) that a month contains, which are 4, 5 or 6 @@ -90,14 +93,12 @@ class CalendarRenderer(_base.CalendarRenderer): align = (2,0), font = S.dow.font, measure = wmeasure) # draw day cells - iso_y, iso_w, iso_d = date(year,month,1).isocalendar() for row in range(weekrows): for col in range(7): R = dom_grid.item(row, col) is_normal = dom > 0 and dom <= span if is_normal or self.options.phantom_days: real_year, real_month, real_dom = year, month, dom - real_iso_w = iso_w # handle phantom days if dom < 1: @@ -118,17 +119,16 @@ class CalendarRenderer(_base.CalendarRenderer): day_style = holiday_tuple[2] else: day_style = S.dom_weekend_phantom if col >= 5 else S.dom_phantom - dcell = _base.DayCell(day = (real_dom, col, iso_w), header = holiday_tuple[0], footer = holiday_tuple[1], + dcell = _base.DayCell(day = (col, date(real_year, real_month, real_dom)), header = holiday_tuple[0], footer = holiday_tuple[1], theme = (day_style, G.dom, L), show_day_name = False, - lightweight = self.options.lightweight ) - dcell.draw(cr, R, self.options.short_daycell_ratio) + options = self.options ) + dcell.draw(cr, R) else: day_style = S.dom_weekend if col >= 5 else S.dom draw_box(cr, rect = R, stroke_rgba = day_style.frame, fill_rgba = day_style.bg, stroke_width = mm_to_dots(day_style.frame_thickness), lightweight = self.options.lightweight) dom += 1 - iso_w += 1 # draw month title (name) mcolor = S.month.color_map_bg[year%2][month] diff --git a/layouts/sparse.py b/layouts/sparse.py index 535db94..8987b4d 100644 --- a/layouts/sparse.py +++ b/layouts/sparse.py @@ -121,7 +121,7 @@ def _draw_day_cell(cr, rect, day, header, footer, theme, show_day_name, text_hei week_nr = date(year, month, day_of_month).isocalendar()[1] draw_str(cr, text = "%s%d" % (L.week_of_year_prefix, week_nr), rect = Rmiddle_top, scaling = -1, stroke_rgba = ds.fg, align = (0,valign), - font = ds.header_font, measure = "W88") + font = ds.header_font, measure = "%s88" % (L.week_of_year_prefix,)) if header: draw_str(cr, text = header, rect = Rright_header, scaling = -1,