[RecordTimer] fix repeat timer issue.
[vuplus_dvbapp] / lib / python / Screens / ChoiceBox.py
1 from Screens.Screen import Screen
2 from Components.ActionMap import NumberActionMap
3 from Components.Label import Label
4 from Components.ChoiceList import ChoiceEntryComponent, ChoiceList
5 from Components.Sources.StaticText import StaticText
6
7 class ChoiceBox(Screen):
8         def __init__(self, session, title = "", list = [], keys = None, selection = 0, skin_name = []):
9                 Screen.__init__(self, session)
10
11                 if isinstance(skin_name, str):
12                         skin_name = [skin_name]
13                 self.skinName = skin_name + ["ChoiceBox"] 
14                 self.lcd_xres = None
15                 self.lcd_xres=self.readlcdxres()
16                 self["text"] = Label(title)
17                 self.list = []
18                 self.summarylist = []
19                 if keys is None:
20                         self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 10) * [""]
21                 else:
22                         self.__keys = keys + (len(list) - len(keys)) * [""]
23                         
24                 self.keymap = {}
25                 pos = 0
26                 for x in list:
27                         strpos = str(self.__keys[pos])
28                         self.list.append(ChoiceEntryComponent(key = strpos, text = x))
29                         if self.__keys[pos] != "":
30                                 self.keymap[self.__keys[pos]] = list[pos]
31                         self.summarylist.append((self.__keys[pos],x[0]))
32                         pos += 1
33                 self["list"] = ChoiceList(list = self.list, selection = selection)
34                 self["summary_list"] = StaticText()
35                 self.updateSummary(selection)
36                                 
37                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
38                 {
39                         "ok": self.go,
40                         "back": self.cancel,
41                         "1": self.keyNumberGlobal,
42                         "2": self.keyNumberGlobal,
43                         "3": self.keyNumberGlobal,
44                         "4": self.keyNumberGlobal,
45                         "5": self.keyNumberGlobal,
46                         "6": self.keyNumberGlobal,
47                         "7": self.keyNumberGlobal,
48                         "8": self.keyNumberGlobal,
49                         "9": self.keyNumberGlobal,
50                         "0": self.keyNumberGlobal,
51                         "red": self.keyRed,
52                         "green": self.keyGreen,
53                         "yellow": self.keyYellow,
54                         "blue": self.keyBlue,
55                         "up": self.up,
56                         "down": self.down
57                 }, -1)
58                 
59         def keyLeft(self):
60                 pass
61         
62         def keyRight(self):
63                 pass
64         
65         def up(self):
66                 if len(self["list"].list) > 0:
67                         while 1:
68                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
69                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
70                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
71                                         break
72
73         def down(self):
74                 if len(self["list"].list) > 0:
75                         while 1:
76                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
77                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
78                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
79                                         break
80
81         # runs a number shortcut
82         def keyNumberGlobal(self, number):
83                 self.goKey(str(number))
84
85         # runs the current selected entry
86         def go(self):
87                 cursel = self["list"].l.getCurrentSelection()
88                 if cursel:
89                         self.goEntry(cursel[0])
90                 else:
91                         self.cancel()
92
93         # runs a specific entry
94         def goEntry(self, entry):
95                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
96                         # CALLFUNC wants to have the current selection as argument
97                         arg = self["list"].l.getCurrentSelection()[0]
98                         entry[2](arg)
99                 else:
100                         self.close(entry)
101
102         # lookups a key in the keymap, then runs it
103         def goKey(self, key):
104                 if self.keymap.has_key(key):
105                         entry = self.keymap[key]
106                         self.goEntry(entry)
107
108         # runs a color shortcut
109         def keyRed(self):
110                 self.goKey("red")
111
112         def keyGreen(self):
113                 self.goKey("green")
114
115         def keyYellow(self):
116                 self.goKey("yellow")
117
118         def keyBlue(self):
119                 self.goKey("blue")
120
121         def updateSummary(self, curpos=0):
122                 pos = 0
123                 summarytext = ""
124                 for entry in self.summarylist:
125                         if self.lcd_xres is not None and self.lcd_xres > 140:
126                                 if pos > curpos-2 and pos < curpos+5:
127                                         if pos == curpos:
128                                                 summarytext += ">"
129                                         else:
130                                                 summarytext += entry[0]
131                                         summarytext += ' ' + entry[1] + '\n'
132                         else:
133                                 if pos == curpos:
134                                         summarytext += entry[0]+' '+ entry[1]
135                         pos += 1
136                 self["summary_list"].setText(summarytext)
137
138         def cancel(self):
139                 self.close(None)
140
141         def readlcdxres(self):
142                 try:
143                         fd = open("/proc/stb/lcd/xres","r")
144                         value = int(fd.read().strip(),16)
145                         fd.close()
146                         return value
147                 except:
148                         return None