[pitivi] titleeditor: Allow adding and editing multiple titles



commit 1526fac83e0956e4f55e7978a0b845b4f0e4789c
Author: Fabian Orccon <fabian orccon pucp pe>
Date:   Sun Mar 2 20:37:53 2014 -0500

    titleeditor: Allow adding and editing multiple titles

 pitivi/mainwindow.py         |    2 +
 pitivi/titleeditor.py        |   65 ++++++++++++++++++++---------------------
 pitivi/utils/timeline.py     |   13 ++++++++-
 tests/test_utils_timeline.py |   22 ++++++++++++++
 4 files changed, 68 insertions(+), 34 deletions(-)
---
diff --git a/pitivi/mainwindow.py b/pitivi/mainwindow.py
index f5081da..ceada53 100644
--- a/pitivi/mainwindow.py
+++ b/pitivi/mainwindow.py
@@ -296,6 +296,8 @@ class PitiviMainWindow(Gtk.ApplicationWindow, Loggable):
         self.timeline_ui.setProjectManager(self.app.project_manager)
         self.vpaned.pack2(self.timeline_ui, resize=True, shrink=False)
 
+        self.timeline_ui.timeline.selection.connect("selection-changed", 
self.title_editor.selectionChangedCb)
+
         # Enable our shortcuts for HeaderBar buttons and menu items:
         self._set_keyboard_shortcuts()
 
diff --git a/pitivi/titleeditor.py b/pitivi/titleeditor.py
index 5827706..cffb3cd 100644
--- a/pitivi/titleeditor.py
+++ b/pitivi/titleeditor.py
@@ -583,7 +583,6 @@ class TitleEditor(Loggable):
         self.bt = {}
         self.settings = {}
         self.source = None
-        self.created = False
         self.seeker = Seeker()
 
         #Drag attributes
@@ -767,49 +766,41 @@ class TitleEditor(Loggable):
                 self.seeker.flush()
                 return
 
-    def _reset(self):
-        #TODO: reset not only text
-        self.markup_button.set_active(False)
-        self.pangobuffer.set_text("")
-        self.textbuffer.set_text("")
-        #Set right buffer
-        self._markupToggleCb(self.markup_button)
-
-    def set_source(self, source, created=False):  # FIXME: this "created" boolean param is a hack
+    def set_source(self, source):
         """
-        Set the GESTitleClip to be used with the title editor.
-        This can be called either from the title editor in _createCb, or by
-        track.py to set the source to None.
+        Set the clip to be edited with this editor.
+
+        @type source: L{GES.TitleSource}
         """
         self.debug("Source set to %s", source)
+        self._deactivate()
+        assert isinstance(source, GES.TextOverlay) or \
+            isinstance(source, GES.TitleSource)
         self.source = source
-        self._reset()
-        self.created = created
-        # We can't just assert source is not None... because track.py may ask us
-        # to reset the source to None
-        if source is None:
-            self._deactivate()
-        else:
-            assert isinstance(source, GES.TextOverlay) or \
-                isinstance(source, GES.TitleSource) or \
-                isinstance(source, GES.TitleClip)
-            self._updateFromSource()
-            self._activate()
+        self._updateFromSource()
+        self._activate()
+
+    def unset_source(self):
+        self.source = None
+        self._deactivate()
+        # TODO: reset not only text
+        self.markup_button.set_active(False)
+        self.pangobuffer.set_text("")
+        self.textbuffer.set_text("")
+        # Set the right buffer
+        self._markupToggleCb(self.markup_button)
 
     def _createCb(self, unused_button):
         """
         The user clicked the "Create and insert" button, initialize the UI
         """
-        source = GES.TitleClip()
-        source.set_text("")
-        source.set_duration(int(Gst.SECOND * 5))
-        self.set_source(source, True)
+        clip = GES.TitleClip()
+        clip.set_text("")
+        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([self.source])
-        self.app.gui.timeline_ui.timeline.selection.setToObj(self.source, SELECT)
-        #After insertion consider as not created
-        self.created = False
+        self.app.gui.timeline_ui.insertEnd([clip])
+        self.app.gui.timeline_ui.timeline.selection.setToObj(clip, SELECT)
 
     def _connect_signals(self):
         if not self._signals_connected:
@@ -879,3 +870,11 @@ class TitleEditor(Loggable):
             self._connect_signals()
         else:
             self._disconnect_signals()
+
+    def selectionChangedCb(self, selection):
+        selected_clip = selection.getSingleClip(GES.TitleClip)
+        source = selected_clip and selected_clip.get_children(False)[0]
+        if source:
+            self.set_source(source)
+        else:
+            self.unset_source()
diff --git a/pitivi/utils/timeline.py b/pitivi/utils/timeline.py
index 3c7e1bb..ddd3a8d 100644
--- a/pitivi/utils/timeline.py
+++ b/pitivi/utils/timeline.py
@@ -143,7 +143,6 @@ class Selection(Signallable):
             for element in obj.get_children(False):
                 if not isinstance(element, GES.BaseEffect) and not isinstance(element, GES.TextOverlay):
                     element.selected.selected = True
-
         self.emit("selection-changed")
 
     def getSelectedTrackElements(self):
@@ -167,6 +166,18 @@ class Selection(Signallable):
                     effects.append(element)
         return effects
 
+    def getSingleClip(self, clip_type):
+        """
+        Returns the single-selected clip, if any.
+
+        @param clip_type: The class the clip must be an instance of.
+        """
+        if len(self.selected) == 1:
+            clip = tuple(self.selected)[0]
+            if isinstance(clip, clip_type):
+                return clip
+        return None
+
     def __len__(self):
         return len(self.selected)
 
diff --git a/tests/test_utils_timeline.py b/tests/test_utils_timeline.py
index b9657eb..fb70b08 100644
--- a/tests/test_utils_timeline.py
+++ b/tests/test_utils_timeline.py
@@ -20,6 +20,8 @@
 import mock
 from unittest import TestCase
 
+from gi.repository import GES
+
 from pitivi.utils.timeline import Selected, Selection, SELECT, SELECT_ADD, \
     UNSELECT
 
@@ -49,3 +51,23 @@ class TestSelection(TestCase):
         self.assertTrue(selection)
         selection.setSelection([clip1], UNSELECT)
         self.assertFalse(selection)
+
+    def testGetSingleClip(self):
+        selection = Selection()
+        clip1 = GES.UriClip()
+        clip2 = GES.TitleClip()
+
+        # Selection empty.
+        self.assertFalse(selection.getSingleClip(GES.TitleClip))
+
+        # Selection contains only a non-requested-type clip.
+        selection.setSelection([clip1], SELECT)
+        self.assertFalse(selection.getSingleClip(GES.TitleClip))
+
+        # Selection contains only requested-type clip.
+        selection.setSelection([clip2], SELECT)
+        self.assertEqual(clip2, selection.getSingleClip(GES.TitleClip))
+
+        # Selection contains more than one clip.
+        selection.setSelection([clip1, clip2], SELECT)
+        self.assertFalse(selection.getSingleClip(GES.UriClip))


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