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 if attrs.has_key('laststep'):
89 self.wizard[self.lastStep]["laststep"] = str(attrs.get('laststep'))
90 elif (name == "text"):
91 self.wizard[self.lastStep]["text"] = str(attrs.get('value')).replace("\\n", "\n")
92 elif (name == "displaytext"):
93 self.wizard[self.lastStep]["displaytext"] = str(attrs.get('value')).replace("\\n", "\n")
94 elif (name == "list"):
95 if (attrs.has_key('type')):
96 if attrs["type"] == "dynamic":
97 self.wizard[self.lastStep]["dynamiclist"] = attrs.get("source")
98 #self.wizard[self.lastStep]["list"].append(("Hallo", "test"))
99 if (attrs.has_key("evaluation")):
101 self.wizard[self.lastStep]["listevaluation"] = attrs.get("evaluation")
102 if (attrs.has_key("onselect")):
103 self.wizard[self.lastStep]["onselect"] = attrs.get("onselect")
104 elif (name == "listentry"):
105 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
106 elif (name == "config"):
107 type = str(attrs.get('type'))
108 self.wizard[self.lastStep]["config"]["type"] = type
109 if type == "ConfigList" or type == "standalone":
111 exec "from Screens." + str(attrs.get('module')) + " import *"
113 exec "from " + str(attrs.get('module')) + " import *"
115 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
116 if (attrs.has_key('args')):
118 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
119 elif type == "dynamic":
120 self.wizard[self.lastStep]["config"]["source"] = str(attrs.get('source'))
121 if (attrs.has_key('evaluation')):
122 self.wizard[self.lastStep]["config"]["evaluation"] = str(attrs.get('evaluation'))
123 elif (name == "code"):
124 self.async_code = attrs.has_key('async') and str(attrs.get('async')) == "yes"
125 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
126 self.codeafter = True
128 self.codeafter = False
129 elif (name == "condition"):
132 def endElement(self, name):
133 self.currContent = ""
137 self.wizard[self.lastStep]["codeafter_async"] = self.wizard[self.lastStep]["codeafter_async"].strip()
139 self.wizard[self.lastStep]["code_async"] = self.wizard[self.lastStep]["code_async"].strip()
142 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
144 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
145 elif name == 'condition':
146 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
148 #print "Step number", self.lastStep, ":", self.wizard[self.lastStep]
151 def characters(self, ch):
152 if self.currContent == "code":
155 self.wizard[self.lastStep]["codeafter_async"] = self.wizard[self.lastStep]["codeafter_async"] + ch
157 self.wizard[self.lastStep]["code_async"] = self.wizard[self.lastStep]["code_async"] + ch
160 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
162 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
163 elif self.currContent == "condition":
164 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
166 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
167 Screen.__init__(self, session)
169 self.isLastWizard = False # can be used to skip a "goodbye"-screen in a wizard
171 self.stepHistory = []
174 parser = make_parser()
175 if not isinstance(self.xmlfile, list):
176 self.xmlfile = [self.xmlfile]
177 print "Reading ", self.xmlfile
178 wizardHandler = self.parseWizard(self.wizard)
179 parser.setContentHandler(wizardHandler)
180 for xmlfile in self.xmlfile:
181 if xmlfile[0] != '/':
182 parser.parse('/usr/share/enigma2/' + xmlfile)
184 parser.parse(xmlfile)
186 self.showSteps = showSteps
187 self.showStepSlider = showStepSlider
188 self.showList = showList
189 self.showConfig = showConfig
191 self.numSteps = len(self.wizard)
192 self.currStep = self.getStepWithID("start") + 1
194 self.timeoutTimer = eTimer()
195 self.timeoutTimer.callback.append(self.timeoutCounterFired)
197 self["text"] = Label()
200 self["config"] = ConfigList([], session = session)
203 self["step"] = Label()
205 if self.showStepSlider:
206 self["stepslider"] = Slider(1, self.numSteps)
210 self["list"] = List(self.list, enableWrapAround = True)
211 self["list"].onSelectionChanged.append(self.selChanged)
212 #self["list"] = MenuList(self.list, enableWrapAround = True)
214 self.onShown.append(self.updateValues)
216 self.configInstance = None
218 self.lcdCallbacks = []
220 self.disableKeys = False
222 self["actions"] = NumberActionMap(["WizardActions", "NumberActions", "ColorActions", "SetupActions", "InputAsciiActions"],
224 "gotAsciiCode": self.keyGotAscii,
233 "yellow": self.yellow,
235 "deleteBackward": self.deleteBackward,
236 "deleteForward": self.deleteForward,
237 "1": self.keyNumberGlobal,
238 "2": self.keyNumberGlobal,
239 "3": self.keyNumberGlobal,
240 "4": self.keyNumberGlobal,
241 "5": self.keyNumberGlobal,
242 "6": self.keyNumberGlobal,
243 "7": self.keyNumberGlobal,
244 "8": self.keyNumberGlobal,
245 "9": self.keyNumberGlobal,
246 "0": self.keyNumberGlobal
265 def deleteForward(self):
267 if (self.wizard[self.currStep]["config"]["screen"] != None):
268 self.configInstance.keyDelete()
269 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
270 self["config"].handleKey(KEY_DELETE)
271 print "deleteForward"
273 def deleteBackward(self):
275 if (self.wizard[self.currStep]["config"]["screen"] != None):
276 self.configInstance.keyBackspace()
277 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
278 self["config"].handleKey(KEY_BACKSPACE)
279 print "deleteBackward"
281 def setLCDTextCallback(self, callback):
282 self.lcdCallbacks.append(callback)
287 print "getting back..."
288 print "stepHistory:", self.stepHistory
289 if len(self.stepHistory) > 1:
290 self.currStep = self.stepHistory[-2]
291 self.stepHistory = self.stepHistory[:-2]
293 self.session.openWithCallback(self.exitWizardQuestion, MessageBox, (_("Are you sure you want to exit this wizard?") ) )
294 if self.currStep < 1:
296 print "currStep:", self.currStep
297 print "new stepHistory:", self.stepHistory
299 print "after updateValues stepHistory:", self.stepHistory
301 def exitWizardQuestion(self, ret = False):
309 def getStepWithID(self, id):
310 print "getStepWithID:", id
312 for x in self.wizard.keys():
313 if self.wizard[x]["id"] == id:
314 print "result:", count
317 print "result: nothing"
320 def finished(self, gotoStep = None, *args, **kwargs):
322 currStep = self.currStep
324 if self.updateValues not in self.onShown:
325 self.onShown.append(self.updateValues)
328 if self.wizard[currStep]["config"]["type"] == "dynamic":
329 eval("self." + self.wizard[currStep]["config"]["evaluation"])()
332 if (len(self.wizard[currStep]["evaluatedlist"]) > 0):
333 print "current:", self["list"].current
334 nextStep = self["list"].current[1]
335 if (self.wizard[currStep].has_key("listevaluation")):
336 exec("self." + self.wizard[self.currStep]["listevaluation"] + "('" + nextStep + "')")
338 self.currStep = self.getStepWithID(nextStep)
341 if ((currStep == self.numSteps and self.wizard[currStep]["nextstep"] is None) or self.wizard[currStep]["id"] == "end"): # wizard finished
342 print "wizard finished"
346 self.codeafter = True
347 self.runCode(self.wizard[currStep]["codeafter"])
348 self.prevStep = currStep
349 self.gotoStep = gotoStep
350 if not self.runCode(self.wizard[currStep]["codeafter_async"]):
351 self.afterAsyncCode()
353 if self.updateValues in self.onShown:
354 self.onShown.remove(self.updateValues)
357 print "Now: " + str(self.currStep)
363 currStep = self.currStep
366 if (self.wizard[currStep]["config"]["screen"] != None):
367 # TODO: don't die, if no run() is available
368 # there was a try/except here, but i can't see a reason
369 # for this. If there is one, please do a more specific check
370 # and/or a comment in which situation there is no run()
371 if callable(getattr(self.configInstance, "runAsync", None)):
372 if self.updateValues in self.onShown:
373 self.onShown.remove(self.updateValues)
374 self.configInstance.runAsync(self.finished)
377 self.configInstance.run()
380 def keyNumberGlobal(self, number):
381 if (self.wizard[self.currStep]["config"]["screen"] != None):
382 self.configInstance.keyNumberGlobal(number)
384 def keyGotAscii(self):
385 if (self.wizard[self.currStep]["config"]["screen"] != None):
386 self["config"].handleKey(KEY_ASCII)
390 if (self.wizard[self.currStep]["config"]["screen"] != None):
391 self.configInstance.keyLeft()
392 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
393 self["config"].handleKey(KEY_LEFT)
398 if (self.wizard[self.currStep]["config"]["screen"] != None):
399 self.configInstance.keyRight()
400 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
401 self["config"].handleKey(KEY_RIGHT)
406 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
407 self["config"].instance.moveSelection(self["config"].instance.moveUp)
408 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
409 self["list"].selectPrevious()
410 if self.wizard[self.currStep].has_key("onselect"):
411 print "current:", self["list"].current
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"] + "()")
419 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
420 self["config"].instance.moveSelection(self["config"].instance.moveDown)
421 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
422 #self["list"].instance.moveSelection(self["list"].instance.moveDown)
423 self["list"].selectNext()
424 if self.wizard[self.currStep].has_key("onselect"):
425 print "current:", self["list"].current
426 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
427 #exec("self." + self.wizard[self.currStep]["onselect"] + "()")
428 self.selection = self["list"].current[-1]
429 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
430 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
433 def selChanged(self):
436 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
437 self["config"].instance.moveSelection(self["config"].instance.moveUp)
438 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
439 if self.wizard[self.currStep].has_key("onselect"):
440 self.selection = self["list"].current[-1]
441 print "self.selection:", self.selection
442 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
444 def resetCounter(self):
445 self.timeoutCounter = self.wizard[self.currStep]["timeout"]
447 def runCode(self, code):
454 def getTranslation(self, text):
457 def updateText(self, firstset = False):
458 text = self.getTranslation(self.wizard[self.currStep]["text"])
459 if text.find("[timeout]") != -1:
460 text = text.replace("[timeout]", str(self.timeoutCounter))
461 self["text"].setText(text)
464 self["text"].setText(text)
466 def updateValues(self):
467 print "Updating values in step " + str(self.currStep)
468 # calling a step which doesn't exist can only happen if the condition in the last step is not fulfilled
469 # if a non-existing step is called, end the wizard
470 if self.currStep > len(self.wizard):
475 self.timeoutTimer.stop()
477 if self.configInstance is not None:
479 self.configInstance["config"].onSelectionChanged = []
480 del self.configInstance["config"]
481 self.configInstance.doClose()
482 self.configInstance = None
484 self.condition = True
485 exec (self.wizard[self.currStep]["condition"])
486 if not self.condition:
487 print "keys*******************:", self.wizard[self.currStep].keys()
488 if self.wizard[self.currStep].has_key("laststep"): # exit wizard, if condition of laststep doesn't hold
496 if self.wizard[self.currStep].has_key("displaytext"):
497 displaytext = self.wizard[self.currStep]["displaytext"]
499 for x in self.lcdCallbacks:
501 if len(self.stepHistory) == 0 or self.stepHistory[-1] != self.currStep:
502 self.stepHistory.append(self.currStep)
503 print "wizard step:", self.wizard[self.currStep]
506 self["step"].setText(self.getTranslation("Step ") + str(self.currStep) + "/" + str(self.numSteps))
507 if self.showStepSlider:
508 self["stepslider"].setValue(self.currStep)
510 if self.wizard[self.currStep]["timeout"] is not None:
512 self.timeoutTimer.start(1000)
514 print "wizard text", self.getTranslation(self.wizard[self.currStep]["text"])
515 self.updateText(firstset = True)
516 if self.wizard[self.currStep].has_key("displaytext"):
517 displaytext = self.wizard[self.currStep]["displaytext"]
519 for x in self.lcdCallbacks:
523 self.runCode(self.wizard[self.currStep]["code"])
524 if self.runCode(self.wizard[self.currStep]["code_async"]):
525 if self.updateValues in self.onShown:
526 self.onShown.remove(self.updateValues)
528 self.afterAsyncCode()
530 def afterAsyncCode(self):
531 if not self.updateValues in self.onShown:
532 self.onShown.append(self.updateValues)
535 if self.wizard[self.prevStep]["nextstep"] is not None:
536 self.currStep = self.getStepWithID(self.wizard[self.prevStep]["nextstep"])
537 if self.gotoStep is not None:
538 self.currStep = self.getStepWithID(self.gotoStep)
541 print "Now: " + str(self.currStep)
544 print "showing list,", self.currStep
545 for renderer in self.renderer:
546 rootrenderer = renderer
547 while renderer.source is not None:
548 print "self.list:", self["list"]
549 if renderer.source is self["list"]:
551 rootrenderer.instance.setZPosition(1)
552 renderer = renderer.source
554 #self["list"].instance.setZPosition(1)
556 if (self.wizard[self.currStep].has_key("dynamiclist")):
557 print "dynamic list, calling", self.wizard[self.currStep]["dynamiclist"]
558 newlist = eval("self." + self.wizard[self.currStep]["dynamiclist"] + "()")
559 #self.wizard[self.currStep]["evaluatedlist"] = []
560 for entry in newlist:
561 #self.wizard[self.currStep]["evaluatedlist"].append(entry)
562 self.list.append(entry)
563 #del self.wizard[self.currStep]["dynamiclist"]
564 if (len(self.wizard[self.currStep]["list"]) > 0):
565 #self["list"].instance.setZPosition(2)
566 for x in self.wizard[self.currStep]["list"]:
567 self.list.append((self.getTranslation(x[0]), x[1]))
568 self.wizard[self.currStep]["evaluatedlist"] = self.list
569 self["list"].list = self.list
570 self["list"].index = 0
575 print "showing config"
576 self["config"].instance.setZPosition(1)
577 if self.wizard[self.currStep]["config"]["type"] == "dynamic":
578 print "config type is dynamic"
579 self["config"].instance.setZPosition(2)
580 self["config"].l.setList(eval("self." + self.wizard[self.currStep]["config"]["source"])())
581 elif (self.wizard[self.currStep]["config"]["screen"] != None):
582 if self.wizard[self.currStep]["config"]["type"] == "standalone":
583 print "Type is standalone"
584 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
586 self["config"].instance.setZPosition(2)
587 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
588 if self.wizard[self.currStep]["config"]["args"] == None:
589 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
591 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
592 self["config"].l.setList(self.configInstance["config"].list)
593 callbacks = self.configInstance["config"].onSelectionChanged
594 self.configInstance["config"].destroy()
595 print "clearConfigList", self.configInstance["config"], self["config"]
596 self.configInstance["config"] = self["config"]
597 self.configInstance["config"].onSelectionChanged = callbacks
598 print "clearConfigList", self.configInstance["config"], self["config"]
600 self["config"].l.setList([])
602 if self.has_key("config"):
603 self["config"].hide()
605 def timeoutCounterFired(self):
606 self.timeoutCounter -= 1
607 print "timeoutCounter:", self.timeoutCounter
608 if self.timeoutCounter == 0:
609 if self.wizard[self.currStep]["timeoutaction"] == "selectnext":
610 print "selection next item"
613 if self.wizard[self.currStep]["timeoutaction"] == "changestep":
614 self.finished(gotoStep = self.wizard[self.currStep]["timeoutstep"])
621 def registerWizard(self, wizard, precondition, priority = 0):
622 self.wizards.append((wizard, precondition, priority))
624 def getWizards(self):
625 # x[1] is precondition
626 for wizard in self.wizards:
627 wizard[0].isLastWizard = False
628 if len(self.wizards) > 0:
629 self.wizards[-1][0].isLastWizard = True
630 return [(x[2], x[0]) for x in self.wizards if x[1] == 1]
632 wizardManager = WizardManager()