InputDeviceSetup: use 20% of list with for sliders
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / InputDeviceSetup.py
1 from Screen import Screen
2 from Screens.HelpMenu import HelpableScreen
3 from Screens.MessageBox import MessageBox
4 from Components.InputDevice import iInputDevices, iRcTypeControl
5 from Components.Sources.StaticText import StaticText
6 from Components.Sources.List import List
7 from Components.config import config, ConfigYesNo, getConfigListEntry, ConfigSelection
8 from Components.ConfigList import ConfigListScreen
9 from Components.ActionMap import ActionMap, HelpableActionMap
10 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
11 from Tools.LoadPixmap import LoadPixmap
12
13 class InputDeviceSelection(Screen,HelpableScreen):
14         skin = """
15         <screen name="InputDeviceSelection" position="center,center" size="560,400" title="Select input device">
16                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on"/>
17                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on"/>
18                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on"/>
19                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on"/>
20                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1"/>
21                 <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1"/>
22                 <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1"/>
23                 <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1"/>
24                 <widget source="list" render="Listbox" position="5,50" size="550,280" zPosition="10" scrollbarMode="showOnDemand">
25                         <convert type="TemplatedMultiContent">
26                         <!--  device, description, devicepng, divpng  -->
27                                                         {"template": [
28                                                                         MultiContentEntryPixmapAlphaTest(pos = (2, 8), size = (54, 54), png = 2), # index 3 is the interface pixmap
29                                                                         MultiContentEntryText(pos = (65, 6), size = (450, 54), font=0, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER|RT_WRAP, text = 1), # index 1 is the interfacename
30                                                                 ],
31                                                         "fonts": [gFont("Regular", 28),gFont("Regular", 20)],
32                                                         "itemHeight": 70
33                                                         }
34                         </convert>
35                 </widget>
36                 <ePixmap pixmap="skin_default/div-h.png" position="0,340" zPosition="1" size="560,2"/>
37                 <widget source="introduction" render="Label" position="0,350" size="560,50" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1"/>
38         </screen>"""
39
40
41         def __init__(self, session):
42                 Screen.__init__(self, session)
43                 HelpableScreen.__init__(self)
44
45                 self.edittext = _("Press OK to edit the settings.")
46
47                 self["key_red"] = StaticText(_("Close"))
48                 self["key_green"] = StaticText(_("Select"))
49                 self["key_yellow"] = StaticText("")
50                 self["key_blue"] = StaticText("")
51                 self["introduction"] = StaticText(self.edittext)
52
53                 self.devices = [(iInputDevices.getDeviceName(x),x) for x in iInputDevices.getDeviceList()]
54                 print "[InputDeviceSelection] found devices :->", len(self.devices),self.devices
55
56                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
57                         {
58                         "cancel": (self.close, _("Exit input device selection.")),
59                         "ok": (self.okbuttonClick, _("Select input device.")),
60                         }, -2)
61
62                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
63                         {
64                         "red": (self.close, _("Exit input device selection.")),
65                         "green": (self.okbuttonClick, _("Select input device.")),
66                         }, -2)
67
68                 self.currentIndex = 0
69                 self.list = []
70                 self["list"] = List(self.list)
71                 self.updateList()
72                 self.onLayoutFinish.append(self.layoutFinished)
73                 self.onClose.append(self.cleanup)
74
75         def layoutFinished(self):
76                 self.setTitle(_("Select input device"))
77
78         def cleanup(self):
79                 self.currentIndex = 0
80
81         def buildInterfaceList(self, device, description, type, isinputdevice = True):
82                 divpng = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/div-h.png"))
83                 activepng = None
84                 devicepng = None
85                 enabled = iInputDevices.getDeviceAttribute(device, 'enabled')
86
87                 if type == 'remote':
88                         if config.misc.rcused.value == 0:
89                                 if enabled:
90                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew-configured.png"))
91                                 else:
92                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew.png"))
93                         else:
94                                 if enabled:
95                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcold-configured.png"))
96                                 else:
97                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcold.png"))
98                 elif type == 'keyboard':
99                         if enabled:
100                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_keyboard-configured.png"))
101                         else:
102                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_keyboard.png"))
103                 elif type == 'mouse':
104                         if enabled:
105                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_mouse-configured.png"))
106                         else:
107                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_mouse.png"))
108                 elif isinputdevice:
109                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew.png"))
110                 return ((device, description, devicepng, divpng))
111
112         def updateList(self):
113                 self.list = []
114
115                 if iRcTypeControl.multipleRcSupported():
116                         self.list.append(self.buildInterfaceList('rctype', _('Configure remote control type'), None, False))
117
118                 for x in self.devices:
119                         dev_type = iInputDevices.getDeviceAttribute(x[1], 'type')
120                         self.list.append(self.buildInterfaceList(x[1],_(x[0]), dev_type))
121
122                 self["list"].setList(self.list)
123                 self["list"].setIndex(self.currentIndex)
124
125         def okbuttonClick(self):
126                 selection = self["list"].getCurrent()
127                 self.currentIndex = self["list"].getIndex()
128                 if selection is not None:
129                         if selection[0] == 'rctype':
130                                 self.session.open(RemoteControlType)
131                         else:
132                                 self.session.openWithCallback(self.DeviceSetupClosed, InputDeviceSetup, selection[0])
133
134         def DeviceSetupClosed(self, *ret):
135                 self.updateList()
136
137
138 class InputDeviceSetup(Screen, ConfigListScreen):
139
140         skin = """
141                 <screen name="InputDeviceSetup" position="center,center" size="560,440" title="Input device setup">
142                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
143                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
144                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
145                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
146                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
147                         <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
148                         <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
149                         <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
150                         <widget name="config" position="5,50" size="550,350" scrollbarMode="showOnDemand" />
151                         <ePixmap pixmap="skin_default/div-h.png" position="0,400" zPosition="1" size="560,2" />
152                         <widget source="introduction" render="Label" position="5,410" size="550,30" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1" />
153                 </screen>"""
154
155         def __init__(self, session, device):
156                 Screen.__init__(self, session)
157                 self.inputDevice = device
158                 iInputDevices.currentDevice = self.inputDevice
159                 self.onChangedEntry = [ ]
160                 self.setup_title = _("Input device setup")
161                 self.isStepSlider = None
162                 self.enableEntry = None
163                 self.repeatEntry = None
164                 self.delayEntry = None
165                 self.nameEntry = None
166                 self.enableConfigEntry = None
167
168                 self.list = [ ]
169                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
170
171                 self["actions"] = ActionMap(["SetupActions", "MenuActions"],
172                         {
173                                 "cancel": self.keyCancel,
174                                 "save": self.apply,
175                                 "menu": self.closeRecursive,
176                         }, -2)
177
178                 self["key_red"] = StaticText(_("Cancel"))
179                 self["key_green"] = StaticText(_("OK"))
180                 self["key_yellow"] = StaticText()
181                 self["key_blue"] = StaticText()
182                 self["introduction"] = StaticText()
183
184                 self.createSetup()
185                 self.onLayoutFinish.append(self.layoutFinished)
186                 self.onClose.append(self.cleanup)
187
188         def layoutFinished(self):
189                 self.setTitle(self.setup_title)
190                 listWidth = self["config"].l.getItemSize().width()
191                 # use 20% of list width for sliders
192                 self["config"].l.setSeperation(int(listWidth*.8))
193
194         def cleanup(self):
195                 iInputDevices.currentDevice = ""
196
197         def createSetup(self):
198                 label = _("Change repeat and delay settings?")
199                 cmd = "self.enableEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".enabled)"
200                 exec (cmd)
201                 label = _("Interval between keys when repeating:")
202                 cmd = "self.repeatEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".repeat)"
203                 exec (cmd)
204                 label = _("Delay before key repeat starts:")
205                 cmd = "self.delayEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".delay)"
206                 exec (cmd)
207                 label = _("Devicename:")
208                 cmd = "self.nameEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".name)"
209                 exec (cmd)
210                 if self.enableEntry:
211                         if isinstance(self.enableEntry[1], ConfigYesNo):
212                                 self.enableConfigEntry = self.enableEntry[1]
213
214                 self.list.append(self.enableEntry)
215                 if self.enableConfigEntry:
216                         if self.enableConfigEntry.value is True:
217                                 self.list.append(self.repeatEntry)
218                                 self.list.append(self.delayEntry)
219                         else:
220                                 self.repeatEntry[1].setValue(self.repeatEntry[1].default)
221                                 self["config"].invalidate(self.repeatEntry)
222                                 self.delayEntry[1].setValue(self.delayEntry[1].default)
223                                 self["config"].invalidate(self.delayEntry)
224                                 self.nameEntry[1].setValue(self.nameEntry[1].default)
225                                 self["config"].invalidate(self.nameEntry)
226
227                 self["config"].list = self.list
228                 self["config"].l.setList(self.list)
229                 if not self.selectionChanged in self["config"].onSelectionChanged:
230                         self["config"].onSelectionChanged.append(self.selectionChanged)
231                 self.selectionChanged()
232
233         def selectionChanged(self):
234                 if self["config"].getCurrent() == self.enableEntry:
235                         self["introduction"].setText(_("Current device: ") + str(iInputDevices.getDeviceAttribute(self.inputDevice, 'name')) )
236                 else:
237                         self["introduction"].setText(_("Current value: ") + self.getCurrentValue() + _(" ms"))
238
239         def newConfig(self):
240                 current = self["config"].getCurrent()
241                 if current:
242                         if current == self.enableEntry:
243                                 self.createSetup()
244
245         def keyLeft(self):
246                 ConfigListScreen.keyLeft(self)
247                 self.newConfig()
248
249         def keyRight(self):
250                 ConfigListScreen.keyRight(self)
251                 self.newConfig()
252
253         def confirm(self, confirmed):
254                 if not confirmed:
255                         print "not confirmed"
256                         return
257                 else:
258                         self.nameEntry[1].setValue(iInputDevices.getDeviceAttribute(self.inputDevice, 'name'))
259                         cmd = "config.inputDevices." + self.inputDevice + ".name.save()"
260                         exec (cmd)
261                         self.keySave()
262
263         def apply(self):
264                 self.session.openWithCallback(self.confirm, MessageBox, _("Use these input device settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = True)
265
266         def cancelConfirm(self, result):
267                 if not result:
268                         return
269                 for x in self["config"].list:
270                         x[1].cancel()
271                 self.close()
272
273         def keyCancel(self):
274                 if self["config"].isChanged():
275                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = True)
276                 else:
277                         self.close()
278         # for summary:
279         def changedEntry(self):
280                 for x in self.onChangedEntry:
281                         x()
282                 self.selectionChanged()
283
284         def getCurrentEntry(self):
285                 return self["config"].getCurrent()[0]
286
287         def getCurrentValue(self):
288                 return str(self["config"].getCurrent()[1].value)
289
290         def createSummary(self):
291                 from Screens.Setup import SetupSummary
292                 return SetupSummary
293
294
295 class RemoteControlType(Screen, ConfigListScreen):
296         rcList = [
297                         ("0", _("Default")),
298                         ("4", _("DMM normal")), ("6", _("DMM advanced")),
299                         ("11", "et9x00/6500"), ("7", "et5000/6000"), ("13", "et4000"),
300                         ("14", "xp1000"),
301                         ("8", "VU+"),
302                 ]
303
304         defaultRcList = [
305                         ("et4000", 13),
306                         ("et5000", 7),
307                         ("et6000", 7),
308                         ("et6500", 11),
309                         ("et9000", 11),
310                         ("et9200", 11),
311                         ("et9500", 11),
312                         ("xp1000", 14),
313                 ]
314
315         def __init__(self, session):
316                 Screen.__init__(self, session)
317                 self.skinName = ["RemoteControlType", "Setup" ]
318
319                 self["actions"] = ActionMap(["SetupActions"],
320                 {
321                         "cancel": self.keyCancel,
322                         "save": self.keySave,
323                 }, -1)
324
325                 self["key_green"] = StaticText(_("Save"))
326                 self["key_red"] = StaticText(_("Cancel"))
327
328                 self.list = []
329                 ConfigListScreen.__init__(self, self.list, session = self.session)
330
331                 rctype = config.plugins.remotecontroltype.rctype.value
332                 self.rctype = ConfigSelection(choices = self.rcList, default = str(rctype))
333                 self.list.append(getConfigListEntry(_("Remote control type"), self.rctype))
334                 self["config"].list = self.list
335
336                 self.defaultRcType = None
337                 self.getDefaultRcType()
338
339         def getDefaultRcType(self):
340                 data = iRcTypeControl.getBoxType()
341                 for x in self.defaultRcList:
342                         if x[0] in data:
343                                 self.defaultRcType = x[1]
344                                 break
345
346         def setDefaultRcType(self):
347                 iRcTypeControl.writeRcType(self.defaultRcType)
348
349         def keySave(self):
350                 if config.plugins.remotecontroltype.rctype.value == int(self.rctype.value):
351                         self.close()
352                 else:
353                         self.setNewSetting()
354                         self.session.openWithCallback(self.keySaveCallback, MessageBox, _("Is this setting ok?"), MessageBox.TYPE_YESNO, timeout = 20, default = False)
355
356         def keySaveCallback(self, answer):
357                 if answer is False:
358                         self.restoreOldSetting()
359                 else:
360                         config.plugins.remotecontroltype.rctype.value = int(self.rctype.value)
361                         config.plugins.remotecontroltype.save()
362                         self.close()
363
364         def restoreOldSetting(self):
365                 if config.plugins.remotecontroltype.rctype.value == 0:
366                         self.setDefaultRcType()
367                 else:
368                         iRcTypeControl.writeRcType(config.plugins.remotecontroltype.rctype.value)
369
370         def setNewSetting(self):
371                 if int(self.rctype.value) == 0:
372                         self.setDefaultRcType()
373                 else:
374                         iRcTypeControl.writeRcType(int(self.rctype.value))
375
376         def keyCancel(self):
377                 self.restoreOldSetting()
378                 self.close()