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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2311 - (show annotations) (download) (as text)
Mon Nov 1 05:51:07 2010 UTC (13 years, 7 months ago) by jpye
File MIME type: text/x-python
File size: 3298 byte(s)
Fix outdated URL.
1 # Pan and Zoom tools for Gaphas by John Pye, 4 Nov 2008.
2
3 import gtk
4 import gtk.gdk as gdk
5
6 from gaphas.tool import Tool
7
8 ZOOM_MASK = gdk.CONTROL_MASK | gdk.SHIFT_MASK | gdk.MOD1_MASK
9 ZOOM_VALUE =gdk.CONTROL_MASK
10
11 class ZoomTool(Tool):
12 """
13 Tool for zooming using either of two techniquies:
14 * ctrl + middle-mouse dragging in the up-down direction.
15 * ctrl + mouse-wheeel
16
17 This tool checks for correct modifier key
18
19
20 """
21
22 def __init__(self):
23 self.x0, self.y0 = 0, 0
24 self.lastdiff = 0;
25
26 def on_button_press(self, context, event):
27 if event.button == 2 \
28 and event.state & ZOOM_MASK == ZOOM_VALUE:
29 context.grab()
30 self.x0 = event.x
31 self.y0 = event.y
32 self.lastdiff = 0
33 return True
34
35 def on_button_release(self, context, event):
36 context.ungrab()
37 self.lastdiff = 0
38 return True
39
40 def on_motion_notify(self, context, event):
41 if event.state & ZOOM_MASK == ZOOM_VALUE:
42 view = context.view
43 dy = event.y - self.y0
44
45 sx = view._matrix[0]
46 sy = view._matrix[3]
47 ox = (view._matrix[4] - self.x0) / sx
48 oy = (view._matrix[5] - self.y0) / sy
49
50 if abs(dy - self.lastdiff) > 20:
51 if dy - self.lastdiff < 0:
52 factor = 1./0.9
53 else:
54 factor = 0.9
55
56 view._matrix.translate(-ox, -oy)
57 view._matrix.scale(factor, factor)
58 view._matrix.translate(+ox, +oy)
59
60 # Make sure everything's updated
61 map(view.update_matrix, view._canvas.get_all_items())
62 view.request_update(view._canvas.get_all_items())
63
64 self.lastdiff = dy;
65 return True
66
67 def on_scroll(self, context, event):
68 if event.state & gtk.gdk.CONTROL_MASK:
69 view = context.view
70 context.grab()
71 sx = view._matrix[0]
72 sy = view._matrix[3]
73 ox = (view._matrix[4] - event.x) / sx
74 oy = (view._matrix[5] - event.y) / sy
75 factor = 0.9
76 if event.direction == gtk.gdk.SCROLL_UP:
77 factor = 1. / factor
78 view._matrix.translate(-ox, -oy)
79 view._matrix.scale(factor, factor)
80 view._matrix.translate(+ox, +oy)
81 # Make sure everything's updated
82 map(view.update_matrix, view._canvas.get_all_items())
83 view.request_update(view._canvas.get_all_items())
84 context.ungrab()
85 return True
86
87
88
89 class PanTool(Tool):
90 """
91 Captures drag events with the middle mouse button and uses them to
92 translate the canvas within the view. Trumps the ZoomTool, so should be
93 placed later in the ToolChain.
94 """
95 def __init__(self):
96 self.x0, self.y0 = 0, 0
97
98 def on_button_press(self,context,event):
99 if event.button == 2:
100 context.grab()
101 self.x0, self.y0 = event.x, event.y
102 return True
103
104 def on_button_release(self, context, event):
105 context.ungrab()
106 self.x0, self.y0 = event.x, event.y
107 return True
108
109 def on_motion_notify(self, context, event):
110 if event.state & gtk.gdk.BUTTON2_MASK:
111 view = context.view
112 self.x1, self.y1 = event.x, event.y
113 dx = self.x1 - self.x0
114 dy = self.y1 - self.y0
115 view._matrix.translate(dx/view._matrix[0],dy/view._matrix[3])
116 # Make sure everything's updated
117 map(view.update_matrix, view._canvas.get_all_items())
118 view.request_update(view._canvas.get_all_items())
119 self.x0 = self.x1
120 self.y0 = self.y1
121 return True

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