Package mbdyn :: Package interface :: Module renderWindowInteractor
[hide private]

Source Code for Module mbdyn.interface.renderWindowInteractor

  1  """ 
  2  A GTK area that hosts a VTK one, module developed  
  3  by John Hunter.  
  4   
  5  Description 
  6  =========== 
  7  Provides a pyGtk vtkRenderWindowInteractor widget.  This embeds a 
  8  vtkRenderWindow inside a GTK widget and uses the 
  9  vtkGenericRenderWindowInteractor for the event handling.  This is 
 10  similar to GtkVTKRenderWindowInteractor.py. 
 11   
 12  The extensions here allow the use of gtkglext rather than gtkgl and 
 13  pygtk-2 rather than pygtk-0.  It requires pygtk-2.0.0 or later. 
 14   
 15  There is a working example at the bottom. 
 16   
 17  Credits 
 18  ======= 
 19  John Hunter <jdhunter@ace.bsd.uchicago.edu> developed and tested 
 20  this code based on VTK's GtkVTKRenderWindow.py and extended it to 
 21  work with pygtk-2.0.0. 
 22   
 23  License 
 24  ======= 
 25  VTK license. 
 26   
 27  """ 
 28   
 29  import sys 
 30  import pygtk 
 31  pygtk.require('2.0') 
 32  import gtk 
 33  from gtk import gdk 
 34  import gtk.gtkgl 
 35  import vtk 
 36   
37 -class GtkGLExtVTKRenderWindowInteractor(gtk.gtkgl.DrawingArea):
38 39 """ Embeds a vtkRenderWindow into a pyGTK widget and uses 40 vtkGenericRenderWindowInteractor for the event handling. This 41 class embeds the RenderWindow correctly. A __getattr__ hook is 42 provided that makes the class behave like a 43 vtkGenericRenderWindowInteractor.""" 44
45 - def __init__(self):
46 gtk.gtkgl.DrawingArea.__init__(self) 47 self.set_double_buffered(False) 48 49 self._RenderWindow = vtk.vtkRenderWindow() 50 51 # private attributes 52 self.__Created = 0 53 self._ActiveButton = 0 54 55 self._Iren = vtk.vtkGenericRenderWindowInteractor() 56 self._Iren.SetRenderWindow(self._RenderWindow) 57 self._Iren.GetInteractorStyle().SetCurrentStyleToTrackballCamera() 58 self._Iren.AddObserver('CreateTimerEvent', self.CreateTimer) 59 self._Iren.AddObserver('DestroyTimerEvent', self.DestroyTimer) 60 self.ConnectSignals() 61 62 # need this to be able to handle key_press events. 63 self.set_flags(gtk.CAN_FOCUS)
64
65 - def set_size_request(self, w, h):
66 gtk.gtkgl.DrawingArea.set_size_request(self, w, h) 67 self._RenderWindow.SetSize(w, h) 68 self._Iren.SetSize(w, h) 69 self._Iren.ConfigureEvent()
70
71 - def ConnectSignals(self):
72 self.connect("realize", self.OnRealize) 73 self.connect("expose_event", self.OnExpose) 74 self.connect("configure_event", self.OnConfigure) 75 self.connect("button_press_event", self.OnButtonDown) 76 self.connect("button_release_event", self.OnButtonUp) 77 self.connect("motion_notify_event", self.OnMouseMove) 78 self.connect("enter_notify_event", self.OnEnter) 79 self.connect("leave_notify_event", self.OnLeave) 80 self.connect("key_press_event", self.OnKeyPress) 81 self.connect("delete_event", self.OnDestroy) 82 self.add_events(gdk.EXPOSURE_MASK| gdk.BUTTON_PRESS_MASK | 83 gdk.BUTTON_RELEASE_MASK | 84 gdk.KEY_PRESS_MASK | 85 gdk.POINTER_MOTION_MASK | 86 gdk.POINTER_MOTION_HINT_MASK | 87 gdk.ENTER_NOTIFY_MASK | gdk.LEAVE_NOTIFY_MASK)
88
89 - def __getattr__(self, attr):
90 """Makes the object behave like a 91 vtkGenericRenderWindowInteractor""" 92 if attr == '__vtk__': 93 return lambda t=self._Iren: t 94 elif hasattr(self._Iren, attr): 95 return getattr(self._Iren, attr) 96 else: 97 raise AttributeError, self.__class__.__name__ + \ 98 " has no attribute named " + attr
99
100 - def CreateTimer(self, obj, event):
101 gtk.timeout_add(10, self._Iren.TimerEvent)
102
103 - def DestroyTimer(self, obj, event):
104 """The timer is a one shot timer so will expire automatically.""" 105 return 1
106
107 - def GetRenderWindow(self):
108 return self._RenderWindow
109
110 - def GetWindowInteractor(self):
111 return self._Iren
112
113 - def Render(self):
114 if self.__Created: 115 self._RenderWindow.Render()
116
117 - def OnRealize(self, *args):
118 if self.__Created == 0: 119 # you can't get the xid without the window being realized. 120 self.realize() 121 if sys.platform=='win32': 122 win_id = str(self.widget.window.handle) 123 else: 124 win_id = str(self.widget.window.xid) 125 126 self._RenderWindow.SetWindowInfo(win_id) 127 #self._Iren.Initialize() 128 self.__Created = 1 129 return True
130
131 - def OnConfigure(self, widget, event):
132 self.widget=widget 133 self._Iren.SetSize(event.width, event.height) 134 self._Iren.ConfigureEvent() 135 self.Render() 136 return True
137
138 - def OnExpose(self, *args):
139 self.Render() 140 return True
141
142 - def OnDestroy(self, event=None):
143 self.hide() 144 del self._RenderWindow 145 self.destroy() 146 return True
147
148 - def _GetCtrlShift(self, event):
149 ctrl, shift = 0, 0 150 if ((event.state & gdk.CONTROL_MASK) == gdk.CONTROL_MASK): 151 ctrl = 1 152 if ((event.state & gdk.SHIFT_MASK) == gdk.SHIFT_MASK): 153 shift = 1 154 return ctrl, shift
155
156 - def OnButtonDown(self, wid, event):
157 """Mouse button pressed.""" 158 m = self.get_pointer() 159 ctrl, shift = self._GetCtrlShift(event) 160 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 161 chr(0), 0, None) 162 button = event.button 163 if button == 3: 164 self._Iren.RightButtonPressEvent() 165 return True 166 elif button == 1: 167 self._Iren.LeftButtonPressEvent() 168 return True 169 elif button == 2: 170 self._Iren.MiddleButtonPressEvent() 171 return True 172 else: 173 return False
174
175 - def OnButtonUp(self, wid, event):
176 """Mouse button released.""" 177 m = self.get_pointer() 178 ctrl, shift = self._GetCtrlShift(event) 179 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 180 chr(0), 0, None) 181 button = event.button 182 if button == 3: 183 self._Iren.RightButtonReleaseEvent() 184 return True 185 elif button == 1: 186 self._Iren.LeftButtonReleaseEvent() 187 return True 188 elif button == 2: 189 self._Iren.MiddleButtonReleaseEvent() 190 return True 191 192 return False
193
194 - def OnMouseMove(self, wid, event):
195 """Mouse has moved.""" 196 m = self.get_pointer() 197 ctrl, shift = self._GetCtrlShift(event) 198 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 199 chr(0), 0, None) 200 self._Iren.MouseMoveEvent() 201 return True
202
203 - def OnEnter(self, wid, event):
204 """Entering the vtkRenderWindow.""" 205 self.grab_focus() 206 m = self.get_pointer() 207 ctrl, shift = self._GetCtrlShift(event) 208 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 209 chr(0), 0, None) 210 self._Iren.EnterEvent() 211 return True
212
213 - def OnLeave(self, wid, event):
214 """Leaving the vtkRenderWindow.""" 215 m = self.get_pointer() 216 ctrl, shift = self._GetCtrlShift(event) 217 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 218 chr(0), 0, None) 219 self._Iren.LeaveEvent() 220 return True
221
222 - def OnKeyPress(self, wid, event):
223 """Key pressed.""" 224 m = self.get_pointer() 225 ctrl, shift = self._GetCtrlShift(event) 226 keycode, keysym = event.keyval, event.string 227 key = chr(0) 228 if keycode < 256: 229 key = chr(keycode) 230 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 231 key, 0, keysym) 232 self._Iren.KeyPressEvent() 233 self._Iren.CharEvent() 234 return True
235
236 - def OnKeyRelease(self, wid, event):
237 "Key released." 238 m = self.get_pointer() 239 ctrl, shift = self._GetCtrlShift(event) 240 keycode, keysym = event.keyval, event.string 241 key = chr(0) 242 if keycode < 256: 243 key = chr(keycode) 244 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 245 key, 0, keysym) 246 self._Iren.KeyReleaseEvent() 247 return True
248
249 - def Initialize(self):
250 if self.__Created: 251 self._Iren.Initialize()
252
253 - def AddObserver(self, event, callback):
254 self._Iren.AddObserver(event, callback)
255
256 - def SetPicker(self, picker):
257 self._Iren.SetPicker(picker)
258
259 - def GetPicker(self, picker):
260 return self._Iren.GetPicker()
261 262 263 GtkRenderWindowInteractor = GtkGLExtVTKRenderWindowInteractor 264