specify encoding
[vuplus_dvbapp-plugin] / autotimer / src / AutoTimerPreview.py
1 # -*- coding: UTF-8 -*-
2 # for localized messages
3 from . import _
4
5 # GUI (Screens)
6 from Screens.Screen import Screen
7
8 # GUI (Components)
9 from Components.ActionMap import ActionMap
10 from Components.Button import Button
11 from Components.Sources.List import List
12
13 from ServiceReference import ServiceReference
14 from Tools.FuzzyDate import FuzzyTime
15
16 class AutoTimerPreview(Screen):
17         """Preview Timers which would be set"""
18
19         skin = """<screen name="AutoTimerPreview" title="Preview AutoTimer" position="75,155" size="565,265">
20                 <widget source="timerlist" render="Listbox" position="5,5" size="555,210" scrollbarMode="showOnDemand">
21                         <convert type="TemplatedMultiContent">
22                                 {"template": [
23                                                 MultiContentEntryText(pos=(2,2), size=(550,24), text = 3, font = 0, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
24                                                 MultiContentEntryText(pos=(2,26), size=(555,30), text = 0, font = 1, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
25                                                 MultiContentEntryText(pos=(2,50), size=(400,20), text = 4, font = 1, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER),
26                                                 MultiContentEntryText(pos=(320,50), size=(233,20), text = 2, font = 1, flags = RT_HALIGN_RIGHT|RT_VALIGN_CENTER),
27                                         ],
28                                  "fonts": [gFont("Regular", 20),gFont("Regular", 18)],
29                                  "itemHeight": 72
30                                 }
31                         </convert>
32                 </widget>
33                 <ePixmap position="0,220" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
34                 <ePixmap position="280,220" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
35                 <widget name="key_red" position="0,220" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
36                 <widget name="key_yellow" position="280,220" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
37         </screen>"""
38
39         def __init__(self, session, timers):
40                 Screen.__init__(self, session)
41
42                 # Sort timers by begin
43                 timers.sort(key = lambda x: x[1])
44                 self.sort_type = 0
45
46                 # name, begin, end, serviceref, timername -> name, begin, timername, sname, timestr
47                 self.timers = [
48                         (x[0], x[1], x[4],
49                         ServiceReference(x[3]).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '').encode('utf-8', 'ignore'),
50                         (("%s, %s ... %s (%d " + _("mins") + ")") % (FuzzyTime(x[1]) + FuzzyTime(x[2])[1:] + ((x[2] - x[1]) / 60,))))
51                         for x in timers
52                 ]
53
54                 self["timerlist"] = List(self.timers)
55
56                 # Initialize Buttons
57                 self["key_red"] = Button(_("Cancel"))
58                 self["key_yellow"] = Button()
59
60                 self.setSortDescription()
61
62                 # Define Actions
63                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
64                         {
65                                 "cancel": self.cancel,
66                                 "save": self.save,
67                                 "yellow": self.sort
68                         }
69                 )
70
71                 self.onLayoutFinish.append(self.setCustomTitle)
72
73         def setCustomTitle(self):
74                 self.setTitle(_("Preview AutoTimer"))
75
76         def setSortDescription(self):
77                 if self.sort_type == 1:
78                         self["key_yellow"].setText(_("Sort Time"))
79                 else:
80                         self["key_yellow"].setText(_("Sort AutoTimer"))
81
82         def sort(self):
83                 timers = self.timers
84                 if timers:
85                         current = self["timerlist"].current
86                         idx = 0
87                         for timer in timers:
88                                 if timer == current:
89                                         break
90                                 idx += 1
91                         if self.sort_type == 1:
92                                 timers.sort(key=lambda x: x[1])
93                                 self.sort_type = 0
94                         else:
95                                 timers.sort(key = lambda x: x[4].lower())
96                                 self.sort_type = 1
97                         self["timerlist"].updateList(timers)
98                         self["timerlist"].index = idx
99                         self.setSortDescription()
100
101         def cancel(self):
102                 self.close(None)
103
104         def save(self):
105                 self.close(True)
106