Dynamic icons for volume control

Registered by Markus Schmidt

(Sorry for any mistake - I'm a complete launchpad-no0b! Just mailed with Pavel, one of the developers of volume-control, and he told me to use launchpad for my matter.)

I needed a more accurate display of the actual volume level. So I modified the existing applet to deal with SVG including placeholders with expressions to be replaced by a calculated value. Placeholders might look like this:

<rect
    height="[[[value = %f * 0.5]]]"
    ... />

...or something like this:

<rect
    style="opacity:[[[if %f > 50: value = 1.0]]];"/>

Placeholders are not limited to one per SVG, you can have some to rotate, some to resize (and so on) in one file.
An extra icon has to be created to display muted status.

Pavel wanted to look at diff, so here comes the output:

--- usr/lib/awn/applets/volume-control/volume-control.py 2008-05-15 01:41:08.000000000 +0200
+++ home/markus/.config/awn/applets/oRainge_volume-control/oRainge_volume-control.py 2008-05-19 03:26:09.000000000 +0200
@@ -31,6 +31,9 @@
 # Listing theme directories and showing icons
 import os

+# Search and replace on SVG
+import re, math
+
 # Drawing the GUI
 import gtk

@@ -73,8 +76,8 @@
         try:
             self.theme = self.awn.settings["theme"]
         except:
- self.theme = "Tango"
- self.awn.settings["theme"] = "Tango"
+ self.theme = "oRainge"
+ self.awn.settings["theme"] = "oRainge"

         # Backend
         self.backend = Backend(self)
@@ -88,24 +91,33 @@
         themepath = os.path.join(os.path.abspath(os.path.dirname(__file__)), \
             "Themes", self.theme)
         return self.awn.icon.file(os.path.join(themepath, \
- "audio-volume-%s.svg" % name))
+ "audio-volume.svg" % name))

     def setIcon(self):
+ regex = re.compile("\[\[\[.+?\]\]\]")
         volume = self.backend.getVolume()

- if volume == self.__lastVolume :
- return
-
- for i in [100, 93, 86, 79, 71, 64, 57, 50, 43, 36, 29, 21, 14, 7]:
- if volume >= i:
- icon = str(i)
- break
-
         themepath = os.path.join(os.path.abspath(os.path.dirname(__file__)), \
             "Themes", self.theme)
- self.awn.icon.file(os.path.join(themepath, \
- "audio-volume-%s.svg" % icon))
-
+
+ #Main changes for dynamic display
+ if volume > 0:
+ infile = open(os.path.join(themepath,"audio-volume.tpl"), "r")
+ icon = infile.read()
+ infile.close()
+ replace = regex.search(icon)
+ while replace != None:
+ value = 0.0
+ exec(replace.group()[3:-3] % float(volume))
+ icon = regex.sub("%f" % value, icon, count=1)
+ replace = regex.search(icon)
+ iconfile = open(os.path.join(themepath,"audio-volume.svg"), "w")
+ iconfile.write(icon)
+ iconfile.close()
+ self.awn.icon.file(os.path.join(themepath, "audio-volume.svg"))
+ else:
+ self.awn.icon.file(os.path.join(themepath, "audio-volume-0.svg"))
+
         self.__lastVolume = volume

     def wheel(self, widget=None, event=None):

I made some themes for this one too. This version of volume-control is published on my blog, you can get the tarball including those themes here: http://mein-neues-blog.de/files/oRainge/oRainge_volume-control.tar.gz
The shipped ones are a demonstration of different changes, values and if-statements. Images are all drawn by myself so there can't be any copyright related problems in the future with those icons. If you like 'em - use 'em :)
(my fav is the marshall button [goes to 11!] that rips off on mute...)

Blueprint information

Status:
Complete
Approver:
None
Priority:
Not
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
Obsolete
Series goal:
None
Implementation:
Unknown
Milestone target:
None
Completed by
onox

Related branches

Sprints

Whiteboard

Rejected. Pasted diff based on old applet and makes the applet more complex.

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.