705962c0e39b96b87e2d01962c7d47088537c11d
[jamaendo] / jamaui / songposition.py
1 import logging
2 import gtk
3 import cairo
4
5 log = logging.getLogger(__name__)
6
7 # shows the current song position (looking a bit nicer than a default widget, hopefully)
8 class SongPosition(gtk.DrawingArea):
9     WIDTH = 8.0
10     HEIGHT = 8.0
11
12     def __init__(self):
13         gtk.DrawingArea.__init__(self)
14         self.connect('expose-event', self.on_expose)
15         self.set_size_request(24, 8)
16         self.pos = 0.0
17
18         orange0 = self.hex_to_flt(0xec, 0xac, 0x1f)
19         orange1 = self.hex_to_flt(0xea, 0x86, 0x1d, 0.25)
20         purple0 = self.hex_to_flt(0x81, 0x3e, 0x82)
21         purple1 = self.hex_to_flt(0x56, 0x2d, 0x5a, 0.25)
22
23         lightclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
24         lightclr.add_color_stop_rgba(0.0, *purple1)
25         lightclr.add_color_stop_rgba(1.0, *purple0)
26
27         darkclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
28         darkclr.add_color_stop_rgba(0.0, 0.0, 0.0, 0.0, 0.0)
29         darkclr.add_color_stop_rgba(1.0, 0.25, 0.25, 0.25, 1.0)
30
31         markerclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
32         markerclr.add_color_stop_rgba(0.0, *orange1)
33         markerclr.add_color_stop_rgba(0.5, *orange0)
34         markerclr.add_color_stop_rgba(1.0, *orange0)
35
36         self.lightclr = lightclr
37         self.darkclr = darkclr
38         self.markerclr = markerclr
39
40     def on_expose(self, widget, event):
41         context = self.window.cairo_create()
42         context.rectangle(event.area.x, event.area.y,
43             event.area.width, event.area.height)
44         context.clip()
45         self.draw(context)
46         return True
47
48     #ecac1f - light orange
49     #ea861d - dark orange
50
51     #813e82 - light purple
52     #562d5a - dark purple
53
54     def hex_to_flt(self, r, g, b, a = 255.0):
55         return float(r)/255.0, float(g)/255.0, float(b)/255.0, float(a)/255.0
56
57     def draw(self, context):
58         rect = self.get_allocation()
59
60
61         #context.set_source_rgb(1.0, 0.5, 0.0)
62         lowx = rect.width*self.pos - self.WIDTH*0.5
63         hix = rect.width*self.pos + self.WIDTH*0.5
64
65         if lowx < 0.0:
66             lowx = 0.0
67             hix = self.WIDTH
68         elif hix > rect.width:
69             lowx = rect.width - self.WIDTH
70             hix = rect.width
71
72         if lowx > 0.01:
73             context.rectangle(0, 0, lowx, rect.height)
74             context.set_source(self.lightclr)
75             context.fill()
76
77         if hix < rect.width-0.01:
78             context.rectangle(hix, 0, rect.width-hix, rect.height)
79             context.set_source(self.darkclr)
80             context.fill()
81
82         context.rectangle(lowx, 0, self.WIDTH, rect.height)
83         context.set_source(self.markerclr)
84         context.fill()
85
86     def set_position(self, pos):
87         assert 0 <= pos <= 1
88         self.pos = pos
89         self.invalidate()
90
91     def invalidate(self):
92         self.queue_draw()