1 from Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.config import config, KEY_LEFT, KEY_RIGHT
7 from Components.Label import Label
8 from Components.Slider import Slider
9 from Components.ActionMap import NumberActionMap
10 from Components.MenuList import MenuList
11 from Components.ConfigList import ConfigList
12 from Components.Sources.List import List
14 from enigma import eTimer
16 from xml.sax import make_parser
17 from xml.sax.handler import ContentHandler
19 class WizardSummary(Screen):
21 <screen position="0,0" size="132,64">
22 <widget name="text" position="6,4" size="120,42" font="Regular;14" transparent="1" />
23 <widget source="parent.list" render="Label" position="6,25" size="120,21" font="Regular;16">
24 <convert type="StringListSelection" />
28 def __init__(self, session, parent):
29 Screen.__init__(self, session, parent)
31 #names = parent.skinName
32 #if not isinstance(names, list):
35 #self.skinName = [x + "_summary" for x in names ]
36 #self.skinName.append("Wizard")
37 #print "*************+++++++++++++++++****************++++++++++******************* WizardSummary", self.skinName
39 self["text"] = Label("")
40 self.onShow.append(self.setCallback)
42 def setCallback(self):
43 self.parent.setLCDTextCallback(self.setText)
45 def setText(self, text):
46 self["text"].setText(text)
48 class Wizard(Screen, HelpableScreen):
49 def createSummary(self):
50 print "WizardCreateSummary"
53 class parseWizard(ContentHandler):
54 def __init__(self, wizard):
55 self.isPointsElement, self.isReboundsElement = 0, 0
60 def startElement(self, name, attrs):
61 #print "startElement", name
62 self.currContent = name
65 if attrs.has_key('id'):
66 id = str(attrs.get('id'))
70 if attrs.has_key('nextstep'):
71 nextstep = str(attrs.get('nextstep'))
74 if attrs.has_key('timeout'):
75 timeout = int(attrs.get('timeout'))
78 if attrs.has_key('timeoutaction'):
79 timeoutaction = str(attrs.get('timeoutaction'))
81 timeoutaction = 'nextpage'
83 if attrs.has_key('timeoutstep'):
84 timeoutstep = str(attrs.get('timeoutstep'))
87 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "timeout": timeout, "timeoutaction": timeoutaction, "timeoutstep": timeoutstep, "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": "", "nextstep": nextstep}
88 elif (name == "text"):
89 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
90 elif (name == "displaytext"):
91 self.wizard[self.lastStep]["displaytext"] = string.replace(str(attrs.get('value')), "\\n", "\n")
92 elif (name == "list"):
93 if (attrs.has_key('type')):
94 if attrs["type"] == "dynamic":
95 self.wizard[self.lastStep]["dynamiclist"] = attrs.get("source")
96 #self.wizard[self.lastStep]["list"].append(("Hallo", "test"))
97 if (attrs.has_key("evaluation")):
99 self.wizard[self.lastStep]["listevaluation"] = attrs.get("evaluation")
100 if (attrs.has_key("onselect")):
101 self.wizard[self.lastStep]["onselect"] = attrs.get("onselect")
102 elif (name == "listentry"):
103 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
104 elif (name == "config"):
105 type = str(attrs.get('type'))
106 self.wizard[self.lastStep]["config"]["type"] = type
107 if type == "ConfigList" or type == "standalone":
108 exec "from Screens." + str(attrs.get('module')) + " import *"
110 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
111 if (attrs.has_key('args')):
113 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
114 elif type == "dynamic":
115 self.wizard[self.lastStep]["config"]["source"] = str(attrs.get('source'))
116 if (attrs.has_key('evaluation')):
117 self.wizard[self.lastStep]["config"]["evaluation"] = str(attrs.get('evaluation'))
118 elif (name == "code"):
119 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
120 self.codeafter = True
122 self.codeafter = False
123 elif (name == "condition"):
126 def endElement(self, name):
127 self.currContent = ""
130 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
132 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
133 elif name == 'condition':
134 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
136 #print "Step number", self.lastStep, ":", self.wizard[self.lastStep]
139 def characters(self, ch):
140 if self.currContent == "code":
142 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
144 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
145 elif self.currContent == "condition":
146 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
148 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
149 Screen.__init__(self, session)
150 HelpableScreen.__init__(self)
152 self.stepHistory = []
155 parser = make_parser()
156 if not isinstance(self.xmlfile, list):
157 self.xmlfile = [self.xmlfile]
158 print "Reading ", self.xmlfile
159 wizardHandler = self.parseWizard(self.wizard)
160 parser.setContentHandler(wizardHandler)
161 for xmlfile in self.xmlfile:
162 if xmlfile[0] != '/':
163 parser.parse('/usr/share/enigma2/' + xmlfile)
165 parser.parse(xmlfile)
167 self.showSteps = showSteps
168 self.showStepSlider = showStepSlider
169 self.showList = showList
170 self.showConfig = showConfig
172 self.numSteps = len(self.wizard)
173 self.currStep = self.getStepWithID("start") + 1
175 self.timeoutTimer = eTimer()
176 self.timeoutTimer.callback.append(self.timeoutCounterFired)
178 self["text"] = Label()
181 self["config"] = ConfigList([])
184 self["step"] = Label()
186 if self.showStepSlider:
187 self["stepslider"] = Slider(1, self.numSteps)
191 self["list"] = List(self.list, enableWrapAround = True)
192 self["list"].onSelectionChanged.append(self.selChanged)
193 #self["list"] = MenuList(self.list, enableWrapAround = True)
195 self.onShown.append(self.updateValues)
197 self.configInstance = None
199 self.lcdCallbacks = []
201 self.disableKeys = False
203 self["actions"] = NumberActionMap(["WizardActions", "NumberActions", "ColorActions"],
213 "yellow": self.yellow,
215 "1": self.keyNumberGlobal,
216 "2": self.keyNumberGlobal,
217 "3": self.keyNumberGlobal,
218 "4": self.keyNumberGlobal,
219 "5": self.keyNumberGlobal,
220 "6": self.keyNumberGlobal,
221 "7": self.keyNumberGlobal,
222 "8": self.keyNumberGlobal,
223 "9": self.keyNumberGlobal,
224 "0": self.keyNumberGlobal
243 def setLCDTextCallback(self, callback):
244 self.lcdCallbacks.append(callback)
249 print "getting back..."
250 print "stepHistory:", self.stepHistory
251 if len(self.stepHistory) > 1:
252 self.currStep = self.stepHistory[-2]
253 self.stepHistory = self.stepHistory[:-2]
254 if self.currStep < 1:
256 print "currStep:", self.currStep
257 print "new stepHistory:", self.stepHistory
259 print "after updateValues stepHistory:", self.stepHistory
264 def getStepWithID(self, id):
265 print "getStepWithID:", id
267 for x in self.wizard.keys():
268 if self.wizard[x]["id"] == id:
269 print "result:", count
272 print "result: nothing"
275 def finished(self, gotoStep = None, *args, **kwargs):
277 currStep = self.currStep
279 if self.updateValues not in self.onShown:
280 self.onShown.append(self.updateValues)
283 if self.wizard[currStep]["config"]["type"] == "dynamic":
284 eval("self." + self.wizard[currStep]["config"]["evaluation"])()
287 if (len(self.wizard[currStep]["evaluatedlist"]) > 0):
288 print "current:", self["list"].current
289 nextStep = self["list"].current[1]
290 if (self.wizard[currStep].has_key("listevaluation")):
291 exec("self." + self.wizard[self.currStep]["listevaluation"] + "('" + nextStep + "')")
293 self.currStep = self.getStepWithID(nextStep)
295 if ((currStep == self.numSteps and self.wizard[currStep]["nextstep"] is None) or self.wizard[currStep]["id"] == "end"): # wizard finished
296 print "wizard finished"
300 self.runCode(self.wizard[currStep]["codeafter"])
301 if self.wizard[currStep]["nextstep"] is not None:
302 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
303 if gotoStep is not None:
304 self.currStep = self.getStepWithID(gotoStep)
308 print "Now: " + str(self.currStep)
315 currStep = self.currStep
318 if (self.wizard[currStep]["config"]["screen"] != None):
319 # TODO: don't die, if no run() is available
320 # there was a try/except here, but i can't see a reason
321 # for this. If there is one, please do a more specific check
322 # and/or a comment in which situation there is no run()
323 if callable(getattr(self.configInstance, "runAsync", None)):
324 self.onShown.remove(self.updateValues)
325 self.configInstance.runAsync(self.finished)
328 self.configInstance.run()
331 def keyNumberGlobal(self, number):
332 if (self.wizard[self.currStep]["config"]["screen"] != None):
333 self.configInstance.keyNumberGlobal(number)
337 if (self.wizard[self.currStep]["config"]["screen"] != None):
338 self.configInstance.keyLeft()
339 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
340 self["config"].handleKey(KEY_LEFT)
345 if (self.wizard[self.currStep]["config"]["screen"] != None):
346 self.configInstance.keyRight()
347 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
348 self["config"].handleKey(KEY_RIGHT)
353 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
354 self["config"].instance.moveSelection(self["config"].instance.moveUp)
355 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
356 self["list"].selectPrevious()
357 if self.wizard[self.currStep].has_key("onselect"):
358 print "current:", self["list"].current
359 self.selection = self["list"].current[-1]
360 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
361 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
366 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
367 self["config"].instance.moveSelection(self["config"].instance.moveDown)
368 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
369 #self["list"].instance.moveSelection(self["list"].instance.moveDown)
370 self["list"].selectNext()
371 if self.wizard[self.currStep].has_key("onselect"):
372 print "current:", self["list"].current
373 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
374 #exec("self." + self.wizard[self.currStep]["onselect"] + "()")
375 self.selection = self["list"].current[-1]
376 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
377 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
380 def selChanged(self):
383 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
384 self["config"].instance.moveSelection(self["config"].instance.moveUp)
385 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
386 if self.wizard[self.currStep].has_key("onselect"):
387 self.selection = self["list"].current[-1]
388 print "self.selection:", self.selection
389 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
391 def resetCounter(self):
392 self.timeoutCounter = self.wizard[self.currStep]["timeout"]
394 def runCode(self, code):
399 def updateText(self, firstset = False):
400 text = _(self.wizard[self.currStep]["text"])
401 if text.find("[timeout]") != -1:
402 text = text.replace("[timeout]", str(self.timeoutCounter))
403 self["text"].setText(text)
406 self["text"].setText(text)
408 def updateValues(self):
409 print "Updating values in step " + str(self.currStep)
410 # calling a step which doesn't exist can only happen if the condition in the last step is not fulfilled
411 # if a non-existing step is called, end the wizard
412 if self.currStep > len(self.wizard):
417 self.timeoutTimer.stop()
419 if self.configInstance is not None:
420 del self.configInstance["config"]
421 self.configInstance.doClose()
422 self.configInstance = None
424 self.condition = True
425 exec (self.wizard[self.currStep]["condition"])
427 if len(self.stepHistory) == 0 or self.stepHistory[-1] != self.currStep:
428 self.stepHistory.append(self.currStep)
429 print "wizard step:", self.wizard[self.currStep]
432 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
433 if self.showStepSlider:
434 self["stepslider"].setValue(self.currStep)
436 if self.wizard[self.currStep]["timeout"] is not None:
438 self.timeoutTimer.start(1000)
440 print "wizard text", _(self.wizard[self.currStep]["text"])
441 self.updateText(firstset = True)
442 if self.wizard[self.currStep].has_key("displaytext"):
443 displaytext = self.wizard[self.currStep]["displaytext"]
445 for x in self.lcdCallbacks:
448 self.runCode(self.wizard[self.currStep]["code"])
451 print "showing list,", self.currStep
452 for renderer in self.renderer:
453 rootrenderer = renderer
454 while renderer.source is not None:
455 print "self.list:", self["list"]
456 if renderer.source is self["list"]:
458 rootrenderer.instance.setZPosition(1)
459 renderer = renderer.source
461 #self["list"].instance.setZPosition(1)
463 if (self.wizard[self.currStep].has_key("dynamiclist")):
464 print "dynamic list, calling", self.wizard[self.currStep]["dynamiclist"]
465 newlist = eval("self." + self.wizard[self.currStep]["dynamiclist"] + "()")
466 #self.wizard[self.currStep]["evaluatedlist"] = []
467 for entry in newlist:
468 #self.wizard[self.currStep]["evaluatedlist"].append(entry)
469 self.list.append(entry)
470 #del self.wizard[self.currStep]["dynamiclist"]
471 if (len(self.wizard[self.currStep]["list"]) > 0):
472 #self["list"].instance.setZPosition(2)
473 for x in self.wizard[self.currStep]["list"]:
474 self.list.append((_(x[0]), x[1]))
475 self.wizard[self.currStep]["evaluatedlist"] = self.list
476 self["list"].list = self.list
477 self["list"].index = 0
482 print "showing config"
483 self["config"].instance.setZPosition(1)
484 if self.wizard[self.currStep]["config"]["type"] == "dynamic":
485 print "config type is dynamic"
486 self["config"].instance.setZPosition(2)
487 self["config"].l.setList(eval("self." + self.wizard[self.currStep]["config"]["source"])())
488 elif (self.wizard[self.currStep]["config"]["screen"] != None):
489 if self.wizard[self.currStep]["config"]["type"] == "standalone":
490 print "Type is standalone"
491 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
493 self["config"].instance.setZPosition(2)
494 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
495 if self.wizard[self.currStep]["config"]["args"] == None:
496 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
498 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
499 self["config"].l.setList(self.configInstance["config"].list)
500 self.configInstance["config"].destroy()
501 self.configInstance["config"] = self["config"]
503 self["config"].l.setList([])
505 if self.has_key("config"):
506 self["config"].hide()
507 else: # condition false
511 def timeoutCounterFired(self):
512 self.timeoutCounter -= 1
513 print "timeoutCounter:", self.timeoutCounter
514 if self.timeoutCounter == 0:
515 if self.wizard[self.currStep]["timeoutaction"] == "selectnext":
516 print "selection next item"
519 if self.wizard[self.currStep]["timeoutaction"] == "changestep":
520 self.finished(gotoStep = self.wizard[self.currStep]["timeoutstep"])
527 def registerWizard(self, wizard, precondition, priority = 0):
528 self.wizards.append((wizard, precondition, priority))
530 def getWizards(self):
532 for x in self.wizards:
533 if x[1] == 1: # precondition
534 list.append((x[2], x[0]))
537 wizardManager = WizardManager()