[pitivi] timeline: Create the title clip at the current position on the top layer



commit b6468f2cf05f1f445e72c399e10b385bdfcd5019
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Thu Nov 5 10:18:29 2015 +0100

    timeline: Create the title clip at the current position on the top layer
    
    Fixes https://phabricator.freedesktop.org/T3433
    
    Differential Revision: https://phabricator.freedesktop.org/D498
    Reviewed-by: Thibault Saunier <tsaunier gnome org>

 pitivi/medialibrary.py      |    2 +-
 pitivi/timeline/timeline.py |   69 ++++++++++++++++++++++++++++++-------------
 pitivi/titleeditor.py       |   24 ++++-----------
 pitivi/utils/timeline.py    |   10 ------
 4 files changed, 55 insertions(+), 50 deletions(-)
---
diff --git a/pitivi/medialibrary.py b/pitivi/medialibrary.py
index 8ecb328..7390b65 100644
--- a/pitivi/medialibrary.py
+++ b/pitivi/medialibrary.py
@@ -364,7 +364,7 @@ class MediaLibraryWidget(Gtk.Box, Loggable):
         self._removeSources()
 
     def _insertEndCb(self, unused_action):
-        self.app.gui.timeline_ui.insertEnd(self.getSelectedAssets())
+        self.app.gui.timeline_ui.insertAssets(self.getSelectedAssets(), -1)
 
     def _searchEntryChangedCb(self, entry):
         # With many hundred clips in an iconview with dynamic columns and
diff --git a/pitivi/timeline/timeline.py b/pitivi/timeline/timeline.py
index 0bd7032..4283bb0 100644
--- a/pitivi/timeline/timeline.py
+++ b/pitivi/timeline/timeline.py
@@ -1132,37 +1132,56 @@ class TimelineContainer(Gtk.Grid, Zoomable, Loggable):
 
     # Public API
 
-    def insertEnd(self, assets):
+    def insertAssets(self, assets, position=None):
         """
-        Allows to add any asset at the end of the current timeline.
+        Add assets to the timeline and create clips on the longest layer.
         """
-        self.app.action_log.begin("add clip")
+        layer = self._getLongestLayer()
+        self._insertClipsAndAssets(assets, position, layer)
+
+    def insertClips(self, clips, position=None):
+        """
+        Add clips to the timeline on the first layer.
+        """
+        layers = self._getLayers()
+        layer = layers[0]
+        self._insertClipsAndAssets(clips, position, layer)
+
+    def _insertClipsAndAssets(self, objs, position, layer):
         if self.bTimeline is None:
             raise TimelineError("No bTimeline set, this is a bug")
 
-        layer = self._getLongestLayer()
-
         # We need to snapshot this value, because we only do the zoom fit at the
         # end of clip insertion, but inserting multiple clips eventually changes
         # the value of self.zoomed_fitted as clips get progressively
         # inserted...
         zoom_was_fitted = self.zoomed_fitted
 
-        for asset in assets:
-            if isinstance(asset, GES.TitleClip):
-                clip_duration = asset.get_duration()
-            elif asset.is_image():
-                clip_duration = self.app.settings.imageClipLength * \
-                    Gst.SECOND / 1000.0
-            else:
-                clip_duration = asset.get_duration()
+        clip_position = self.__getInsertPosition(position)
+
+        self.app.action_log.begin("add asset")
+        for obj in objs:
+            if isinstance(obj, GES.Clip):
+                obj.set_start(clip_position)
+                layer.add_clip(obj)
+                duration = obj.get_duration()
+            elif isinstance(obj, GES.Asset):
+                if obj.is_image():
+                    duration = self.app.settings.imageClipLength * \
+                        Gst.SECOND / 1000.0
+                else:
+                    duration = obj.get_duration()
 
-            if not isinstance(asset, GES.TitleClip):
-                layer.add_asset(asset, self.bTimeline.props.duration,
-                                0, clip_duration, asset.get_supported_formats())
+                layer.add_asset(obj,
+                                start=clip_position,
+                                inpoint=0,
+                                duration=duration,
+                                track_types=obj.get_supported_formats())
             else:
-                asset.set_start(self.bTimeline.props.duration)
-                layer.add_clip(asset)
+                raise TimelineError("Cannot insert: %s" % type(obj))
+            clip_position += duration
+        self.app.action_log.commit()
+        self._project.pipeline.commit_timeline()
 
         if zoom_was_fitted:
             self._setBestZoomRatio()
@@ -1170,8 +1189,12 @@ class TimelineContainer(Gtk.Grid, Zoomable, Loggable):
             self.scrollToPixel(
                 Zoomable.nsToPixel(self.bTimeline.props.duration))
 
-        self.app.action_log.commit()
-        self._project.pipeline.commit_timeline()
+    def __getInsertPosition(self, position):
+        if position is None:
+            return self._project.pipeline.getPosition()
+        if position < 0:
+            return self.bTimeline.props.duration
+        return position
 
     def purgeObject(self, asset_id):
         """Remove all instances of an asset from the timeline."""
@@ -1325,7 +1348,11 @@ class TimelineContainer(Gtk.Grid, Zoomable, Loggable):
 
     def _getLayers(self):
         """
-        Make sure we have at least one layer in our timeline.
+        Get the layers of the timeline.
+
+        Makes sure there is at least one layer in the timeline.
+
+        @rtype: list of GES.Layer
         """
         layers = self.bTimeline.get_layers()
         if not layers:
diff --git a/pitivi/titleeditor.py b/pitivi/titleeditor.py
index b1f0d2d..a839f56 100644
--- a/pitivi/titleeditor.py
+++ b/pitivi/titleeditor.py
@@ -62,7 +62,6 @@ class TitleEditor(Loggable):
         self._drag_events = []
         self._signals_connected = False
         self._setting_props = False
-        self._setting_initial_props = False
         self._children_props_handler = None
 
         self._createUI()
@@ -244,31 +243,20 @@ class TitleEditor(Loggable):
         self.source = None
 
     def _createCb(self, unused_button):
-        """
-        The user clicked the "Create and insert" button, initialize the UI
-        """
         clip = GES.TitleClip()
         clip.set_duration(int(Gst.SECOND * 5))
-
-        # TODO: insert on the current layer at the playhead position.
-        # If no space is available, create a new layer to insert to on top.
-        self.app.gui.timeline_ui.insertEnd([clip])
-        self.app.gui.timeline_ui.timeline.selection.setToObj(clip, SELECT)
-
-        self._setting_initial_props = True
-        source = self.source = clip.get_children(False)[0]
+        self.app.gui.timeline_ui.insertClips([clip])
+        # Now that the clip is inserted in the timeline, it has a source which
+        # can be used to set its properties.
+        source = clip.get_children(False)[0]
         assert(source.set_child_property("text", ""))
         assert(source.set_child_property("foreground-color", BACKGROUND_DEFAULT_COLOR))
         assert(source.set_child_property("color", FOREGROUND_DEFAULT_COLOR))
         assert(source.set_child_property("font-desc", "Sans 10"))
-        self._setting_initial_props = False
-
-        self._updateFromSource()
+        # Select it so the Title editor becomes active.
+        self.app.gui.timeline_ui.timeline.selection.setSelection([clip], SELECT)
 
     def _propertyChangedCb(self, source, unused_gstelement, pspec):
-        if self._setting_initial_props:
-            return
-
         if self._setting_props:
             self.seeker.flush()
             return
diff --git a/pitivi/utils/timeline.py b/pitivi/utils/timeline.py
index ec24d71..1909346 100644
--- a/pitivi/utils/timeline.py
+++ b/pitivi/utils/timeline.py
@@ -103,14 +103,6 @@ class Selection(GObject.Object, Loggable):
         Loggable.__init__(self)
         self.selected = set()
 
-    def setToObj(self, obj, mode):
-        """
-        Convenience method for calling L{setSelection} with a single L{GES.Clip}
-
-        @see: L{setSelection}
-        """
-        self.setSelection(set([obj]), mode)
-
     def setSelection(self, objs, mode):
         """
         Update the current selection.
@@ -122,8 +114,6 @@ class Selection(GObject.Object, Loggable):
 
         @param objs: Timeline objects to update the selection with.
         @param mode: The type of update to apply. Can be C{SELECT}, C{UNSELECT} or C{SELECT_ADD}
-
-        @see: L{setToObj}
         """
         selection = set()
         for obj in objs:


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