small optimizations and cleanups by Moritz Venn
[enigma2.git] / lib / python / Screens / VirtualKeyBoard.py
1 # -*- coding: iso-8859-1 -*-
2 from Components.Language import language
3 from Components.ActionMap import ActionMap
4 from Components.Label import Label
5 from Components.Pixmap import Pixmap
6 from Components.MenuList import MenuList
7 from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest
8 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_CENTER, RT_VALIGN_CENTER
9 from Screen import Screen
10 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE
11 from Tools.LoadPixmap import LoadPixmap
12
13 class VirtualKeyBoardList(MenuList):
14         def __init__(self, list, enableWrapAround=False):
15                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
16                 self.l.setFont(0, gFont("Regular", 22))
17                 self.l.setItemHeight(45)
18
19 def VirtualKeyBoardEntryComponent(keys, selectedKey,shiftMode=False):
20         key_backspace = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_backspace.png"))
21         key_bg = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_bg.png"))
22         key_clr = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_clr.png"))
23         key_esc = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_esc.png"))
24         key_ok = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_ok.png"))
25         key_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_sel.png"))
26         key_shift = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_shift.png"))
27         key_shift_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_shift_sel.png"))
28         key_space = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/vkey_space.png"))
29         
30         res = [ (keys) ]
31         
32         x = 0
33         count = 0
34         if shiftMode:
35                 shiftkey_png = key_shift_sel
36         else:
37                 shiftkey_png = key_shift
38         for key in keys:
39                 if key == "EXIT":
40                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_esc))
41                 elif key == "BACKSPACE":
42                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_backspace))
43                 elif key == "CLEAR":
44                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_clr))
45                 elif key == "SHIFT":
46                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=shiftkey_png))
47                 elif key == "SPACE":
48                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_space))
49                 elif key == "OK":
50                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_ok))
51                 #elif key == "<-":
52                 #       res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_left))
53                 #elif key == "->":
54                 #       res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_right))
55                 
56                 else:
57                         res.extend((
58                                 MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_bg),
59                                 MultiContentEntryText(pos=(x, 0), size=(45, 45), font=0, text=key.encode("utf-8"), flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER)
60                         ))
61                 
62                 if selectedKey == count:
63                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_sel))
64                 
65                 x += 45
66                 count += 1
67         
68         return res
69
70
71 class VirtualKeyBoard(Screen):
72
73         def __init__(self, session, title="", text=""):
74                 Screen.__init__(self, session)
75                 self.keys_list = []
76                 self.shiftkeys_list = []
77                 self.lang = language.getLanguage()
78                 if self.lang == 'de_DE':
79                         self.keys_list = [
80                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
81                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ü", u"+"],
82                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],
83                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
84                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"OK"]]
85                         
86                         self.shiftkeys_list = [
87                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
88                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"Ü", u"*"],
89                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],
90                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
91                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"OK"]]
92                         
93                 elif self.lang == 'es_ES':
94                         #still missing keys (u"ùÙ")
95                         self.keys_list = [
96                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
97                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ú", u"+"],
98                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ó", u"á", u"#"],
99                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
100                                 [u"SHIFT", u"SPACE", u"@", u"£", u"à", u"é", u"è", u"í", u"ì", u"ñ", u"ò", u"OK"]]
101                         
102                         self.shiftkeys_list = [
103                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
104                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"Ú", u"*"],
105                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ó", u"Á", u"'"],
106                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
107                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"À", u"É", u"È",  u"Í", u"Ì", u"Ñ", u"Ò", u"OK"]]
108                                 
109                 elif self.lang in ('sv_SE', 'fi_FI'):
110                         self.keys_list = [
111                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
112                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"é", u"+"],
113                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],
114                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
115                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"å", u"OK"]]
116                                 
117                         self.shiftkeys_list = [
118                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
119                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"É", u"*"],
120                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],
121                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
122
123                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"Å", u"OK"]]
124                 else:
125                         self.keys_list = [
126                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
127                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"+", u"@"],
128                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"#", u"\\"],
129                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
130                                 [u"SHIFT", u"SPACE", u"OK"]]
131                         
132                         self.shiftkeys_list = [
133                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
134                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"*"],
135                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"'", u"?"],
136                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
137                                 [u"SHIFT", u"SPACE", u"OK"]]
138                 
139                 self.shiftMode = False
140                 self.text = text
141                 self.selectedKey = 0
142                 
143                 self["header"] = Label(title)
144                 self["text"] = Label(self.text)
145                 self["list"] = VirtualKeyBoardList([])
146                 
147                 self["actions"] = ActionMap(["OkCancelActions", "WizardActions", "ColorActions"],
148                         {
149                                 "ok": self.okClicked,
150                                 "cancel": self.exit,
151                                 "left": self.left,
152                                 "right": self.right,
153                                 "up": self.up,
154                                 "down": self.down,
155                                 "red": self.backClicked,
156                                 "green": self.ok
157                         }, -2)
158                 
159                 self.onLayoutFinish.append(self.buildVirtualKeyBoard)
160
161         def buildVirtualKeyBoard(self, selectedKey=0):
162                 list = []
163                 
164                 if self.shiftMode:
165                         self.k_list = self.shiftkeys_list
166                         for keys in self.k_list:
167                                 if selectedKey < 12 and selectedKey > -1:
168                                         list.append(VirtualKeyBoardEntryComponent(keys, selectedKey,True))
169                                 else:
170                                         list.append(VirtualKeyBoardEntryComponent(keys, -1,True))
171                                 selectedKey -= 12
172                 else:
173                         self.k_list = self.keys_list
174                         for keys in self.k_list:
175                                 if selectedKey < 12 and selectedKey > -1:
176                                         list.append(VirtualKeyBoardEntryComponent(keys, selectedKey))
177                                 else:
178                                         list.append(VirtualKeyBoardEntryComponent(keys, -1))
179                                 selectedKey -= 12
180                 
181                 self["list"].setList(list)
182
183         
184         def backClicked(self):
185                 self.text = self["text"].getText()[:-1]
186                 self["text"].setText(self.text)
187                         
188         def okClicked(self):
189                 if self.shiftMode:
190                         list = self.shiftkeys_list
191                 else:
192                         list = self.keys_list
193                 
194                 selectedKey = self.selectedKey
195
196                 for x in list:
197                         if selectedKey < 12:
198                                 text = x[selectedKey]
199                                 break
200                         else:
201                                 selectedKey -= 12
202
203                 text = text.encode("utf-8")
204
205                 if text == "EXIT":
206                         self.close(None)
207                 
208                 elif text == "BACKSPACE":
209                         self.text = self["text"].getText()[:-1]
210                         self["text"].setText(self.text)
211                 
212                 elif text == "CLEAR":
213                         self.text = ""
214                         self["text"].setText(self.text)
215                 
216                 elif text == "SHIFT":
217                         if self.shiftMode:
218                                 self.shiftMode = False
219                         else:
220                                 self.shiftMode = True
221                         
222                         self.buildVirtualKeyBoard(self.selectedKey)
223                 
224                 elif text == "SPACE":
225                         self.text += " "
226                         self["text"].setText(self.text)
227                 
228                 elif text == "OK":
229                         self.close(self["text"].getText())
230                 
231                 else:
232                         self.text = self["text"].getText()
233                         self.text += text
234                         self["text"].setText(self.text)
235
236         def ok(self):
237                 self.close(self["text"].getText())
238
239         def exit(self):
240                 self.close(None)
241
242         def left(self):
243                 self.selectedKey -= 1
244                 
245                 if self.selectedKey == -1:
246                         self.selectedKey = 11
247                 elif self.selectedKey == 11:
248                         self.selectedKey = 23
249                 elif self.selectedKey == 23:
250                         self.selectedKey = 35
251                 elif self.selectedKey == 35:
252                         self.selectedKey = 47
253                 elif self.selectedKey == 47:
254                         self.selectedKey = 59
255                 
256                 self.showActiveKey()
257
258         def right(self):
259                 self.selectedKey += 1
260                 
261                 if self.selectedKey == 12:
262                         self.selectedKey = 0
263                 elif self.selectedKey == 24:
264                         self.selectedKey = 12
265                 elif self.selectedKey == 36:
266                         self.selectedKey = 24
267                 elif self.selectedKey == 48:
268                         self.selectedKey = 36
269                 elif self.selectedKey == 60:
270                         self.selectedKey = 48
271                 
272                 self.showActiveKey()
273
274         def up(self):
275                 self.selectedKey -= 12
276                 
277                 if self.selectedKey < 0:
278                         self.selectedKey += 60
279                 
280                 self.showActiveKey()
281
282         def down(self):
283                 self.selectedKey += 12
284                 
285                 if self.selectedKey > 59:
286                         self.selectedKey -= 60
287                 
288                 self.showActiveKey()
289
290         def showActiveKey(self):
291                 self.buildVirtualKeyBoard(self.selectedKey)