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, showList = True, showConfig = 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
68 self.showList = showList
69 self.showConfig = showConfig
71 self.numSteps = len(self.wizard)
74 self["text"] = Label()
77 self["config"] = ConfigList([])
80 self["step"] = Label()
82 if self.showStepSlider:
83 self["stepslider"] = Slider(1, self.numSteps)
87 self["list"] = MenuList(self.list)
89 self.onShown.append(self.updateValues)
91 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
99 "1": self.keyNumberGlobal,
100 "2": self.keyNumberGlobal,
101 "3": self.keyNumberGlobal,
102 "4": self.keyNumberGlobal,
103 "5": self.keyNumberGlobal,
104 "6": self.keyNumberGlobal,
105 "7": self.keyNumberGlobal,
106 "8": self.keyNumberGlobal,
107 "9": self.keyNumberGlobal,
108 "0": self.keyNumberGlobal
111 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
113 #"ok": (self.ok, _("Close this Screen...")),
118 if self.currStep < 1:
128 if (self.wizard[self.currStep]["config"]["screen"] != None):
129 try: # don't die, if no run() is available
130 self.configInstance.run()
132 print "Failed to run configInstance"
135 if (len(self.wizard[self.currStep]["list"]) > 0):
136 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
137 if nextStep == "end":
138 self.currStep = self.numSteps
139 elif nextStep == "next":
142 self.currStep = int(nextStep) - 1
144 if (self.currStep == self.numSteps): # wizard finished
151 print "Now: " + str(self.currStep)
153 def keyNumberGlobal(self, number):
154 if (self.wizard[self.currStep]["config"]["screen"] != None):
155 self.configInstance.keyNumberGlobal(number)
158 if (self.wizard[self.currStep]["config"]["screen"] != None):
159 self.configInstance.keyLeft()
163 if (self.wizard[self.currStep]["config"]["screen"] != None):
164 self.configInstance.keyRight()
168 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
169 self["config"].instance.moveSelection(self["config"].instance.moveUp)
170 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
171 self["list"].instance.moveSelection(self["list"].instance.moveUp)
175 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
176 self["config"].instance.moveSelection(self["config"].instance.moveDown)
177 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
178 self["list"].instance.moveSelection(self["list"].instance.moveDown)
181 def updateValues(self):
182 print "Updating values in step " + str(self.currStep)
184 self.condition = True
185 exec (self.wizard[self.currStep]["condition"])
188 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
189 if self.showStepSlider:
190 self["stepslider"].setValue(self.currStep)
192 print _(self.wizard[self.currStep]["text"])
193 self["text"].setText(_(self.wizard[self.currStep]["text"]))
195 if self.wizard[self.currStep]["code"] != "":
196 print self.wizard[self.currStep]["code"]
197 exec(self.wizard[self.currStep]["code"])
200 self["list"].instance.setZPosition(1)
202 if (len(self.wizard[self.currStep]["list"]) > 0):
203 self["list"].instance.setZPosition(2)
204 for x in self.wizard[self.currStep]["list"]:
205 self.list.append((_(x[0]), None))
206 self["list"].l.setList(self.list)
209 self["config"].instance.setZPosition(1)
210 if (self.wizard[self.currStep]["config"]["screen"] != None):
211 if self.wizard[self.currStep]["config"]["type"] == "standalone":
212 print "Type is standalone"
213 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
215 self["config"].instance.setZPosition(2)
216 print self.wizard[self.currStep]["config"]["screen"]
217 if self.wizard[self.currStep]["config"]["args"] == None:
218 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
220 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
221 self["config"].l.setList(self.configInstance["config"].list)
222 self.configInstance["config"] = self["config"]
224 self["config"].l.setList([])
225 else: # condition false
233 def registerWizard(self, wizard, precondition):
234 self.wizards.append((wizard, precondition))
236 def getWizards(self):
238 for x in self.wizards:
239 if x[1] == 1: # precondition
243 wizardManager = WizardManager()