0367b60fd0a072b0d58838a4312e9c81d2b10e00
[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" font="Arial:20" valign="center" halign="center" />
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="80,350" size="540,150" title="InfoBar">
27                         <widget name="CurrentTime" position="10,10" size="40,30" />
28                         <widget name="ServiceName" position="50,20" size="200,30" />
29                         <widget name="Event_Now" position="100,40" size="300,30" valign="top" halign="left" />
30                         <widget name="Event_Next" position="100,90" size="300,30" valign="top" halign="left" />
31                         <widget name="Event_Now_Duration" position="440,40" size="80,30" valign="top" halign="left" />
32                         <widget name="Event_Next_Duration" position="440,90" size="80,30" valign="top" halign="left" />
33                         <eLabel position="70,0" size="300,30" text=".oO skin Oo." font="Arial:20" />
34                 </screen>
35                 <screen name="channelSelection" position="100,80" size="500,240" title="Channel Selection">
36                         <widget name="list" position="20,50" size="300,150" />
37                         <widget name="okbutton" position="340,50" size="140,30" />
38                 </screen>
39                 <screen name="serviceScan" position="150,100" size="300,200" title="Service Scan">
40                         <widget name="scan_progress" position="10,10" size="280,50" />
41                         <widget name="scan_state" position="10,60" size="280,30" />
42                         <widget name="okbutton" position="10,100" size="280,40" />
43                 </screen>
44         </skin>
45 """)
46
47 # filters all elements of childNode with the specified function
48 # example: nodes = elementsWithTag(childNodes, lambda x: x == "bla")
49 def elementsWithTag(el, tag):
50         for x in el:
51                 if x.nodeType != xml.dom.minidom.Element.nodeType:
52                         continue
53                 if tag(x.tagName):
54                         yield x
55
56 def parsePosition(str):
57         x, y = str.split(',')
58         return ePoint(int(x), int(y))
59
60 def parseSize(str):
61         x, y = str.split(',')
62         return eSize(int(x), int(y))
63
64 def parseFont(str):
65         name, size = str.split(':')
66         return gFont(name, int(size))
67
68 def applyAttributes(guiObject, node):
69         # walk all attributes
70         for p in range(node.attributes.length):
71                 a = node.attributes.item(p)
72                 
73                 # convert to string (was: unicode)
74                 attrib = str(a.name)
75                 # TODO: proper UTF8 translation?! (for value)
76                 # TODO: localization? as in e1?
77                 value = str(a.value)
78                 
79                 # and set attributes
80                 try:
81                         if attrib == 'position':
82                                 guiObject.move(parsePosition(value))
83                         elif attrib == 'size':
84                                 guiObject.resize(parseSize(value))
85                         elif attrib == 'title':
86                                 guiObject.setTitle(value)
87                         elif attrib == 'text':
88                                 guiObject.setText(value)
89                         elif attrib == 'font':
90                                 guiObject.setFont(parseFont(value))
91                         elif attrib == "valign":
92                                 try:
93                                         guiObject.setVAlign(
94                                                 { "top": guiObject.alignTop,
95                                                         "center": guiObject.alignCenter,
96                                                         "bottom": guiObject.alignBottom
97                                                 }[value])
98                                 except KeyError:
99                                         print "valign must be either top, center or bottom!"
100                         elif attrib == "halign":
101                                 try:
102                                         guiObject.setHAlign(
103                                                 { "left": guiObject.alignLeft,
104                                                         "center": guiObject.alignCenter,
105                                                         "right": guiObject.alignRight,
106                                                         "block": guiObject.alignBlock
107                                                 }[value])
108                                 except KeyError:
109                                         print "halign must be either left, center, right or block!"
110                         elif attrib != 'name':
111                                 print "unsupported attribute " + attrib + "=" + value
112                 except AttributeError:
113                         print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
114
115 def applyGUIskin(screen, skin, name):
116         myscreen = None
117         
118         # first, find the corresponding screen element
119         skin = dom.childNodes[0]
120         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
121         
122         for x in elementsWithTag(skin.childNodes, lambda x: x == "screen"):
123                 if x.getAttribute('name') == name:
124                         myscreen = x
125         del skin
126         
127         assert myscreen != None, "no skin for screen '" + name + "' found!"
128         
129         applyAttributes(screen.instance, myscreen)
130         
131         # now walk all widgets
132         for widget in elementsWithTag(myscreen.childNodes, lambda x: x == "widget"):
133                 wname = widget.getAttribute('name')
134                 if wname == None:
135                         print "widget has no name!"
136                         continue
137                 
138                 # get corresponding gui object
139                 try:
140                         guiObject = screen[wname].instance
141                 except:
142                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
143                 
144                 applyAttributes(guiObject, widget)
145
146         # now walk additional objects
147         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
148                 if widget.tagName == "eLabel":
149                         guiObject = eLabel(screen.instance)
150                 else:
151                         raise "unsupported stuff : %s" % widget.tagName
152                 
153                 applyAttributes(guiObject, widget)
154                 guiObject.thisown = 0