pitivi r1372 - trunk/pitivi/ui



Author: edwardrv
Date: Fri Nov 28 17:04:02 2008
New Revision: 1372
URL: http://svn.gnome.org/viewvc/pitivi?rev=1372&view=rev

Log:
added new ui classes based on receiver

Added:
   trunk/pitivi/ui/controller.py
   trunk/pitivi/ui/transform.py
   trunk/pitivi/ui/view.py

Added: trunk/pitivi/ui/controller.py
==============================================================================
--- (empty file)
+++ trunk/pitivi/ui/controller.py	Fri Nov 28 17:04:02 2008
@@ -0,0 +1,129 @@
+from pitivi.receiver import Receiver, handler
+from point import Point
+
+# Controllers are reusable and implement specific behaviors. Currently this
+# Includes only click, and drag. Multiple controllers could be attached to a
+# given view, but might interfere with each other if they attempt to handle
+# the same set of signals. It is probably better to define a new controller
+# that explictly combines the functionality of both when custom behavior is
+# desired.
+
+#TODO: refactor to handle cursors
+
+class BaseController(Receiver):
+
+    def __init__(self, model=None, view=None):
+        Receiver.__init__(self)
+        self.model = model
+        self.view = view
+
+class Controller(BaseController):
+
+    """A controller which implements drag-and-drop bahavior on connected view
+    objects. Subclasses may override the drag_start, drag_end, pos, and
+    set_pos methods"""
+
+    _dragging = None
+    _canvas = None
+    _mouse_down = None
+    _ptr_within = False
+    _last_click = None
+
+    def __init__(self, model=None, view=None):
+        BaseController.__init__(self, model, view)
+
+## signal handlers
+
+    @handler("view", "enter_notify_event")
+    def enter_notify_event(self, item, target, event):
+        self.enter(item, target)
+        self._ptr_within = True
+        return True
+
+    @handler("view", "leave_notify_event")
+    def leave_notify_event(self, item, target, event):
+        self._ptr_within = False
+        if not self._dragging:
+            self.leave(item, target)
+        return True
+
+    @handler("view", "button_press_event")
+    def button_press_event(self, item, target, event):
+        if not self._canvas:
+            self._canvas = item.get_canvas()
+        self._dragging = target
+        self._mouse_down = self.pos - self.transform(Point.from_event,
+            canvas, event)
+        self._drag_start(item, target, event)
+        return True
+
+    @handler("view", "motion_notify_event")
+    def motion_notify_event(self, item, target, event):
+        if self._dragging:
+            self.set_pos(self._dragging, 
+                self.transform(self._mouse_down + Point.from_event(canvas,
+                    event)))
+            return True
+        return False
+
+    @handler("view", "button_release_event")
+    def button_release_event(self, item, target, event):
+        self._drag_end(item, self._dragging, event)
+        self._dragging = None
+        return True
+
+## internal callbacks
+
+   def _drag_start(self, item, target, event):
+        self._view.activate()
+        self.drag_start()
+
+    def _drag_end(self, item, target, event):
+        self.drag_end()
+        if self._ptr_within:
+            self._view.focus()
+            if self._last_click and (event.time - self._last_click < 400):
+                self.double_click(event_coords(self._canvas, event))
+            else:
+                self.click(event_coords(self._canvas, event))
+            self._last_click = event.time
+        else:
+            self._view.normal()
+
+## protected interface for subclasses
+
+    def click(self, pos):
+        pass
+
+    def double_click(self, pos):
+        pass
+ 
+    def drag_start(self):
+        pass
+
+    def drag_end(self):
+        pass
+
+
+    def set_pos(self, obj, pos):
+        obj.props.x, obj.props.y = pos
+
+    def pos(self, obj):
+        return obj.props.x, obj.props.y
+
+    def transform(self, pos):
+        return pos
+
+    def enter(self, item, target):
+        if not self._dragging:
+            self._view.focus()
+
+    def leave(self, item, target):
+        if not self._dragging:
+            self._view.normal()
+
+class ClickController(Controller):
+
+    def set_pos(self, obj, pos):
+        pass
+

Added: trunk/pitivi/ui/transform.py
==============================================================================
--- (empty file)
+++ trunk/pitivi/ui/transform.py	Fri Nov 28 17:04:02 2008
@@ -0,0 +1,50 @@
+# This is an idea i got from reading an article on SmallTalk MVC. 
+# FIXME/TODO: do we really need windoing transformation? or would just the
+# coordinate system class be enough. Then you could have
+# view.cs.convertTo(point, model.cs) or something similar.
+
+class WindowingTransformation(object):
+
+    """Represents a transformation between two arbitrary 2D coordinate system"""
+
+    class System(object):
+
+        def __init__(self, *args, **kwargs):
+            object.__init__(self)
+
+        def setBounds(self, min, max):
+            self._min = min
+            self._max = max
+            self._range = point_difference(max, min)
+
+        def getMin(self):
+            return self._min
+
+        def getRange(self):
+            return self._range
+
+        def convertTo(self, point, other):
+            # ((point - min) * other.range / (self.range)) + other.min) 
+            return point_sum(
+                point_div(
+                    point_mul(
+                        point_difference(point, self._min), 
+                        other.getRange()),
+                self._range), 
+                other.getMin())
+
+    def __init__(self, A=None, B=None, *args, **kwargs):
+        super(WindowingTransformation, self).__init__(*args, **kwargs)
+        self._A = self.System()
+        self._B = self.System()
+
+    def setBounds(self, a, b):
+        self._A.setBounds(*a)
+        self._B.setBounds(*b)
+
+    def aToB(self, point):
+        return self._A.convertTo(point, self._B)
+
+    def bToA(self, point):
+        return self._B.convertTo(point, self._A)
+

Added: trunk/pitivi/ui/view.py
==============================================================================
--- (empty file)
+++ trunk/pitivi/ui/view.py	Fri Nov 28 17:04:02 2008
@@ -0,0 +1,27 @@
+from receiver import Receiver, handler
+import controller
+
+class View(Receiver):
+    
+    _controller = None
+
+    Controller = controller.Controller
+    
+    def __init__(self, model=None):
+        self._controller = self.Controller(view=self)
+        self.model = model
+        self.normal()
+
+## public interface
+
+    def focus(self):
+        pass
+
+    def select(self):
+        pass
+
+    def activate(self):
+        pass
+
+    def normal(self):
+        pass



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