Merge branch 'master' of git.opendreambox.org:/git/enigma2
[enigma2.git] / lib / python / Plugins / SystemPlugins / VideoEnhancement / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import getConfigListEntry, config, ConfigNothing, ConfigSelection
4 from Components.ActionMap import ActionMap
5 from Components.Sources.StaticText import StaticText
6 from Screens.Screen import Screen
7 from Screens.MessageBox import MessageBox
8 import VideoEnhancement
9 from os import path as os_path
10
11 class VideoEnhancementSetup(Screen, ConfigListScreen):
12
13         skin = """
14                 <screen name="VideoEnhancementSetup" position="center,center" size="560,430" title="VideoEnhancementSetup">
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                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
18                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
19                 <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" />
20                 <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" />
21                 <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" />
22                 <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" />
23                 <widget name="config" position="5,50" size="550,340" scrollbarMode="showOnDemand" />
24                 <ePixmap pixmap="skin_default/div-h.png" position="0,390" zPosition="1" size="560,2" />
25                 <widget source="introduction" render="Label" position="5,400" size="550,25" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1" />
26         </screen>"""
27
28         def __init__(self, session):
29                 Screen.__init__(self, session)
30
31                 self.session = session
32                 self.onChangedEntry = [ ]
33                 self.setup_title = "Videoenhancement"
34
35                 self.list = [ ]
36                 self.xtdlist = [ ]
37                 ConfigListScreen.__init__(self, self.list, session = self.session, on_change = self.changedEntry)
38
39                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
40                         {
41                                 "cancel": self.keyCancel,
42                                 "save": self.apply,
43                                 "yellow": self.keyYellow,
44                                 "blue": self.keyBlue,
45                         }, -2)
46
47                 self["key_red"] = StaticText(_("Cancel"))
48                 self["key_green"] = StaticText(_("OK"))
49                 self["key_yellow"] = StaticText(_("Last config"))
50                 self["key_blue"] = StaticText(_("Default"))
51                 self["introduction"] = StaticText()
52
53                 self.createSetup()
54                 self.rememberOldSettings()
55                 self.onLayoutFinish.append(self.layoutFinished)
56
57         def layoutFinished(self):
58                 self.setTitle(_("Video enhancement setup"))
59
60         def rememberOldSettings(self):
61                 self.oldContrast = config.pep.contrast.value
62                 self.oldSaturation = config.pep.saturation.value
63                 self.oldHue = config.pep.hue.value
64                 self.oldBrightness = config.pep.brightness.value
65                 self.oldBlock_noise = config.pep.block_noise_reduction.value
66                 self.oldMosquito_noise = config.pep.mosquito_noise_reduction.value
67                 self.oldDigital_contour = config.pep.digital_contour_removal.value
68                 self.oldScaler_sharpness = config.av.scaler_sharpness.value
69                 self.oldSplit = config.pep.split.value
70                 self.oldSharpness = config.pep.sharpness.value
71                 self.oldAuto_flesh = config.pep.auto_flesh.value
72                 self.oldGreen_boost = config.pep.green_boost.value
73                 self.oldBlue_boost = config.pep.blue_boost.value
74                 self.oldDynamic_contrast = config.pep.dynamic_contrast.value
75
76         def addToConfigList(self, description, configEntry, add_to_xtdlist=False):
77                 if isinstance(configEntry, ConfigNothing):
78                         return None
79                 entry = getConfigListEntry(description, configEntry)
80                 self.list.append(entry);
81                 if add_to_xtdlist:
82                         self.xtdlist.append(entry)
83                 return entry
84
85         def createSetup(self):
86                 self.list = []
87                 self.xtdlist = []
88                 addToConfigList = self.addToConfigList
89                 self.contrastEntry = addToConfigList(_("Contrast"), config.pep.contrast)
90                 self.saturationEntry = addToConfigList(_("Saturation"), config.pep.saturation)
91                 self.hueEntry = addToConfigList(_("Hue"), config.pep.hue)
92                 self.brightnessEntry = addToConfigList(_("Brightness"), config.pep.brightness)
93                 self.scaler_sharpnessEntry = addToConfigList(_("Scaler sharpness"), config.av.scaler_sharpness)
94                 self.splitEntry = addToConfigList(_("Split preview mode"), config.pep.split, True)
95                 add_to_xtdlist = self.splitEntry is not None
96                 self.sharpnessEntry = addToConfigList(_("Sharpness"), config.pep.sharpness, add_to_xtdlist)
97                 self.auto_fleshEntry = addToConfigList(_("Auto flesh"), config.pep.auto_flesh, add_to_xtdlist)
98                 self.green_boostEntry = addToConfigList(_("Green boost"), config.pep.green_boost, add_to_xtdlist)
99                 self.blue_boostEntry = addToConfigList(_("Blue boost"), config.pep.blue_boost, add_to_xtdlist)
100                 self.dynamic_contrastEntry = addToConfigList(_("Dynamic contrast"), config.pep.dynamic_contrast, add_to_xtdlist)
101                 self.block_noise_reductionEntry = addToConfigList(_("Block noise reduction"), config.pep.block_noise_reduction, add_to_xtdlist)
102                 self.mosquito_noise_reductionEntry = addToConfigList(_("Mosquito noise reduction"), config.pep.mosquito_noise_reduction, add_to_xtdlist)
103                 self.digital_contour_removalEntry = addToConfigList(_("Digital contour removal"), config.pep.digital_contour_removal, add_to_xtdlist)
104
105                 self["config"].list = self.list
106                 self["config"].l.setSeperation(300)
107                 self["config"].l.setList(self.list)
108                 if not self.selectionChanged in self["config"].onSelectionChanged:
109                         self["config"].onSelectionChanged.append(self.selectionChanged)
110                 self.selectionChanged()
111
112         def selectionChanged(self):
113                 self["introduction"].setText(_("Current value: ") + self.getCurrentValue())
114
115         def PreviewClosed(self):
116                 self["config"].invalidate(self["config"].getCurrent())
117                 self.createSetup()
118
119         def keyLeft(self):
120                 current = self["config"].getCurrent()
121                 if current == self.splitEntry:
122                         ConfigListScreen.keyLeft(self)
123                 elif current != self.splitEntry and current in self.xtdlist:
124                         self.previewlist = [
125                                 current,
126                                 self.splitEntry
127                         ]
128                         maxvalue = current[1].max
129                         self.session.openWithCallback(self.PreviewClosed, VideoEnhancementPreview, configEntry = self.previewlist, oldSplitMode = config.pep.split.value, maxValue = maxvalue)
130                 else:
131                         self.previewlist = [
132                                 current
133                         ]
134                         maxvalue = current[1].max
135                         self.session.openWithCallback(self.PreviewClosed, VideoEnhancementPreview, configEntry = self.previewlist, oldSplitMode = None, maxValue = maxvalue)
136
137         def keyRight(self):
138                 current = self["config"].getCurrent()
139                 if current == self.splitEntry:
140                         ConfigListScreen.keyRight(self)
141                 elif current != self.splitEntry and current in self.xtdlist:
142                         self.previewlist = [
143                                 current,
144                                 self.splitEntry
145                         ]
146                         maxvalue = current[1].max
147                         self.session.openWithCallback(self.PreviewClosed, VideoEnhancementPreview, configEntry = self.previewlist, oldSplitMode = config.pep.split.value, maxValue = maxvalue )
148                 else:
149                         self.previewlist = [
150                                 current
151                         ]
152                         maxvalue = current[1].max
153                         self.session.openWithCallback(self.PreviewClosed, VideoEnhancementPreview, configEntry = self.previewlist, oldSplitMode = None, maxValue = maxvalue)
154
155         def confirm(self, confirmed):
156                 if not confirmed:
157                         print "not confirmed"
158                 else:
159                         if self.splitEntry is not None:
160                                 config.pep.split.setValue('off')
161                         self.keySave()
162
163         def apply(self):
164                 self.session.openWithCallback(self.confirm, MessageBox, _("Use this video enhancement settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = False)
165
166         def cancelConfirm(self, result):
167                 if not result:
168                         return
169                 self.keyYellowConfirm(True)
170                 self.close()
171
172         def keyCancel(self):
173                 if self["config"].isChanged():
174                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
175                 else:
176                         self.close()
177
178         def keyYellowConfirm(self, confirmed):
179                 if not confirmed:
180                         print "not confirmed"
181                 else:
182                         if self.contrastEntry is not None:
183                                 config.pep.contrast.setValue(self.oldContrast)
184                         if self.saturationEntry is not None:
185                                 config.pep.saturation.setValue(self.oldSaturation)
186                         if self.hueEntry is not None:
187                                 config.pep.hue.setValue(self.oldHue)
188                         if self.brightnessEntry is not None:
189                                 config.pep.brightness.setValue(self.oldBrightness)
190                         if self.block_noise_reductionEntry is not None:
191                                 config.pep.block_noise_reduction.setValue(self.oldBlock_noise)
192                         if self.mosquito_noise_reductionEntry is not None:
193                                 config.pep.mosquito_noise_reduction.setValue(self.oldMosquito_noise)
194                         if self.digital_contour_removalEntry is not None:
195                                 config.pep.digital_contour_removal.setValue(self.oldDigital_contour)
196                         if self.scaler_sharpnessEntry is not None:
197                                 config.av.scaler_sharpness.setValue(self.oldScaler_sharpness)
198                         if self.splitEntry is not None:
199                                 config.pep.split.setValue('off')
200                         if self.sharpnessEntry is not None:
201                                 config.pep.sharpness.setValue(self.oldSharpness)
202                         if self.auto_fleshEntry is not None:
203                                 config.pep.auto_flesh.setValue(self.oldAuto_flesh)
204                         if self.green_boostEntry is not None:
205                                 config.pep.green_boost.setValue(self.oldGreen_boost)
206                         if self.blue_boostEntry is not None:
207                                 config.pep.blue_boost.setValue(self.oldBlue_boost)
208                         if self.dynamic_contrastEntry is not None:
209                                 config.pep.dynamic_contrast.setValue(self.oldDynamic_contrast)
210                         self.keySave()
211
212         def keyYellow(self):
213                 self.session.openWithCallback(self.keyYellowConfirm, MessageBox, _("Reset video enhancement settings to your last configuration?"), MessageBox.TYPE_YESNO, timeout = 20, default = False)
214
215         def keyBlueConfirm(self, confirmed):
216                 if not confirmed:
217                         print "not confirmed"
218                 else:
219                         if self.contrastEntry is not None:
220                                 config.pep.contrast.setValue(128)
221                         if self.saturationEntry is not None:
222                                 config.pep.saturation.setValue(128)
223                         if self.hueEntry is not None:
224                                 config.pep.hue.setValue(128)
225                         if self.brightnessEntry is not None:
226                                 config.pep.brightness.setValue(128)
227                         if self.block_noise_reductionEntry is not None:
228                                 config.pep.block_noise_reduction.setValue(0)
229                         if self.mosquito_noise_reductionEntry is not None:
230                                 config.pep.mosquito_noise_reduction.setValue(0)
231                         if self.digital_contour_removalEntry is not None:
232                                 config.pep.digital_contour_removal.setValue(0)
233                         if self.scaler_sharpnessEntry is not None:
234                                 config.av.scaler_sharpness.setValue(13)
235                         if self.splitEntry is not None:
236                                 config.pep.split.setValue('off')
237                         if self.sharpnessEntry is not None:
238                                 config.pep.sharpness.setValue(0)
239                         if self.auto_fleshEntry is not None:
240                                 config.pep.auto_flesh.setValue(0)
241                         if self.green_boostEntry is not None:
242                                 config.pep.green_boost.setValue(0)
243                         if self.blue_boostEntry is not None:
244                                 config.pep.blue_boost.setValue(0)
245                         if self.dynamic_contrastEntry is not None:
246                                 config.pep.dynamic_contrast.setValue(0)
247                         self.keySave()
248
249         def keyBlue(self):
250                 self.session.openWithCallback(self.keyBlueConfirm, MessageBox, _("Reset video enhancement settings to system defaults?"), MessageBox.TYPE_YESNO, timeout = 20, default = False)
251
252         # for summary:
253         def changedEntry(self):
254                 for x in self.onChangedEntry:
255                         x()
256                 self.selectionChanged()
257
258         def getCurrentEntry(self):
259                 return self["config"].getCurrent()[0]
260
261         def getCurrentValue(self):
262                 return str(self["config"].getCurrent()[1].getText())
263
264         def createSummary(self):
265                 from Screens.Setup import SetupSummary
266                 return SetupSummary
267
268
269 class VideoEnhancementPreview(Screen, ConfigListScreen):
270
271         skin = """
272                 <screen name="VideoEnhancementPreview" position="center,360" size="560,170" title="VideoEnhancementPreview">
273                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
274                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
275                 <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" />
276                 <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" />
277                 <widget name="config" position="5,50" size="550,80" scrollbarMode="showOnDemand" />
278                 <ePixmap pixmap="skin_default/div-h.png" position="0,130" zPosition="1" size="560,2" />
279                 <widget source="introduction" render="Label" position="0,140" size="550,25" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1" />
280         </screen>"""
281
282         def __init__(self, session, configEntry = None, oldSplitMode = None, maxValue = None):
283                 Screen.__init__(self, session)
284
285                 self.onChangedEntry = [ ]
286                 self.setup_title = "Videoenhancement"
287                 self.oldSplitMode = oldSplitMode
288                 self.maxValue = maxValue
289                 self.configStepsEntry = None
290                 self.isStepSlider = None
291
292                 self.list = [ ]
293                 self.configEntry = configEntry
294                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
295
296                 self["actions"] = ActionMap(["SetupActions"],
297                         {
298                                 "cancel": self.keyCancel,
299                                 "save": self.keySave,
300                         }, -2)
301
302                 self["key_red"] = StaticText(_("Cancel"))
303                 self["key_green"] = StaticText(_("OK"))
304                 self["introduction"] = StaticText()
305
306                 self.createSetup()
307                 self.onLayoutFinish.append(self.layoutFinished)
308
309         def layoutFinished(self):
310                 self.setTitle(_("Video enhancement preview"))
311
312         def createSetup(self):
313                 self.list = [ ]
314                 if self.maxValue == 256:
315                         self.configStepsEntry = getConfigListEntry(_("Change step size"), config.pep.configsteps)
316
317                 if self.configEntry is not None:
318                         self.list = self.configEntry
319                 if self.maxValue == 256:
320                         self.list.append(self.configStepsEntry)
321
322                 self["config"].list = self.list
323                 self["config"].l.setSeperation(300)
324                 self["config"].l.setList(self.list)
325                 if not self.selectionChanged in self["config"].onSelectionChanged:
326                         self["config"].onSelectionChanged.append(self.selectionChanged)
327                 self.selectionChanged()
328
329         def selectionChanged(self):
330                 self["introduction"].setText(_("Current value: ") + self.getCurrentValue())
331                 try:
332                         max_avail=self["config"].getCurrent()[1].max
333                         if max_avail == 256:
334                                 self.isStepSlider = True
335                         else:
336                                 self.isStepSlider = False
337                 except AttributeError:
338                         print "no max value"
339
340         def keyLeft(self):
341                 if self.isStepSlider is True:
342                         self["config"].getCurrent()[1].increment = config.pep.configsteps.value
343                 ConfigListScreen.keyLeft(self)
344
345         def keyRight(self):
346                 if self.isStepSlider is True:
347                         self["config"].getCurrent()[1].increment = config.pep.configsteps.value
348                 ConfigListScreen.keyRight(self)
349
350         def keySave(self):
351                 if self.oldSplitMode is not None:
352                         currentSplitMode = config.pep.split.value
353                         if self.oldSplitMode == 'off' and currentSplitMode != 'off':
354                                 config.pep.split.setValue('off')
355                         else:
356                                 pass
357                 self.close()
358
359         def keyCancel(self):
360                 for x in self["config"].list:
361                         x[1].cancel()
362                 if self.oldSplitMode is not None:
363                         currentSplitMode = config.pep.split.value
364                         if self.oldSplitMode == 'off' and currentSplitMode != 'off':
365                                 config.pep.split.setValue('off')
366                         else:
367                                 pass
368                 self.close()
369
370         # for summary:
371         def changedEntry(self):
372                 for x in self.onChangedEntry:
373                         x()
374                 self.selectionChanged()
375
376         def getCurrentEntry(self):
377                 return self["config"].getCurrent()[0]
378
379         def getCurrentValue(self):
380                 return str(self["config"].getCurrent()[1].getText())
381
382         def createSummary(self):
383                 from Screens.Setup import SetupSummary
384                 return SetupSummary
385
386 def videoEnhancementSetupMain(session, **kwargs):
387         session.open(VideoEnhancementSetup)
388
389 def startSetup(menuid):
390         if menuid != "system":
391                 return [ ]
392         return [(_("Video enhancement settings") , videoEnhancementSetupMain, "videoenhancement_setup", 41)]
393
394 def Plugins(**kwargs):
395         list = []
396         if config.usage.setup_level.index >= 2 and os_path.exists("/proc/stb/vmpeg/0/pep_apply"):
397                 list.append(PluginDescriptor(name=_("Videoenhancement Setup"), description=_("Advanced Video Enhancement Setup"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup))
398         return list