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