network setup stuff
[enigma2.git] / skin.py
1 from enigma import *
2 import xml.dom.minidom
3 from xml.dom import EMPTY_NAMESPACE
4
5 from Tools.XMLTools import elementsWithTag, mergeText
6
7 colorNames = dict()
8
9 def dump(x, i=0):
10         print " " * i + str(x)
11         try:
12                 for n in x.childNodes:
13                         dump(n, i + 1)
14         except:
15                 None
16
17 # read the skin
18 try:
19         # first we search in the current path
20         skinfile = file('data/skin.xml', 'r')
21 except:
22         # if not found in the current path, we use the global datadir-path
23         skinfile = file('/usr/share/enigma2/skin.xml', 'r')
24 dom = xml.dom.minidom.parseString(skinfile.read())
25 skinfile.close()
26
27
28 def parsePosition(str):
29         x, y = str.split(',')
30         return ePoint(int(x), int(y))
31
32 def parseSize(str):
33         x, y = str.split(',')
34         return eSize(int(x), int(y))
35
36 def parseFont(str):
37         name, size = str.split(';')
38         return gFont(name, int(size))
39
40 def parseColor(str):
41         if str[0] != '#':
42                 try:
43                         return colorNames[str]
44                 except:
45                         raise ("color '%s' must be #aarrggbb or valid named color" % (str))
46         return gRGB(int(str[1:], 0x10))
47
48 def collectAttributes(skinAttributes, node):
49         # walk all attributes
50         for p in range(node.attributes.length):
51                 a = node.attributes.item(p)
52                 
53                 # convert to string (was: unicode)
54                 attrib = str(a.name)
55                 # TODO: proper UTF8 translation?! (for value)
56                 # TODO: localization? as in e1?
57                 value = str(a.value)
58                 
59                 skinAttributes.append((attrib, value))
60
61 def applySingleAttribute(guiObject, desktop, attrib, value):            
62         # and set attributes
63         try:
64                 if attrib == 'position':
65                         guiObject.move(parsePosition(value))
66                 elif attrib == 'size':
67                         guiObject.resize(parseSize(value))
68                 elif attrib == 'title':
69                         guiObject.setTitle(value)
70                 elif attrib == 'text':
71                         guiObject.setText(value)
72                 elif attrib == 'font':
73                         guiObject.setFont(parseFont(value))
74                 elif attrib == "pixmap":
75                         ptr = gPixmapPtr()
76                         if loadPNG(ptr, value):
77                                 raise "loading PNG failed!"
78                         x = ptr
79                         ptr = ptr.__deref__()
80                         desktop.makeCompatiblePixmap(ptr)
81                         guiObject.setPixmap(ptr)
82                         # guiObject.setPixmapFromFile(value)
83                 elif attrib == "valign":
84                         try:
85                                 guiObject.setVAlign(
86                                         { "top": guiObject.alignTop,
87                                                 "center": guiObject.alignCenter,
88                                                 "bottom": guiObject.alignBottom
89                                         }[value])
90                         except KeyError:
91                                 print "valign must be either top, center or bottom!"
92                 elif attrib == "halign":
93                         try:
94                                 guiObject.setHAlign(
95                                         { "left": guiObject.alignLeft,
96                                                 "center": guiObject.alignCenter,
97                                                 "right": guiObject.alignRight,
98                                                 "block": guiObject.alignBlock
99                                         }[value])
100                         except KeyError:
101                                 print "halign must be either left, center, right or block!"
102                 elif attrib == "flags":
103                         flags = value.split(',')
104                         for f in flags:
105                                 try:
106                                         fv = eWindow.__dict__[f]
107                                         guiObject.setFlag(fv)
108                                 except KeyError:
109                                         print "illegal flag %s!" % f
110                 elif attrib == "backgroundColor":
111                         guiObject.setBackgroundColor(parseColor(value))
112                 elif attrib == "foregroundColor":
113                         guiObject.setForegroundColor(parseColor(value))
114                 elif attrib != 'name':
115                         print "unsupported attribute " + attrib + "=" + value
116         except int:
117 # AttributeError:
118                 print "widget %s (%s) doesn't support attribute %s!" % ("", guiObject.__class__.__name__, attrib)
119
120 def applyAllAttributes(guiObject, desktop, attributes):
121         for (attrib, value) in attributes:
122                 applySingleAttribute(guiObject, desktop, attrib, value)
123
124 def loadSkin(desktop):
125         print "loading skin..."
126         
127         def getPNG(x):
128                 g = gPixmapPtr()
129                 loadPNG(g, x)
130                 g = g.grabRef()
131                 return g
132         
133         skin = dom.childNodes[0]
134         assert skin.tagName == "skin", "root element in skin must be 'skin'!"
135         
136         for c in elementsWithTag(skin.childNodes, "colors"):
137                 for color in elementsWithTag(c.childNodes, "color"):
138                         name = str(color.getAttribute("name"))
139                         color = str(color.getAttribute("value"))
140                         
141                         if not len(color):
142                                 raise ("need color and name, got %s %s" % (name, color))
143                                 
144                         colorNames[name] = parseColor(color)
145         
146         for windowstyle in elementsWithTag(skin.childNodes, "windowstyle"):
147                 style = eWindowStyleSkinned()
148                 
149                 for borderset in elementsWithTag(windowstyle.childNodes, "borderset"):
150                         bsName = str(borderset.getAttribute("name"))
151                         for pixmap in elementsWithTag(borderset.childNodes, "pixmap"):
152                                 bpName = str(pixmap.getAttribute("pos"))
153                                 filename = str(pixmap.getAttribute("filename"))
154                                 
155                                 png = getPNG(filename)
156                                 
157                                 # adapt palette
158                                 desktop.makeCompatiblePixmap(png)
159                                 style.setPixmap(eWindowStyleSkinned.__dict__[bsName], eWindowStyleSkinned.__dict__[bpName], png)
160
161                 for color in elementsWithTag(windowstyle.childNodes, "color"):
162                         type = str(color.getAttribute("name"))
163                         color = parseColor(color.getAttribute("color"))
164                         
165                         try:
166                                 style.setColor(eWindowStyleSkinned.__dict__["col" + type], color)
167                         except:
168                                 raise ("Unknown color %s" % (type))
169                         
170                 x = eWindowStyleManagerPtr()
171                 eWindowStyleManager.getInstance(x)
172                 x.setStyle(style)
173
174 def readSkin(screen, skin, name, desktop):
175         myscreen = None
176         
177         # first, find the corresponding screen element
178         skin = dom.childNodes[0]
179         
180         for x in elementsWithTag(skin.childNodes, "screen"):
181                 if x.getAttribute('name') == name:
182                         myscreen = x
183         del skin
184         
185         assert myscreen != None, "no skin for screen '" + name + "' found!"
186
187         screen.skinAttributes = [ ]
188         collectAttributes(screen.skinAttributes, myscreen)
189         
190         screen.additionalWidgets = [ ]
191         
192         # now walk all widgets
193         for widget in elementsWithTag(myscreen.childNodes, "widget"):
194                 wname = widget.getAttribute('name')
195                 if wname == None:
196                         print "widget has no name!"
197                         continue
198                 
199                 # get corresponding gui object
200                 try:
201                         attributes = screen[wname].skinAttributes = [ ]
202                 except:
203                         raise str("component with name '" + wname + "' was not found in skin of screen '" + name + "'!")
204                 
205                 collectAttributes(attributes, widget)
206
207         # now walk additional objects
208         for widget in elementsWithTag(myscreen.childNodes, lambda x: x != "widget"):
209                 if widget.tagName == "applet":
210                         codeText = mergeText(widget.childNodes).strip()
211                         type = widget.getAttribute('type')
212
213                         code = compile(codeText, "skin applet", "exec")
214                         
215                         if type == "onLayoutFinish":
216                                 screen.onLayoutFinish.append(code)
217                         else:
218                                 raise str("applet type '%s' unknown!" % type)
219                         
220                         continue
221                 
222                 class additionalWidget:
223                         pass
224                 
225                 w = additionalWidget()
226                 
227                 if widget.tagName == "eLabel":
228                         w.widget = eLabel
229                 elif widget.tagName == "ePixmap":
230                         w.widget = ePixmap
231                 else:
232                         raise str("unsupported stuff : %s" % widget.tagName)
233                 
234                 w.skinAttributes = [ ]
235                 collectAttributes(w.skinAttributes, widget)
236                 
237                 # applyAttributes(guiObject, widget, desktop)
238                 # guiObject.thisown = 0
239                 print screen.additionalWidgets
240                 screen.additionalWidgets.append(w)