1 from Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.config import config, KEY_LEFT, KEY_RIGHT, KEY_DELETE, KEY_BACKSPACE
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)
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": "", "code_async": "", "codeafter_async": "", "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":
109 exec "from Screens." + str(attrs.get('module')) + " import *"
111 exec "from " + str(attrs.get('module')) + " import *"
113 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
114 if (attrs.has_key('args')):
116 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
117 elif type == "dynamic":
118 self.wizard[self.lastStep]["config"]["source"] = str(attrs.get('source'))
119 if (attrs.has_key('evaluation')):
120 self.wizard[self.lastStep]["config"]["evaluation"] = str(attrs.get('evaluation'))
121 elif (name == "code"):
122 self.async_code = attrs.has_key('async') and str(attrs.get('async')) == "yes"
123 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
124 self.codeafter = True
126 self.codeafter = False
127 elif (name == "condition"):
130 def endElement(self, name):
131 self.currContent = ""
135 self.wizard[self.lastStep]["codeafter_async"] = self.wizard[self.lastStep]["codeafter_async"].strip()
137 self.wizard[self.lastStep]["code_async"] = self.wizard[self.lastStep]["code_async"].strip()
140 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
142 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
143 elif name == 'condition':
144 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
146 #print "Step number", self.lastStep, ":", self.wizard[self.lastStep]
149 def characters(self, ch):
150 if self.currContent == "code":
153 self.wizard[self.lastStep]["codeafter_async"] = self.wizard[self.lastStep]["codeafter_async"] + ch
155 self.wizard[self.lastStep]["code_async"] = self.wizard[self.lastStep]["code_async"] + ch
158 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
160 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
161 elif self.currContent == "condition":
162 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
164 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
165 Screen.__init__(self, session)
167 self.stepHistory = []
170 parser = make_parser()
171 if not isinstance(self.xmlfile, list):
172 self.xmlfile = [self.xmlfile]
173 print "Reading ", self.xmlfile
174 wizardHandler = self.parseWizard(self.wizard)
175 parser.setContentHandler(wizardHandler)
176 for xmlfile in self.xmlfile:
177 if xmlfile[0] != '/':
178 parser.parse('/usr/share/enigma2/' + xmlfile)
180 parser.parse(xmlfile)
182 self.showSteps = showSteps
183 self.showStepSlider = showStepSlider
184 self.showList = showList
185 self.showConfig = showConfig
187 self.numSteps = len(self.wizard)
188 self.currStep = self.getStepWithID("start") + 1
190 self.timeoutTimer = eTimer()
191 self.timeoutTimer.callback.append(self.timeoutCounterFired)
193 self["text"] = Label()
196 self["config"] = ConfigList([], session = session)
199 self["step"] = Label()
201 if self.showStepSlider:
202 self["stepslider"] = Slider(1, self.numSteps)
206 self["list"] = List(self.list, enableWrapAround = True)
207 self["list"].onSelectionChanged.append(self.selChanged)
208 #self["list"] = MenuList(self.list, enableWrapAround = True)
210 self.onShown.append(self.updateValues)
212 self.configInstance = None
214 self.lcdCallbacks = []
216 self.disableKeys = False
218 self["actions"] = NumberActionMap(["WizardActions", "NumberActions", "ColorActions", "SetupActions"],
228 "yellow": self.yellow,
230 "deleteBackward": self.deleteBackward,
231 "deleteForward": self.deleteForward,
232 "1": self.keyNumberGlobal,
233 "2": self.keyNumberGlobal,
234 "3": self.keyNumberGlobal,
235 "4": self.keyNumberGlobal,
236 "5": self.keyNumberGlobal,
237 "6": self.keyNumberGlobal,
238 "7": self.keyNumberGlobal,
239 "8": self.keyNumberGlobal,
240 "9": self.keyNumberGlobal,
241 "0": self.keyNumberGlobal
260 def deleteForward(self):
262 if (self.wizard[self.currStep]["config"]["screen"] != None):
263 self.configInstance.keyDelete()
264 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
265 self["config"].handleKey(KEY_DELETE)
266 print "deleteForward"
268 def deleteBackward(self):
270 if (self.wizard[self.currStep]["config"]["screen"] != None):
271 self.configInstance.keyBackspace()
272 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
273 self["config"].handleKey(KEY_BACKSPACE)
274 print "deleteBackward"
276 def setLCDTextCallback(self, callback):
277 self.lcdCallbacks.append(callback)
282 print "getting back..."
283 print "stepHistory:", self.stepHistory
284 if len(self.stepHistory) > 1:
285 self.currStep = self.stepHistory[-2]
286 self.stepHistory = self.stepHistory[:-2]
287 if self.currStep < 1:
289 print "currStep:", self.currStep
290 print "new stepHistory:", self.stepHistory
292 print "after updateValues stepHistory:", self.stepHistory
297 def getStepWithID(self, id):
298 print "getStepWithID:", id
300 for x in self.wizard.keys():
301 if self.wizard[x]["id"] == id:
302 print "result:", count
305 print "result: nothing"
308 def finished(self, gotoStep = None, *args, **kwargs):
310 currStep = self.currStep
312 if self.updateValues not in self.onShown:
313 self.onShown.append(self.updateValues)
316 if self.wizard[currStep]["config"]["type"] == "dynamic":
317 eval("self." + self.wizard[currStep]["config"]["evaluation"])()
320 if (len(self.wizard[currStep]["evaluatedlist"]) > 0):
321 print "current:", self["list"].current
322 nextStep = self["list"].current[1]
323 if (self.wizard[currStep].has_key("listevaluation")):
324 exec("self." + self.wizard[self.currStep]["listevaluation"] + "('" + nextStep + "')")
326 self.currStep = self.getStepWithID(nextStep)
329 if ((currStep == self.numSteps and self.wizard[currStep]["nextstep"] is None) or self.wizard[currStep]["id"] == "end"): # wizard finished
330 print "wizard finished"
334 self.codeafter = True
335 self.runCode(self.wizard[currStep]["codeafter"])
336 self.prevStep = currStep
337 self.gotoStep = gotoStep
338 if not self.runCode(self.wizard[currStep]["codeafter_async"]):
339 self.afterAsyncCode()
341 if self.updateValues in self.onShown:
342 self.onShown.remove(self.updateValues)
345 print "Now: " + str(self.currStep)
351 currStep = self.currStep
354 if (self.wizard[currStep]["config"]["screen"] != None):
355 # TODO: don't die, if no run() is available
356 # there was a try/except here, but i can't see a reason
357 # for this. If there is one, please do a more specific check
358 # and/or a comment in which situation there is no run()
359 if callable(getattr(self.configInstance, "runAsync", None)):
360 if self.updateValues in self.onShown:
361 self.onShown.remove(self.updateValues)
362 self.configInstance.runAsync(self.finished)
365 self.configInstance.run()
368 def keyNumberGlobal(self, number):
369 if (self.wizard[self.currStep]["config"]["screen"] != None):
370 self.configInstance.keyNumberGlobal(number)
374 if (self.wizard[self.currStep]["config"]["screen"] != None):
375 self.configInstance.keyLeft()
376 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
377 self["config"].handleKey(KEY_LEFT)
382 if (self.wizard[self.currStep]["config"]["screen"] != None):
383 self.configInstance.keyRight()
384 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
385 self["config"].handleKey(KEY_RIGHT)
390 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
391 self["config"].instance.moveSelection(self["config"].instance.moveUp)
392 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
393 self["list"].selectPrevious()
394 if self.wizard[self.currStep].has_key("onselect"):
395 print "current:", self["list"].current
396 self.selection = self["list"].current[-1]
397 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
398 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
403 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
404 self["config"].instance.moveSelection(self["config"].instance.moveDown)
405 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
406 #self["list"].instance.moveSelection(self["list"].instance.moveDown)
407 self["list"].selectNext()
408 if self.wizard[self.currStep].has_key("onselect"):
409 print "current:", self["list"].current
410 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
411 #exec("self." + self.wizard[self.currStep]["onselect"] + "()")
412 self.selection = self["list"].current[-1]
413 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
414 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
417 def selChanged(self):
420 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
421 self["config"].instance.moveSelection(self["config"].instance.moveUp)
422 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
423 if self.wizard[self.currStep].has_key("onselect"):
424 self.selection = self["list"].current[-1]
425 print "self.selection:", self.selection
426 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
428 def resetCounter(self):
429 self.timeoutCounter = self.wizard[self.currStep]["timeout"]
431 def runCode(self, code):
438 def getTranslation(self, text):
441 def updateText(self, firstset = False):
442 text = self.getTranslation(self.wizard[self.currStep]["text"])
443 if text.find("[timeout]") != -1:
444 text = text.replace("[timeout]", str(self.timeoutCounter))
445 self["text"].setText(text)
448 self["text"].setText(text)
450 def updateValues(self):
451 print "Updating values in step " + str(self.currStep)
452 # calling a step which doesn't exist can only happen if the condition in the last step is not fulfilled
453 # if a non-existing step is called, end the wizard
454 if self.currStep > len(self.wizard):
459 self.timeoutTimer.stop()
461 if self.configInstance is not None:
463 self.configInstance["config"].onSelectionChanged = []
464 del self.configInstance["config"]
465 self.configInstance.doClose()
466 self.configInstance = None
468 self.condition = True
469 exec (self.wizard[self.currStep]["condition"])
470 if not self.condition:
474 if len(self.stepHistory) == 0 or self.stepHistory[-1] != self.currStep:
475 self.stepHistory.append(self.currStep)
476 print "wizard step:", self.wizard[self.currStep]
479 self["step"].setText(self.getTranslation("Step ") + str(self.currStep) + "/" + str(self.numSteps))
480 if self.showStepSlider:
481 self["stepslider"].setValue(self.currStep)
483 if self.wizard[self.currStep]["timeout"] is not None:
485 self.timeoutTimer.start(1000)
487 print "wizard text", self.getTranslation(self.wizard[self.currStep]["text"])
488 self.updateText(firstset = True)
489 if self.wizard[self.currStep].has_key("displaytext"):
490 displaytext = self.wizard[self.currStep]["displaytext"]
492 for x in self.lcdCallbacks:
496 self.runCode(self.wizard[self.currStep]["code"])
497 if self.runCode(self.wizard[self.currStep]["code_async"]):
498 if self.updateValues in self.onShown:
499 self.onShown.remove(self.updateValues)
501 self.afterAsyncCode()
503 def afterAsyncCode(self):
504 if not self.updateValues in self.onShown:
505 self.onShown.append(self.updateValues)
508 if self.wizard[self.prevStep]["nextstep"] is not None:
509 self.currStep = self.getStepWithID(self.wizard[self.prevStep]["nextstep"])
510 if self.gotoStep is not None:
511 self.currStep = self.getStepWithID(self.gotoStep)
514 print "Now: " + str(self.currStep)
517 print "showing list,", self.currStep
518 for renderer in self.renderer:
519 rootrenderer = renderer
520 while renderer.source is not None:
521 print "self.list:", self["list"]
522 if renderer.source is self["list"]:
524 rootrenderer.instance.setZPosition(1)
525 renderer = renderer.source
527 #self["list"].instance.setZPosition(1)
529 if (self.wizard[self.currStep].has_key("dynamiclist")):
530 print "dynamic list, calling", self.wizard[self.currStep]["dynamiclist"]
531 newlist = eval("self." + self.wizard[self.currStep]["dynamiclist"] + "()")
532 #self.wizard[self.currStep]["evaluatedlist"] = []
533 for entry in newlist:
534 #self.wizard[self.currStep]["evaluatedlist"].append(entry)
535 self.list.append(entry)
536 #del self.wizard[self.currStep]["dynamiclist"]
537 if (len(self.wizard[self.currStep]["list"]) > 0):
538 #self["list"].instance.setZPosition(2)
539 for x in self.wizard[self.currStep]["list"]:
540 self.list.append((self.getTranslation(x[0]), x[1]))
541 self.wizard[self.currStep]["evaluatedlist"] = self.list
542 self["list"].list = self.list
543 self["list"].index = 0
548 print "showing config"
549 self["config"].instance.setZPosition(1)
550 if self.wizard[self.currStep]["config"]["type"] == "dynamic":
551 print "config type is dynamic"
552 self["config"].instance.setZPosition(2)
553 self["config"].l.setList(eval("self." + self.wizard[self.currStep]["config"]["source"])())
554 elif (self.wizard[self.currStep]["config"]["screen"] != None):
555 if self.wizard[self.currStep]["config"]["type"] == "standalone":
556 print "Type is standalone"
557 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
559 self["config"].instance.setZPosition(2)
560 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
561 if self.wizard[self.currStep]["config"]["args"] == None:
562 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
564 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
565 self["config"].l.setList(self.configInstance["config"].list)
566 callbacks = self.configInstance["config"].onSelectionChanged
567 self.configInstance["config"].destroy()
568 print "clearConfigList", self.configInstance["config"], self["config"]
569 self.configInstance["config"] = self["config"]
570 self.configInstance["config"].onSelectionChanged = callbacks
571 print "clearConfigList", self.configInstance["config"], self["config"]
573 self["config"].l.setList([])
575 if self.has_key("config"):
576 self["config"].hide()
578 def timeoutCounterFired(self):
579 self.timeoutCounter -= 1
580 print "timeoutCounter:", self.timeoutCounter
581 if self.timeoutCounter == 0:
582 if self.wizard[self.currStep]["timeoutaction"] == "selectnext":
583 print "selection next item"
586 if self.wizard[self.currStep]["timeoutaction"] == "changestep":
587 self.finished(gotoStep = self.wizard[self.currStep]["timeoutstep"])
594 def registerWizard(self, wizard, precondition, priority = 0):
595 self.wizards.append((wizard, precondition, priority))
597 def getWizards(self):
599 for x in self.wizards:
600 if x[1] == 1: # precondition
601 list.append((x[2], x[0]))
604 wizardManager = WizardManager()