[pitivi/ges: 172/287] utils: Move UI utils to ui.py



commit 371f892fa3812eaaed89c136ba2ac624ce83597f
Author: Thibault Saunier <thibault saunier collabora com>
Date:   Mon Jan 9 17:14:24 2012 -0300

    utils: Move UI utils to ui.py
    
    Make some little refactoring on the way

 pitivi/sourcelist.py            |    2 +-
 pitivi/ui/dynamic.py            |    5 +-
 pitivi/ui/filechooserpreview.py |   13 ++-
 pitivi/ui/ruler.py              |    2 +-
 pitivi/utils/align.py           |    7 +-
 pitivi/utils/misc.py            |  226 ++++++++++++++-------------------------
 pitivi/utils/ui.py              |   67 +++++++++++-
 tests/test_utils.py             |    2 +-
 8 files changed, 165 insertions(+), 159 deletions(-)
---
diff --git a/pitivi/sourcelist.py b/pitivi/sourcelist.py
index b639c06..016787c 100644
--- a/pitivi/sourcelist.py
+++ b/pitivi/sourcelist.py
@@ -39,7 +39,7 @@ from hashlib import md5
 from pitivi.configure import get_pixmap_dir
 from pitivi.settings import GlobalSettings
 
-from pitivi.utils.misc import beautify_length
+from pitivi.utils.ui import beautify_length
 from pitivi.utils.signal import SignalGroup
 from pitivi.utils.signal import Signallable
 from pitivi.utils.loggable import Loggable
diff --git a/pitivi/ui/dynamic.py b/pitivi/ui/dynamic.py
index bf2a958..d0bb4c0 100644
--- a/pitivi/ui/dynamic.py
+++ b/pitivi/ui/dynamic.py
@@ -29,10 +29,9 @@ import re
 import sys
 import gst
 from gettext import gettext as _
-from pitivi.utils.misc import time_to_string
-from pitivi.utils.ui import unpack_color, pack_color_32, pack_color_64
 import pango
-from pitivi.utils.ui import SPACING
+from pitivi.utils.ui import unpack_color, pack_color_32, pack_color_64, \
+    time_to_string, SPACING
 
 
 class DynamicWidget(object):
diff --git a/pitivi/ui/filechooserpreview.py b/pitivi/ui/filechooserpreview.py
index 91ecfe9..acfec3e 100644
--- a/pitivi/ui/filechooserpreview.py
+++ b/pitivi/ui/filechooserpreview.py
@@ -5,13 +5,16 @@ import gtk
 import pango
 import os
 
-from pitivi.utils.loggable import Loggable
-from pitivi.utils.ui import beautify_stream
-from pitivi.utils.misc import beautify_length, uri_is_valid
+from gettext import gettext as _
+
 from pitivi.configure import get_pixmap_dir
 from pitivi.settings import GlobalSettings
-from gettext import gettext as _
-from pitivi.utils.ui import SPACING
+
+from pitivi.utils.loggable import Loggable
+from pitivi.utils.misc import uri_is_valid
+from pitivi.utils.ui import beautify_length, beautify_stream,\
+    SPACING
+
 from pitivi.ui.viewer import ViewerWidget
 
 DEFAULT_AUDIO_IMAGE = os.path.join(get_pixmap_dir(), "pitivi-sound.png")
diff --git a/pitivi/ui/ruler.py b/pitivi/ui/ruler.py
index f2a4771..9c5ab79 100644
--- a/pitivi/ui/ruler.py
+++ b/pitivi/ui/ruler.py
@@ -31,7 +31,7 @@ from pitivi.utils.playback import Seeker
 
 from pitivi.ui.zoominterface import Zoomable
 from pitivi.utils.loggable import Loggable
-from pitivi.utils.misc import time_to_string
+from pitivi.utils.ui import time_to_string
 
 
 class ScaleRuler(gtk.DrawingArea, Zoomable, Loggable):
diff --git a/pitivi/utils/align.py b/pitivi/utils/align.py
index 55343bf..6b7355f 100644
--- a/pitivi/utils/align.py
+++ b/pitivi/utils/align.py
@@ -23,16 +23,17 @@
 Classes for automatic alignment of L{TimelineObject}s
 """
 
+import gobject
+import gst
 import array
 import time
+
 try:
     import numpy
 except ImportError:
     numpy = None
 
-import gobject
-import gst
-from pitivi.utils.misc import beautify_ETA, call_false
+from pitivi.utils.ui import beautify_ETA, call_false
 from pitivi.utils.loggable import Loggable
 from pitivi.utils.alignalgs import rigidalign
 
diff --git a/pitivi/utils/misc.py b/pitivi/utils/misc.py
index 34db7e6..a433012 100644
--- a/pitivi/utils/misc.py
+++ b/pitivi/utils/misc.py
@@ -55,71 +55,6 @@ def between(a, b, c):
     return (a <= b) and (b <= c)
 
 
-def time_to_string(value):
-    """
-    Converts the given time in nanoseconds to a human readable string
-
-    Format HH:MM:SS.XXX
-    """
-    if value == gst.CLOCK_TIME_NONE:
-        return "--:--:--.---"
-    ms = value / gst.MSECOND
-    sec = ms / 1000
-    ms = ms % 1000
-    mins = sec / 60
-    sec = sec % 60
-    hours = mins / 60
-    mins = mins % 60
-    return "%01d:%02d:%02d.%03d" % (hours, mins, sec, ms)
-
-
-def beautify_length(length):
-    """
-    Converts the given time in nanoseconds to a human readable string
-    """
-    sec = length / gst.SECOND
-    mins = sec / 60
-    sec = sec % 60
-    hours = mins / 60
-    mins = mins % 60
-
-    parts = []
-    if hours:
-        parts.append(ngettext("%d hour", "%d hours", hours) % hours)
-
-    if mins:
-        parts.append(ngettext("%d minute", "%d minutes", mins) % mins)
-
-    if not hours and sec:
-        parts.append(ngettext("%d second", "%d seconds", sec) % sec)
-
-    return ", ".join(parts)
-
-
-def beautify_ETA(length):
-    """
-    Converts the given time in nanoseconds to a fuzzy estimate,
-    intended for progress ETAs, not to indicate a clip's duration.
-    """
-    sec = length / gst.SECOND
-    mins = sec / 60
-    sec = sec % 60
-    hours = mins / 60
-    mins = mins % 60
-
-    parts = []
-    if hours:
-        parts.append(ngettext("%d hour", "%d hours", hours) % hours)
-
-    if mins:
-        parts.append(ngettext("%d minute", "%d minutes", mins) % mins)
-
-    if not hours and mins < 2 and sec:
-        parts.append(ngettext("%d second", "%d seconds", sec) % sec)
-
-    return ", ".join(parts)
-
-
 def call_false(function, *args, **kwargs):
     """ Helper function for calling an arbitrary function once in the gobject
         mainloop.  Any positional or keyword arguments after the function will
@@ -147,86 +82,8 @@ def bin_contains(bin, element):
             return True
     return False
 
-# Python re-implementation of binary search algorithm found here:
-# http://en.wikipedia.org/wiki/Binary_search
-#
-# This is the iterative version without the early termination branch, which
-# also tells us the element of A that are nearest to Value, if the element we
-# want is not found. This is useful for implementing edge snaping in the UI,
-# where we repeatedly search through a list of control points for the one
-# closes to the cursor. Because we don't care whether the cursor position
-# matches the list, this function returns the index of the lement closest to
-# value in the array.
-
-
-def binary_search(col, value):
-    low = 0
-    high = len(col)
-    while (low < high):
-        mid = (low + high) / 2
-        if (col[mid] < value):
-            low = mid + 1
-        else:
-            #can't be high = mid-1: here col[mid] >= value,
-            #so high can't be < mid if col[mid] == value
-            high = mid
-    return low
-
-
-def argmax(func, seq):
-    """return the element of seq that gives max(map(func, seq))"""
-    def compare(a1, b1):
-        if a1[0] > b1[0]:
-            return a1
-        return b1
-    # using a generator expression here should save memory
-    objs = ((func(val), val) for val in seq)
-    return reduce(compare, objs)[1]
-
-
-def same(seq):
-    i = iter(seq)
-    first = i.next()
-    for item in i:
-        if first != item:
-            return None
-    return first
-
-
-def linkDynamic(element, target):
-
-    def pad_added(bin, pad, target):
-        compatpad = target.get_compatible_pad(pad)
-        if compatpad:
-            pad.link_full(compatpad, gst.PAD_LINK_CHECK_NOTHING)
-    element.connect("pad-added", pad_added, target)
-
-
-def element_make_many(*args):
-    return tuple((gst.element_factory_make(arg) for arg in args))
-
-
-def pipeline(graph):
-    E = graph.iteritems()
-    V = graph.iterkeys()
-    p = gst.Pipeline()
-    p.add(*V)
-    for u, v in E:
-        if v:
-            try:
-                u.link(v)
-            except gst.LinkError:
-                linkDynamic(u, v)
-    return p
-
 
-def filter_(caps):
-    f = gst.element_factory_make("capsfilter")
-    f.props.caps = gst.caps_from_string(caps)
-    return f
-
-
-## URI functions
+#------------------------------ URI helpers   --------------------------------#
 def isWritable(path):
     """Check if the file/path is writable"""
     try:
@@ -272,6 +129,7 @@ def get_filesystem_encoding():
     return sys.getfilesystemencoding() or "utf-8"
 
 
+#------------------------------ Gst helpers   --------------------------------#
 def get_controllable_properties(element):
     """
     Returns a list of controllable properties for the given
@@ -294,6 +152,40 @@ def get_controllable_properties(element):
     return res
 
 
+def linkDynamic(element, target):
+
+    def pad_added(bin, pad, target):
+        compatpad = target.get_compatible_pad(pad)
+        if compatpad:
+            pad.link_full(compatpad, gst.PAD_LINK_CHECK_NOTHING)
+    element.connect("pad-added", pad_added, target)
+
+
+def element_make_many(*args):
+    return tuple((gst.element_factory_make(arg) for arg in args))
+
+
+def pipeline(graph):
+    E = graph.iteritems()
+    V = graph.iterkeys()
+    p = gst.Pipeline()
+    p.add(*V)
+    for u, v in E:
+        if v:
+            try:
+                u.link(v)
+            except gst.LinkError:
+                linkDynamic(u, v)
+    return p
+
+
+def filter_(caps):
+    f = gst.element_factory_make("capsfilter")
+    f.props.caps = gst.caps_from_string(caps)
+    return f
+
+
+#-------------------------- Sorting helpers   --------------------------------#
 def start_insort_left(a, x, lo=0, hi=None):
     if hi is None:
         hi = len(a)
@@ -365,6 +257,52 @@ def quantize(input, interval):
     return (input // interval) * interval
 
 
+# Python re-implementation of binary search algorithm found here:
+# http://en.wikipedia.org/wiki/Binary_search
+#
+# This is the iterative version without the early termination branch, which
+# also tells us the element of A that are nearest to Value, if the element we
+# want is not found. This is useful for implementing edge snaping in the UI,
+# where we repeatedly search through a list of control points for the one
+# closes to the cursor. Because we don't care whether the cursor position
+# matches the list, this function returns the index of the lement closest to
+# value in the array.
+
+
+def binary_search(col, value):
+    low = 0
+    high = len(col)
+    while (low < high):
+        mid = (low + high) / 2
+        if (col[mid] < value):
+            low = mid + 1
+        else:
+            #can't be high = mid-1: here col[mid] >= value,
+            #so high can't be < mid if col[mid] == value
+            high = mid
+    return low
+
+
+def argmax(func, seq):
+    """return the element of seq that gives max(map(func, seq))"""
+    def compare(a1, b1):
+        if a1[0] > b1[0]:
+            return a1
+        return b1
+    # using a generator expression here should save memory
+    objs = ((func(val), val) for val in seq)
+    return reduce(compare, objs)[1]
+
+
+def same(seq):
+    i = iter(seq)
+    first = i.next()
+    for item in i:
+        if first != item:
+            return None
+    return first
+
+
 def show_user_manual():
     time_now = int(time.time())
     for uri in (APPMANUALURL_OFFLINE, APPMANUALURL_ONLINE):
diff --git a/pitivi/utils/ui.py b/pitivi/utils/ui.py
index 0131ed6..0de63c9 100644
--- a/pitivi/utils/ui.py
+++ b/pitivi/utils/ui.py
@@ -154,7 +154,7 @@ def hex_to_rgb(value):
     return tuple(float(int(value[i:i + 2], 16)) / 255.0 for i in range(0, 6, 2))
 
 
-#--- Utils to make gst.DiscovererInfo ready to be displayed in the UI --------#
+#------ Helper to help beatify indos so they can be displayed in the UI -----#
 def beautify_info(info):
     ranks = {
         gst.pbutils.DiscovererVideoInfo: 0,
@@ -211,6 +211,71 @@ def beautify_stream(stream):
     raise NotImplementedError
 
 
+def time_to_string(value):
+    """
+    Converts the given time in nanoseconds to a human readable string
+
+    Format HH:MM:SS.XXX
+    """
+    if value == gst.CLOCK_TIME_NONE:
+        return "--:--:--.---"
+    ms = value / gst.MSECOND
+    sec = ms / 1000
+    ms = ms % 1000
+    mins = sec / 60
+    sec = sec % 60
+    hours = mins / 60
+    mins = mins % 60
+    return "%01d:%02d:%02d.%03d" % (hours, mins, sec, ms)
+
+
+def beautify_length(length):
+    """
+    Converts the given time in nanoseconds to a human readable string
+    """
+    sec = length / gst.SECOND
+    mins = sec / 60
+    sec = sec % 60
+    hours = mins / 60
+    mins = mins % 60
+
+    parts = []
+    if hours:
+        parts.append(ngettext("%d hour", "%d hours", hours) % hours)
+
+    if mins:
+        parts.append(ngettext("%d minute", "%d minutes", mins) % mins)
+
+    if not hours and sec:
+        parts.append(ngettext("%d second", "%d seconds", sec) % sec)
+
+    return ", ".join(parts)
+
+
+def beautify_ETA(length):
+    """
+    Converts the given time in nanoseconds to a fuzzy estimate,
+    intended for progress ETAs, not to indicate a clip's duration.
+    """
+    sec = length / gst.SECOND
+    mins = sec / 60
+    sec = sec % 60
+    hours = mins / 60
+    mins = mins % 60
+
+    parts = []
+    if hours:
+        parts.append(ngettext("%d hour", "%d hours", hours) % hours)
+
+    if mins:
+        parts.append(ngettext("%d minute", "%d minutes", mins) % mins)
+
+    if not hours and mins < 2 and sec:
+        parts.append(ngettext("%d second", "%d seconds", sec) % sec)
+
+    return ", ".join(parts)
+
+
 #--------------------- UI drawing helper -------------------------------------#
 # from http://cairographics.org/cookbook/roundedrectangles/
 def roundedrec(context, x, y, w, h, r=10):
diff --git a/tests/test_utils.py b/tests/test_utils.py
index e434f90..3076f91 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -23,7 +23,7 @@
 from unittest import TestCase
 
 import gst
-from pitivi.utils.misc import beautify_length
+from pitivi.utils.ui import beautify_length
 
 second = gst.SECOND
 minute = second * 60



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