remove unneeded del...
[enigma2.git] / lib / python / Components / Converter / ServicePosition.py
index e072aa5ec1646543f088692e634e6903598a42d9..b488258b11a822926cb351cc6c9cbb7cf6a213cb 100644 (file)
@@ -1,16 +1,30 @@
 from Converter import Converter
 from Poll import Poll
 from enigma import iPlayableService
+from Components.Element import cached
 
 class ServicePosition(Converter, Poll, object):
-       TYPE_LENGTH = 0,
-       TYPE_POSITION = 1,
-       TYPE_REMAINING = 2,
+       TYPE_LENGTH = 0
+       TYPE_POSITION = 1
+       TYPE_REMAINING = 2
        TYPE_GAUGE = 3
-       
+
        def __init__(self, type):
                Poll.__init__(self)
                Converter.__init__(self, type)
+
+               args = type.split(',')
+               type = args.pop(0)
+
+               self.negate = 'Negate' in args
+               self.detailed = 'Detailed' in args
+               self.showHours = 'ShowHours' in args
+
+               if self.detailed:
+                       self.poll_interval = 100
+               else:
+                       self.poll_interval = 500
+
                if type == "Length":
                        self.type = self.TYPE_LENGTH
                elif type == "Position":
@@ -19,14 +33,16 @@ class ServicePosition(Converter, Poll, object):
                        self.type = self.TYPE_REMAINING
                elif type == "Gauge":
                        self.type = self.TYPE_GAUGE
+               else:
+                       raise "type must be {Length|Position|Remaining|Gauge} with optional arguments {Negate|Detailed|ShowHours}"
 
-               self.poll_interval = 500
                self.poll_enabled = self.type != self.TYPE_LENGTH
-       
+
        def getSeek(self):
                s = self.source.service
                return s and s.seek()
-       
+
+       @cached
        def getPosition(self):
                seek = self.getSeek()
                if seek is None:
@@ -35,7 +51,8 @@ class ServicePosition(Converter, Poll, object):
                if pos[0]:
                        return 0
                return pos[1]
-       
+
+       @cached
        def getLength(self):
                seek = self.getSeek()
                if seek is None:
@@ -44,12 +61,14 @@ class ServicePosition(Converter, Poll, object):
                if length[0]:
                        return 0
                return length[1]
-       
+
+       @cached
        def getCutlist(self):
                service = self.source.service
                cue = service and service.cueSheet()
                return cue and cue.getCutList()
-       
+
+       @cached
        def getText(self):
                seek = self.getSeek()
                if seek is None:
@@ -61,22 +80,53 @@ class ServicePosition(Converter, Poll, object):
                                l = self.position
                        elif self.type == self.TYPE_REMAINING:
                                l = self.length - self.position
-                       
-                       l /= 90000
-                       return "%d:%02d" % (l/60, l%60)
+
+                       if not self.detailed:
+                               l /= 90000
+
+                       if self.negate: l = -l
+
+                       if l > 0:
+                               sign = ""
+                       else:
+                               l = -l
+                               sign = "-"
+
+                       if not self.detailed:
+                               if self.showHours:
+                                       return sign + "%d:%02d:%02d" % (l/3600, l%3600/60, l%60)
+                               else:
+                                       return sign + "%d:%02d" % (l/60, l%60)
+                       else:
+                               if self.showHours:
+                                       return sign + "%d:%02d:%02d:%03d" % ((l/3600/90000), (l/90000)%3600/60, (l/90000)%60, (l%90000)/90)
+                               else:
+                                       return sign + "%d:%02d:%03d" % ((l/60/90000), (l/90000)%60, (l%90000)/90)
+
+       # range/value are for the Progress renderer
+       range = 10000
+
+       @cached
+       def getValue(self):
+               pos = self.position
+               len = self.length
+               if pos is None or len is None or len <= 0:
+                       return None
+               return pos * 10000 / len
 
        position = property(getPosition)
        length = property(getLength)
        cutlist = property(getCutlist)
        text = property(getText)
-       
-       def changed(self, *args):
-               cutlist_refresh = len(args) and args[0] in [iPlayableService.evCuesheetChanged, iPlayableService.evStart, iPlayableService.evEnd]
-               time_refresh = not len(args) or args[0] in [iPlayableService.evStart, iPlayableService.evEnd]
-               
+       value = property(getValue)
+
+       def changed(self, what):
+               cutlist_refresh = what[0] != self.CHANGED_SPECIFIC or what[1] in [iPlayableService.evCuesheetChanged]
+               time_refresh = what[0] == self.CHANGED_POLL or what[0] == self.CHANGED_SPECIFIC and what[1] in [iPlayableService.evCuesheetChanged]
+
                if cutlist_refresh:
                        if self.type == self.TYPE_GAUGE:
                                self.downstream_elements.cutlist_changed()
 
                if time_refresh:
-                       self.downstream_elements.changed()
+                       self.downstream_elements.changed(what)