~timo-jyrinki/ubuntu/trusty/pitivi/backport_utopic_fixes

« back to all changes in this revision

Viewing changes to pitivi/timeline/gap.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2014-04-05 15:28:16 UTC
  • mfrom: (6.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20140405152816-6lijoax4cngiz5j5
Tags: 0.93-3
* debian/control:
  + Depend on python-gi (>= 3.10), older versions do not work
    with pitivi (Closes: #732813).
  + Add missing dependency on gir1.2-clutter-gst-2.0 (Closes: #743692).
  + Add suggests on gir1.2-notify-0.7 and gir1.2-gnomedesktop-3.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# PiTiVi , Non-linear video editor
2
 
#
3
 
#       pitivi/timeline/gap.py
4
 
#
5
 
# Copyright (c) 2009, Alessandro Decina <alessandro.decina@collabora.co.uk>
6
 
#
7
 
# This program is free software; you can redistribute it and/or
8
 
# modify it under the terms of the GNU Lesser General Public
9
 
# License as published by the Free Software Foundation; either
10
 
# version 2.1 of the License, or (at your option) any later version.
11
 
#
12
 
# This program is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
# Lesser General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU Lesser General Public
18
 
# License along with this program; if not, write to the
19
 
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20
 
# Boston, MA 02110-1301, USA.
21
 
 
22
 
from pitivi.utils import infinity
23
 
 
24
 
 
25
 
class Gap(object):
26
 
    def __init__(self, left_object, right_object, start, duration):
27
 
        self.left_object = left_object
28
 
        self.right_object = right_object
29
 
        self.start = start
30
 
        self.initial_duration = duration
31
 
 
32
 
    def __cmp__(self, other):
33
 
        if other is None or other is invalid_gap:
34
 
            return -1
35
 
        return cmp(self.duration, other.duration)
36
 
 
37
 
    @classmethod
38
 
    def findAroundObject(self, timeline_object, priority=-1, tracks=None):
39
 
        from pitivi.timeline.timeline import TimelineError
40
 
 
41
 
        timeline = timeline_object.timeline
42
 
        try:
43
 
            prev = timeline.getPreviousTimelineObject(timeline_object,
44
 
                    priority, tracks)
45
 
        except TimelineError:
46
 
            left_object = None
47
 
            right_object = timeline_object
48
 
            start = 0
49
 
            duration = timeline_object.start
50
 
        else:
51
 
            left_object = prev
52
 
            right_object = timeline_object
53
 
            start = prev.start + prev.duration
54
 
            duration = timeline_object.start - start
55
 
 
56
 
        left_gap = Gap(left_object, right_object, start, duration)
57
 
 
58
 
        try:
59
 
            next = timeline.getNextTimelineObject(timeline_object,
60
 
                    priority, tracks)
61
 
        except TimelineError:
62
 
            left_object = timeline_object
63
 
            right_object = None
64
 
            start = timeline_object.start + timeline_object.duration
65
 
            duration = infinity
66
 
        else:
67
 
            left_object = timeline_object
68
 
            right_object = next
69
 
            start = timeline_object.start + timeline_object.duration
70
 
            duration = next.start - start
71
 
 
72
 
        right_gap = Gap(left_object, right_object, start, duration)
73
 
 
74
 
        return left_gap, right_gap
75
 
 
76
 
    @classmethod
77
 
    def findAllGaps(self, objs):
78
 
        """Find all the gaps in a given set of objects: i.e. find all the
79
 
        spans of time which are covered by no object in the given set"""
80
 
        duration = 0
81
 
        gaps = []
82
 
        prev = None
83
 
 
84
 
        # examine each object in order of increasing start time
85
 
        for obj in sorted(objs, key=lambda x: x.start):
86
 
            start = obj.start
87
 
            end = obj.start + obj.duration
88
 
 
89
 
            # only if the current object starts after the total timeline
90
 
            # duration is a gap created.
91
 
            if start > duration:
92
 
                gaps.append(Gap(prev, obj, duration, start - duration))
93
 
            duration = max(duration, end)
94
 
            prev = obj
95
 
        return gaps
96
 
 
97
 
    @property
98
 
    def duration(self):
99
 
        if self.left_object is None and self.right_object is None:
100
 
            return self.initial_duration
101
 
 
102
 
        if self.initial_duration is infinity:
103
 
            return self.initial_duration
104
 
 
105
 
        if self.left_object is None:
106
 
            return self.right_object.start
107
 
 
108
 
        if self.right_object is None:
109
 
            return infinity
110
 
 
111
 
        res = self.right_object.start - \
112
 
                (self.left_object.start + self.left_object.duration)
113
 
        return res
114
 
 
115
 
 
116
 
class InvalidGap(object):
117
 
    pass
118
 
 
119
 
invalid_gap = InvalidGap()
120
 
 
121
 
 
122
 
class SmallestGapsFinder(object):
123
 
    def __init__(self, internal_objects):
124
 
        self.left_gap = None
125
 
        self.right_gap = None
126
 
        self.internal_objects = internal_objects
127
 
 
128
 
    def update(self, left_gap, right_gap):
129
 
        self.updateGap(left_gap, "left_gap")
130
 
        self.updateGap(right_gap, "right_gap")
131
 
 
132
 
    def updateGap(self, gap, min_gap_name):
133
 
        if self.isInternalGap(gap):
134
 
            return
135
 
 
136
 
        min_gap = getattr(self, min_gap_name)
137
 
 
138
 
        if min_gap is invalid_gap or gap.duration < 0:
139
 
            setattr(self, min_gap_name, invalid_gap)
140
 
            return
141
 
 
142
 
        if min_gap is None or gap < min_gap:
143
 
            setattr(self, min_gap_name, gap)
144
 
 
145
 
    def isInternalGap(self, gap):
146
 
        gap_objects = set([gap.left_object, gap.right_object])
147
 
 
148
 
        return gap_objects.issubset(self.internal_objects)