reduce ammount of try/except-blocks used,
[enigma2-plugins.git] / autotimer / src / AutoTimerPreview.py
1 # for localized messages
2 from . import _
3
4 # GUI (Screens)
5 from Screens.Screen import Screen
6
7 # GUI (Components)
8 from Components.ActionMap import ActionMap
9 from Components.Button import Button
10 from AutoTimerList import AutoTimerPreviewList
11
12 class AutoTimerPreview(Screen):
13         """Preview Timers which would be set"""
14
15         skin = """<screen name="AutoTimerPreview" title="Preview AutoTimer" position="75,155" size="565,265">
16                 <widget name="timerlist" position="5,5" size="555,210" scrollbarMode="showOnDemand" />
17                 <ePixmap position="0,220" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
18                 <ePixmap position="280,220" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
19                 <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" />
20                 <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" />
21         </screen>"""
22
23         def __init__(self, session, timers):
24                 Screen.__init__(self, session)
25
26                 # Sort timers by begin
27                 timers.sort(key = lambda x: x[1])
28                 self.sort_type = 0
29
30                 self.timers = timers
31
32                 self["timerlist"] = AutoTimerPreviewList(self.timers)
33
34                 # Initialize Buttons
35                 self["key_red"] = Button(_("Cancel"))
36                 self["key_yellow"] = Button()
37
38                 self.setSortDescription()
39
40                 # Define Actions
41                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
42                         {
43                                 "cancel": self.cancel,
44                                 "save": self.save,
45                                 "yellow": self.sort
46                         }
47                 )
48
49         def setSortDescription(self):
50                 if self.sort_type == 1:
51                         self["key_yellow"].setText(_("Sort Time"))
52                 else:
53                         self["key_yellow"].setText(_("Sort AutoTimer"))
54
55         def sort(self):
56                 if len(self.timers):
57                         timer = self["timerlist"].getCurrent()
58                         if self.sort_type == 1:
59                                 self.timers.sort(key=lambda x: x[1])
60                                 self.sort_type = 0
61                         else:
62                                 self.timers.sort(key = lambda x: x[4].lower())
63                                 self.sort_type = 1
64                         self["timerlist"].setList(self.timers)
65                         self["timerlist"].moveToEntry(timer)
66                         self.setSortDescription()
67
68         def cancel(self):
69                 self.close(None)
70
71         def save(self):
72                 self.close(True)
73