1 from Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.Label import Label
7 from Components.Slider import Slider
8 from Components.ActionMap import HelpableActionMap, NumberActionMap
9 from Components.Pixmap import *
10 from Components.MenuList import MenuList
11 from Components.ConfigList import ConfigList
13 from xml.sax import make_parser
14 from xml.sax.handler import ContentHandler
16 class Wizard(Screen, HelpableScreen):
18 class parseWizard(ContentHandler):
19 def __init__(self, wizard):
20 self.isPointsElement, self.isReboundsElement = 0, 0
24 def startElement(self, name, attrs):
26 self.currContent = name
28 self.lastStep = int(attrs.get('number'))
29 self.wizard[self.lastStep] = {"condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": ""}
30 elif (name == "text"):
31 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
32 elif (name == "listentry"):
33 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
34 elif (name == "config"):
35 exec "from Screens." + str(attrs.get('module')) + " import *"
36 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
37 if (attrs.has_key('args')):
39 self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
40 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
41 elif (name == "condition"):
43 def endElement(self, name):
46 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
47 elif name == 'condition':
48 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
50 def characters(self, ch):
51 if self.currContent == "code":
52 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
53 elif self.currContent == "condition":
54 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
55 def __init__(self, session, showSteps = True, showStepSlider = True):
56 Screen.__init__(self, session)
57 HelpableScreen.__init__(self)
60 parser = make_parser()
61 print "Reading " + self.xmlfile
62 wizardHandler = self.parseWizard(self.wizard)
63 parser.setContentHandler(wizardHandler)
64 parser.parse('/usr/share/enigma2/' + self.xmlfile)
66 self.showSteps = showSteps
67 self.showStepSlider = showStepSlider
69 self.numSteps = len(self.wizard)
72 self["text"] = Label()
74 self["config"] = ConfigList([])
77 self["step"] = Label()
79 if self.showStepSlider:
80 self["stepslider"] = Slider(1, self.numSteps)
83 self["list"] = MenuList(self.list)
85 self.onShown.append(self.updateValues)
87 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
95 "1": self.keyNumberGlobal,
96 "2": self.keyNumberGlobal,
97 "3": self.keyNumberGlobal,
98 "4": self.keyNumberGlobal,
99 "5": self.keyNumberGlobal,
100 "6": self.keyNumberGlobal,
101 "7": self.keyNumberGlobal,
102 "8": self.keyNumberGlobal,
103 "9": self.keyNumberGlobal,
104 "0": self.keyNumberGlobal
107 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
109 #"ok": (self.ok, _("Close this Screen...")),
114 if self.currStep < 1:
123 if (self.wizard[self.currStep]["config"]["screen"] != None):
124 try: # don't die, if no run() is available
125 self.configInstance.run()
127 print "Failed to run configInstance"
129 if (len(self.wizard[self.currStep]["list"]) > 0):
130 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
131 if nextStep == "end":
132 self.currStep = self.numSteps
133 elif nextStep == "next":
136 self.currStep = int(nextStep) - 1
138 if (self.currStep == self.numSteps): # wizard finished
145 print "Now: " + str(self.currStep)
147 def keyNumberGlobal(self, number):
148 if (self.wizard[self.currStep]["config"]["screen"] != None):
149 self.configInstance.keyNumberGlobal(number)
152 if (self.wizard[self.currStep]["config"]["screen"] != None):
153 self.configInstance.keyLeft()
157 if (self.wizard[self.currStep]["config"]["screen"] != None):
158 self.configInstance.keyRight()
162 if (self.wizard[self.currStep]["config"]["screen"] != None):
163 self["config"].instance.moveSelection(self["config"].instance.moveUp)
164 elif (len(self.wizard[self.currStep]["list"]) > 0):
165 self["list"].instance.moveSelection(self["list"].instance.moveUp)
169 if (self.wizard[self.currStep]["config"]["screen"] != None):
170 self["config"].instance.moveSelection(self["config"].instance.moveDown)
171 elif (len(self.wizard[self.currStep]["list"]) > 0):
172 self["list"].instance.moveSelection(self["list"].instance.moveDown)
175 def updateValues(self):
176 print "Updating values in step " + str(self.currStep)
178 self.condition = True
179 exec (self.wizard[self.currStep]["condition"])
182 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
183 if self.showStepSlider:
184 self["stepslider"].setValue(self.currStep)
186 print _(self.wizard[self.currStep]["text"])
187 self["text"].setText(_(self.wizard[self.currStep]["text"]))
189 if self.wizard[self.currStep]["code"] != "":
190 print self.wizard[self.currStep]["code"]
191 exec(self.wizard[self.currStep]["code"])
193 self["list"].instance.setZPosition(1)
195 if (len(self.wizard[self.currStep]["list"]) > 0):
196 self["list"].instance.setZPosition(2)
197 for x in self.wizard[self.currStep]["list"]:
198 self.list.append((_(x[0]), None))
199 self["list"].l.setList(self.list)
201 self["config"].instance.setZPosition(1)
202 if (self.wizard[self.currStep]["config"]["screen"] != None):
203 if self.wizard[self.currStep]["config"]["type"] == "standalone":
204 print "Type is standalone"
205 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
207 self["config"].instance.setZPosition(2)
208 print self.wizard[self.currStep]["config"]["screen"]
209 if self.wizard[self.currStep]["config"]["args"] == None:
210 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
212 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
213 self["config"].l.setList(self.configInstance["config"].list)
214 self.configInstance["config"] = self["config"]
216 self["config"].l.setList([])
217 else: # condition false
225 def registerWizard(self, wizard, precondition):
226 self.wizards.append((wizard, precondition))
228 def getWizards(self):
230 for x in self.wizards:
231 if x[1] == 1: # precondition
235 wizardManager = WizardManager()