some fixes and improvements for the start-wizard
[enigma2.git] / lib / python / Screens / Wizard.py
1 from Screen import Screen
2
3 from Screens.HelpMenu import HelpableScreen
4 from Components.Label import Label
5 from Components.Slider import Slider
6 from Components.ActionMap import HelpableActionMap, NumberActionMap
7 from Components.config import config, configElementBoolean
8 from Components.Pixmap import *
9 from Components.MenuList import MenuList
10 from Components.ConfigList import ConfigList
11
12 from xml.sax import make_parser
13 from xml.sax.handler import ContentHandler
14
15 config.misc.firstrun = configElementBoolean("config.misc.firstrun", 1);
16
17 class WelcomeWizard(Screen, HelpableScreen):
18
19         skin = """
20                 <screen position="0,0" size="720,560" title="Welcome..." flags="wfNoBorder" >
21                         <widget name="text" position="50,100" size="440,200" font="Arial;23" />
22                         <widget name="list" position="50,300" zPosition="1" size="440,200" />
23                         <widget name="config" position="50,300" zPosition="1" size="440,200" transparent="1" />                 
24                         <widget name="step" position="50,50" size="440,25" font="Arial;23" />
25                         <widget name="stepslider" position="50,500" zPosition="1" size="440,20" backgroundColor="dark" />
26                         <widget name="rc" pixmap="/usr/share/enigma2/rc.png" position="500,600" zPosition="10" size="154,475" transparent="1" alphatest="on"/>
27                         <widget name="arrowdown" pixmap="/usr/share/enigma2/arrowdown.png" position="0,0" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
28                         <widget name="arrowup" pixmap="/usr/share/enigma2/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
29                 </screen>"""
30
31         class parseWizard(ContentHandler):
32                 def __init__(self, wizard):
33                         self.isPointsElement, self.isReboundsElement = 0, 0
34                         self.wizard = wizard
35                         self.currContent = ""
36                 
37                 def startElement(self, name, attrs):
38                         print name
39                         self.currContent = name
40                         if (name == "step"):
41                                 self.lastStep = int(attrs.get('number'))
42                                 self.wizard[self.lastStep] = {"text": "", "list": [], "config": {"screen": None, "args": None }, "code": ""}
43                         elif (name == "text"):
44                                 self.wizard[self.lastStep]["text"] = str(attrs.get('value'))
45                         elif (name == "listentry"):
46                                 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
47                         elif (name == "config"):
48                                 exec "from Screens." + str(attrs.get('module')) + " import *"
49                                 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
50                                 if (attrs.has_key('args')):
51                                         print "has args"
52                                         self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
53                 def endElement(self, name):
54                         self.currContent = ""
55                         if name == 'code':
56                                 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
57                                 
58                 def characters(self, ch):
59                         if self.currContent == "code":
60                                  self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
61                                 
62         def __init__(self, session):
63                 self.skin = WelcomeWizard.skin
64
65                 Screen.__init__(self, session)
66                 HelpableScreen.__init__(self)
67
68                 self.wizard = {}
69                 parser = make_parser()
70                 print "Reading startwizard.xml"
71                 wizardHandler = self.parseWizard(self.wizard)
72                 parser.setContentHandler(wizardHandler)
73                 parser.parse('/usr/share/enigma2/startwizard.xml')
74                 
75                 self.numSteps = len(self.wizard)
76                 self.currStep = 1
77
78                 self["text"] = Label()
79                 self["rc"] = MovingPixmap()
80                 self["arrowdown"] = MovingPixmap()
81                 self["arrowup"] = MovingPixmap()
82
83                 self["config"] = ConfigList([])
84
85                 self["step"] = Label()
86                                 
87                 self["stepslider"] = Slider(1, self.numSteps)
88                 
89                 self.list = []
90                 self["list"] = MenuList(self.list)
91
92                 self.onShown.append(self.updateValues)
93                 
94                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
95                 {
96                         "ok": self.ok,
97                         #"cancel": self.keyCancel,
98                         "left": self.left,
99                         "right": self.right,
100                         "up": self.up,
101                         "down": self.down,
102                         "1": self.keyNumberGlobal,
103                         "2": self.keyNumberGlobal,
104                         "3": self.keyNumberGlobal,
105                         "4": self.keyNumberGlobal,
106                         "5": self.keyNumberGlobal,
107                         "6": self.keyNumberGlobal,
108                         "7": self.keyNumberGlobal,
109                         "8": self.keyNumberGlobal,
110                         "9": self.keyNumberGlobal,
111                         "0": self.keyNumberGlobal
112                 }, -1)
113                 
114                 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
115                         #{
116                                 #"ok": (self.ok, _("Close this Screen...")),
117                         #})
118
119         def ok(self):
120                 if (self.wizard[self.currStep]["config"]["screen"] != None):
121                         self.configInstance.run()
122                 
123                 if (len(self.wizard[self.currStep]["list"]) > 0):
124                         nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
125                         if nextStep == "end":
126                                 self.currStep = self.numSteps
127                         elif nextStep == "next":
128                                 pass
129                         else:
130                                 self.currStep = int(nextStep) - 1
131
132                 if (self.currStep == self.numSteps): # wizard finished
133                         config.misc.firstrun.value = 0;
134                         config.misc.firstrun.save()
135                         self.session.close()
136                 else:
137                         self.currStep += 1
138                         self.updateValues()
139
140         def keyNumberGlobal(self, number):
141                 if (self.wizard[self.currStep]["config"]["screen"] != None):
142                         self.configInstance.keyNumberGlobal(number)
143                 
144         def left(self):
145                 if (self.wizard[self.currStep]["config"]["screen"] != None):
146                         self.configInstance.keyLeft()
147                 print "left"
148         
149         def right(self):
150                 if (self.wizard[self.currStep]["config"]["screen"] != None):
151                         self.configInstance.keyRight()
152                 print "right"
153
154         def up(self):
155                 if (self.wizard[self.currStep]["config"]["screen"] != None):
156                         self["config"].instance.moveSelection(self["config"].instance.moveUp)
157                 elif (len(self.wizard[self.currStep]["list"]) > 0):
158                         self["list"].instance.moveSelection(self["config"].instance.moveUp)
159                 print "up"
160                 
161         def down(self):
162                 if (self.wizard[self.currStep]["config"]["screen"] != None):
163                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
164                 elif (len(self.wizard[self.currStep]["list"]) > 0):
165                         self["list"].instance.moveSelection(self["config"].instance.moveDown)
166                 print "down"
167                 
168         def updateValues(self):
169                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
170                 self["stepslider"].setValue(self.currStep)
171
172                 self["text"].setText(self.wizard[self.currStep]["text"])
173                 
174                 self["list"].instance.setZPosition(1)
175                 self.list = []
176                 if (len(self.wizard[self.currStep]["list"]) > 0):
177                         self["list"].instance.setZPosition(2)
178                         for x in self.wizard[self.currStep]["list"]:
179                                 self.list.append((x[0], None))
180                 self["list"].l.setList(self.list)
181
182                 self["config"].instance.setZPosition(1)
183                 if (self.wizard[self.currStep]["config"]["screen"] != None):
184                         self["config"].instance.setZPosition(2)
185                         print self.wizard[self.currStep]["config"]["screen"]
186                         if self.wizard[self.currStep]["config"]["args"] == None:
187                                 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
188                         else:
189                                 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
190                         self["config"].l.setList(self.configInstance["config"].list)
191                         self.configInstance["config"] = self["config"]
192                 else:
193                         self["config"].l.setList([])
194
195                 if self.wizard[self.currStep]["code"] != "":
196                         print self.wizard[self.currStep]["code"]
197                         exec(self.wizard[self.currStep]["code"])
198
199 def listActiveWizards():
200         wizards = [ ]
201
202         if config.misc.firstrun.value:
203                 wizards.append(WelcomeWizard)
204         
205         return wizards