Merge branch 'bug_538_ignore_global_actions_in_standby'
[vuplus_dvbapp] / 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
5 from Components.Sources.StaticText import StaticText
6 from Components.Sources.Boolean import Boolean
7 from Components.Sources.List import List
8 from Components.config import config, ConfigSlider, ConfigSubsection, ConfigYesNo, ConfigText, getConfigListEntry, ConfigNothing
9 from Components.ConfigList import ConfigListScreen
10 from Components.ActionMap import ActionMap, NumberActionMap, HelpableActionMap
11 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
12 from Tools.LoadPixmap import LoadPixmap
13
14 class InputDeviceSelection(Screen,HelpableScreen):
15         skin = """
16         <screen name="InputDeviceSelection" position="center,center" size="560,400" title="Select input device">
17                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on"/>
18                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on"/>
19                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on"/>
20                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on"/>
21                 <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"/>
22                 <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"/>
23                 <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"/>
24                 <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"/>
25                 <widget source="list" render="Listbox" position="5,50" size="550,280" zPosition="10" scrollbarMode="showOnDemand">
26                         <convert type="TemplatedMultiContent">
27                         <!--  device, description, devicepng, divpng  -->
28                                                         {"template": [
29                                                                         MultiContentEntryPixmapAlphaTest(pos = (2, 8), size = (54, 54), png = 2), # index 3 is the interface pixmap
30                                                                         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
31                                                                 ],
32                                                         "fonts": [gFont("Regular", 28),gFont("Regular", 20)],
33                                                         "itemHeight": 70
34                                                         }
35                                                 
36                         </convert>
37                 </widget>
38                 <ePixmap pixmap="skin_default/div-h.png" position="0,340" zPosition="1" size="560,2"/>
39                 <widget source="introduction" render="Label" position="0,350" size="560,50" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1"/>
40         </screen>"""
41
42
43         def __init__(self, session):
44                 Screen.__init__(self, session)
45                 HelpableScreen.__init__(self)
46                 
47                 self.edittext = _("Press OK to edit the settings.")
48                 
49                 self["key_red"] = StaticText(_("Close"))
50                 self["key_green"] = StaticText(_("Select"))
51                 self["key_yellow"] = StaticText("")
52                 self["key_blue"] = StaticText("")
53                 self["introduction"] = StaticText(self.edittext)
54                 
55                 self.devices = [(iInputDevices.getDeviceName(x),x) for x in iInputDevices.getDeviceList()]
56                 print "[InputDeviceSelection] found devices :->", len(self.devices),self.devices
57                         
58                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
59                         {
60                         "cancel": (self.close, _("Exit input device selection.")),
61                         "ok": (self.okbuttonClick, _("Select input device.")),
62                         }, -2)
63
64                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
65                         {
66                         "red": (self.close, _("Exit input device selection.")),
67                         "green": (self.okbuttonClick, _("Select input device.")),
68                         }, -2)
69                 
70                 self.currentIndex = 0
71                 self.list = []
72                 self["list"] = List(self.list)
73                 self.updateList()
74                 self.onLayoutFinish.append(self.layoutFinished)
75                 self.onClose.append(self.cleanup)
76
77         def layoutFinished(self):
78                 self.setTitle(_("Select input device"))
79
80         def cleanup(self):
81                 self.currentIndex = 0
82
83         def buildInterfaceList(self,device,description,type ):
84                 divpng = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/div-h.png"))
85                 activepng = None
86                 devicepng = None
87                 enabled = iInputDevices.getDeviceAttribute(device, 'enabled')
88
89                 if type == 'remote':
90                         if config.misc.rcused.value == 0:
91                                 if enabled:
92                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew-configured.png"))
93                                 else:
94                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew.png"))
95                         else:
96                                 if enabled:
97                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcold-configured.png"))
98                                 else:
99                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcold.png"))
100                 elif type == 'keyboard':
101                         if enabled:
102                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_keyboard-configured.png"))
103                         else:
104                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_keyboard.png"))
105                 elif type == 'mouse':
106                         if enabled:
107                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_mouse-configured.png"))
108                         else:
109                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_mouse.png"))
110                 else:
111                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew.png"))
112                 return((device, description, devicepng, divpng))        
113
114         def updateList(self):
115                 self.list = []
116                 for x in self.devices:
117                         dev_type = iInputDevices.getDeviceAttribute(x[1], 'type')
118                         self.list.append(self.buildInterfaceList(x[1],_(x[0]), dev_type ))
119                 self["list"].setList(self.list)
120                 self["list"].setIndex(self.currentIndex)
121
122         def okbuttonClick(self):
123                 selection = self["list"].getCurrent()
124                 self.currentIndex = self["list"].getIndex()
125                 if selection is not None:
126                         self.session.openWithCallback(self.DeviceSetupClosed, InputDeviceSetup, selection[0])
127
128         def DeviceSetupClosed(self, *ret):
129                 self.updateList()
130
131
132 class InputDeviceSetup(Screen, ConfigListScreen):
133
134         skin = """
135                 <screen name="InputDeviceSetup" position="center,center" size="560,440" title="Input device setup">
136                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
137                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
138                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
139                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
140                         <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" />
141                         <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" />
142                         <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" />
143                         <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" />
144                         <widget name="config" position="5,50" size="550,350" scrollbarMode="showOnDemand" />
145                         <ePixmap pixmap="skin_default/div-h.png" position="0,400" zPosition="1" size="560,2" />
146                         <widget source="introduction" render="Label" position="5,410" size="550,30" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1" />
147                 </screen>"""
148
149         def __init__(self, session, device):
150                 Screen.__init__(self, session)
151                 self.inputDevice = device
152                 iInputDevices.currentDevice = self.inputDevice
153                 self.onChangedEntry = [ ]
154                 self.setup_title = _("Input device setup")
155                 self.isStepSlider = None
156                 self.enableEntry = None
157                 self.repeatEntry = None
158                 self.delayEntry = None
159                 self.nameEntry = None
160                 self.enableConfigEntry = None
161
162                 self.list = [ ]
163                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
164
165                 self["actions"] = ActionMap(["SetupActions"],
166                         {
167                                 "cancel": self.keyCancel,
168                                 "save": self.apply,
169                         }, -2)
170
171                 self["key_red"] = StaticText(_("Cancel"))
172                 self["key_green"] = StaticText(_("OK"))
173                 self["key_yellow"] = StaticText()
174                 self["key_blue"] = StaticText()
175                 self["introduction"] = StaticText()
176
177                 self.createSetup()
178                 self.onLayoutFinish.append(self.layoutFinished)
179                 self.onClose.append(self.cleanup)
180
181         def layoutFinished(self):
182                 self.setTitle(self.setup_title)
183
184         def cleanup(self):
185                 iInputDevices.currentDevice = ""
186
187         def createSetup(self):
188                 self.list = [ ]
189                 cmd = "self.enableEntry = getConfigListEntry(_('"'Change repeat and delay settings?'"'), config.inputDevices." + self.inputDevice + ".enabled)"
190                 exec (cmd)
191                 cmd = "self.repeatEntry = getConfigListEntry(_('"'Interval between keys when repeating:'"'), config.inputDevices." + self.inputDevice + ".repeat)"
192                 exec (cmd)
193                 cmd = "self.delayEntry = getConfigListEntry(_('"'Delay before key repeat starts:'"'), config.inputDevices." + self.inputDevice + ".delay)"
194                 exec (cmd)
195                 cmd = "self.nameEntry = getConfigListEntry(_('"'Devicename:'"'), config.inputDevices." + self.inputDevice + ".name)"
196                 exec (cmd)
197                 if self.enableEntry:
198                         if isinstance(self.enableEntry[1], ConfigYesNo):
199                                 self.enableConfigEntry = self.enableEntry[1]
200
201                 self.list.append(self.enableEntry)
202                 if self.enableConfigEntry:
203                         if self.enableConfigEntry.value is True:
204                                 self.list.append(self.repeatEntry)
205                                 self.list.append(self.delayEntry)
206                         else:
207                                 self.repeatEntry[1].setValue(self.repeatEntry[1].default)
208                                 self["config"].invalidate(self.repeatEntry)
209                                 self.delayEntry[1].setValue(self.delayEntry[1].default)
210                                 self["config"].invalidate(self.delayEntry)
211                                 self.nameEntry[1].setValue(self.nameEntry[1].default)
212                                 self["config"].invalidate(self.nameEntry)
213                                 
214                 self["config"].list = self.list
215                 self["config"].l.setSeperation(400)
216                 self["config"].l.setList(self.list)
217                 if not self.selectionChanged in self["config"].onSelectionChanged:
218                         self["config"].onSelectionChanged.append(self.selectionChanged)
219                 self.selectionChanged()
220
221         def selectionChanged(self):
222                 if self["config"].getCurrent() == self.enableEntry:
223                         self["introduction"].setText(_("Current device: ") + str(iInputDevices.getDeviceAttribute(self.inputDevice, 'name')) )
224                 else:
225                         self["introduction"].setText(_("Current value: ") + self.getCurrentValue() + _(" ms"))
226
227         def newConfig(self):
228                 current = self["config"].getCurrent()
229                 if current:
230                         if current == self.enableEntry:
231                                 self.createSetup()
232
233         def keyLeft(self):
234                 ConfigListScreen.keyLeft(self)
235                 self.newConfig()
236
237         def keyRight(self):
238                 ConfigListScreen.keyRight(self)
239                 self.newConfig()
240
241         def confirm(self, confirmed):
242                 if not confirmed:
243                         print "not confirmed"
244                         return
245                 else:
246                         self.nameEntry[1].setValue(iInputDevices.getDeviceAttribute(self.inputDevice, 'name'))
247                         cmd = "config.inputDevices." + self.inputDevice + ".name.save()"
248                         exec (cmd)
249                         self.keySave()
250
251         def apply(self):
252                 self.session.openWithCallback(self.confirm, MessageBox, _("Use this input device settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = True)
253
254         def cancelConfirm(self, result):
255                 if not result:
256                         return
257                 for x in self["config"].list:
258                         x[1].cancel()
259                 self.close()
260
261         def keyCancel(self):
262                 if self["config"].isChanged():
263                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = True)
264                 else:
265                         self.close()
266         # for summary:
267         def changedEntry(self):
268                 for x in self.onChangedEntry:
269                         x()
270                 self.selectionChanged()
271
272         def getCurrentEntry(self):
273                 return self["config"].getCurrent()[0]
274
275         def getCurrentValue(self):
276                 return str(self["config"].getCurrent()[1].value)
277
278         def createSummary(self):
279                 from Screens.Setup import SetupSummary
280                 return SetupSummary