TrackAutoselect: explicitely set language ChoiceBox keylist entries to False to preve...
[enigma2-plugins.git] / trackautoselect / src / TrackAutoselectSetup.py
1 from Components.ActionMap import ActionMap
2 from Components.config import config, getConfigListEntry, ConfigSet, ConfigElement
3 from Components.ConfigList import ConfigListScreen
4 from Components.Label import Label
5 from Components.Sources.Boolean import Boolean
6 from Components.ServiceEventTracker import ServiceEventTracker
7 from Components.SortableList import SortableListScreen
8 from Screens.Screen import Screen
9 from Screens.ChoiceBox import ChoiceBox
10 from Tools.BoundFunction import boundFunction
11
12 class TrackAutoselectSetup(Screen, ConfigListScreen):
13         skin = """
14                 <screen name="TrackAutoselectSetup" position="center,center" size="820,520" title="Track Autoselect configuration">
15                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
16                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
17                         <widget source="button_yellow" render="Pixmap" position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" alphatest="on">
18                                 <convert type="ConditionalShowHide" />
19                         </widget>
20                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
21                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
22                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
23                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
24                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
25                         <widget name="config" position="5,50" size="810,460" scrollbarMode="showOnDemand" zPosition="1"/>
26                 </screen>"""
27
28         def __init__(self, session, track_autoselect_config):
29                 Screen.__init__(self, session)
30                 ConfigListScreen.__init__(self, [], session=session)
31                 self.track_autoselect_config = track_autoselect_config
32
33                 self["button_yellow"] = Boolean(False)
34
35                 self["key_red"] = Label(_("Cancel"))
36                 self["key_green"] = Label(_("Save"))
37                 self["key_yellow"] = Label("")
38                 self["key_blue"] = Label(_("Edit"))
39
40                 self["SetupActions"] = ActionMap(["SetupActions", "ColorActions"],
41                 {
42                         "save": self.keySave,
43                         "cancel": self.keyCancel,
44                         "ok" : self.modifyEntry,
45                         "blue": self.modifyEntry,
46                         "yellow": self.keyYellow
47                 }, -3)
48                 self.onLayoutFinish.append(self.__layoutFinished)
49                 self.onClose.append(self.removeNotifiers)
50
51         def __layoutFinished(self):
52                 cptas = config.plugins.TrackAutoselect
53                 for element in [cptas.audio_autoselect_enable, cptas.audio_parameter_order, cptas.subtitle_autoselect_enable, cptas.subtitle_parameter_order]:
54                         element.addNotifier(self.fillList, initial_call=False)
55                 self.fillList()
56
57         def fillList(self, element=None):
58                 C = self.track_autoselect_config
59                 cptas = config.plugins.TrackAutoselect
60                 conflist = []
61                 conflist.append(getConfigListEntry(_("Enable automatic selection of audio tracks"), config.plugins.TrackAutoselect.audio_autoselect_enable))
62                 if cptas.audio_autoselect_enable.getValue():
63                         conflist.append(getConfigListEntry(C.CATEGORY_AUDIO_ORDER, cptas.audio_parameter_order))
64                         for parameter in cptas.audio_parameter_order.getValue():
65                                 if parameter == C.AUDIO_ORDER_FORMAT:
66                                         conflist.append(getConfigListEntry(C.CATEGORY_AUDIO_FORMAT, cptas.audio_format_preference))
67                                 if parameter == C.AUDIO_ORDER_LANGUAGE:
68                                         conflist.append(getConfigListEntry(C.CATEGORY_AUDIO_LANGUAGE, cptas.audio_language_preference))
69                                 #if parameter == C.AUDIO_ORDER_DESCRIPTION:
70                                         #conflist.append(getConfigListEntry(C.CATEGORY_AUDIO_DESCRIPTION, cptas.audio_description_preference))
71
72                 conflist.append(getConfigListEntry(_("Enable automatic selection of subtitle tracks"), cptas.subtitle_autoselect_enable))
73                 if cptas.subtitle_autoselect_enable.getValue():
74                         conflist.append(getConfigListEntry(C.CATEGORY_SUBTITLE_ENABLE, cptas.subtitle_enable_conditions))
75                         conflist.append(getConfigListEntry(C.CATEGORY_SUBTITLE_ORDER, cptas.subtitle_parameter_order))
76                         for parameter in cptas.subtitle_parameter_order.getValue():
77                                 if parameter == C.SUBTITLE_ORDER_LANGUAGE:
78                                         conflist.append(getConfigListEntry(C.CATEGORY_SUBTITLE_LANGUAGE, cptas.subtitle_language_preference))
79                                 if parameter == C.SUBTITLE_ORDER_FORMAT:
80                                         conflist.append(getConfigListEntry(C.CATEGORY_SUBTITLE_FORMAT, cptas.subtitle_format_preference))
81
82                 conflist.append(getConfigListEntry(C.CATEGORY_HANDLE_SERVICES, cptas.handle_services))
83                 conflist.append(getConfigListEntry(_("subtitle and audio language may be the same"), cptas.same_languages))
84                 conflist.append(getConfigListEntry(_("EIT wait timeout (ms) before auto selection"), cptas.wait_for_eit))
85                 self["config"].l.setList(conflist)
86                 self["config"].list = conflist
87
88         def removeNotifiers(self):
89                 cptas = config.plugins.TrackAutoselect
90                 for element in [cptas.audio_autoselect_enable, cptas.audio_parameter_order, cptas.subtitle_autoselect_enable, cptas.subtitle_parameter_order]:
91                         element.clearNotifiers()
92
93         def keySave(self):
94                 #print "[TrackAutoselectSetup] keySave"
95                 ConfigListScreen.keySave(self)
96
97         def keyCancel(self):
98                 #print "[TrackAutoselectSetup] keyCancel"
99                 ConfigListScreen.keyCancel(self)
100
101         def keyYellow(self):
102                 print "[TrackAutoselectSetup] keyYellow"
103
104         def modifyEntry(self):
105                 cur = self["config"].getCurrent()
106                 if isinstance(cur, tuple) and isinstance(cur[1], ConfigSet):
107                         preference_list = cur[1].getValue()
108                         category = cur[0]
109                         (available_choices, preference_tuplelist) = self.track_autoselect_config.getAvailableChoicesAndPreferenceTuplelist(category, preference_list)
110                         self.session.openWithCallback(boundFunction(self.preferencesSetCB, cur[1]), TrackAutoselectPreferenceListScreen, category, available_choices, preference_tuplelist)
111                 else:
112                         ConfigListScreen.keyRight(self)
113
114         def preferencesSetCB(self, config_item, preference_tuplelist):
115                 if isinstance(preference_tuplelist, list) and isinstance(config_item, ConfigElement):
116                         new_value = [entry[0] for entry in preference_tuplelist]
117                         if new_value != config_item.getValue():
118                                 #print "[TrackAutoselectSetup]:preferencesSetCB old value", config_item.getValue()
119                                 config_item.value = [entry[0] for entry in preference_tuplelist]
120                                 #print "[TrackAutoselectSetup]:preferencesSetCB new value", config_item.getValue()
121
122         def close(self):
123                 Screen.close(self)
124
125 class TrackAutoselectPreferenceListScreen(Screen, SortableListScreen):
126         skin = """
127                 <screen name="TrackAutoselectPreferenceListScreen" position="center,center" size="580,500">
128                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
129                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
130                         <widget source="button_yellow" render="Pixmap" position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" alphatest="on">
131                                 <convert type="ConditionalShowHide" />
132                         </widget>
133                         <widget source="button_blue" render="Pixmap" position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" alphatest="on">
134                                 <convert type="ConditionalShowHide" />
135                         </widget>
136                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
137                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
138                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
139                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
140                         <widget name="list" position="10,50" size="560,405" scrollbarMode="showOnDemand"/>
141                         <widget name="info" position="10,460" zPosition="1" size="560,40" font="Regular;20" valign="center" transparent="1" />
142                 </screen>"""
143
144         def __init__(self, session, category, available_choices, preference_tuplelist):
145                 Screen.__init__(self, session)
146
147                 self.category = category
148                 self.available_choices = available_choices
149
150                 SortableListScreen.__init__(self, session, preference_tuplelist)
151
152                 self["button_yellow"] = Boolean(True)
153                 self["button_blue"] = Boolean(True)
154                 self["key_yellow"] = Label(_("Add"))
155                 self["key_blue"] = Label(_("Remove"))
156                 self.setTitle(category)
157
158                 self["ColorActions"] = ActionMap(["ColorActions"],
159                 {
160                         "blue": self.keyBlue,
161                         "yellow": self.keyYellow
162                 }, -3)
163
164         def keyYellow(self):
165                 #print "[TrackAutoselectPreferenceListScreen] keyYellow", self.available_choices
166                 if len(self.available_choices):
167                         self.session.openWithCallback(self.entryChosen, ChoiceBox, title=self.category, list=self.available_choices, keys=[str(x)[-1] for x in list(range(1,11))]+(len(self.available_choices)-10)*[False])
168
169         def entryChosen(self, entry):
170                 #print "[TrackAutoselectPreferenceListScreen] entryChosen", entry, type(entry)
171                 if isinstance(entry, tuple):
172                         #print "[TrackAutoselectPreferenceListScreen] available_choices", self.available_choices, " -> entry", entry
173                         self.available_choices.remove(entry)
174                         self["list"].insertEntry(tuple(reversed(entry)))
175
176         def keyBlue(self):
177                 cur = self["list"].getCurrent()
178                 #print "[TrackAutoselectPreferenceListScreen] keyBlue", cur
179                 if isinstance(cur, list):
180                         #print "[TrackAutoselectPreferenceListScreen] cur[0]", cur[0], "-> available_choices", self.available_choices
181                         self.available_choices.append(tuple(reversed(cur[0][:2])))
182                         self.available_choices.sort()
183                         #print "[TrackAutoselectPreferenceListScreen] new available_choices", self.available_choices
184                 self["list"].removeCurrentEntry()