Merge lp:~doctormo/indiv-screenlets/color-changing-ringsensor into lp:indiv-screenlets

Proposed by Martin Owens
Status: Merged
Approved by: Märt Põder
Approved revision: 874
Merge reported by: Märt Põder
Merged at revision: not available
Proposed branch: lp:~doctormo/indiv-screenlets/color-changing-ringsensor
Merge into: lp:indiv-screenlets
Diff against target: 173 lines (+73/-43)
1 file modified
src/RingSensors/RingSensorsScreenlet.py (+73/-43)
To merge this branch: bzr merge lp:~doctormo/indiv-screenlets/color-changing-ringsensor
Reviewer Review Type Date Requested Status
Märt Põder Approve
Review via email: mp+55178@code.launchpad.net

Description of the change

Adds some new colour options into the ring screenlet. Fancy and awesome.

To post a comment you must log in.
Revision history for this message
Märt Põder (boamaod) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/RingSensors/RingSensorsScreenlet.py'
2--- src/RingSensors/RingSensorsScreenlet.py 2011-02-23 23:36:12 +0000
3+++ src/RingSensors/RingSensorsScreenlet.py 2011-03-28 16:18:30 +0000
4@@ -17,7 +17,8 @@
5
6 import screenlets
7 from screenlets import sensors
8-from screenlets.options import FloatOption, BoolOption, StringOption, IntOption, ColorOption
9+from screenlets.options import FloatOption, BoolOption, StringOption,\
10+ IntOption, ColorOption, ColorsOption
11 import cairo
12 import pango
13 import sys
14@@ -57,8 +58,9 @@
15 linear = ''
16 hourMiddle = False
17 color_back = (1,1,1,0.6)
18- color_front =(1, 1, 1, 0.9)
19+ color_front = (1, 1, 1, 0.9)
20 color_text = (1, 1, 1, 1)
21+ style_front = 'Fan'
22 graph_type = 'Graph'
23 thickness = 24.0
24 ringSpacing = 26.0
25@@ -71,7 +73,7 @@
26 new_cpu = 0
27 wire_list = []
28 wire_data = []
29- big_bars = True
30+ segment_count = 12
31
32 down = 0
33 download = 0
34@@ -139,16 +141,23 @@
35 self.add_option(ColorOption(_('Sensors'),'color_back',
36 self.color_back, 'background color', ''))
37
38- self.add_option(ColorOption(_('Sensors'),'color_front',
39- self.color_front, _('Front color'),
40- ''))
41+ self.add_option(ColorsOption(_('Sensors'),'color_front',
42+ default=self.color_front, label=_('Active Color')))
43+
44+ self.add_option(StringOption(_('Sensors'),'style_front',
45+ default=self.style_front, label=_('Active Style'),
46+ choices=['Fan', 'Grad']))
47+
48 self.add_option(ColorOption(_('Sensors'),'color_text',
49 self.color_text, _('Text color'),
50 ''))
51 self.add_option(FloatOption(_('Sensors'), 'thickness', self.thickness, _('Ring Thickness'), '',min=0.0, max=500.0, increment=0.1))
52 self.add_option(FloatOption(_('Sensors'), 'blockSpacing', self.blockSpacing, _('Block Spacing'), '',min=0.0, max=6.0, increment=0.1))
53- self.add_option(BoolOption(_('Sensors'), 'big_bars',
54- self.big_bars, _('Use Big bars'), ''))
55+
56+ self.add_option(IntOption(_('Sensors'), 'segment_count',
57+ default=self.segment_count, label=_('Number of Segments'),
58+ desc=_('Big Bars is 12, Small is 60'), min=10, max=80))
59+
60 self.add_option(FloatOption(_('Sensors'), 'max_download', self.max_download, _('Max dl speed (kilobytes/s)'), '',min=0.0, max=1000000.0, increment=10))
61 self.add_option(FloatOption(_('Sensors'), 'max_upload', self.max_upload, _('Max ul speed (kilobytes/s)'), '',min=0.0, max=1000000.0, increment=10))
62
63@@ -278,54 +287,75 @@
64 self.redraw_canvas()
65 return True
66
67+ def gradiant_colours(self, colours, index, max):
68+ """Take a set of colours and make a pretty rainbow."""
69+ # These first checks provide compatability and stop wasteful
70+ # Mathamatics when we know the outcome.
71+ if type(colours[0]) in [float, int]:
72+ return colours
73+ if len(colours) == 1:
74+ return colours[0]
75+ if index == max:
76+ return colours[-1]
77+
78+ grad = float(index) / max
79+ isect = len(colours) - 1
80+ sect = int(grad * isect)
81+ grad = (float(index) - (max / isect * sect)) / (max / isect)
82+ first = colours[sect]
83+ second = colours[sect+1]
84+ return (
85+ first[0] + (second[0] - first[0]) * grad,
86+ first[1] + (second[1] - first[1]) * grad,
87+ first[2] + (second[2] - first[2]) * grad,
88+ first[3] + (second[3] - first[3]) * grad,
89+ )
90
91 def on_draw(self, ctx):
92-
93+ """Draw a rainbow coloured arc as the sensor value."""
94 ctx.scale(self.scale, self.scale)
95 # draw bg (if theme available)
96 ctx.set_operator(cairo.OPERATOR_ADD)
97-
98-
99- startrad = self.size-(self.thickness/2.0)
100- a = float(self.load*11)/100
101-
102-
103- ctx.set_line_width( self.thickness )
104- if self.big_bars:
105- for i in range(12):
106- if i == a or (i<=a ): col = self.color_front
107- else: col = self.color_back
108- if self.hourMiddle: radius = startrad-(self.ringSpacing*2.0)
109- else: radius = startrad
110- pos = -90+(self.blockSpacing/2.0)+(i*30)
111-
112- ctx.arc( 100, 100, radius, math.radians(pos), math.radians(pos+30-self.blockSpacing) )
113- ctx.set_source_rgba(col[0],col[1],col[2],col[3])
114- ctx.stroke()
115- else:
116- a = float(self.load*59)/100
117- for i in range(60):
118- if i == a or (i<=a ): col = self.color_front
119- else: col = self.color_back
120- if self.hourMiddle: radius = startrad-(self.ringSpacing*2.0)
121- else: radius = startrad
122- pos = -90+(self.blockSpacing/2.0)+(i*6)
123-
124- ctx.arc( 100, 100, radius, math.radians(pos), math.radians(pos+6-self.blockSpacing) )
125- ctx.set_source_rgba(col[0],col[1],col[2],col[3])
126- ctx.stroke()
127+
128+ startrad = self.size-(self.thickness/2.0)
129+ ctx.set_line_width( self.thickness )
130+
131+ # This could now be made into a more variable option.
132+ segments = self.segment_count
133+ fract = float(self.load * (segments-1)) / 100
134+
135+ col = (0, 0, 0, 1)
136+ if self.style_front == 'Fan':
137+ col = self.gradiant_colours(self.color_front, fract, segments - 1)
138+
139+ for i in range(segments):
140+ if i > fract:
141+ col = self.color_back
142+ elif self.style_front == 'Grad':
143+ col = self.gradiant_colours(self.color_front, i, segments - 1)
144+
145+ if self.hourMiddle:
146+ radius = startrad-(self.ringSpacing*2.0)
147+ else:
148+ radius = startrad
149+
150+ rad_width = 360 / segments
151+ pos = -90 + (self.blockSpacing / 2.0) + (i * rad_width)
152+ rad_from = math.radians(pos)
153+ rad_to = math.radians(pos + rad_width - self.blockSpacing)
154+ ctx.arc( 100, 100, radius, rad_from, rad_to )
155+ ctx.set_source_rgba(col[0], col[1], col[2], col[3])
156+ ctx.stroke()
157+
158 if len(str(self.load))==1:
159 self.load = "0" + str(self.load)
160 ctx.set_source_rgba(self.color_text[0],self.color_text[1],self.color_text[2],self.color_text[3])
161 if self.sensor.endswith('RPM') or self.sensor.endswith('C') or self.sensor.endswith('V') or self.sensor.find(':') != -1:
162- text = '<small><small><small><small>' +str(self.sensor.split(':')[0]) +'</small></small></small></small>\n'+str(self.sensor.split(':')[1])
163+ text = '<small><small><small><small>' +str(self.sensor.split(':')[0]) +'</small></small></small></small>\n'+str(self.sensor.split(':')[1])
164 else:
165 text = '<small><small><small><small>' +self.sensor +'</small></small></small></small>\n'+self.text_prefix + str(self.load) + self.text_suffix
166 ctx.set_operator(cairo.OPERATOR_OVER)
167 if self.show_text and self.theme:self.theme.draw_text(ctx,text, 0, 70, 'Free Sans', 25, self.width,pango.ALIGN_CENTER)
168-
169-
170-
171
172 def on_draw_shape(self,ctx):
173 self.on_draw(ctx)

Subscribers

People subscribed via source and target branches