67e7a1261de4f62a49d037dcf3ef8fcd9825bb70
[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 = 16.0
11
12     def __init__(self):
13         gtk.DrawingArea.__init__(self)
14         self.connect('expose-event', self.on_expose)
15         self.set_size_request(int(self.WIDTH), int(self.HEIGHT))
16         self.pos = 0.0
17
18         orange0 = self.hex_to_flt(0xec, 0xac, 0x1f)
19         orange1 = self.hex_to_flt(0xea, 0x86, 0x1d)
20         orange2 = self.hex_to_flt(0xda, 0x76, 0x0d)
21         orange3 = self.hex_to_flt(0xd0, 0x70, 0x00)
22         purple0 = self.hex_to_flt(0x81, 0x3e, 0x82)
23         purple1 = self.hex_to_flt(0x56, 0x2d, 0x5a)
24
25         lightclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
26         lightclr.add_color_stop_rgb(0.0, 1.0, 1.0, 1.0)
27         lightclr.add_color_stop_rgb(0.1, *orange0)
28         lightclr.add_color_stop_rgb(0.5, *orange1)
29         lightclr.add_color_stop_rgb(0.5, *orange2)
30         lightclr.add_color_stop_rgb(1.0, *orange3)
31
32         darkclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
33         darkclr.add_color_stop_rgb(0.0, 0.5, 0.5, 0.5)
34         darkclr.add_color_stop_rgb(0.5, 0.0, 0.0, 0.0)
35         darkclr.add_color_stop_rgb(1.0, 0.25, 0.25, 0.25)
36
37         markerclr = cairo.LinearGradient(0.0, 0.0, 0.0, self.HEIGHT)
38         markerclr.add_color_stop_rgb(0.0, 1.0, 1.0, 1.0)
39         markerclr.add_color_stop_rgb(0.5, 1.0, 1.0, 1.0)
40         markerclr.add_color_stop_rgb(1.0, 1.0, 1.0, 1.0)
41
42         self.lightclr = lightclr
43         self.darkclr = darkclr
44         self.markerclr = markerclr
45
46     def on_expose(self, widget, event):
47         context = self.window.cairo_create()
48         context.rectangle(event.area.x, event.area.y,
49             event.area.width, event.area.height)
50         context.clip()
51         self.draw(context)
52         return True
53
54     #ecac1f - light orange
55     #ea861d - dark orange
56
57     #813e82 - light purple
58     #562d5a - dark purple
59
60     def hex_to_flt(self, r, g, b):
61         return float(r)/255.0, float(g)/255.0, float(b)/255.0
62
63     def draw(self, context):
64         rect = self.get_allocation()
65
66
67         #context.set_source_rgb(1.0, 0.5, 0.0)
68         lowx = rect.width*self.pos
69         hix = rect.width*self.pos
70
71         if lowx < 0.0:
72             lowx = 0.0
73             hix = self.WIDTH
74         elif hix > rect.width:
75             lowx = rect.width - self.WIDTH
76             hix = rect.width
77
78         context.rectangle(0, 0, rect.width, rect.height)
79         context.set_source(self.darkclr)
80         context.fill()
81
82         if lowx > 0.01:
83             context.rectangle(0, 0, lowx, rect.height)
84             context.set_source(self.lightclr)
85             context.fill()
86
87         context.rectangle(0, 0, rect.width, rect.height)
88         context.set_source_rgb(0.3, 0.3, 0.3)
89         context.stroke()
90
91         #context.rectangle(lowx, 0, self.WIDTH, rect.height)
92         #context.set_source(self.markerclr)
93         #context.fill()
94
95     def set_position(self, pos):
96         if pos < 0.0:
97             pos = 0.0
98         elif pos > 1.0:
99             pos = 1.0
100         self.pos = pos
101         self.invalidate()
102
103     def invalidate(self):
104         self.queue_draw()