dynamically importing the screens
[enigma2.git] / lib / python / Screens / Menu.py
1 #from Screen import *
2 from Components.MenuList import MenuList
3 from Components.ActionMap import ActionMap
4 from Components.Header import Header
5
6 # hack ... must be made dynamic
7 #from Screens.Setup import Setup
8 #from ServiceScan import ServiceScan
9 #from ScartLoopThrough import ScartLoopThrough
10 #from HarddiskSetup import HarddiskSelection
11 #from Components.Button import Button
12 #from Components.Label import Label
13 #from Components.ProgressBar import ProgressBar
14 #from ConfigMenu import *
15
16 #from About import *
17
18 #from Network import *
19
20 #from TimerEdit import *
21
22 # hack.... dynamically importing all screens
23 from __init__ import __all__
24 for i in __all__:
25         print "import " + i
26         if (i is not "Menu" ):
27                 exec "from " + i + " import *"
28
29 from enigma import quitMainloop
30
31 import xml.dom.minidom
32 from xml.dom import EMPTY_NAMESPACE
33 from skin import elementsWithTag
34
35 from Tools import XMLTools
36
37 # some screens
38 def doGlobal(screen):
39         screen["clock"] = Clock()
40
41
42 #               <item text="TV-Mode">self.setModeTV()</item>
43 #               <item text="Radio-Mode">self.setModeRadio()</item>
44 #               <item text="File-Mode">self.setModeFile()</item>
45 #               <item text="Scart">self.openDialog(ScartLoopThrough)</item>
46 #                       <item text="Sleep Timer"></item>
47
48
49 # read the menu
50 try:
51         # first we search in the current path
52         menufile = file('data/menu.xml', 'r')
53 except:
54         # if not found in the current path, we use the global datadir-path
55         menufile = file('/usr/share/enigma2/menu.xml', 'r')
56 mdom = xml.dom.minidom.parseString(menufile.read())
57 menufile.close()
58
59
60
61 def getValbyAttr(x, attr):
62         for p in range(x.attributes.length):
63                 a = x.attributes.item(p)
64                 attrib = str(a.name)
65                 value = str(a.value)
66                 if attrib == attr:
67                         return value
68                         
69         return ""
70
71 class boundFunction:
72         def __init__(self, fnc, *args):
73                 self.fnc = fnc
74                 self.args = args
75         def __call__(self):
76                 self.fnc(*self.args)
77
78 class configOSD(Screen):
79         #this needs focus handling - so not useable
80
81         def okbuttonClick(self):
82                 self.close
83  
84         def __init__(self, session):
85                 Screen.__init__(self, session)
86
87                 self["actions"] = ActionMap(["OkCancelActions"], 
88                         {
89                                 "ok": self.okbuttonClick,
90                                 "cancel": self.close
91                         })
92
93                 self["okbutton"] = Button("Save")
94
95                 self["txt_alpha"] = Label("Alpha:")
96                 self["sld_alpha"] = ProgressBar()
97                 self["sld_alpha"].setValue(50)
98
99                 self["txt_brightness"] = Label("Brightness:")
100                 self["sld_brightness"] = ProgressBar()
101                 self["sld_brightness"].setValue(50)
102
103                 self["txt_gamma"] = Label("Contrast:")
104                 self["sld_gamma"] = ProgressBar()
105                 self["sld_gamma"].setValue(50)
106
107 class Menu(Screen):
108         def okbuttonClick(self):
109                 print "okbuttonClick"
110                 selection = self["menu"].getCurrent()
111                 selection[1]()
112
113         def evalText(self, text):
114                 eval(text)
115                 
116         def nothing(self):                                                                                                                                      #dummy
117                 pass
118
119         def openDialog(self, dialog):                           # in every layer needed
120                 self.session.open(dialog)
121
122         def openSetup(self, dialog):
123                 self.session.open(Setup, dialog)
124
125         def addMenu(self, destList, node):
126                 MenuTitle = getValbyAttr(node, "text")
127                 if MenuTitle != "":                                                                                                                                     #check for title
128                         a = boundFunction(self.session.open, Menu, node, node.childNodes)
129                         #TODO add check if !empty(node.childNodes)
130                         destList.append((MenuTitle, a))
131                 
132         def addItem(self, destList, node):
133                 ItemText = getValbyAttr(node, "text")
134                 if ItemText != "":                                                                                                                                      #check for name
135                         b = XMLTools.mergeText(node.childNodes)
136                         if b != "":                                                                                                                                                             #check for function
137                                 destList.append((ItemText,boundFunction(self.evalText,b)))
138                         else:
139                                 destList.append((ItemText,self.nothing))                                #use dummy as function
140
141         def __init__(self, session, parent, childNode):
142                 Screen.__init__(self, session)
143                 
144                 list = []
145
146                 for x in childNode:                                                     #walk through the actual nodelist
147                         if x.nodeType != xml.dom.minidom.Element.nodeType:
148                             continue
149                         elif x.tagName == 'item':
150                                 self.addItem(list, x)
151                         elif x.tagName == 'menu':
152                                 self.addMenu(list, x)
153
154                 self["menu"] = MenuList(list)   
155                                                         
156                 self["actions"] = ActionMap(["OkCancelActions"], 
157                         {
158                                 "ok": self.okbuttonClick,
159                                 "cancel": self.close
160                         })
161                 
162                 a = getValbyAttr(parent, "title")
163                 if a == "":                                                                                                             #if empty use name
164                         a = getValbyAttr(parent, "text")
165                 self["title"] = Header(a)
166
167 class FixedMenu(Screen):
168         def okbuttonClick(self):
169                 selection = self["menu"].getCurrent()
170                 selection[1]()
171
172         def __init__(self, session, title, list):
173                 Screen.__init__(self, session)
174                 
175                 self["menu"] = MenuList(list)   
176                                                         
177                 self["actions"] = ActionMap(["OkCancelActions"], 
178                         {
179                                 "ok": self.okbuttonClick,
180                                 "cancel": self.close
181                         })
182                 
183                 self["title"] = Header(title)
184
185
186 class MainMenu(Menu):
187         #add file load functions for the xml-file
188         #remove old code (i.e. goScan / goClock...)
189         
190         def __init__(self, *x):
191                 Menu.__init__(self, *x)
192                 self.skinName = "Menu"
193
194         def openDialog(self, dialog):
195                 self.session.open(dialog)
196
197         def openSetup(self, dialog):
198                 self.session.open(Setup, dialog)
199
200         def goSetup(self):
201                 self.session.open(configTest)
202         
203         def setModeTV(self):
204                 print "set Mode to TV"
205                 pass
206
207         def setModeRadio(self):
208                 print "set Mode to Radio"
209                 pass
210
211         def setModeFile(self):
212                 print "set Mode to File"
213                 pass
214
215         def goScan(self):
216                 self.session.open(ServiceScan)
217         
218         def goClock(self):
219                 self.session.open(clockDisplay, Clock())