ADD support for --iso-week-number in classical and bar layouts

This commit is contained in:
George Tzoumas 2022-01-02 22:44:01 +01:00
parent e5fe71685e
commit 2f5f352a36
4 changed files with 35 additions and 22 deletions

View File

@ -84,19 +84,20 @@ class DayCell(object):
@type show_day_name: bool @type show_day_name: bool
@ivar show_day_name: whether day name is displayed @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.day = day
self.header = header self.header = header
self.footer = footer self.footer = footer
self.theme = theme self.theme = theme
self.show_day_name = show_day_name self.show_day_name = show_day_name
self.options = options
self.lightweight = lightweight self.lightweight = lightweight
def _draw_short(self, cr, rect): def _draw_short(self, cr, rect):
"""render the day cell in short mode""" """render the day cell in short mode"""
S,G,L = self.theme S,G,L = self.theme
x, y, w, h = rect 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 S_bg = S.bg
#if day_of_week >= 5 and (week_of_year & 1): S_bg = color_scale(S.bg,0.9) #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), draw_box(cr, rect, S.frame, S_bg, mm_to_dots(S.frame_thickness),
@ -108,8 +109,15 @@ class DayCell(object):
Rdom = R Rdom = R
valign = 0 if self.show_day_name else 2 valign = 0 if self.show_day_name else 2
# draw day of month (number) # 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") 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 # draw name of day
if self.show_day_name: if self.show_day_name:
draw_str(cr, text = L.day_name[day_of_week][0], rect = Rdow, scaling = -1, stroke_rgba = S.fg, 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""" """render the day cell in long mode"""
S,G,L = self.theme S,G,L = self.theme
x, y, w, h = rect 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 S_bg = S.bg
#if day_of_week >= 5 and (week_of_year & 1): S_bg = tuple(reversed(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), 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]) Rdom = rect_rel_scale(R1, G.size[0], G.size[1])
valign = 0 if self.show_day_name else 2 valign = 0 if self.show_day_name else 2
# draw day of month (number) # 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") 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 # draw name of day
if self.show_day_name: if self.show_day_name:
draw_str(cr, text = L.day_name[day_of_week], rect = Rdow, scaling = -1, stroke_rgba = S.fg, 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), draw_str(cr, text = self.footer, rect = Rf, scaling = -1, stroke_rgba = S.footer, align = (1,2),
font = S.footer_font) font = S.footer_font)
def draw(self, cr, rect, short_thres): def draw(self, cr, rect):
"""automatically render a short or long day cell depending on threshold I{short_thres} """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) self._draw_short(cr, rect)
else: else:
self._draw_long(cr, rect) self._draw_long(cr, rect)

View File

@ -27,6 +27,8 @@ from datetime import date, timedelta
from . import _base from . import _base
parser = _base.get_parser(__name__) 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) parser.set_defaults(rows=2)
class CalendarRenderer(_base.CalendarRenderer): class CalendarRenderer(_base.CalendarRenderer):
@ -51,22 +53,19 @@ class CalendarRenderer(_base.CalendarRenderer):
draw_shadow(cr, rect_from_origin(rect), shad) draw_shadow(cr, rect_from_origin(rect), shad)
# draw day cells # draw day cells
iso_y, iso_w, iso_d = date(year,month,1).isocalendar()
for dom in range(1,rows+1): for dom in range(1,rows+1):
R = dom_grid.item(dom-1) R = dom_grid.item(dom-1)
if dom <= span: if dom <= span:
holiday_tuple = self.holiday_provider(year, month, dom, day) holiday_tuple = self.holiday_provider(year, month, dom, day)
day_style = holiday_tuple[2] day_style = holiday_tuple[2]
dcell = _base.DayCell(day = (dom, day, iso_w), header = holiday_tuple[0], footer = holiday_tuple[1], 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) theme = (day_style, G.dom, L), show_day_name = True, options = self.options)
dcell.draw(cr, R, self.options.short_daycell_ratio) dcell.draw(cr, R)
else: else:
day_style = S.dom day_style = S.dom
draw_box(cr, rect = R, stroke_rgba = day_style.frame, fill_rgba = day_style.bg, draw_box(cr, rect = R, stroke_rgba = day_style.frame, fill_rgba = day_style.bg,
stroke_width = mm_to_dots(day_style.frame_thickness)) stroke_width = mm_to_dots(day_style.frame_thickness))
day = (day + 1) % 7 day = (day + 1) % 7
iso_w += (iso_d == 7)
iso_d = (iso_d + 1) if iso_d < 7 else 1
# draw month title (name) # draw month title (name)
mcolor = S.month.color_map_bg[year%2][month] mcolor = S.month.color_map_bg[year%2][month]

View File

@ -32,6 +32,9 @@ parser.add_option("--lightweight", action="store_true", default=False,
help="lightweight cell rendering (lighter/no borders)") help="lightweight cell rendering (lighter/no borders)")
parser.add_option("--lw-inner-padding", type="float", default=1.5, parser.add_option("--lw-inner-padding", type="float", default=1.5,
help="inner padding for month box in lightweight mode") 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): 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 """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) align = (2,0), font = S.dow.font, measure = wmeasure)
# draw day cells # draw day cells
iso_y, iso_w, iso_d = date(year,month,1).isocalendar()
for row in range(weekrows): for row in range(weekrows):
for col in range(7): for col in range(7):
R = dom_grid.item(row, col) R = dom_grid.item(row, col)
is_normal = dom > 0 and dom <= span is_normal = dom > 0 and dom <= span
if is_normal or self.options.phantom_days: if is_normal or self.options.phantom_days:
real_year, real_month, real_dom = year, month, dom real_year, real_month, real_dom = year, month, dom
real_iso_w = iso_w
# handle phantom days # handle phantom days
if dom < 1: if dom < 1:
@ -118,17 +119,16 @@ class CalendarRenderer(_base.CalendarRenderer):
day_style = holiday_tuple[2] day_style = holiday_tuple[2]
else: else:
day_style = S.dom_weekend_phantom if col >= 5 else S.dom_phantom 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, theme = (day_style, G.dom, L), show_day_name = False,
lightweight = self.options.lightweight ) options = self.options )
dcell.draw(cr, R, self.options.short_daycell_ratio) dcell.draw(cr, R)
else: else:
day_style = S.dom_weekend if col >= 5 else S.dom 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, draw_box(cr, rect = R, stroke_rgba = day_style.frame, fill_rgba = day_style.bg,
stroke_width = mm_to_dots(day_style.frame_thickness), stroke_width = mm_to_dots(day_style.frame_thickness),
lightweight = self.options.lightweight) lightweight = self.options.lightweight)
dom += 1 dom += 1
iso_w += 1
# draw month title (name) # draw month title (name)
mcolor = S.month.color_map_bg[year%2][month] mcolor = S.month.color_map_bg[year%2][month]

View File

@ -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] 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, 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), 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: if header:
draw_str(cr, text = header, rect = Rright_header, scaling = -1, draw_str(cr, text = header, rect = Rright_header, scaling = -1,