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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1981 - (hide annotations) (download) (as text)
Mon Jan 12 13:33:44 2009 UTC (13 years, 7 months ago) by jpye
File MIME type: text/x-python
File size: 3305 byte(s)
Some changes to the Pan and Zoom.
Fixed ctrl-scrollwheel for zooming.
Still some problems with the pan tool.
1 jpye 1942 # Pan and Zoom tools for Gaphas by John Pye, 4 Nov 2008.
2     # http://pye.dyndns.org
3    
4     import gtk
5     from gaphas.tool import Tool
6    
7     class ZoomTool(Tool):
8 jpye 1981 """
9     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
11     the view will zoom in and out. This tool should be added BEFORE the PanTool
12     in the toolchain.
13     """
14 jpye 1942
15 jpye 1981 def __init__(self):
16     self.x0, self.y0 = 0, 0
17     self.lastdiff = 0;
18 jpye 1942
19 jpye 1981 def on_button_press(self, context, event):
20     if event.state & (
21     gtk.gdk.BUTTON2_MASK | gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK
22     ):
23     context.grab()
24     self.x0 = event.x
25     self.y0 = event.y
26     self.lastdiff = 0
27 jpye 1942
28    
29 jpye 1981 return True
30 jpye 1942
31 jpye 1981 def on_button_release(self, context, event):
32     context.ungrab()
33     self.lastdiff = 0
34     return True
35 jpye 1942
36 jpye 1981 def on_motion_notify(self, context, event):
37     if event.state & gtk.gdk.BUTTON2_MASK:
38     view = context.view
39     dy = event.y - self.y0
40 jpye 1942
41 jpye 1981 sx = view._matrix[0]
42     sy = view._matrix[3]
43     ox = (view._matrix[4] - self.x0) / sx
44     oy = (view._matrix[5] - self.y0) / sy
45 jpye 1942
46 jpye 1981 if abs(dy - self.lastdiff) > 20:
47     if dy - self.lastdiff < 0:
48     factor = 1./0.9
49     else:
50     factor = 0.9
51    
52     view._matrix.translate(-ox, -oy)
53     view._matrix.scale(factor, factor)
54     view._matrix.translate(+ox, +oy)
55    
56     # 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    
83    
84    
85 jpye 1942 class PanTool(Tool):
86     """
87     Captures drag events with the middle mouse button and uses them to
88     translate the canvas within the view. Trumps the ZoomTool, so should be
89     placed later in the ToolChain.
90     """
91     def __init__(self):
92     self.x0, self.y0 = 0, 0
93    
94     def on_button_press(self,context,event):
95     if event.button == 2:
96     context.grab()
97     self.x0, self.y0 = event.x, event.y
98     return True
99    
100     def on_button_release(self, context, event):
101     context.ungrab()
102     self.x0, self.y0 = event.x, event.y
103     return True
104    
105     def on_motion_notify(self, context, event):
106     if event.state & gtk.gdk.BUTTON2_MASK:
107     view = context.view
108     self.x1, self.y1 = event.x, event.y
109     dx = self.x1 - self.x0
110     dy = self.y1 - self.y0
111     view._matrix.translate(dx/view._matrix[0],dy/view._matrix[3])
112     # Make sure everything's updated
113     map(view.update_matrix, view._canvas.get_all_items())
114     view.request_update(view._canvas.get_all_items())
115     self.x0 = self.x1
116     self.y0 = self.y1
117     return True

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