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