Added 'helldon' to transparently port the thing to the desktop :P
[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 = 32.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(0.5, *purple0)
26         lightclr.add_color_stop_rgba(0.501, *purple1)
27         lightclr.add_color_stop_rgba(1.0, *purple1)
28
29         darkclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
30         darkclr.add_color_stop_rgba(0.0, 0.0, 0.0, 0.0, 0.0)
31         darkclr.add_color_stop_rgba(1.0, 0.25, 0.25, 0.25, 1.0)
32
33         markerclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
34         markerclr.add_color_stop_rgba(0.0, 1.0, 1.0, 1.0, 0.0)
35         markerclr.add_color_stop_rgba(0.5, 1.0, 1.0, 1.0, 0.75)
36         markerclr.add_color_stop_rgba(1.0, 1.0, 1.0, 1.0, 1.0)
37
38         self.lightclr = lightclr
39         self.darkclr = darkclr
40         self.markerclr = markerclr
41
42     def on_expose(self, widget, event):
43         context = self.window.cairo_create()
44         context.rectangle(event.area.x, event.area.y,
45             event.area.width, event.area.height)
46         context.clip()
47         self.draw(context)
48         return True
49
50     #ecac1f - light orange
51     #ea861d - dark orange
52
53     #813e82 - light purple
54     #562d5a - dark purple
55
56     def hex_to_flt(self, r, g, b, a = 255.0):
57         return float(r)/255.0, float(g)/255.0, float(b)/255.0, float(a)/255.0
58
59     def draw(self, context):
60         rect = self.get_allocation()
61
62
63         #context.set_source_rgb(1.0, 0.5, 0.0)
64         lowx = rect.width*self.pos - self.WIDTH*0.5
65         hix = rect.width*self.pos + self.WIDTH*0.5
66
67         if lowx < 0.0:
68             lowx = 0.0
69             hix = self.WIDTH
70         elif hix > rect.width:
71             lowx = rect.width - self.WIDTH
72             hix = rect.width
73
74         if lowx > 0.01:
75             context.rectangle(0, 0, lowx, rect.height)
76             context.set_source(self.lightclr)
77             context.fill()
78
79         if hix < rect.width-0.01:
80             context.rectangle(hix, 0, rect.width-hix, rect.height)
81             context.set_source(self.darkclr)
82             context.fill()
83
84         context.rectangle(lowx, 0, self.WIDTH, rect.height)
85         context.set_source(self.markerclr)
86         context.fill()
87
88     def set_position(self, pos):
89         if pos < 0.0:
90             pos = 0.0
91         elif pos > 1.0:
92             pos = 1.0
93         self.pos = pos
94         self.invalidate()
95
96     def invalidate(self):
97         self.queue_draw()