1 from Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.config import config
7 from Components.Label import Label
8 from Components.Slider import Slider
9 from Components.ActionMap import HelpableActionMap, NumberActionMap
10 from Components.Pixmap import *
11 from Components.MenuList import MenuList
12 from Components.ConfigList import ConfigList
14 from xml.sax import make_parser
15 from xml.sax.handler import ContentHandler
17 class Wizard(Screen, HelpableScreen):
19 class parseWizard(ContentHandler):
20 def __init__(self, wizard):
21 self.isPointsElement, self.isReboundsElement = 0, 0
26 def startElement(self, name, attrs):
27 print "startElement", name
28 self.currContent = name
31 if attrs.has_key('id'):
32 id = str(attrs.get('id'))
35 if attrs.has_key('nextstep'):
36 nextstep = str(attrs.get('nextstep'))
39 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": "", "nextstep": nextstep}
40 elif (name == "text"):
41 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
42 elif (name == "listentry"):
43 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
44 elif (name == "config"):
45 exec "from Screens." + str(attrs.get('module')) + " import *"
46 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
47 if (attrs.has_key('args')):
49 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
50 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
51 elif (name == "code"):
52 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
55 self.codeafter = False
56 elif (name == "condition"):
58 def endElement(self, name):
62 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
64 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
65 elif name == 'condition':
66 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
68 def characters(self, ch):
69 if self.currContent == "code":
71 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
73 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
74 elif self.currContent == "condition":
75 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
77 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
78 Screen.__init__(self, session)
79 HelpableScreen.__init__(self)
84 parser = make_parser()
85 print "Reading " + self.xmlfile
86 wizardHandler = self.parseWizard(self.wizard)
87 parser.setContentHandler(wizardHandler)
88 parser.parse('/usr/share/enigma2/' + self.xmlfile)
90 self.showSteps = showSteps
91 self.showStepSlider = showStepSlider
92 self.showList = showList
93 self.showConfig = showConfig
95 self.numSteps = len(self.wizard)
98 self["text"] = Label()
101 self["config"] = ConfigList([])
104 self["step"] = Label()
106 if self.showStepSlider:
107 self["stepslider"] = Slider(1, self.numSteps)
111 self["list"] = MenuList(self.list)
113 self.onShown.append(self.updateValues)
115 self.configInstance = None
117 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
125 "1": self.keyNumberGlobal,
126 "2": self.keyNumberGlobal,
127 "3": self.keyNumberGlobal,
128 "4": self.keyNumberGlobal,
129 "5": self.keyNumberGlobal,
130 "6": self.keyNumberGlobal,
131 "7": self.keyNumberGlobal,
132 "8": self.keyNumberGlobal,
133 "9": self.keyNumberGlobal,
134 "0": self.keyNumberGlobal
138 if len(self.stepHistory) > 1:
139 self.currStep = self.stepHistory[-2]
140 self.stepHistory = self.stepHistory[:-2]
141 if self.currStep < 1:
148 def getStepWithID(self, id):
150 for x in self.wizard:
151 if self.wizard[x]["id"] == id:
158 currStep = self.currStep
161 if (self.wizard[currStep]["config"]["screen"] != None):
162 # TODO: don't die, if no run() is available
163 # there was a try/except here, but i can't see a reason
164 # for this. If there is one, please do a more specific check
165 # and/or a comment in which situation there is no run()
166 self.configInstance.run()
169 if (len(self.wizard[currStep]["list"]) > 0):
170 nextStep = self.wizard[currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
171 self.currStep = self.getStepWithID(nextStep)
173 if (currStep == self.numSteps): # wizard finished
177 self.runCode(self.wizard[currStep]["codeafter"])
178 if self.wizard[currStep]["nextstep"] is not None:
179 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
183 print "Now: " + str(self.currStep)
185 def keyNumberGlobal(self, number):
186 if (self.wizard[self.currStep]["config"]["screen"] != None):
187 self.configInstance.keyNumberGlobal(number)
190 if (self.wizard[self.currStep]["config"]["screen"] != None):
191 self.configInstance.keyLeft()
195 if (self.wizard[self.currStep]["config"]["screen"] != None):
196 self.configInstance.keyRight()
200 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
201 self["config"].instance.moveSelection(self["config"].instance.moveUp)
202 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
203 self["list"].instance.moveSelection(self["list"].instance.moveUp)
207 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
208 self["config"].instance.moveSelection(self["config"].instance.moveDown)
209 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
210 self["list"].instance.moveSelection(self["list"].instance.moveDown)
213 def runCode(self, code):
218 def updateValues(self):
219 print "Updating values in step " + str(self.currStep)
221 self.stepHistory.append(self.currStep)
223 if self.configInstance is not None:
224 del self.configInstance["config"]
225 self.configInstance.doClose()
226 self.configInstance = None
228 self.condition = True
229 exec (self.wizard[self.currStep]["condition"])
232 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
233 if self.showStepSlider:
234 self["stepslider"].setValue(self.currStep)
236 print "wizard text", _(self.wizard[self.currStep]["text"])
237 self["text"].setText(_(self.wizard[self.currStep]["text"]))
239 self.runCode(self.wizard[self.currStep]["code"])
242 self["list"].instance.setZPosition(1)
244 if (len(self.wizard[self.currStep]["list"]) > 0):
245 self["list"].instance.setZPosition(2)
246 for x in self.wizard[self.currStep]["list"]:
247 self.list.append((_(x[0]), None))
248 self["list"].l.setList(self.list)
249 self["list"].moveToIndex(0)
252 self["config"].instance.setZPosition(1)
253 if (self.wizard[self.currStep]["config"]["screen"] != None):
254 if self.wizard[self.currStep]["config"]["type"] == "standalone":
255 print "Type is standalone"
256 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
258 self["config"].instance.setZPosition(2)
259 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
260 if self.wizard[self.currStep]["config"]["args"] == None:
261 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
263 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
264 self["config"].l.setList(self.configInstance["config"].list)
265 self.configInstance["config"].destroy()
266 self.configInstance["config"] = self["config"]
268 self["config"].l.setList([])
269 else: # condition false
277 def registerWizard(self, wizard, precondition):
278 self.wizards.append((wizard, precondition))
280 def getWizards(self):
282 for x in self.wizards:
283 if x[1] == 1: # precondition
287 wizardManager = WizardManager()