/[ascend]/trunk/pygtk/canvas/panzoom.py
ViewVC logotype

Diff of /trunk/pygtk/canvas/panzoom.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1980 by jpye, Tue Nov 4 07:19:26 2008 UTC revision 1981 by jpye, Mon Jan 12 13:33:44 2009 UTC
# Line 5  import gtk Line 5  import gtk
5  from gaphas.tool import Tool  from gaphas.tool import Tool
6    
7  class ZoomTool(Tool):  class ZoomTool(Tool):
8      """      """
9      Tool for zooming using ctrl + shift + middle-mouse dragging. Move the      Tool for zooming using ctrl + shift + middle-mouse dragging. Move the
10      mouse up and down while pressing middle mouse as well as ctrl+shift and      mouse up and down while pressing middle mouse as well as ctrl+shift and
11      the view will zoom in and out. This tool should be added BEFORE the PanTool      the view will zoom in and out. This tool should be added BEFORE the PanTool
12      in the toolchain.      in the toolchain.
13      """      """
14    
15      def __init__(self):      def __init__(self):
16          self.x0, self.y0 = 0, 0          self.x0, self.y0 = 0, 0
17          self.lastdiff = 0;          self.lastdiff = 0;
18    
19      def on_button_press(self, context, event):      def on_button_press(self, context, event):
20          if event.state & (          if event.state & (
21              gtk.gdk.BUTTON2_MASK | gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK              gtk.gdk.BUTTON2_MASK | gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK
22          ):          ):
23              context.grab()              context.grab()
24              self.x0 = event.x              self.x0 = event.x
25              self.y0 = event.y              self.y0 = event.y
26              self.lastdiff = 0              self.lastdiff = 0
27              return True  
28    
29      def on_button_release(self, context, event):              return True
30          context.ungrab()  
31          self.lastdiff = 0      def on_button_release(self, context, event):
32          return True          context.ungrab()
33            self.lastdiff = 0
34      def on_motion_notify(self, context, event):          return True
35          if event.state & gtk.gdk.BUTTON2_MASK:  
36              view = context.view      def on_motion_notify(self, context, event):
37              dy = event.y - self.y0          if event.state & gtk.gdk.BUTTON2_MASK:
38                view = context.view
39              sx = view._matrix[0]              dy = event.y - self.y0
40              sy = view._matrix[3]  
41              ox = (view._matrix[4] - self.x0) / sx              sx = view._matrix[0]
42              oy = (view._matrix[5] - self.y0) / sy              sy = view._matrix[3]
43                            ox = (view._matrix[4] - self.x0) / sx
44              if abs(dy - self.lastdiff) > 20:              oy = (view._matrix[5] - self.y0) / sy
45                  if dy - self.lastdiff < 0:  
46                      factor = 1./0.9              if abs(dy - self.lastdiff) > 20:
47                  else:                  if dy - self.lastdiff < 0:
48                      factor = 0.9                      factor = 1./0.9
49                                else:
50                  view._matrix.translate(-ox, -oy)                      factor = 0.9
51                  view._matrix.scale(factor, factor)  
52                  view._matrix.translate(+ox, +oy)                  view._matrix.translate(-ox, -oy)
53                    view._matrix.scale(factor, factor)
54                  # Make sure everything's updated                  view._matrix.translate(+ox, +oy)
55                  map(view.update_matrix, view._canvas.get_all_items())  
56                  view.request_update(view._canvas.get_all_items())                  # Make sure everything's updated
57                    map(view.update_matrix, view._canvas.get_all_items())
58                    view.request_update(view._canvas.get_all_items())
59    
60                    self.lastdiff = dy;
61                return True
62    
63        def on_scroll(self, context, event):
64            if event.state & gtk.gdk.CONTROL_MASK:
65                view = context.view
66                context.grab()
67                sx = view._matrix[0]
68                sy = view._matrix[3]
69                ox = (view._matrix[4] - event.x) / sx
70                oy = (view._matrix[5] - event.y) / sy
71                factor = 0.9
72                if event.direction == gtk.gdk.SCROLL_UP:    
73                    factor = 1. / factor
74                view._matrix.translate(-ox, -oy)
75                view._matrix.scale(factor, factor)
76                view._matrix.translate(+ox, +oy)
77                # Make sure everything's updated
78                map(view.update_matrix, view._canvas.get_all_items())
79                view.request_update(view._canvas.get_all_items())
80                context.ungrab()
81                return True
82    
                 self.lastdiff = dy;  
             return True  
83    
84    
85  class PanTool(Tool):  class PanTool(Tool):

Legend:
Removed from v.1980  
changed lines
  Added in v.1981

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22