[pitivi] Add drag'n'drop reordering for layers



commit 262ea2598bff7a0a4f1d3d1c2165cff264637b2f
Author: Paul Lange <palango gmx de>
Date:   Tue Jul 10 15:03:37 2012 +0200

    Add drag'n'drop reordering for layers

 pitivi/timeline/layer.py    |   14 +++++++-
 pitivi/timeline/timeline.py |   78 +++++++++++++++++++++++++++++++++++++++----
 pitivi/utils/ui.py          |    7 +++-
 3 files changed, 89 insertions(+), 10 deletions(-)
---
diff --git a/pitivi/timeline/layer.py b/pitivi/timeline/layer.py
index 3e0c867..1a33969 100644
--- a/pitivi/timeline/layer.py
+++ b/pitivi/timeline/layer.py
@@ -27,7 +27,8 @@ import gobject
 from gettext import gettext as _
 
 from pitivi.utils.loggable import Loggable
-from pitivi.utils.ui import LAYER_SPACING
+from pitivi.utils.ui import LAYER_SPACING, LAYER_CONTROL_TUPLE, \
+    TYPE_PITIVI_LAYER_CONTROL
 
 
 # TODO add tooltips
@@ -125,6 +126,17 @@ class BaseLayerControl(gtk.VBox, Loggable):
         self.popup.append(menu_dellayer)
         self.popup.show_all()
 
+        # Drag and drop
+        self.connect("drag_data_get", self._dragDataGetCb)
+        self.drag_source_set(gtk.gdk.BUTTON1_MASK,
+                             [LAYER_CONTROL_TUPLE],
+                             gtk.gdk.ACTION_MOVE)
+
+    def _dragDataGetCb(self, widget, context, selection, targetType, eventTime):
+        if targetType == TYPE_PITIVI_LAYER_CONTROL:
+            selection.set(selection.target, 8,
+                          str(self._app.gui.timeline_ui.controls.getControlIdString(self)))
+
     def getSelected(self):
         return self._selected
 
diff --git a/pitivi/timeline/timeline.py b/pitivi/timeline/timeline.py
index 565743e..be2c263 100644
--- a/pitivi/timeline/timeline.py
+++ b/pitivi/timeline/timeline.py
@@ -28,6 +28,7 @@ import sys
 import time
 
 import gtk
+import gtk.gdk
 import gst
 import ges
 import glib
@@ -58,7 +59,7 @@ from pitivi.utils.loggable import Loggable
 from pitivi.utils.ui import SPACING, CANVAS_SPACING, unpack_cairo_pattern, \
     LAYER_SPACING, TYPE_PITIVI_FILESOURCE, VIDEO_EFFECT_TUPLE, Point, \
     AUDIO_EFFECT_TUPLE, EFFECT_TUPLE, FILESOURCE_TUPLE, TYPE_PITIVI_EFFECT, \
-    LAYER_CREATION_BLOCK_TIME
+    LAYER_CREATION_BLOCK_TIME, LAYER_CONTROL_TUPLE
 
 # FIXME GES Port regression
 # from pitivi.utils.align import AutoAligner
@@ -510,6 +511,13 @@ class TimelineControls(gtk.VBox, Loggable):
         self.priority_block = sys.maxint
         self.priority_block_time = time.time()
 
+        # drag'n' drop
+        self.connect("drag_data_received", self._dragDataReceivedCb)
+        self.drag_dest_set(gtk.DEST_DEFAULT_MOTION |
+                             gtk.DEST_DEFAULT_HIGHLIGHT |
+                             gtk.DEST_DEFAULT_DROP,
+                             [LAYER_CONTROL_TUPLE], gtk.gdk.ACTION_MOVE)
+
     def _sizeAllocatedCb(self, widget, alloc):
         if self.children():
             self.separator_height = self.children()[0].getSeparatorHeight()
@@ -561,15 +569,12 @@ class TimelineControls(gtk.VBox, Loggable):
         self._hideLastSeparator()
 
     def _orderControls(self):
-        i = 0
-        j = len(self.get_children()) / 2
+        middle = len(self.get_children()) / 2
         for child in self.get_children():
             if isinstance(child, VideoLayerControl):
-                self.reorder_child(child, i)
-                i += 1
+                self.reorder_child(child, child._layer.get_priority())
             elif isinstance(child, AudioLayerControl):
-                self.reorder_child(child, j)
-                j += 1
+                self.reorder_child(child, middle + child._layer.get_priority())
 
     def _hideLastSeparator(self):
         if self.children():
@@ -701,6 +706,65 @@ class TimelineControls(gtk.VBox, Loggable):
             else:
                 current_y += child.getHeight()
 
+    def _dragDataReceivedCb(self, widget, context, x, y, selection,
+                            targetType, time):
+        """
+        Handles received drag data to reorder layers
+        """
+        # get the moved control widget
+        widget = self.getControlFromId(int(selection.data))
+        widget_type = type(widget)
+
+        counter = 0
+        index = 0
+
+        # find new position
+        for child in self.get_children():
+            next = counter + child.getHeight()
+            if y >= counter and y < next:
+                self.reorder_child(widget, index)
+
+            counter = next
+            index += 1
+
+        # reorder linked audio/video layer
+        index = 0
+        for child in self.get_children():
+            # only set layer priority once
+            if type(child) == widget_type:
+                child._layer.set_priority(index)
+                index += 1
+
+        # order controls and update separators
+        self._orderControls()
+        self._hideLastSeparator()
+
+    def getControlIdString(self, control):
+        """
+        Returns an unique ID of a control
+
+        Used for drag and drop
+        """
+        counter = 0
+        for child in self.get_children():
+            if child == control:
+                return counter
+
+            counter += 1
+
+    def getControlFromId(self, id):
+        """
+        Returns the control for an ID
+
+        Used for drag and drop
+        """
+        counter = 0
+        for child in self.get_children():
+            if counter == id:
+                return child
+
+            counter += 1
+
 
 class InfoStub(gtk.HBox, Loggable):
     """
diff --git a/pitivi/utils/ui.py b/pitivi/utils/ui.py
index ee7e545..8bca7d7 100644
--- a/pitivi/utils/ui.py
+++ b/pitivi/utils/ui.py
@@ -53,6 +53,9 @@ PADDING = 6
 
 CANVAS_SPACING = 21
 
+# Layer creation blocking time in s
+LAYER_CREATION_BLOCK_TIME = 0.2
+
 ##
 #   Drag'n drop constants
 ##
@@ -69,8 +72,7 @@ TYPE_PITIVI_VIDEO_EFFECT = 29
 TYPE_PITIVI_AUDIO_TRANSITION = 30
 TYPE_PITIVI_VIDEO_TRANSITION = 31
 
-# Layer creation blocking time in s
-LAYER_CREATION_BLOCK_TIME = 0.2
+TYPE_PITIVI_LAYER_CONTROL = 32
 
 FILE_TUPLE = ("text/plain", 0, TYPE_TEXT_PLAIN)
 URI_TUPLE = ("text/uri-list", 0, TYPE_URI_LIST)
@@ -80,6 +82,7 @@ AUDIO_EFFECT_TUPLE = ("pitivi/audio-effect", 0, TYPE_PITIVI_AUDIO_EFFECT)
 VIDEO_EFFECT_TUPLE = ("pitivi/video-effect", 0, TYPE_PITIVI_VIDEO_EFFECT)
 AUDIO_TRANSITION_TUPLE = ("pitivi/audio-transition", 0, TYPE_PITIVI_AUDIO_TRANSITION)
 VIDEO_TRANSITION_TUPLE = ("pitivi/video-transition", 0, TYPE_PITIVI_VIDEO_TRANSITION)
+LAYER_CONTROL_TUPLE = ("pitivi/layer-control", 0, TYPE_PITIVI_LAYER_CONTROL)
 
 
 # ---------------------- ARGB color helper-------------------------------------#



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