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