-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekview.py
757 lines (606 loc) · 23.7 KB
/
weekview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# Calendar, Graphical calendar applet with novel interface
#
# weekview.py
#
# Copyright (c) 2010, Brandon Lewis <brandon_lewis@berkeley.edu>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import gtk
import cairo
import gobject
import pango
import datetime
import math
import recurrence
import settings
import shapes
from behavior import TextInput
from command import Command, MouseCommand
from dispatcher import MouseCommandDispatcher
from schedule import Schedule
from calendarwidget import CalendarWidget, CalendarInfo, scaled_property
from calendarwidget import DateNotVisible, quantize, KineticScrollAnimation
class WeekViewBase(CalendarWidget):
_day_width = settings.day_width
hour_height = gobject.property(type=float, default=settings.hour_height)
day_width = gobject.property(type=float, default=settings.day_width)
def get_week_pixel_offset(self):
return self.day_width - (self.date * self.day_width % self.day_width)
def date_to_x(self, date):
return (date.toordinal() - self.date + 1) * self.day_width
def days_visible(self):
return int(self.width / self.day_width)
def dates_visible(self):
s = datetime.date.fromordinal(int(self.date))
e = datetime.date.fromordinal(int(self.date) + self.days_visible())
return s, e
def draw_comfort_lines(self, cr):
cr.save()
cr.set_line_width(2.0)
cr.set_source(settings.comfort_line_color)
cr.rectangle(0, 0, self.width, self.height)
cr.stroke()
cr.move_to(self.day_width, 0)
cr.line_to(self.day_width, self.height)
cr.stroke()
cr.restore()
class DayHeader(WeekViewBase):
def __init__(self, info, history, *args, **kwargs):
WeekViewBase.__init__(self, info, *args, **kwargs)
self.scrolling = MouseCommandDispatcher(
history,
(DragCalendarHorizontal,))
self.scrolling.observe(self)
self.set_size_request(600, int(30))
def paint(self, cr):
cr.identity_matrix()
self.clear_background(cr)
self.draw_day_headers(cr)
self.draw_top_left_corner(cr)
cr.save()
cr.rectangle(1, 0, self.width - 2, self.height / 2)
cr.set_source(settings.gloss_gradient)
m = cairo.Matrix()
m.scale(1.0/self.width, 1.0/self.height)
settings.gloss_gradient.set_matrix(m)
cr.fill()
cr.restore()
def draw_day_header(self, cr, nth_day):
x = self.scale * (self.get_week_pixel_offset() +
nth_day * self.day_width)
leftmost_day = int(self.date)
date = self.get_date(leftmost_day + nth_day)
weekday = date.weekday()
if weekday > 4:
bgcolor = settings.weekday_bg_color
else:
bgcolor = settings.weekend_bg_color
area = shapes.Area(x, 0, self.day_width * self.scale, self.height)
shapes.labeled_box(
cr,
area,
date.strftime("%a\n%x"),
bgcolor,
settings.heading_outline_color,
settings.text_color)
def draw_day_headers(self, cr):
cr.save()
cr.rectangle(self.day_width * self.scale,
0, self.width - self.day_width * self.scale,
self.height)
cr.clip()
for i in xrange(0, (self.days_visible()) + 1):
self.draw_day_header(cr, i)
cr.restore()
def draw_top_left_corner(self, cr):
area = shapes.Area(0, 0, self.day_width * self.scale, self.height)
shapes.labeled_box(
cr,
area,
datetime.date.fromordinal(int(self.date + 1)).strftime("%x"),
settings.corner_bg_color,
settings.heading_outline_color,
settings.text_color)
class UntimedEvents(WeekViewBase):
__gtype_name__ = "UntimedEvents"
def __init__(self, info, undo, *args, **kwargs):
WeekViewBase.__init__(self, info, *args, **kwargs)
self.info = info
self.undo = undo
self.set_size_request(600, 50)
def sort_allday_events_by_date(self):
events = {}
for event, period in self.info.model.timedOccurrences(*self.dates_visible()):
if period.all_day:
date = period.date
if not date in events:
events[date] = []
events[date].append(event.description)
return events
def paint(self, cr):
cr.identity_matrix()
cr.scale(self.scale, self.scale)
self.clear_background(cr)
cr.set_source(settings.grid_line_color)
cr.set_line_width(settings.grid_line_width)
events = self.sort_allday_events_by_date()
x = self.get_week_pixel_offset()
cr.save()
cr.set_antialias(cairo.ANTIALIAS_NONE)
for i in xrange(0, (self.days_visible()) + 1):
x += self.day_width
cr.move_to (x, 0)
cr.line_to (x, self.height)
cr.stroke()
cr.restore()
cr.save()
cr.rectangle(self.day_width, 0, self.width - self.day_width, self.height)
cr.clip()
for date, evts in events.iteritems():
y = 0
x = self.date_to_x(date)
for event in evts:
th = shapes.text_height(cr, self.day_width, event)
area = shapes.Area(x, y + 2, self.day_width, th).shrink(3, 0)
shapes.filled_box(cr, area, settings.default_event_bg_color,
settings.default_event_outline_color)
shapes.left_aligned_text(cr, area, event,
settings.default_event_text_color)
y += 20
cr.restore()
self.draw_comfort_lines(cr)
class TimedEvents(WeekViewBase):
__gtype_name__ = "WeekView"
x = gobject.property(type=int, default=0)
y = gobject.property(type=int, default=0)
_y_scroll_offset = 0
height = scaled_property("height")
y_scroll_offset = scaled_property("y_scroll_offset")
handle_locations = None
def __init__(self, info, undo, *args, **kwargs):
WeekViewBase.__init__(self, info, *args, **kwargs)
self.editing = False
self.occurrences = {}
self.selected = None
self.font_desc = pango.FontDescription("Sans 8")
self.cursor_showing = True
self.ti = TextInput(self.text_changed_cb)
self.ti.observe(self)
gobject.timeout_add(500, self._blink_cursor)
self.dispatcher = MouseCommandDispatcher(
undo,
drag_commands = (DragCalendarVertical,
SetEventStart,
SetEventEnd,
MoveTimedEvent,
SelectArea),
click_commands = (SelectPoint,))
self.dispatcher.observe(self)
def _blink_cursor(self):
if not self.editing:
return True
self.cursor_showing = not self.cursor_showing
self.queue_draw()
return True
def point_to_datetime(self, x, y, snap=True):
x /= self.scale
y /= self.scale
hour = ((y + (- self.y_scroll_offset)) /
self.hour_height)
if snap:
minute = quantize(hour % 1 * 60, 15)
else:
minute = hour % 1 * 60
date = int(self.date + (x - self.day_width)
/ self.day_width)
ret = datetime.datetime.fromordinal(date)
delta = datetime.timedelta(hours=int(hour), minutes=minute)
return ret + delta
def point_to_timedelta(self, x, y, snap=True):
x /= self.scale
y /= self.scale
day = int(x / self.day_width)
if y < 0:
day += 1
minute = (day * 24 * 60) + quantize(int((y / self.hour_height) * 60), 15)
return datetime.timedelta(minutes=minute)
def date_visible(self, dt):
return (self.date - 1 <
dt.toordinal() <
self.date + self.days_visible())
def datetime_to_point(self, dt):
if not self.date_visible(dt):
raise DateNotVisible(dt)
return (self.date_to_x(dt),
(dt.hour + dt.minute / 60.0)
* self.hour_height + self.y_scroll_offset)
def area_from_start_end(self, start, end):
start = self.datetime_to_point(start)
end = self.datetime_to_point(end)
return shapes.Area(start[0], start[1], self.day_width, end[1] - start[1])
def point_to_occurrence(self, x, y):
point = (x / self.scale, y / self.scale)
for ordinal, (event, area, period) in self.occurrences.iteritems():
if area.contains_point(point):
return event, ordinal, area
return None
def point_to_event(self, x, y):
ret = self.point_to_occurrence(x, y)
return ret[0] if ret else None
def selection_handles(self, cr):
area = self.occurrences[self.selected][1]
radius = 10
top = area.above(2, radius)
bottom = area.below(2, radius)
shapes.upward_tab(cr, top)
shapes.downward_tab(cr, bottom)
cr.set_source(settings.handle_arrow_color)
shapes.upward_triangle(cr, top.scale(0.1, 0.8))
shapes.downward_triangle(cr, bottom.scale(0.1, 0.8))
self.handle_locations = top, bottom
def point_in_handle (self, x, y):
if not self.handle_locations:
return 0
top, bottom = self.handle_locations
point = (x / self.scale, y / self.scale)
if top.contains_point(point):
return 1
if bottom.contains_point(point):
return 2
return 0
def draw_grid(self, cr):
cr.save()
cr.rectangle(0, 0,
self.width, self.height)
cr.clip()
cr.set_source(settings.grid_line_color)
cr.set_line_width(settings.grid_line_width)
cr.set_antialias(cairo.ANTIALIAS_NONE)
for i in xrange(1, 25):
cr.move_to(0, self.y_scroll_offset + i * self.hour_height)
cr.line_to(self.width, self.y_scroll_offset + i * self.hour_height)
cr.stroke()
x = self.get_week_pixel_offset()
max_height = min(self.hour_height * 24 + self.y_scroll_offset, self.height)
for i in xrange(0, (self.days_visible()) + 1):
# draw vertical lines
x += self.day_width
cr.move_to (x, 0)
cr.line_to (x, max_height)
cr.stroke()
cr.restore()
def draw_hour_header(self, cr, hour):
area = shapes.Area(0,
hour * self.hour_height + self.y_scroll_offset,
self.day_width,
self.hour_height)
shapes.centered_text(
cr,
area,
"%2d:00" % hour,
settings.text_color)
def draw_hour_headers(self, cr):
cr.save()
cr.rectangle(0, 0, self.day_width, self.height)
cr.clip()
for i in range(0, 24):
self.draw_hour_header(cr, i)
cr.restore()
def draw_event(self, cr, event, period):
try:
area = self.area_from_start_end(period.start, period.end).shrink(3, 0)
except DateNotVisible:
return
#shapes.filled_box(cr, area, settings.default_event_bg_color)
shapes.rounded_rect(cr, area, 5)
m = cairo.Matrix()
m.scale(1.0/area.width, 1.0/area.height)
m.translate(-area.x, -area.y)
settings.default_event_bg_color.set_matrix(m)
cr.set_source(settings.default_event_bg_color)
cr.fill_preserve()
#cr.set_line_width(1.0)
cr.set_source(settings.default_event_outline_color)
settings.default_event_outline_color.set_matrix(m)
#cr.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
cr.stroke()
if (self.selected and
(event == self.selected[0]) and
(period.ordinal == self.selected[1]) and
(self.cursor_showing)):
cursor_pos = self.ti.get_cursor_pos()
else:
cursor_pos = -1
lyt = shapes.left_aligned_text(cr, area.shrink(1, 3), event.description,
settings.default_event_text_color,
cursor_pos)
self.occurrences[(event, period.ordinal)] = (event, area, period)
def draw_events(self, cr):
cr.save()
cr.rectangle(self.day_width, 0, self.width - self.day_width, self.height)
cr.clip()
self.occurrences = {}
for evt, period in self.model.timedOccurrences(*self.dates_visible()):
if period.all_day:
continue
self.draw_event(cr, evt, period)
cr.restore()
def draw_marquee(self, cr):
if not self.selection_recurrence:
return
s, e = self.dates_visible()
for instance in self.selection_recurrence.timedOccurrences(s, e):
try:
area = self.area_from_start_end(instance.start,
instance.end).shrink(2, 0)
except DateNotVisible:
return
shapes.filled_box(cr, area, settings.marquee_fill_color)
cr.set_source(settings.marquee_text_color)
text = instance.start.strftime ("%X")
shapes.text_above(cr, text, area.x, area.y - 2, area.width)
text = instance.end.strftime ("%X")
shapes.text_below(cr, text, area.x, area.y2 + 2, area.width)
duration = instance.duration
m = int (duration.seconds / 60) % 60
h = int (duration.seconds / 60 / 60)
text = "%dh %dm" % (h, m)
shapes.centered_text(cr, area, text, settings.text_color)
def paint(self, cr):
cr.identity_matrix()
cr.scale(self.scale, self.scale)
self.clear_background(cr)
self.draw_hour_headers(cr)
self.draw_grid(cr)
self.draw_events(cr)
cr.save()
cr.rectangle(self.day_width, 0,
self.width - self.day_width, self.height)
cr.clip()
self.draw_marquee(cr)
if (self.selected) and (self.selected in self.occurrences):
self.selection_handles (cr)
cr.restore()
self.draw_comfort_lines(cr)
def select_area(self, x, y, width, height, quantize=True):
try:
start = self.point_to_datetime (x, y, quantize)
end = self.point_to_datetime(x + width, y + height, quantize)
except DateNotVisible:
return
start_date = datetime.date(start.year, start.month, start.day)
end_date = datetime.date(end.year, end.month, end.day)
start_time = datetime.time(start.hour, start.minute)
end_time = datetime.time(end.hour, end.minute)
if (end_date - start_date).days < 4:
self.info.selection_recurrence = recurrence.Period(
recurrence.DateSet(*recurrence.dateRange(start_date, end_date)),
start_time,
end_time)
else:
self.info.selection_recurrence = recurrence.Period(
recurrence.Until(recurrence.Daily(start_date, 1), end_date),
start_time,
end_time)
def select_point(self, x, y):
occurrence = self.point_to_occurrence(x, y)
if occurrence:
self.select_occurrence(occurrence[1])
else:
self.select_occurrence(None)
def get_occurence_event(self, occurrence):
try:
return self.occurrences[occurrence][0]
except KeyError:
return None
def select_occurrence(self, occurrence):
self.info.selected = occurrence
if occurrence:
self.configure_editor(self.get_occurence_event(occurrence))
self.queue_draw()
def configure_editor(self, event):
if event:
self.grab_focus()
self.ti.set_text(event.description)
self.editing = True
else:
self.editing = False
def text_changed_cb(self):
if not self.editing:
return
self.get_occurence_event(self.selected).description = self.ti.get_text()
self.queue_draw()
class WeekView(gtk.VBox):
def __init__(self, info, undo, history):
gtk.VBox.__init__(self)
self.day_header = DayHeader(info, history)
self.timed = TimedEvents(info, undo)
self.untimed = UntimedEvents(info, history)
pane = gtk.VPaned()
pane.add(self.untimed)
pane.add(self.timed)
pane.show()
self.pack_start(self.day_header, False, False)
self.pack_start(pane, True, True)
self.show()
def zoom_in(self):
scale = self.day_header.scale
self.set_scale(scale + 0.1);
def zoom_out(self):
scale = self.day_header.scale
self.set_scale(scale - 0.1);
def set_scale(self, scale):
self.day_header.scale = scale
self.timed.scale = scale
self.untimed.scale = scale
class SelectPoint(Command):
@classmethod
def create_for_point(cls, instance, point):
return cls(instance, *point)
@classmethod
def can_do(cls, instance, point):
return True
def __init__(self, instance, x, y):
self.instance = instance
self.point = x, y
self.selected = self.instance.selected
self.selection = self.instance.selection_recurrence
def do(self):
self.instance.select_point(*self.point)
self.instance.info.selection_recurrence = None
return True
def undo(self):
self.instance.select_occurrence(self.selected)
self.instance.info.selection_recurrence = self.selection
class SelectArea(MouseCommand):
@classmethod
def can_do(cls, instance, abs):
return True
def __init__(self, instance, abs):
self.instance = instance
self.mdown = abs
self.selected = self.instance.selected
self.selection_recurrence = self.instance.selection_recurrence
def do(self):
self.instance.select_occurrence(None)
# normalize to x, y, width, height with positive values
x1 = min(self.mdown[0], self.abs[0])
y1 = min(self.mdown[1], self.abs[1])
x2 = max(self.mdown[0], self.abs[0])
y2 = max(self.mdown[1], self.abs[1])
width = x2 - x1
height = y2 - y1
# constrain selection to the day where the mouse was first clicked.
self.instance.select_area (x1, y1, width, height,
self.shift)
return True
def undo(self):
self.instance.select_occurrence(self.selected)
self.instance.info.selection_recurrence = self.selection_recurrence
class MoveTimedEvent(MouseCommand):
cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
@classmethod
def create_for_point(cls, instance, abs):
event = instance.point_to_event(*abs)
if event:
return MoveTimedEvent(instance, event, abs)
@classmethod
def can_do(cls, instance, abs):
return not (instance.point_to_event(*abs) is None)
def __init__(self, instance, event, abs):
self.instance = instance
self.selected = instance.selected
self.mdown = abs
self.old = event.recurrence
self.event = event
self.offset = instance.day_width / 2
self.allday = self.old.toAllday()
def do(self):
x, y = self.rel
delta = self.instance.point_to_timedelta(int(x + self.offset), y, self.shift)
if self.abs[1] < 0:
delta = datetime.timedelta(delta.days)
self.event.recurrence = self.allday + delta
else:
self.event.recurrence = self.old + delta
return True
def undo(self):
self.event.recurrence = self.old
self.instance.info.selected = self.selected
class SetEventStart(MouseCommand):
cursor = gtk.gdk.Cursor(gtk.gdk.TOP_SIDE)
@classmethod
def can_do(cls, instance, abs):
return instance.point_in_handle(*abs) == 1
def __init__(self, instance, abs):
self.mdown = abs
self.instance = instance
self.selected = instance.selected
occurrence = instance.occurrences[self.selected][2]
self.recurrence = occurrence.creator
self.pos = self.recurrence.start
def do(self):
self.recurrence.start = min(
self.instance.point_to_datetime(self.mdown[0], self.abs[1],
self.shift).time(),
self.recurrence.end)
self.instance.queue_draw()
return True
def undo(self):
self.recurrence.start = self.pos
self.selected = self.selected
class SetEventEnd(MouseCommand):
cursor = gtk.gdk.Cursor(gtk.gdk.BOTTOM_SIDE)
@classmethod
def can_do(cls, instance, abs):
return instance.point_in_handle(*abs) == 2
def __init__(self, instance, abs):
self.mdown = abs
self.instance = instance
self.selected = instance.selected
occurrence = instance.occurrences[self.selected][2]
self.recurrence = occurrence.creator
self.pos = self.recurrence.end
def do(self):
self.recurrence.end = max(
self.instance.point_to_datetime(self.mdown[0], self.abs[1],
self.shift).time(),
self.recurrence.start)
self.instance.queue_draw()
return True
def undo(self):
self.recurrence.end = self.pos
class DragCalendarHorizontal(MouseCommand):
cursor = gtk.gdk.Cursor(gtk.gdk.HAND1)
@classmethod
def can_do(cls, instance, abs):
return (0 <= abs[0] / instance.scale <= instance.width and
0 <= abs[1] / instance.scale <= instance.hour_height)
def __init__(self, instance, abs):
self.mdown = abs
self.instance = instance
self.pos = instance.date
self.flick_pos = None
self.scroller = KineticScrollAnimation(30, finished_cb=self._upate_pos)
def do(self):
if self.flick_pos is None:
self.instance.info.date = (self.pos - (self.rel[0] / self.instance.scale) /
self.instance.day_width)
else:
self.instance.info.date = self.flick_pos
def undo(self):
self.instance.date = self.pos
def flick_start(self):
self.scroller.observe(self.instance)
self.scroller.flick(self.flick_velocity[0] / self.instance.day_width)
def flick_stop(self):
self.scroller.stop()
def _upate_pos(self):
self.flick_pos = self.instance.date
class DragCalendarVertical(MouseCommand):
undoable = False
cursor = gtk.gdk.Cursor(gtk.gdk.HAND1)
@classmethod
def can_do(cls, instance, abs):
return instance.x <= abs[0] <= (instance.x + instance.day_width)
def __init__(self, instance, abs):
self.instance = instance
self.pos = self.instance.y_scroll_offset
def do(self):
self.instance.y_scroll_offset = max(
-((self.instance.hour_height * 25) - self.instance.height),
min(
0, self.pos + self.rel[1]))