[pitivi: 18/27] Implement a TimeWidget class and use it to jump to a specific time



commit c726ed3848d851c0865e7b65264cbec1d5c04cbb
Author: Thibault Saunier <thibault saunier collabora co uk>
Date:   Tue Jan 18 23:09:19 2011 +0100

    Implement a TimeWidget class and use it to jump to a specific time
    
    Remove the string_to_time function

 pitivi/ui/dynamic.py |   26 ++++++++++++++++++++++++++
 pitivi/ui/viewer.py  |   14 +++++++-------
 pitivi/utils.py      |   19 -------------------
 3 files changed, 33 insertions(+), 26 deletions(-)
---
diff --git a/pitivi/ui/dynamic.py b/pitivi/ui/dynamic.py
index 10001af..7c8d9f0 100644
--- a/pitivi/ui/dynamic.py
+++ b/pitivi/ui/dynamic.py
@@ -223,6 +223,32 @@ class NumericWidget(gtk.HBox, DynamicWidget):
         self.adjustment.set_all(value, minimum, maximum, step, page, 0)
         self.spinner.set_adjustment(self.adjustment)
 
+class TimeWidget(TextWidget, DynamicWidget):
+    """ A widget that contains a time """
+
+    regex = re.compile("^([0-9][0-9]:[0-5][0-9]:[0-5][0-9])\.[0-9][0-9][0-9]$")
+    __gtype_name__ = 'TimeWidget'
+
+    def __init__(self, default=None):
+        DynamicWidget.__init__(self, default)
+        TextWidget.__init__(self, self.regex)
+
+    def getWidgetValue(self):
+
+      timecode = TextWidget.getWidgetValue(self)
+
+      hh, mm, end = timecode.split(":")
+      ss, xxx = end.split(".")
+      nanosecs = int(hh) * 3.6 * 10e12 \
+          + int(mm) * 6 * 10e10 \
+          + int(ss) * 10e9 \
+          + int(xxx) * 10e6
+
+      nanosecs = nanosecs / 10 # Compensate the 10 factor of e notation
+
+      return nanosecs
+
+
 class FractionWidget(TextWidget, DynamicWidget):
 
     """A gtk.ComboBoxEntry """
diff --git a/pitivi/ui/viewer.py b/pitivi/ui/viewer.py
index fe2d37c..21d97fd 100644
--- a/pitivi/ui/viewer.py
+++ b/pitivi/ui/viewer.py
@@ -31,11 +31,12 @@ from gettext import gettext as _
 from pitivi.action import ViewAction
 
 from pitivi.stream import VideoStream
-from pitivi.utils import time_to_string, string_to_time, Seeker
+from pitivi.utils import time_to_string, Seeker
 from pitivi.log.loggable import Loggable
 from pitivi.pipeline import PipelineError
 from pitivi.ui.common import SPACING
 from pitivi.settings import GlobalSettings
+from pitivi.ui.dynamic import TimeWidget
 
 GlobalSettings.addConfigSection("viewer")
 GlobalSettings.addConfigOption("viewerDocked",
@@ -327,9 +328,9 @@ class PitiviViewer(gtk.VBox, Loggable):
         bbox.pack_start(self.goToEnd_button, expand=False)
 
         # current time
-        self.timecode_entry = gtk.Entry()
-        self.timecode_entry.set_text("00:00:00.000")
-        self.timecode_entry.connect("activate", self._jumpToTimecodeCb)
+        self.timecode_entry = TimeWidget()
+        self.timecode_entry.setWidgetValue("00:00:00.000")
+        self.timecode_entry.connect("value-changed", self._jumpToTimecodeCb)
         bbox.pack_start(self.timecode_entry, expand=False, padding=10)
         self._haveUI = True
 
@@ -492,9 +493,8 @@ class PitiviViewer(gtk.VBox, Loggable):
     ## Callback for jumping to a specific timecode
 
     def _jumpToTimecodeCb(self, widget):
-        nanoseconds = string_to_time(widget.get_text())
-        if nanoseconds:  # The timecode has been correctly parsed
-            self.seek(nanoseconds)
+        nanoseconds = widget.getWidgetValue()
+        self.seek(nanoseconds)
 
     ## public methods for controlling playback
 
diff --git a/pitivi/utils.py b/pitivi/utils.py
index 75dc705..f0309ad 100644
--- a/pitivi/utils.py
+++ b/pitivi/utils.py
@@ -58,25 +58,6 @@ def time_to_string(value):
     mins = mins % 60
     return "%02d:%02d:%02d.%03d" % (hours, mins, sec, ms)
 
-def string_to_time(timecode):
-    """
-    Converts the given timecode string to nanoseconds.
-    Format must be HH:MM:SS.XXX
-    
-    Returns the time in nanoseconds, or False if the format is incorrect.
-    """
-    try:
-        hh, mm, foo = timecode.split(":")
-        ss, xxx = foo.split(".")
-        nanosecs = int(hh) * 3.6 * 10e12 \
-            + int(mm) * 6 * 10e10 \
-            + int(ss) * 10e9 \
-            + int(xxx) * 10e6
-        nanosecs = nanosecs / 10 # Compensate the 10 factor of e notation
-        return nanosecs
-    except:
-        return False
-
 def beautify_length(length):
     """
     Converts the given time in nanoseconds to a human readable string



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]