Commit 2a269ef5 authored by fred's avatar fred

handle some corner cases in the grid

parent 21e8ec0c
...@@ -28,7 +28,6 @@ class ProgramView(TemplateView): ...@@ -28,7 +28,6 @@ class ProgramView(TemplateView):
program = ProgramView.as_view() program = ProgramView.as_view()
class TimeCell: class TimeCell:
emissions = None
nonstop = None nonstop = None
w = 1 w = 1
h = 1 h = 1
...@@ -116,20 +115,29 @@ class Grid(TemplateView): ...@@ -116,20 +115,29 @@ class Grid(TemplateView):
grid = grid[2*Schedule.DAY_HOUR_START:] + grid[:2*Schedule.DAY_HOUR_START] grid = grid[2*Schedule.DAY_HOUR_START:] + grid[:2*Schedule.DAY_HOUR_START]
# merge adjacent cells # merge adjacent cells
# 1st thing is to merge cells on the same line, this will mostly catch
# consecutive nonstop cells
for i in range(nb_lines): for i in range(nb_lines):
for j, cell in enumerate(grid[i]): for j, cell in enumerate(grid[i]):
if grid[i][j] is None: if grid[i][j] is None:
continue continue
t = 1 t = 1
try: try:
# if the cells are identical, they are removed from the
# grid, and current cell width is increased
while grid[i][j+t] == cell: while grid[i][j+t] == cell:
cell.w += 1 cell.w += 1
grid[i][j+t] = None grid[i][j+t] = None
t += 1 t += 1
except IndexError: except IndexError:
pass pass
# once we're done we remove empty cells
grid[i] = [x for x in grid[i] if x is not None] grid[i] = [x for x in grid[i] if x is not None]
# 2nd thing is to merge cells vertically, this is emissions that last
# for more than 30 minutes
for i in range(nb_lines): for i in range(nb_lines):
grid[i] = [x for x in grid[i] if x is not None] grid[i] = [x for x in grid[i] if x is not None]
for j, cell in enumerate(grid[i]): for j, cell in enumerate(grid[i]):
...@@ -138,13 +146,61 @@ class Grid(TemplateView): ...@@ -138,13 +146,61 @@ class Grid(TemplateView):
t = 1 t = 1
try: try:
while True: while True:
# we look if the next time cell has the same emissions
same_cell_below = [(bj, x) for bj, x in enumerate(grid[i+cell.h]) same_cell_below = [(bj, x) for bj, x in enumerate(grid[i+cell.h])
if x == cell and x.y == cell.y and x.w == cell.w] if x == cell and x.y == cell.y and x.w == cell.w]
if not same_cell_below: if same_cell_below:
# if the cell was identical, we remove it and
# increase current cell height
bj, same_cell_below = same_cell_below[0]
del grid[i+cell.h][bj]
cell.h += 1
else:
# if the cell is different, we have a closer look
# to it, so we can remove emissions that will
# already be mentioned in the current cell.
#
# For example:
# - 7am30, seuls contre tout, 1h30
# - 8am, du pied gauche & la voix de la rue, 1h
# should produce: (this is case A)
# | 7:30-9:00 |
# | seuls contre tout |
# |---------------------|
# | 8:00-9:00 |
# | du pied gauche |
# | la voix de la rue |
#
# On the other hand, if all three emissions started
# at 7am30, we want: (this is case B)
# | 7:30-9:00 |
# | seuls contre tout |
# | du pied gauche |
# | la voix de la rue |
# that is we merge all of them, ignoring the fact
# that the other emissions will stop at 8am30
current_cell_schedules = set(grid[i][j].schedules)
cursor = 1
while True and current_cell_schedules:
same_cell_below = [x for x in grid[i+cursor] if x.y == grid[i][j].y]
if not same_cell_below:
cursor += 1
continue
same_cell_below = same_cell_below[0]
if current_cell_schedules.issubset(same_cell_below.schedules):
# this handles case A (see comment above)
for schedule in current_cell_schedules:
if schedule in same_cell_below.schedules:
print 'removing schedule:', schedule
same_cell_below.schedules.remove(schedule)
elif same_cell_below.schedules and \
current_cell_schedules.issuperset(same_cell_below.schedules):
# this handles case B (see comment above)
grid[i][j].time_label = same_cell_below.time_label
grid[i][j].h += 1
grid[i+cursor].remove(same_cell_below)
cursor += 1
break break
bj, same_cell_below = same_cell_below[0]
del grid[i+cell.h][bj]
cell.h += 1
except IndexError: except IndexError:
pass pass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment