GUISkin: add getSkinTitle
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / ChoiceBox.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3 from Components.ActionMap import NumberActionMap
4 from Components.config import config, ConfigSubsection, ConfigText
5 from Components.Label import Label
6 from Components.ChoiceList import ChoiceEntryComponent, ChoiceList
7 from Components.Sources.StaticText import StaticText
8 import enigma
9
10 config.misc.pluginlist = ConfigSubsection()
11 config.misc.pluginlist.eventinfo_order = ConfigText(default="")
12 config.misc.pluginlist.extension_order = ConfigText(default="")
13
14 class ChoiceBox(Screen):
15         def __init__(self, session, title="", list=[], keys=None, selection=0, skin_name=[], reorderConfig="", windowTitle=None):
16                 Screen.__init__(self, session)
17
18                 if isinstance(skin_name, str):
19                         skin_name = [skin_name]
20                 self.skinName = skin_name + ["ChoiceBox"]
21
22                 self.reorderConfig = reorderConfig
23                 self["text"] = Label(title)
24                 self.list = []
25                 self.summarylist = []
26                 if keys is None:
27                         self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ] + (len(list) - 14) * [""]
28                 else:
29                         self.__keys = keys + (len(list) - len(keys)) * [""]
30
31                 self.keymap = {}
32                 pos = 0
33                 if self.reorderConfig:
34                         self.config_type = eval("config.misc.pluginlist." + self.reorderConfig)
35                         if self.config_type.value:
36                                 prev_list = zip(list, self.__keys)
37                                 new_list = []
38                                 for x in self.config_type.value.split(","):
39                                         for entry in prev_list:
40                                                 if entry[0][0] == x:
41                                                         new_list.append(entry)
42                                                         prev_list.remove(entry)
43                                 list = zip(*(new_list + prev_list))
44                                 list, self.__keys = list[0], list[1]
45                                 number = 1
46                                 new_keys = []
47                                 for x in self.__keys:
48                                         if (not x or x.isdigit()) and number <= 10:
49                                                 new_keys.append(str(number % 10))
50                                                 number+=1
51                                         else:
52                                                 new_keys.append(not x.isdigit() and x or "")
53                                 self.__keys = new_keys
54                 for x in list:
55                         strpos = str(self.__keys[pos])
56                         self.list.append(ChoiceEntryComponent(key = strpos, text = x))
57                         if self.__keys[pos] != "":
58                                 self.keymap[self.__keys[pos]] = list[pos]
59                         self.summarylist.append((self.__keys[pos],x[0]))
60                         pos += 1
61                 self["list"] = ChoiceList(list = self.list, selection = selection)
62                 self["summary_list"] = StaticText()
63                 self["summary_selection"] = StaticText()
64                 self.updateSummary(selection)
65
66                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions", "MenuActions"],
67                 {
68                         "ok": self.go,
69                         "back": self.cancel,
70                         "1": self.keyNumberGlobal,
71                         "2": self.keyNumberGlobal,
72                         "3": self.keyNumberGlobal,
73                         "4": self.keyNumberGlobal,
74                         "5": self.keyNumberGlobal,
75                         "6": self.keyNumberGlobal,
76                         "7": self.keyNumberGlobal,
77                         "8": self.keyNumberGlobal,
78                         "9": self.keyNumberGlobal,
79                         "0": self.keyNumberGlobal,
80                         "red": self.keyRed,
81                         "green": self.keyGreen,
82                         "yellow": self.keyYellow,
83                         "blue": self.keyBlue,
84                         "up": self.up,
85                         "down": self.down,
86                         "moveUp": self.additionalMoveUp,
87                         "moveDown": self.additionalMoveDown,
88                         "menu": self.setDefaultChoiceList
89                 }, -1)
90                 self.windowTitle = windowTitle
91                 self.onLayoutFinish.append(self.layoutFinished)
92
93         def layoutFinished(self):
94                 if self.windowTitle is None:
95                         if self.getSkinTitle():
96                                 self.windowTitle = _("Select")
97                         else:
98                                 self.windowTitle = ""
99                 self.setTitle(self.windowTitle)
100
101         def autoResize(self):
102                 orgwidth = self.instance.size().width()
103                 orgpos = self.instance.position()
104                 textsize = self["text"].getSize()
105                 count = len(self.list)
106                 if count > 10:
107                         count = 10
108                 offset = 25 * count
109                 wsizex = textsize[0] + 60
110                 wsizey = textsize[1] + offset
111                 if (520 > wsizex):
112                         wsizex = 520
113                 wsize = (wsizex, wsizey)
114                 # resize
115                 self.instance.resize(enigma.eSize(*wsize))
116                 # resize label
117                 self["text"].instance.resize(enigma.eSize(*textsize))
118                 # move list
119                 listsize = (wsizex, 25 * count)
120                 self["list"].instance.move(enigma.ePoint(0, textsize[1]))
121                 self["list"].instance.resize(enigma.eSize(*listsize))
122                 # center window
123                 newwidth = wsize[0]
124                 self.instance.move(enigma.ePoint((720-wsizex)/2, (576-wsizey)/(count > 7 and 2 or 3)))
125
126         def keyLeft(self):
127                 pass
128
129         def keyRight(self):
130                 pass
131
132         def up(self):
133                 if len(self["list"].list) > 0:
134                         while 1:
135                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
136                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
137                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
138                                         break
139
140         def down(self):
141                 if len(self["list"].list) > 0:
142                         while 1:
143                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
144                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
145                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
146                                         break
147
148         # runs a number shortcut
149         def keyNumberGlobal(self, number):
150                 self.goKey(str(number))
151
152         # runs the current selected entry
153         def go(self):
154                 cursel = self["list"].l.getCurrentSelection()
155                 if cursel:
156                         self.goEntry(cursel[0])
157                 else:
158                         self.cancel()
159
160         # runs a specific entry
161         def goEntry(self, entry):
162                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
163                         # CALLFUNC wants to have the current selection as argument
164                         arg = self["list"].l.getCurrentSelection()[0]
165                         entry[2](arg)
166                 else:
167                         self.close(entry)
168
169         # lookups a key in the keymap, then runs it
170         def goKey(self, key):
171                 if self.keymap.has_key(key):
172                         entry = self.keymap[key]
173                         self.goEntry(entry)
174
175         # runs a color shortcut
176         def keyRed(self):
177                 self.goKey("red")
178
179         def keyGreen(self):
180                 self.goKey("green")
181
182         def keyYellow(self):
183                 self.goKey("yellow")
184
185         def keyBlue(self):
186                 self.goKey("blue")
187
188         def updateSummary(self, curpos=0):
189                 pos = 0
190                 summarytext = ""
191                 for entry in self.summarylist:
192                         if pos > curpos-2 and pos < curpos+5:
193                                 if pos == curpos:
194                                         summarytext += ">"
195                                         self["summary_selection"].setText(entry[1])
196                                 else:
197                                         summarytext += entry[0]
198                                 summarytext += ' ' + entry[1] + '\n'
199                         pos += 1
200                 self["summary_list"].setText(summarytext)
201
202         def cancel(self):
203                 self.close(None)
204
205         def setDefaultChoiceList(self):
206                 if self.reorderConfig:
207                         if len(self.list) > 0 and self.config_type.value != "":
208                                 self.session.openWithCallback(self.setDefaultChoiceListCallback, MessageBox, _("Sort list to default and exit?"), MessageBox.TYPE_YESNO)
209                 else:
210                         self.cancel()
211
212         def setDefaultChoiceListCallback(self, answer):
213                 if answer:
214                         self.config_type.value = ""
215                         self.config_type.save()
216                         self.cancel()
217
218         def additionalMoveUp(self):
219                 if self.reorderConfig:
220                         self.additionalMove(-1)
221
222         def additionalMoveDown(self):
223                 if self.reorderConfig:
224                         self.additionalMove(1)
225
226         def additionalMove(self, direction):
227                 if len(self.list) > 1:
228                         currentIndex = self["list"].getSelectionIndex()
229                         swapIndex = (currentIndex + direction) % len(self.list)
230                         if currentIndex == 0 and swapIndex != 1:
231                                 self.list = self.list[1:] + [self.list[0]]
232                         elif swapIndex == 0 and currentIndex != 1:
233                                 self.list = [self.list[-1]] + self.list[:-1]
234                         else:
235                                 self.list[currentIndex], self.list[swapIndex] = self.list[swapIndex], self.list[currentIndex]
236                         self["list"].l.setList(self.list)
237                         if direction == 1:
238                                 self["list"].down()
239                         else:
240                                 self["list"].up()
241                         self.config_type.value = ",".join(x[0][0] for x in self.list)
242                         self.config_type.save()