1 from Screen import Screen
3 from Screens.HelpMenu import HelpableScreen
4 from Screens.MessageBox import MessageBox
5 from Components.config import config, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_0, KEY_DELETE, KEY_BACKSPACE, KEY_OK, KEY_TOGGLEOW, KEY_ASCII, KEY_TIMEOUT, KEY_NUMBERS
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"] = str(attrs.get('value')).replace("\\n", "\n")
90 elif (name == "displaytext"):
91 self.wizard[self.lastStep]["displaytext"] = str(attrs.get('value')).replace("\\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", "InputAsciiActions"],
220 "gotAsciiCode": self.keyGotAscii,
229 "yellow": self.yellow,
231 "deleteBackward": self.deleteBackward,
232 "deleteForward": self.deleteForward,
233 "1": self.keyNumberGlobal,
234 "2": self.keyNumberGlobal,
235 "3": self.keyNumberGlobal,
236 "4": self.keyNumberGlobal,
237 "5": self.keyNumberGlobal,
238 "6": self.keyNumberGlobal,
239 "7": self.keyNumberGlobal,
240 "8": self.keyNumberGlobal,
241 "9": self.keyNumberGlobal,
242 "0": self.keyNumberGlobal
261 def deleteForward(self):
263 if (self.wizard[self.currStep]["config"]["screen"] != None):
264 self.configInstance.keyDelete()
265 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
266 self["config"].handleKey(KEY_DELETE)
267 print "deleteForward"
269 def deleteBackward(self):
271 if (self.wizard[self.currStep]["config"]["screen"] != None):
272 self.configInstance.keyBackspace()
273 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
274 self["config"].handleKey(KEY_BACKSPACE)
275 print "deleteBackward"
277 def setLCDTextCallback(self, callback):
278 self.lcdCallbacks.append(callback)
283 print "getting back..."
284 print "stepHistory:", self.stepHistory
285 if len(self.stepHistory) > 1:
286 self.currStep = self.stepHistory[-2]
287 self.stepHistory = self.stepHistory[:-2]
289 self.session.openWithCallback(self.exitWizardQuestion, MessageBox, (_("Are you sure you want to exit this wizard?") ) )
290 if self.currStep < 1:
292 print "currStep:", self.currStep
293 print "new stepHistory:", self.stepHistory
295 print "after updateValues stepHistory:", self.stepHistory
297 def exitWizardQuestion(self, ret = False):
305 def getStepWithID(self, id):
306 print "getStepWithID:", id
308 for x in self.wizard.keys():
309 if self.wizard[x]["id"] == id:
310 print "result:", count
313 print "result: nothing"
316 def finished(self, gotoStep = None, *args, **kwargs):
318 currStep = self.currStep
320 if self.updateValues not in self.onShown:
321 self.onShown.append(self.updateValues)
324 if self.wizard[currStep]["config"]["type"] == "dynamic":
325 eval("self." + self.wizard[currStep]["config"]["evaluation"])()
328 if (len(self.wizard[currStep]["evaluatedlist"]) > 0):
329 print "current:", self["list"].current
330 nextStep = self["list"].current[1]
331 if (self.wizard[currStep].has_key("listevaluation")):
332 exec("self." + self.wizard[self.currStep]["listevaluation"] + "('" + nextStep + "')")
334 self.currStep = self.getStepWithID(nextStep)
337 if ((currStep == self.numSteps and self.wizard[currStep]["nextstep"] is None) or self.wizard[currStep]["id"] == "end"): # wizard finished
338 print "wizard finished"
342 self.codeafter = True
343 self.runCode(self.wizard[currStep]["codeafter"])
344 self.prevStep = currStep
345 self.gotoStep = gotoStep
346 if not self.runCode(self.wizard[currStep]["codeafter_async"]):
347 self.afterAsyncCode()
349 if self.updateValues in self.onShown:
350 self.onShown.remove(self.updateValues)
353 print "Now: " + str(self.currStep)
359 currStep = self.currStep
362 if (self.wizard[currStep]["config"]["screen"] != None):
363 # TODO: don't die, if no run() is available
364 # there was a try/except here, but i can't see a reason
365 # for this. If there is one, please do a more specific check
366 # and/or a comment in which situation there is no run()
367 if callable(getattr(self.configInstance, "runAsync", None)):
368 if self.updateValues in self.onShown:
369 self.onShown.remove(self.updateValues)
370 self.configInstance.runAsync(self.finished)
373 self.configInstance.run()
376 def keyNumberGlobal(self, number):
377 if (self.wizard[self.currStep]["config"]["screen"] != None):
378 self.configInstance.keyNumberGlobal(number)
380 def keyGotAscii(self):
381 if (self.wizard[self.currStep]["config"]["screen"] != None):
382 self["config"].handleKey(KEY_ASCII)
386 if (self.wizard[self.currStep]["config"]["screen"] != None):
387 self.configInstance.keyLeft()
388 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
389 self["config"].handleKey(KEY_LEFT)
394 if (self.wizard[self.currStep]["config"]["screen"] != None):
395 self.configInstance.keyRight()
396 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
397 self["config"].handleKey(KEY_RIGHT)
402 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
403 self["config"].instance.moveSelection(self["config"].instance.moveUp)
404 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
405 self["list"].selectPrevious()
406 if self.wizard[self.currStep].has_key("onselect"):
407 print "current:", self["list"].current
408 self.selection = self["list"].current[-1]
409 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
410 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
415 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
416 self["config"].instance.moveSelection(self["config"].instance.moveDown)
417 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
418 #self["list"].instance.moveSelection(self["list"].instance.moveDown)
419 self["list"].selectNext()
420 if self.wizard[self.currStep].has_key("onselect"):
421 print "current:", self["list"].current
422 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
423 #exec("self." + self.wizard[self.currStep]["onselect"] + "()")
424 self.selection = self["list"].current[-1]
425 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
426 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
429 def selChanged(self):
432 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
433 self["config"].instance.moveSelection(self["config"].instance.moveUp)
434 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
435 if self.wizard[self.currStep].has_key("onselect"):
436 self.selection = self["list"].current[-1]
437 print "self.selection:", self.selection
438 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
440 def resetCounter(self):
441 self.timeoutCounter = self.wizard[self.currStep]["timeout"]
443 def runCode(self, code):
450 def getTranslation(self, text):
453 def updateText(self, firstset = False):
454 text = self.getTranslation(self.wizard[self.currStep]["text"])
455 if text.find("[timeout]") != -1:
456 text = text.replace("[timeout]", str(self.timeoutCounter))
457 self["text"].setText(text)
460 self["text"].setText(text)
462 def updateValues(self):
463 print "Updating values in step " + str(self.currStep)
464 # calling a step which doesn't exist can only happen if the condition in the last step is not fulfilled
465 # if a non-existing step is called, end the wizard
466 if self.currStep > len(self.wizard):
471 self.timeoutTimer.stop()
473 if self.configInstance is not None:
475 self.configInstance["config"].onSelectionChanged = []
476 del self.configInstance["config"]
477 self.configInstance.doClose()
478 self.configInstance = None
480 self.condition = True
481 exec (self.wizard[self.currStep]["condition"])
482 if not self.condition:
486 if self.wizard[self.currStep].has_key("displaytext"):
487 displaytext = self.wizard[self.currStep]["displaytext"]
489 for x in self.lcdCallbacks:
491 if len(self.stepHistory) == 0 or self.stepHistory[-1] != self.currStep:
492 self.stepHistory.append(self.currStep)
493 print "wizard step:", self.wizard[self.currStep]
496 self["step"].setText(self.getTranslation("Step ") + str(self.currStep) + "/" + str(self.numSteps))
497 if self.showStepSlider:
498 self["stepslider"].setValue(self.currStep)
500 if self.wizard[self.currStep]["timeout"] is not None:
502 self.timeoutTimer.start(1000)
504 print "wizard text", self.getTranslation(self.wizard[self.currStep]["text"])
505 self.updateText(firstset = True)
506 if self.wizard[self.currStep].has_key("displaytext"):
507 displaytext = self.wizard[self.currStep]["displaytext"]
509 for x in self.lcdCallbacks:
513 self.runCode(self.wizard[self.currStep]["code"])
514 if self.runCode(self.wizard[self.currStep]["code_async"]):
515 if self.updateValues in self.onShown:
516 self.onShown.remove(self.updateValues)
518 self.afterAsyncCode()
520 def afterAsyncCode(self):
521 if not self.updateValues in self.onShown:
522 self.onShown.append(self.updateValues)
525 if self.wizard[self.prevStep]["nextstep"] is not None:
526 self.currStep = self.getStepWithID(self.wizard[self.prevStep]["nextstep"])
527 if self.gotoStep is not None:
528 self.currStep = self.getStepWithID(self.gotoStep)
531 print "Now: " + str(self.currStep)
534 print "showing list,", self.currStep
535 for renderer in self.renderer:
536 rootrenderer = renderer
537 while renderer.source is not None:
538 print "self.list:", self["list"]
539 if renderer.source is self["list"]:
541 rootrenderer.instance.setZPosition(1)
542 renderer = renderer.source
544 #self["list"].instance.setZPosition(1)
546 if (self.wizard[self.currStep].has_key("dynamiclist")):
547 print "dynamic list, calling", self.wizard[self.currStep]["dynamiclist"]
548 newlist = eval("self." + self.wizard[self.currStep]["dynamiclist"] + "()")
549 #self.wizard[self.currStep]["evaluatedlist"] = []
550 for entry in newlist:
551 #self.wizard[self.currStep]["evaluatedlist"].append(entry)
552 self.list.append(entry)
553 #del self.wizard[self.currStep]["dynamiclist"]
554 if (len(self.wizard[self.currStep]["list"]) > 0):
555 #self["list"].instance.setZPosition(2)
556 for x in self.wizard[self.currStep]["list"]:
557 self.list.append((self.getTranslation(x[0]), x[1]))
558 self.wizard[self.currStep]["evaluatedlist"] = self.list
559 self["list"].list = self.list
560 self["list"].index = 0
565 print "showing config"
566 self["config"].instance.setZPosition(1)
567 if self.wizard[self.currStep]["config"]["type"] == "dynamic":
568 print "config type is dynamic"
569 self["config"].instance.setZPosition(2)
570 self["config"].l.setList(eval("self." + self.wizard[self.currStep]["config"]["source"])())
571 elif (self.wizard[self.currStep]["config"]["screen"] != None):
572 if self.wizard[self.currStep]["config"]["type"] == "standalone":
573 print "Type is standalone"
574 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
576 self["config"].instance.setZPosition(2)
577 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
578 if self.wizard[self.currStep]["config"]["args"] == None:
579 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
581 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
582 self["config"].l.setList(self.configInstance["config"].list)
583 callbacks = self.configInstance["config"].onSelectionChanged
584 self.configInstance["config"].destroy()
585 print "clearConfigList", self.configInstance["config"], self["config"]
586 self.configInstance["config"] = self["config"]
587 self.configInstance["config"].onSelectionChanged = callbacks
588 print "clearConfigList", self.configInstance["config"], self["config"]
590 self["config"].l.setList([])
592 if self.has_key("config"):
593 self["config"].hide()
595 def timeoutCounterFired(self):
596 self.timeoutCounter -= 1
597 print "timeoutCounter:", self.timeoutCounter
598 if self.timeoutCounter == 0:
599 if self.wizard[self.currStep]["timeoutaction"] == "selectnext":
600 print "selection next item"
603 if self.wizard[self.currStep]["timeoutaction"] == "changestep":
604 self.finished(gotoStep = self.wizard[self.currStep]["timeoutstep"])
611 def registerWizard(self, wizard, precondition, priority = 0):
612 self.wizards.append((wizard, precondition, priority))
614 def getWizards(self):
615 # x[1] is precondition
616 return [(x[2], x[0]) for x in self.wizards if x[1] == 1]
618 wizardManager = WizardManager()