Add windowTitle when open ChoiceBox
[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                         self.windowTitle = _("Select")
96                 if not self.getSkinTitle():
97                         self.windowTitle = ""
98                 self.setTitle(self.windowTitle)
99
100         def autoResize(self):
101                 orgwidth = self.instance.size().width()
102                 orgpos = self.instance.position()
103                 textsize = self["text"].getSize()
104                 count = len(self.list)
105                 if count > 10:
106                         count = 10
107                 offset = 25 * count
108                 wsizex = textsize[0] + 60
109                 wsizey = textsize[1] + offset
110                 if (520 > wsizex):
111                         wsizex = 520
112                 wsize = (wsizex, wsizey)
113                 # resize
114                 self.instance.resize(enigma.eSize(*wsize))
115                 # resize label
116                 self["text"].instance.resize(enigma.eSize(*textsize))
117                 # move list
118                 listsize = (wsizex, 25 * count)
119                 self["list"].instance.move(enigma.ePoint(0, textsize[1]))
120                 self["list"].instance.resize(enigma.eSize(*listsize))
121                 # center window
122                 newwidth = wsize[0]
123                 self.instance.move(enigma.ePoint((720-wsizex)/2, (576-wsizey)/(count > 7 and 2 or 3)))
124
125         def keyLeft(self):
126                 pass
127
128         def keyRight(self):
129                 pass
130
131         def up(self):
132                 if len(self["list"].list) > 0:
133                         while 1:
134                                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
135                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
136                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == 0:
137                                         break
138
139         def down(self):
140                 if len(self["list"].list) > 0:
141                         while 1:
142                                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
143                                 self.updateSummary(self["list"].l.getCurrentSelectionIndex())
144                                 if self["list"].l.getCurrentSelection()[0][0] != "--" or self["list"].l.getCurrentSelectionIndex() == len(self["list"].list) - 1:
145                                         break
146
147         # runs a number shortcut
148         def keyNumberGlobal(self, number):
149                 self.goKey(str(number))
150
151         # runs the current selected entry
152         def go(self):
153                 cursel = self["list"].l.getCurrentSelection()
154                 if cursel:
155                         self.goEntry(cursel[0])
156                 else:
157                         self.cancel()
158
159         # runs a specific entry
160         def goEntry(self, entry):
161                 if len(entry) > 2 and isinstance(entry[1], str) and entry[1] == "CALLFUNC":
162                         # CALLFUNC wants to have the current selection as argument
163                         arg = self["list"].l.getCurrentSelection()[0]
164                         entry[2](arg)
165                 else:
166                         self.close(entry)
167
168         # lookups a key in the keymap, then runs it
169         def goKey(self, key):
170                 if self.keymap.has_key(key):
171                         entry = self.keymap[key]
172                         self.goEntry(entry)
173
174         # runs a color shortcut
175         def keyRed(self):
176                 self.goKey("red")
177
178         def keyGreen(self):
179                 self.goKey("green")
180
181         def keyYellow(self):
182                 self.goKey("yellow")
183
184         def keyBlue(self):
185                 self.goKey("blue")
186
187         def updateSummary(self, curpos=0):
188                 pos = 0
189                 summarytext = ""
190                 for entry in self.summarylist:
191                         if pos > curpos-2 and pos < curpos+5:
192                                 if pos == curpos:
193                                         summarytext += ">"
194                                         self["summary_selection"].setText(entry[1])
195                                 else:
196                                         summarytext += entry[0]
197                                 summarytext += ' ' + entry[1] + '\n'
198                         pos += 1
199                 self["summary_list"].setText(summarytext)
200
201         def cancel(self):
202                 self.close(None)
203
204         def setDefaultChoiceList(self):
205                 if self.reorderConfig:
206                         if len(self.list) > 0 and self.config_type.value != "":
207                                 self.session.openWithCallback(self.setDefaultChoiceListCallback, MessageBox, _("Sort list to default and exit?"), MessageBox.TYPE_YESNO)
208                 else:
209                         self.cancel()
210
211         def setDefaultChoiceListCallback(self, answer):
212                 if answer:
213                         self.config_type.value = ""
214                         self.config_type.save()
215                         self.cancel()
216
217         def additionalMoveUp(self):
218                 if self.reorderConfig:
219                         self.additionalMove(-1)
220
221         def additionalMoveDown(self):
222                 if self.reorderConfig:
223                         self.additionalMove(1)
224
225         def additionalMove(self, direction):
226                 if len(self.list) > 1:
227                         currentIndex = self["list"].getSelectionIndex()
228                         swapIndex = (currentIndex + direction) % len(self.list)
229                         if currentIndex == 0 and swapIndex != 1:
230                                 self.list = self.list[1:] + [self.list[0]]
231                         elif swapIndex == 0 and currentIndex != 1:
232                                 self.list = [self.list[-1]] + self.list[:-1]
233                         else:
234                                 self.list[currentIndex], self.list[swapIndex] = self.list[swapIndex], self.list[currentIndex]
235                         self["list"].l.setList(self.list)
236                         if direction == 1:
237                                 self["list"].down()
238                         else:
239                                 self["list"].up()
240                         self.config_type.value = ",".join(x[0][0] for x in self.list)
241                         self.config_type.save()