Merge remote branch 'origin/acid-burn/virtualkeyboard' into experimental
[vuplus_dvbapp] / lib / python / Screens / VirtualKeyBoard.py
1 # -*- coding: UTF-8 -*-
2 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_CENTER, RT_VALIGN_CENTER, getPrevAsciiCode
3 from Screen import Screen
4 from Components.Language import language
5 from Components.ActionMap import ActionMap
6 from Components.Sources.StaticText import StaticText
7 from Components.Label import Label
8 from Components.Pixmap import Pixmap
9 from Components.MenuList import MenuList
10 from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest
11 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
12 from Tools.LoadPixmap import LoadPixmap
13
14 class VirtualKeyBoardList(MenuList):
15         def __init__(self, list, enableWrapAround=False):
16                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
17                 self.l.setFont(0, gFont("Regular", 28))
18                 self.l.setItemHeight(45)
19
20 def VirtualKeyBoardEntryComponent(keys, selectedKey,shiftMode=False):
21         key_backspace = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_backspace.png"))
22         key_bg = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_bg.png"))
23         key_clr = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_clr.png"))
24         key_esc = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_esc.png"))
25         key_ok = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_ok.png"))
26         key_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_sel.png"))
27         key_shift = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_shift.png"))
28         key_shift_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_shift_sel.png"))
29         key_space = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_space.png"))
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                 width = None
40                 if key == "EXIT":
41                         width = key_esc.size().width()
42                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_esc))
43                 elif key == "BACKSPACE":
44                         width = key_backspace.size().width()
45                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_backspace))
46                 elif key == "CLEAR":
47                         width = key_clr.size().width()
48                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_clr))
49                 elif key == "SHIFT":
50                         width = shiftkey_png.size().width()
51                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=shiftkey_png))
52                 elif key == "SPACE":
53                         width = key_space.size().width()
54                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_space))
55                 elif key == "OK":
56                         width = key_ok.size().width()
57                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_ok))
58                 #elif key == "<-":
59                 #       res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_left))
60                 #elif key == "->":
61                 #       res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(45, 45), png=key_right))
62                 
63                 else:
64                         width = key_bg.size().width()
65                         res.extend((
66                                 MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_bg),
67                                 MultiContentEntryText(pos=(x, 0), size=(width, 45), font=0, text=key.encode("utf-8"), flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER)
68                         ))
69                 
70                 if selectedKey == count:
71                         width = key_sel.size().width()
72                         res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, 45), png=key_sel))
73
74                 if width is not None:
75                         x += width
76                 else:
77                         x += 45
78                 count += 1
79         
80         return res
81
82
83 class VirtualKeyBoard(Screen):
84
85         def __init__(self, session, title="", text=""):
86                 Screen.__init__(self, session)
87                 self.keys_list = []
88                 self.shiftkeys_list = []
89                 self.lang = language.getLanguage()
90                 self.nextLang = None
91                 self.shiftMode = False
92                 self.text = text
93                 self.selectedKey = 0
94                 
95                 self["country"] = StaticText("")
96                 self["header"] = Label(title)
97                 self["text"] = Label(self.text)
98                 self["list"] = VirtualKeyBoardList([])
99                 
100                 self["actions"] = ActionMap(["OkCancelActions", "WizardActions", "ColorActions", "KeyboardInputActions", "InputBoxActions", "InputAsciiActions"],
101                         {
102                                 "gotAsciiCode": self.keyGotAscii,
103                                 "ok": self.okClicked,
104                                 "cancel": self.exit,
105                                 "left": self.left,
106                                 "right": self.right,
107                                 "up": self.up,
108                                 "down": self.down,
109                                 "red": self.backClicked,
110                                 "green": self.ok,
111                                 "yellow": self.switchLang,
112                                 "deleteBackward": self.backClicked,
113                                 "back": self.exit                               
114                         }, -2)
115                 self.setLanq()
116                 self.onExecBegin.append(self.setKeyboardModeAscii)
117                 self.onLayoutFinish.append(self.buildVirtualKeyBoard)
118         
119         def switchLang(self):
120                 self.lang = self.nextLang
121                 self.setLanq()
122                 self.buildVirtualKeyBoard()
123
124         def setLanq(self):
125                 if self.lang == 'de_DE':
126                         self.keys_list = [
127                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
128                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ü", u"+"],
129                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],
130                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
131                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"OK"]]
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"Ü", u"*"],
135                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", 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"?", u"\\", u"OK"]]
138                         self.nextLang = 'es_ES'
139                 elif self.lang == 'es_ES':
140                         #still missing keys (u"ùÙ")
141                         self.keys_list = [
142                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
143                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ú", u"+"],
144                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ó", u"á", u"#"],
145                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
146                                 [u"SHIFT", u"SPACE", u"@", u"Ł", u"ŕ", u"é", u"č", u"í", u"ě", u"ń", u"ň", u"OK"]]
147                         self.shiftkeys_list = [
148                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
149                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"Ú", u"*"],
150                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ó", u"Á", u"'"],
151                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
152                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"Ŕ", u"É", u"Č", u"Í", u"Ě", u"Ń", u"Ň", u"OK"]]
153                         self.nextLang = 'fi_FI'
154                 elif self.lang == 'fi_FI':
155                         self.keys_list = [
156                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
157                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"é", u"+"],
158                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],
159                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
160                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"ĺ", u"OK"]]
161                         self.shiftkeys_list = [
162                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
163                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"É", u"*"],
164                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],
165                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
166                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"Ĺ", u"OK"]]
167                         self.nextLang = 'sv_SE'
168                 elif self.lang == 'sv_SE':
169                         self.keys_list = [
170                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
171                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"é", u"+"],
172                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],
173                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
174                                 [u"SHIFT", u"SPACE", u"@", u"ß", u"ĺ", u"OK"]]
175                         self.shiftkeys_list = [
176                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
177                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"É", u"*"],
178                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],
179                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
180                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"Ĺ", u"OK"]]
181                         self.nextLang = 'sk_SK'
182                 elif self.lang =='sk_SK':
183                         self.keys_list = [
184                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
185                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ú", u"+"],
186                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ľ", u"@", u"#"],
187                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
188                                 [u"SHIFT", u"SPACE", u"š", u"č", u"ž", u"ý", u"á", u"í", u"é", u"OK"]]
189                         self.shiftkeys_list = [
190                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
191                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"ť", u"*"],
192                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"ň", u"ď", u"'"],
193                                 [u"Á", u"É", u"Ď", u"Í", u"Ý", u"Ó", u"Ú", u"Ž", u"Š", u"Č", u"Ť", u"Ň"],
194                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
195                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"ä", u"ö", u"ü", u"ô", u"ŕ", u"ĺ", u"OK"]]
196                         self.nextLang = 'cs_CZ'
197                 elif self.lang == 'cs_CZ':
198                         self.keys_list = [
199                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
200                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ú", u"+"],
201                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ů", u"@", u"#"],
202                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
203                                 [u"SHIFT", u"SPACE", u"ě", u"š", u"č", u"ř", u"ž", u"ý", u"á", u"í", u"é", u"OK"]]
204                         self.shiftkeys_list = [
205                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
206                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"ť", u"*"],
207                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"ň", u"ď", u"'"],
208                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
209                                 [u"SHIFT", u"SPACE", u"?", u"\\", u"Č", u"Ř", u"Š", u"Ž", u"Ú", u"Á", u"É", u"OK"]]
210                         self.nextLang = 'en_EN'
211                 else:
212                         self.keys_list = [
213                                 [u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
214                                 [u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"+", u"@"],
215                                 [u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"#", u"\\"],
216                                 [u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
217                                 [u"SHIFT", u"SPACE", u"OK"]]
218                         self.shiftkeys_list = [
219                                 [u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
220                                 [u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"*"],
221                                 [u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"'", u"?"],
222                                 [u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
223                                 [u"SHIFT", u"SPACE", u"OK"]]
224                         self.lang = 'en_EN'
225                         self.nextLang = 'de_DE'         
226                 self["country"].setText(self.lang)
227                 self.max_key=47+len(self.keys_list[4])
228
229         def buildVirtualKeyBoard(self, selectedKey=0):
230                 list = []
231                 
232                 if self.shiftMode:
233                         self.k_list = self.shiftkeys_list
234                         for keys in self.k_list:
235                                 if selectedKey < 12 and selectedKey > -1:
236                                         list.append(VirtualKeyBoardEntryComponent(keys, selectedKey,True))
237                                 else:
238                                         list.append(VirtualKeyBoardEntryComponent(keys, -1,True))
239                                 selectedKey -= 12
240                 else:
241                         self.k_list = self.keys_list
242                         for keys in self.k_list:
243                                 if selectedKey < 12 and selectedKey > -1:
244                                         list.append(VirtualKeyBoardEntryComponent(keys, selectedKey))
245                                 else:
246                                         list.append(VirtualKeyBoardEntryComponent(keys, -1))
247                                 selectedKey -= 12
248                 
249                 self["list"].setList(list)
250         
251         def backClicked(self):
252                 self.text = self["text"].getText()[:-1]
253                 self["text"].setText(self.text)
254                         
255         def okClicked(self):
256                 if self.shiftMode:
257                         list = self.shiftkeys_list
258                 else:
259                         list = self.keys_list
260                 
261                 selectedKey = self.selectedKey
262
263                 text = None
264
265                 for x in list:
266                         if selectedKey < 12:
267                                 if selectedKey < len(x):
268                                         text = x[selectedKey]
269                                 break
270                         else:
271                                 selectedKey -= 12
272
273                 if text is None:
274                         return
275
276                 text = text.encode("UTF-8")
277
278                 if text == "EXIT":
279                         self.close(None)
280                 
281                 elif text == "BACKSPACE":
282                         self.text = self["text"].getText()[:-1]
283                         self["text"].setText(self.text)
284                 
285                 elif text == "CLEAR":
286                         self.text = ""
287                         self["text"].setText(self.text)
288                 
289                 elif text == "SHIFT":
290                         if self.shiftMode:
291                                 self.shiftMode = False
292                         else:
293                                 self.shiftMode = True
294                         
295                         self.buildVirtualKeyBoard(self.selectedKey)
296                 
297                 elif text == "SPACE":
298                         self.text += " "
299                         self["text"].setText(self.text)
300                 
301                 elif text == "OK":
302                         self.close(self["text"].getText())
303                 
304                 else:
305                         self.text = self["text"].getText()
306                         self.text += text
307                         self["text"].setText(self.text)
308
309         def ok(self):
310                 self.close(self["text"].getText())
311
312         def exit(self):
313                 self.close(None)
314
315         def left(self):
316                 self.selectedKey -= 1
317                 
318                 if self.selectedKey == -1:
319                         self.selectedKey = 11
320                 elif self.selectedKey == 11:
321                         self.selectedKey = 23
322                 elif self.selectedKey == 23:
323                         self.selectedKey = 35
324                 elif self.selectedKey == 35:
325                         self.selectedKey = 47
326                 elif self.selectedKey == 47:
327                         self.selectedKey = self.max_key
328                 
329                 self.showActiveKey()
330
331         def right(self):
332                 self.selectedKey += 1
333                 
334                 if self.selectedKey == 12:
335                         self.selectedKey = 0
336                 elif self.selectedKey == 24:
337                         self.selectedKey = 12
338                 elif self.selectedKey == 36:
339                         self.selectedKey = 24
340                 elif self.selectedKey == 48:
341                         self.selectedKey = 36
342                 elif self.selectedKey > self.max_key:
343                         self.selectedKey = 48
344                 
345                 self.showActiveKey()
346
347         def up(self):
348                 self.selectedKey -= 12
349                 
350                 if (self.selectedKey < 0) and (self.selectedKey > (self.max_key-60)):
351                         self.selectedKey += 48
352                 elif self.selectedKey < 0:
353                         self.selectedKey += 60  
354                 
355                 self.showActiveKey()
356
357         def down(self):
358                 self.selectedKey += 12
359                 
360                 if (self.selectedKey > self.max_key) and (self.selectedKey > 59):
361                         self.selectedKey -= 60
362                 elif self.selectedKey > self.max_key:
363                         self.selectedKey -= 48
364                 
365                 self.showActiveKey()
366
367         def showActiveKey(self):
368                 self.buildVirtualKeyBoard(self.selectedKey)
369
370         def inShiftKeyList(self,key):
371                 for KeyList in self.shiftkeys_list:
372                         for char in KeyList:
373                                 if char == key:
374                                         return True
375                 return False
376
377         def keyGotAscii(self):
378                 char = str(unichr(getPrevAsciiCode()).encode('utf-8'))
379                 if self.inShiftKeyList(char):
380                         self.shiftMode = True
381                         list = self.shiftkeys_list
382                 else:
383                         self.shiftMode = False
384                         list = self.keys_list   
385
386                 selkey = 0
387                 for keylist in list:
388                         for key in keylist:
389                                 if key == char:
390                                         self.selectedKey = selkey
391                                         self.okClicked()
392                                         self.showActiveKey()
393                                         return
394                                 else:
395                                         selkey += 1