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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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