nicer code (api v3 / oldapi)
[enigma2.git] / skin.py
1 from enigma import *
2 import xml.dom.minidom
3 from xml.dom import EMPTY_NAMESPACE
4
5 def dump(x, i=0):
6         print " " * i + str(x)
7         try:
8                 for n in x.childNodes:
9                         dump(n, i + 1)
10         except:
11                 None
12
13 dom = xml.dom.minidom.parseString(
14         """
15         <skin>
16                 <screen name="testDialog">
17                         <widget name="okbutton" position="10,10" size="280,40" />
18                         <widget name="title" position="10,120" size="280,50" />
19                 </screen>
20                 <screen name="clockDisplay" position="300,100" size="300,300">
21                         <widget name="okbutton" position="10,10" size="280,40" />
22                         <widget name="title" position="10,120" size="280,50" />
23                         <widget name="theClock" position="10,60" size="280,50" />
24                 </screen>
25         </skin>
26 """)
27
28
29
30 def parsePosition(str):
31         x, y = str.split(',')
32         return ePoint(int(x), int(y))
33
34 def parseSize(str):
35         x, y = str.split(',')
36         return eSize(int(x), int(y))
37
38 def applyAttributes(guiObject, node):
39         # walk all attributes
40         for p in range(node.attributes.length):
41                 a = node.attributes.item(p)
42                 
43                 # and set attributes
44                 if a.name == 'position':
45                         guiObject.move(parsePosition(a.value))
46                 elif a.name == 'size':
47                         guiObject.resize(parseSize(a.value))
48                 elif a.name != 'name':
49                         print "unsupported attribute " + a.name
50
51 def applyGUIskin(screen, skin, name):
52         
53         myscreen = None
54         
55         # first, find the corresponding screen element
56         skin = dom.getElementsByTagName("skin")[0]
57         screens = skin.getElementsByTagName("screen")
58         del skin
59         for x in screens:
60                 if x.getAttribute('name') == name:
61                         myscreen = x
62         
63         assert myscreen != None, "no skin for screen " + name + " found!"
64         
65         print "ok, found screen.."
66         
67         # now walk all widgets
68         for widget in myscreen.getElementsByTagName("widget"):
69                 name = widget.getAttribute('name')
70                 if name == None:
71                         print "widget has no name!"
72                         continue
73                 
74                 # get corresponding gui object
75                 guiObject = screen.data[name]["instance"]
76                 applyAttributes(guiObject, widget)