936dcc965ab96dacd18f81d805e4f9111aec6462
[openblackhole/openblackhole-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="mainMenu" position="300,100" size="300,300" title="real main menu">
17                         <widget name="okbutton" position="10,190" size="280,50" />
18                         <widget name="title" position="10,10" size="280,20" />
19                         <widget name="menu" position="10,30" size="280,140" />
20                 </screen>
21                 <screen name="clockDisplay" position="300,100" size="300,300">
22                         <widget name="okbutton" position="10,10" size="280,40" />
23                         <widget name="title" position="10,120" size="280,50" />
24                         <widget name="theClock" position="10,60" size="280,50" />
25                 </screen>
26                 <screen name="infoBar" position="100,100" size="300,400" title="InfoBar">
27                         <widget name="channelSwitcher" position="10,190" size="280,50" />
28                 </screen>
29                 <screen name="channelSelection" position="300,100" size="300,300" title="Channel Selection">
30                         <widget name="okbutton" position="10,190" size="280,50" />
31                         <widget name="list" position="10,30" size="280,140" />
32                 </screen>
33                 <screen name="serviceScan" position="150,100" size="300,200" title="Service Scan">
34                         <widget name="scan_progress" position="10,10" size="280,50" />
35                         <widget name="scan_state" position="10,60" size="280,30" />
36                         <widget name="okbutton" position="10,100" size="280,40" />
37                 </screen>
38         </skin>
39 """)
40
41
42
43 def parsePosition(str):
44         x, y = str.split(',')
45         return ePoint(int(x), int(y))
46
47 def parseSize(str):
48         x, y = str.split(',')
49         return eSize(int(x), int(y))
50
51 def applyAttributes(guiObject, node):
52         # walk all attributes
53         for p in range(node.attributes.length):
54                 a = node.attributes.item(p)
55                 
56                 # convert to string (was: unicode)
57                 attrib = str(a.name)
58                 # TODO: proper UTF8 translation?! (for value)
59                 # TODO: localization? as in e1?
60                 value = str(a.value)
61                 
62                 # and set attributes
63                 if attrib == 'position':
64                         guiObject.move(parsePosition(value))
65                 elif attrib == 'size':
66                         guiObject.resize(parseSize(value))
67                 elif attrib == 'title':
68                         guiObject.setTitle(value)
69                 elif attrib != 'name':
70                         print "unsupported attribute " + attrib + "=" + value
71
72 def applyGUIskin(screen, parent, skin, name):
73         
74         myscreen = None
75         
76         # first, find the corresponding screen element
77         skin = dom.getElementsByTagName("skin")[0]
78         screens = skin.getElementsByTagName("screen")
79         del skin
80         for x in screens:
81                 if x.getAttribute('name') == name:
82                         myscreen = x
83         
84         assert myscreen != None, "no skin for screen '" + name + "' found!"
85         
86         applyAttributes(parent, myscreen)
87         
88         # now walk all widgets
89         for widget in myscreen.getElementsByTagName("widget"):
90                 wname = widget.getAttribute('name')
91                 if wname == None:
92                         print "widget has no name!"
93                         continue
94                 
95                 # get corresponding gui object
96                 try:
97                         guiObject = screen.data[wname]["instance"]
98                 except:
99                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
100                 
101                 applyAttributes(guiObject, widget)