finish language-menu - the RT_VALIGN_CENTER doesn't work in eListboxPythonMultiConten...
[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, "type": "" }, "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                                 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
54                 def endElement(self, name):
55                         self.currContent = ""
56                         if name == 'code':
57                                 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
58                                 
59                 def characters(self, ch):
60                         if self.currContent == "code":
61                                  self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
62                                 
63         def __init__(self, session):
64                 self.skin = WelcomeWizard.skin
65
66                 Screen.__init__(self, session)
67                 HelpableScreen.__init__(self)
68
69                 self.wizard = {}
70                 parser = make_parser()
71                 print "Reading startwizard.xml"
72                 wizardHandler = self.parseWizard(self.wizard)
73                 parser.setContentHandler(wizardHandler)
74                 parser.parse('/usr/share/enigma2/startwizard.xml')
75                 
76                 self.numSteps = len(self.wizard)
77                 self.currStep = 1
78
79                 self["text"] = Label()
80                 self["rc"] = MovingPixmap()
81                 self["arrowdown"] = MovingPixmap()
82                 self["arrowup"] = MovingPixmap()
83
84                 self["config"] = ConfigList([])
85
86                 self["step"] = Label()
87                                 
88                 self["stepslider"] = Slider(1, self.numSteps)
89                 
90                 self.list = []
91                 self["list"] = MenuList(self.list)
92
93                 self.onShown.append(self.updateValues)
94                 
95                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
96                 {
97                         "ok": self.ok,
98                         #"cancel": self.keyCancel,
99                         "left": self.left,
100                         "right": self.right,
101                         "up": self.up,
102                         "down": self.down,
103                         "1": self.keyNumberGlobal,
104                         "2": self.keyNumberGlobal,
105                         "3": self.keyNumberGlobal,
106                         "4": self.keyNumberGlobal,
107                         "5": self.keyNumberGlobal,
108                         "6": self.keyNumberGlobal,
109                         "7": self.keyNumberGlobal,
110                         "8": self.keyNumberGlobal,
111                         "9": self.keyNumberGlobal,
112                         "0": self.keyNumberGlobal
113                 }, -1)
114                 
115                 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
116                         #{
117                                 #"ok": (self.ok, _("Close this Screen...")),
118                         #})
119
120         def ok(self):
121                 print "OK"
122                 if (self.wizard[self.currStep]["config"]["screen"] != None):
123                         try: # don't die, if no run() is available
124                                 self.configInstance.run()
125                         except:
126                                 print "Failed to run configInstance"
127                 
128                 if (len(self.wizard[self.currStep]["list"]) > 0):
129                         nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
130                         if nextStep == "end":
131                                 self.currStep = self.numSteps
132                         elif nextStep == "next":
133                                 pass
134                         else:
135                                 self.currStep = int(nextStep) - 1
136
137                 if (self.currStep == self.numSteps): # wizard finished
138                         config.misc.firstrun.value = 0;
139                         config.misc.firstrun.save()
140                         self.session.close()
141                 else:
142                         self.currStep += 1
143                         self.updateValues()
144                         
145                 print "Now: " + str(self.currStep)
146
147         def keyNumberGlobal(self, number):
148                 if (self.wizard[self.currStep]["config"]["screen"] != None):
149                         self.configInstance.keyNumberGlobal(number)
150                 
151         def left(self):
152                 if (self.wizard[self.currStep]["config"]["screen"] != None):
153                         self.configInstance.keyLeft()
154                 print "left"
155         
156         def right(self):
157                 if (self.wizard[self.currStep]["config"]["screen"] != None):
158                         self.configInstance.keyRight()
159                 print "right"
160
161         def up(self):
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["config"].instance.moveUp)
166                 print "up"
167                 
168         def down(self):
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["config"].instance.moveDown)
173                 print "down"
174                 
175         def updateValues(self):
176                 print "Updating values in step " + str(self.currStep)
177                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
178                 self["stepslider"].setValue(self.currStep)
179
180                 self["text"].setText(self.wizard[self.currStep]["text"])
181
182                 if self.wizard[self.currStep]["code"] != "":
183                         print self.wizard[self.currStep]["code"]
184                         exec(self.wizard[self.currStep]["code"])
185                 
186                 self["list"].instance.setZPosition(1)
187                 self.list = []
188                 if (len(self.wizard[self.currStep]["list"]) > 0):
189                         self["list"].instance.setZPosition(2)
190                         for x in self.wizard[self.currStep]["list"]:
191                                 self.list.append((x[0], None))
192                 self["list"].l.setList(self.list)
193
194                 self["config"].instance.setZPosition(1)
195                 if (self.wizard[self.currStep]["config"]["screen"] != None):
196                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
197                                 print "Type is standalone"
198                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
199                         else:
200                                 self["config"].instance.setZPosition(2)
201                                 print self.wizard[self.currStep]["config"]["screen"]
202                                 if self.wizard[self.currStep]["config"]["args"] == None:
203                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
204                                 else:
205                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
206                                 self["config"].l.setList(self.configInstance["config"].list)
207                                 self.configInstance["config"] = self["config"]
208                 else:
209                         self["config"].l.setList([])
210
211
212
213 def listActiveWizards():
214         wizards = [ ]
215
216         if config.misc.firstrun.value:
217                 wizards.append(WelcomeWizard)
218         
219         return wizards