1 from Screen import Screen
3 from Screens.HelpMenu import HelpableScreen
4 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
6 from Components.Label import Label
7 from Components.Slider import Slider
8 from Components.ActionMap import NumberActionMap
9 from Components.MenuList import MenuList
10 from Components.ConfigList import ConfigList
11 from Components.Sources.List import List
13 from enigma import eTimer
15 from xml.sax import make_parser
16 from xml.sax.handler import ContentHandler
18 class WizardSummary(Screen):
20 <screen position="0,0" size="132,64">
21 <widget name="text" position="6,4" size="120,42" font="Regular;14" transparent="1" />
22 <widget source="parent.list" render="Label" position="6,25" size="120,21" font="Regular;16">
23 <convert type="StringListSelection" />
27 def __init__(self, session, parent):
28 Screen.__init__(self, session, parent)
30 #names = parent.skinName
31 #if not isinstance(names, list):
34 #self.skinName = [x + "_summary" for x in names ]
35 #self.skinName.append("Wizard")
36 #print "*************+++++++++++++++++****************++++++++++******************* WizardSummary", self.skinName
38 self["text"] = Label("")
39 self.onShow.append(self.setCallback)
41 def setCallback(self):
42 self.parent.setLCDTextCallback(self.setText)
44 def setText(self, text):
45 self["text"].setText(text)
48 def createSummary(self):
49 print "WizardCreateSummary"
52 class parseWizard(ContentHandler):
53 def __init__(self, wizard):
54 self.isPointsElement, self.isReboundsElement = 0, 0
59 def startElement(self, name, attrs):
60 #print "startElement", name
61 self.currContent = name
64 if attrs.has_key('id'):
65 id = str(attrs.get('id'))
69 if attrs.has_key('nextstep'):
70 nextstep = str(attrs.get('nextstep'))
73 if attrs.has_key('timeout'):
74 timeout = int(attrs.get('timeout'))
77 if attrs.has_key('timeoutaction'):
78 timeoutaction = str(attrs.get('timeoutaction'))
80 timeoutaction = 'nextpage'
82 if attrs.has_key('timeoutstep'):
83 timeoutstep = str(attrs.get('timeoutstep'))
86 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}
87 elif (name == "text"):
88 self.wizard[self.lastStep]["text"] = str(attrs.get('value')).replace("\\n", "\n")
89 elif (name == "displaytext"):
90 self.wizard[self.lastStep]["displaytext"] = str(attrs.get('value')).replace("\\n", "\n")
91 elif (name == "list"):
92 if (attrs.has_key('type')):
93 if attrs["type"] == "dynamic":
94 self.wizard[self.lastStep]["dynamiclist"] = attrs.get("source")
95 #self.wizard[self.lastStep]["list"].append(("Hallo", "test"))
96 if (attrs.has_key("evaluation")):
98 self.wizard[self.lastStep]["listevaluation"] = attrs.get("evaluation")
99 if (attrs.has_key("onselect")):
100 self.wizard[self.lastStep]["onselect"] = attrs.get("onselect")
101 elif (name == "listentry"):
102 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
103 elif (name == "config"):
104 type = str(attrs.get('type'))
105 self.wizard[self.lastStep]["config"]["type"] = type
106 if type == "ConfigList" or type == "standalone":
108 exec "from Screens." + str(attrs.get('module')) + " import *"
110 exec "from " + str(attrs.get('module')) + " import *"
112 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
113 if (attrs.has_key('args')):
115 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
116 elif type == "dynamic":
117 self.wizard[self.lastStep]["config"]["source"] = str(attrs.get('source'))
118 if (attrs.has_key('evaluation')):
119 self.wizard[self.lastStep]["config"]["evaluation"] = str(attrs.get('evaluation'))
120 elif (name == "code"):
121 self.async_code = attrs.has_key('async') and str(attrs.get('async')) == "yes"
122 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
123 self.codeafter = True
125 self.codeafter = False
126 elif (name == "condition"):
129 def endElement(self, name):
130 self.currContent = ""
134 self.wizard[self.lastStep]["codeafter_async"] = self.wizard[self.lastStep]["codeafter_async"].strip()
136 self.wizard[self.lastStep]["code_async"] = self.wizard[self.lastStep]["code_async"].strip()
139 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
141 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
142 elif name == 'condition':
143 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
145 #print "Step number", self.lastStep, ":", self.wizard[self.lastStep]
148 def characters(self, ch):
149 if self.currContent == "code":
152 self.wizard[self.lastStep]["codeafter_async"] = self.wizard[self.lastStep]["codeafter_async"] + ch
154 self.wizard[self.lastStep]["code_async"] = self.wizard[self.lastStep]["code_async"] + ch
157 self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
159 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
160 elif self.currContent == "condition":
161 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
163 def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
164 Screen.__init__(self, session)
166 self.stepHistory = []
169 parser = make_parser()
170 if not isinstance(self.xmlfile, list):
171 self.xmlfile = [self.xmlfile]
172 print "Reading ", self.xmlfile
173 wizardHandler = self.parseWizard(self.wizard)
174 parser.setContentHandler(wizardHandler)
175 for xmlfile in self.xmlfile:
176 if xmlfile[0] != '/':
177 parser.parse('/usr/share/enigma2/' + xmlfile)
179 parser.parse(xmlfile)
181 self.showSteps = showSteps
182 self.showStepSlider = showStepSlider
183 self.showList = showList
184 self.showConfig = showConfig
186 self.numSteps = len(self.wizard)
187 self.currStep = self.getStepWithID("start") + 1
189 self.timeoutTimer = eTimer()
190 self.timeoutTimer.callback.append(self.timeoutCounterFired)
192 self["text"] = Label()
195 self["config"] = ConfigList([], session = session)
198 self["step"] = Label()
200 if self.showStepSlider:
201 self["stepslider"] = Slider(1, self.numSteps)
205 self["list"] = List(self.list, enableWrapAround = True)
206 self["list"].onSelectionChanged.append(self.selChanged)
207 #self["list"] = MenuList(self.list, enableWrapAround = True)
209 self.onShown.append(self.updateValues)
211 self.configInstance = None
213 self.lcdCallbacks = []
215 self.disableKeys = False
217 self["actions"] = NumberActionMap(["WizardActions", "NumberActions", "ColorActions", "SetupActions", "InputAsciiActions"],
219 "gotAsciiCode": self.keyGotAscii,
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)
372 def keyGotAscii(self):
373 if (self.wizard[self.currStep]["config"]["screen"] != None):
374 self["config"].handleKey(KEY_ASCII)
378 if (self.wizard[self.currStep]["config"]["screen"] != None):
379 self.configInstance.keyLeft()
380 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
381 self["config"].handleKey(KEY_LEFT)
386 if (self.wizard[self.currStep]["config"]["screen"] != None):
387 self.configInstance.keyRight()
388 elif (self.wizard[self.currStep]["config"]["type"] == "dynamic"):
389 self["config"].handleKey(KEY_RIGHT)
394 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
395 self["config"].instance.moveSelection(self["config"].instance.moveUp)
396 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
397 self["list"].selectPrevious()
398 if self.wizard[self.currStep].has_key("onselect"):
399 print "current:", self["list"].current
400 self.selection = self["list"].current[-1]
401 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
402 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
407 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None or self.wizard[self.currStep]["config"]["type"] == "dynamic"):
408 self["config"].instance.moveSelection(self["config"].instance.moveDown)
409 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
410 #self["list"].instance.moveSelection(self["list"].instance.moveDown)
411 self["list"].selectNext()
412 if self.wizard[self.currStep].has_key("onselect"):
413 print "current:", self["list"].current
414 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
415 #exec("self." + self.wizard[self.currStep]["onselect"] + "()")
416 self.selection = self["list"].current[-1]
417 #self.selection = self.wizard[self.currStep]["evaluatedlist"][self["list"].l.getCurrentSelectionIndex()][1]
418 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
421 def selChanged(self):
424 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
425 self["config"].instance.moveSelection(self["config"].instance.moveUp)
426 elif (self.showList and len(self.wizard[self.currStep]["evaluatedlist"]) > 0):
427 if self.wizard[self.currStep].has_key("onselect"):
428 self.selection = self["list"].current[-1]
429 print "self.selection:", self.selection
430 exec("self." + self.wizard[self.currStep]["onselect"] + "()")
432 def resetCounter(self):
433 self.timeoutCounter = self.wizard[self.currStep]["timeout"]
435 def runCode(self, code):
442 def getTranslation(self, text):
445 def updateText(self, firstset = False):
446 text = self.getTranslation(self.wizard[self.currStep]["text"])
447 if text.find("[timeout]") != -1:
448 text = text.replace("[timeout]", str(self.timeoutCounter))
449 self["text"].setText(text)
452 self["text"].setText(text)
454 def updateValues(self):
455 print "Updating values in step " + str(self.currStep)
456 # calling a step which doesn't exist can only happen if the condition in the last step is not fulfilled
457 # if a non-existing step is called, end the wizard
458 if self.currStep > len(self.wizard):
463 self.timeoutTimer.stop()
465 if self.configInstance is not None:
467 self.configInstance["config"].onSelectionChanged = []
468 del self.configInstance["config"]
469 self.configInstance.doClose()
470 self.configInstance = None
472 self.condition = True
473 exec (self.wizard[self.currStep]["condition"])
474 if not self.condition:
478 if self.wizard[self.currStep].has_key("displaytext"):
479 displaytext = self.wizard[self.currStep]["displaytext"]
481 for x in self.lcdCallbacks:
483 if len(self.stepHistory) == 0 or self.stepHistory[-1] != self.currStep:
484 self.stepHistory.append(self.currStep)
485 print "wizard step:", self.wizard[self.currStep]
488 self["step"].setText(self.getTranslation("Step ") + str(self.currStep) + "/" + str(self.numSteps))
489 if self.showStepSlider:
490 self["stepslider"].setValue(self.currStep)
492 if self.wizard[self.currStep]["timeout"] is not None:
494 self.timeoutTimer.start(1000)
496 print "wizard text", self.getTranslation(self.wizard[self.currStep]["text"])
497 self.updateText(firstset = True)
498 if self.wizard[self.currStep].has_key("displaytext"):
499 displaytext = self.wizard[self.currStep]["displaytext"]
501 for x in self.lcdCallbacks:
505 self.runCode(self.wizard[self.currStep]["code"])
506 if self.runCode(self.wizard[self.currStep]["code_async"]):
507 if self.updateValues in self.onShown:
508 self.onShown.remove(self.updateValues)
510 self.afterAsyncCode()
512 def afterAsyncCode(self):
513 if not self.updateValues in self.onShown:
514 self.onShown.append(self.updateValues)
517 if self.wizard[self.prevStep]["nextstep"] is not None:
518 self.currStep = self.getStepWithID(self.wizard[self.prevStep]["nextstep"])
519 if self.gotoStep is not None:
520 self.currStep = self.getStepWithID(self.gotoStep)
523 print "Now: " + str(self.currStep)
526 print "showing list,", self.currStep
527 for renderer in self.renderer:
528 rootrenderer = renderer
529 while renderer.source is not None:
530 print "self.list:", self["list"]
531 if renderer.source is self["list"]:
533 rootrenderer.instance.setZPosition(1)
534 renderer = renderer.source
536 #self["list"].instance.setZPosition(1)
538 if (self.wizard[self.currStep].has_key("dynamiclist")):
539 print "dynamic list, calling", self.wizard[self.currStep]["dynamiclist"]
540 newlist = eval("self." + self.wizard[self.currStep]["dynamiclist"] + "()")
541 #self.wizard[self.currStep]["evaluatedlist"] = []
542 for entry in newlist:
543 #self.wizard[self.currStep]["evaluatedlist"].append(entry)
544 self.list.append(entry)
545 #del self.wizard[self.currStep]["dynamiclist"]
546 if (len(self.wizard[self.currStep]["list"]) > 0):
547 #self["list"].instance.setZPosition(2)
548 for x in self.wizard[self.currStep]["list"]:
549 self.list.append((self.getTranslation(x[0]), x[1]))
550 self.wizard[self.currStep]["evaluatedlist"] = self.list
551 self["list"].list = self.list
552 self["list"].index = 0
557 print "showing config"
558 self["config"].instance.setZPosition(1)
559 if self.wizard[self.currStep]["config"]["type"] == "dynamic":
560 print "config type is dynamic"
561 self["config"].instance.setZPosition(2)
562 self["config"].l.setList(eval("self." + self.wizard[self.currStep]["config"]["source"])())
563 elif (self.wizard[self.currStep]["config"]["screen"] != None):
564 if self.wizard[self.currStep]["config"]["type"] == "standalone":
565 print "Type is standalone"
566 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
568 self["config"].instance.setZPosition(2)
569 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
570 if self.wizard[self.currStep]["config"]["args"] == None:
571 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
573 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
574 self["config"].l.setList(self.configInstance["config"].list)
575 callbacks = self.configInstance["config"].onSelectionChanged
576 self.configInstance["config"].destroy()
577 print "clearConfigList", self.configInstance["config"], self["config"]
578 self.configInstance["config"] = self["config"]
579 self.configInstance["config"].onSelectionChanged = callbacks
580 print "clearConfigList", self.configInstance["config"], self["config"]
582 self["config"].l.setList([])
584 if self.has_key("config"):
585 self["config"].hide()
587 def timeoutCounterFired(self):
588 self.timeoutCounter -= 1
589 print "timeoutCounter:", self.timeoutCounter
590 if self.timeoutCounter == 0:
591 if self.wizard[self.currStep]["timeoutaction"] == "selectnext":
592 print "selection next item"
595 if self.wizard[self.currStep]["timeoutaction"] == "changestep":
596 self.finished(gotoStep = self.wizard[self.currStep]["timeoutstep"])
603 def registerWizard(self, wizard, precondition, priority = 0):
604 self.wizards.append((wizard, precondition, priority))
606 def getWizards(self):
608 for x in self.wizards:
609 if x[1] == 1: # precondition
610 list.append((x[2], x[0]))
613 wizardManager = WizardManager()