Skin-Components support for automaticvolumeadjustment, merlinmusicplayer, partnerbox...
[enigma2-plugins.git] / automaticvolumeadjustment / src / AutomaticVolumeAdjustmentSetup.py
1 # -*- coding: utf-8 -*-
2 #
3 #  AutomaticVolumeAdjustment E2
4 #
5 #  $Id$
6 #
7 #  Coded by Dr.Best (c) 2010
8 #  Support: www.dreambox-tools.info
9 #
10 #  This plugin is licensed under the Creative Commons 
11 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
12 #  License. To view a copy of this license, visit
13 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
14 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
15 #
16 #  Alternatively, this plugin may be distributed and executed on hardware which
17 #  is licensed by Dream Multimedia GmbH.
18
19 #  This plugin is NOT free software. It is open source, you are allowed to
20 #  modify it (if you keep the license), but it may not be commercially 
21 #  distributed other than under the conditions noted above.
22 #
23 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, RT_VALIGN_CENTER, eServiceReference
24 from Screens.Screen import Screen
25 from Screens.MessageBox import MessageBox
26 from Screens.ChannelSelection import SimpleChannelSelection
27 from Components.MenuList import MenuList
28 from Components.Sources.StaticText import StaticText
29 from Components.ActionMap import ActionMap
30 from Components.ConfigList import ConfigListScreen
31 from Components.config import getConfigListEntry, config
32 from ServiceReference import ServiceReference
33 from AutomaticVolumeAdjustment import AutomaticVolumeAdjustment
34 from AutomaticVolumeAdjustmentConfig import AutomaticVolumeAdjustmentConfig
35 from skin import TemplatedListFonts, componentSizes
36
37                 
38 class AutomaticVolumeAdjustmentConfigScreen(ConfigListScreen, Screen):
39         skin = """
40                 <screen name="AutomaticVolumeAdjustmentConfigScreen" position="center,center" size="550,400">
41                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
42                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
43                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
44                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
45                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
46
47                         <widget source="key_red" render="Label" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
48                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
49                         <widget render="Label" source="key_blue" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
50                 </screen>"""
51
52         def __init__(self, session):
53                 Screen.__init__(self, session)
54                 self.title = _("Automatic Volume Adjustment - Config")
55                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
56                 {
57                         "green": self.keySave,
58                         "red": self.keyCancel,
59                         "blue": self.blue,
60                         "cancel": self.keyCancel,
61                 }, -2)
62                 self["key_red"] = StaticText(_("Cancel"))
63                 self["key_green"] = StaticText(_("OK"))
64                 self["key_blue"] = StaticText()
65                 self.configVA = AutomaticVolumeAdjustmentConfig()
66                 self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance
67                 self.list = []
68                 ConfigListScreen.__init__(self, self.list, session = session)
69                 self.createSetup("config")
70                 
71         def createSetup(self, widget):
72                 self.list = []
73                 self.config_enable = getConfigListEntry(_("Enable"), self.configVA.config.enable)
74                 self.list.append(self.config_enable)
75                 if self.configVA.config.enable.value:
76                         self.config_modus = getConfigListEntry(_("Modus"), self.configVA.config.modus)
77                         self.list.append(self.config_modus)
78                         if self.configVA.config.modus.value == "0":
79                                 self.list.append(getConfigListEntry(_("Default volume adjustment value for AC3/DTS"), self.configVA.config.adustvalue))
80                                 self.list.append(getConfigListEntry(_("Max. volume for mpeg audio"), self.configVA.config.mpeg_max_volume))
81                                 self["key_blue"].text = _("Services")
82                         else:
83                                 self["key_blue"].text = ""
84                         self.list.append(getConfigListEntry(_("Show volumebar when volume-value was changed"), self.configVA.config.show_volumebar))
85                 else:
86                         self.config_modus = None
87                 self[widget].list = self.list
88                 self[widget].l.setList(self.list)
89         
90         def newConfig(self):
91                 if self["config"].getCurrent() in (self.config_enable, self.config_modus):
92                         self.createSetup("config")
93
94         def keyLeft(self):
95                         ConfigListScreen.keyLeft(self)
96                         self.newConfig()
97
98         def keyRight(self):
99                         ConfigListScreen.keyRight(self)
100                         self.newConfig()
101                 
102         def blue(self):
103                 if self.configVA.config.modus.value == "0":
104                         self.session.open(AutomaticVolumeAdjustmentEntriesListConfigScreen, self.configVA)
105
106         def keySave(self):
107                 for x in self["config"].list:
108                         x[1].save()
109                 self.configVA.save()
110                 if self.automaticVolumeAdjustmentInstance is not None:
111                         self.automaticVolumeAdjustmentInstance.initializeConfigValues(self.configVA, True) # submit config values
112                 self.close()
113
114         def keyCancel(self):
115                 ConfigListScreen.cancelConfirm(self, True)
116                 
117
118 class AutomaticVolumeAdjustmentEntriesListConfigScreen(Screen):
119         skin = """
120                 <screen position="center,center" size="550,400">
121                         <widget render="Label" source="name" position="5,0" size="350,50" font="Regular;20" halign="left"/>
122                         <widget render="Label" source="adjustvalue" position="355,0" size="200,50" font="Regular;20" halign="left"/>
123                         <widget name="entrylist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
124                         <widget render="Label" source="key_red" position="0,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
125                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
126                         <widget render="Label" source="key_yellow" position="280,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="yellow" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
127                         <widget render="Label" source="key_blue" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
128                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
129                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
130                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
131                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
132                 </screen>"""
133
134         def __init__(self, session, configVA):
135                 Screen.__init__(self, session)
136                 self.title = _("Automatic Volume Adjustment - Service Config")
137                 self["name"] = StaticText(_("Servicename"))
138                 self["adjustvalue"] = StaticText(_("Adjustment value"))
139                 self["key_red"] = StaticText(_("Add"))
140                 self["key_green"] = StaticText(_("OK"))
141                 self["key_yellow"] = StaticText(_("Edit"))
142                 self["key_blue"] = StaticText(_("Delete"))
143                 self["entrylist"] = AutomaticVolumeAdjustmentEntryList([])
144                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
145                         {
146                          "ok"   :       self.keyOK,
147                          "back" :       self.keyClose,
148                          "red"  :       self.keyRed,
149                          "green":       self.keyClose,
150                          "yellow":      self.keyYellow,
151                          "blue":        self.keyDelete,
152                          }, -1)
153                 self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance
154                 self["entrylist"].setConfig(configVA)
155                 self.updateList()
156
157         def updateList(self):
158                 self["entrylist"].buildList()
159
160         def keyClose(self):
161                 self.close(-1, None)
162
163         def keyRed(self):
164                 self.session.openWithCallback(self.updateList,AutomaticVolumeAdjustmentEntryConfigScreen,None, self["entrylist"].configVA)
165
166         def keyOK(self):
167                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
168                 except: sel = None
169                 self.close(self["entrylist"].getCurrentIndex(), sel)
170
171         def keyYellow(self):
172                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
173                 except: sel = None
174                 if sel is None:
175                         return
176                 self.session.openWithCallback(self.updateList,AutomaticVolumeAdjustmentEntryConfigScreen,sel, self["entrylist"].configVA)
177
178         def keyDelete(self):
179                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
180                 except: sel = None
181                 if sel is None:
182                         return
183                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Do you really want to delete this entry?"))
184
185         def deleteConfirm(self, result):
186                 if not result:
187                         return
188                 sel = self["entrylist"].l.getCurrentSelection()[0]
189                 self["entrylist"].configVA.remove(sel)
190                 if self.automaticVolumeAdjustmentInstance is not None:
191                         self.automaticVolumeAdjustmentInstance.initializeConfigValues(self["entrylist"].configVA, True) # submit config values
192                 self.updateList()
193
194 class AutomaticVolumeAdjustmentEntryList(MenuList):
195         SKIN_COMPONENT_KEY = "AutomaticVolumeAdjustmentList"
196         SKIN_COMPONENT_TEXT_WIDTH = "textWidth"
197         SKIN_COMPONENT_TEXT_HEIGHT = "textHeight"
198         
199         def __init__(self, list, enableWrapAround = True):
200                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
201                 tlf = TemplatedListFonts()
202                 self.l.setFont(0, gFont(tlf.face(tlf.SMALL), tlf.size(tlf.SMALL)))
203                 self.configVA = None
204
205         def postWidgetCreate(self, instance):
206                 MenuList.postWidgetCreate(self, instance)
207                 instance.setItemHeight(componentSizes.itemHeight(self.SKIN_COMPONENT_KEY, 20))
208
209         def getCurrentIndex(self):
210                 return self.instance.getCurrentIndex()
211                 
212         def setConfig(self, configVA):
213                 self.configVA = configVA
214                 
215         def buildList(self):
216                 list = []
217                 
218                 sizes = componentSizes[AutomaticVolumeAdjustmentEntryList.SKIN_COMPONENT_KEY]
219                 textWidth = sizes.get(AutomaticVolumeAdjustmentEntryList.SKIN_COMPONENT_TEXT_WIDTH, 355)
220                 textHeight = sizes.get(AutomaticVolumeAdjustmentEntryList.SKIN_COMPONENT_TEXT_HEIGHT, 20)
221                                 
222                 for c in self.configVA.config.Entries:
223                         c.name.value = ServiceReference(eServiceReference(c.servicereference.value)).getServiceName()
224                         res = [
225                                 c,
226                                 (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, textWidth-10, textHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, c.name.value),
227                                 (eListboxPythonMultiContent.TYPE_TEXT, textWidth, 0,textWidth, textHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.adjustvalue.value)),
228                         ]
229                         list.append(res)
230                 self.list = list
231                 self.l.setList(list)
232                 self.moveToIndex(0)
233
234 class AutomaticVolumeAdjustmentEntryConfigScreen(ConfigListScreen, Screen):
235         skin = """
236                 <screen name="AutomaticVolumeAdjustmentEntryConfigScreen" position="center,center" size="550,400">
237                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
238                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
239                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
240                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
241                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
242
243                         <widget source="key_red" render="Label" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
244                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
245                 </screen>"""
246
247         def __init__(self, session, entry, configVA):   
248                 self.session = session
249                 Screen.__init__(self, session)
250                 self.title = _("Automatic Volume Adjustment - Entry Config")
251                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
252                 {
253                         "green": self.keySave,
254                         "red": self.keyCancel,
255                         "cancel": self.keyCancel,
256                         "ok": self.keySelect,
257                 }, -2)
258                 self["key_red"] = StaticText(_("Cancel"))
259                 self["key_green"] = StaticText(_("OK"))
260                 self.configVA = configVA
261                 if entry is None:
262                         self.newmode = 1
263                         self.current = self.configVA.initEntryConfig()
264                         self.currentvalue = self.current.adjustvalue.value
265                 else:
266                         self.newmode = 0
267                         self.current = entry
268                         self.currentref = entry.servicereference.value
269                         self.currentvalue = entry.adjustvalue.value
270                 self.list = [ ]
271                 self.service = getConfigListEntry(_("Servicename"), self.current.name)
272                 self.list.append(self.service)
273                 self.adjustValue = getConfigListEntry(_("Adjustment value"), self.current.adjustvalue)
274                 self.list.append(self.adjustValue)
275                 ConfigListScreen.__init__(self, self.list, session)
276                 self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance
277                 
278         def keySelect(self):
279                 cur = self["config"].getCurrent()
280                 if cur == self.service:
281                         self.session.openWithCallback(self.channelSelected, SimpleChannelSelection, _("Channel Selection"))
282                         
283         def channelSelected(self, ref = None):
284                 if ref:
285                         self.current.name.value = ServiceReference(ref).getServiceName()
286                         self.current.servicereference.value = ref.toString()
287                         self.current.save()
288
289         def keySave(self):
290                 if self.current.servicereference.value:
291                         if self.newmode == 1:
292                                 self.configVA.config.entriescount.value = self.configVA.config.entriescount.value + 1
293                                 self.configVA.config.entriescount.save()
294                         for x in self["config"].list:
295                                 x[1].save()
296                         self.configVA.save()
297                         if self.automaticVolumeAdjustmentInstance is not None:
298                                 self.automaticVolumeAdjustmentInstance.initializeConfigValues(self.configVA, True) # submit config values
299                         self.close()
300                 else:
301                         self.session.open(MessageBox, _("You must select a valid service!"), type = MessageBox.TYPE_INFO)
302
303         def keyCancel(self):
304                 if self.newmode == 1:
305                         self.configVA.config.Entries.remove(self.current)
306                         self.configVA.config.Entries.save()
307                 else:
308                         self.current.servicereference.value = self.currentref
309                         self.current.adjustvalue.value = self.currentvalue
310                         self.current.save()
311                 ConfigListScreen.cancelConfirm(self, True)