Package windSimSuite :: Package interface :: Module vtk_area
[hide private]

Source Code for Module windSimSuite.interface.vtk_area

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # This file is part of MBDyn sim suite. 
  5  # Copyright (C) 2007 André ESPAZE, as part of a Master thesis supervised by 
  6  # Martin O.L.Hansen (DTU) and Nicolas Chauvat (Logilab) 
  7   
  8  # MBDyn Sim Suite is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 21  # 
 22  """The definition of the VTK area. 
 23  """ 
 24  import vtk 
 25   
 26  from mbdyn.interface.renderWindowInteractor import GtkRenderWindowInteractor 
 27   
 28   
29 -class GtkVtkAreaBase(GtkRenderWindowInteractor):
30 """A class that inherits from the John Hunter's work 31 and overrides some of the methods for setting custom actions 32 when GTK signals are emitted. 33 """ 34
35 - def __init__(self):
36 GtkRenderWindowInteractor.__init__(self) 37 #self.gvtk.SetDesiredUpdateRate(1000) 38 self.Initialize() 39 self.Start() 40 # prevents 'q' from exiting the app. 41 self.AddObserver("ExitEvent", lambda o, e, x=None: x)
42
43 - def OnEnter(self, wid, event):
44 """Callback on the signal 'enter_notify_event' emitted.""" 45 self.grab_focus() 46 m = self.get_pointer() 47 ctrl, shift = self._GetCtrlShift(event) 48 self.win_interactor.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 49 chr(0), 0, None) 50 self.win_interactor.EnterEvent() 51 self.speak() 52 return True
53 54
55 - def OnLeave(self, wid, event):
56 """Callback on the signal 'leave_notify_event' emitted.""" 57 m = self.get_pointer() 58 ctrl, shift = self._GetCtrlShift(event) 59 self.win_interactor.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 60 chr(0), 0, None) 61 self.win_interactor.LeaveEvent() 62 self.quiet() 63 return True
64
65 - def OnButtonDown(self, wid, event):
66 """Callback on a mouse button pressed.""" 67 m = self.get_pointer() 68 ctrl, shift = self._GetCtrlShift(event) 69 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 70 chr(0), 0, None) 71 button = event.button 72 if button == 3: 73 self._Iren.RightButtonPressEvent() 74 return self.on_right_click(wid, event) 75 #self.on_right_click(wid, event) 76 #return True 77 elif button == 1: 78 self._Iren.LeftButtonPressEvent() 79 return True 80 elif button == 2: 81 self._Iren.MiddleButtonPressEvent() 82 return True 83 else: 84 return False
85
86 - def OnMouseMove(self, wid, event):
87 """Callback when the mouse pointer has moved.""" 88 m = self.get_pointer() 89 ctrl, shift = self._GetCtrlShift(event) 90 self._Iren.SetEventInformationFlipY(m[0], m[1], ctrl, shift, 91 chr(0), 0, None) 92 self._Iren.MouseMoveEvent() 93 self.on_motion_notify_event(wid, event) 94 return True
95 96
97 -class ListStore:
98 """Return the Python object reference from 99 a VTK actor selected. 100 """ 101
102 - def __init__(self):
103 self.actors = [] 104 self.references = []
105
106 - def add(self, actor, reference):
107 """Add an actor and its corresponding Python reference""" 108 self.actors.append(actor) 109 self.references.append(reference)
110
111 - def remove(self, actor):
112 """Remove an actor and its corresponding Python reference""" 113 aid = self.actors.index(actor) 114 self.actors.remove(actor) 115 self.references.remove(self.references[aid])
116
117 - def get_from(self, actor):
118 """Return the Python reference from the VTK actor one""" 119 return self.references[self.actors.index(actor)]
120 121
122 -class GtkVtkArea(GtkVtkAreaBase):
123 """The VTK area that can be packed inside GTK. 124 """ 125
126 - def __init__(self, status_bar):
127 GtkVtkAreaBase.__init__(self) 128 129 self.status_bar = status_bar 130 self.mess_nothing = "No actor selected" 131 self.mess_actor = "Actor selected: %s" 132 133 self.renderer = vtk.vtkRenderer() 134 self.renderer.SetBackground(1., 1., 1.) 135 self.GetRenderWindow().AddRenderer(self.renderer) 136 self.win_interactor = self.GetWindowInteractor() 137 self.actors = [] 138 139 self.picker = vtk.vtkPicker() 140 self.picker.PickFromListOn() 141 self.selected_items = [] 142 self.has_selected_item = False 143 self.list_store = ListStore() 144 145 self.on_right_click = None 146 self.on_motion_notify_event = None
147
148 - def set_on_right_click(self, callback):
149 """Set by the C{Manager} in L{windSimSuite.interface.manager}""" 150 self.on_right_click = callback
151
152 - def set_on_motion_notify_event(self, callback):
153 """Set by the C{Manager} in L{windSimSuite.interface.manager}""" 154 self.on_motion_notify_event = callback
155
156 - def speak(self):
157 """Action to perform on 'enter_notify_event'""" 158 pass
159
160 - def quiet(self):
161 """Action to perform on 'leave_notify_event'""" 162 self.status_bar.write("")
163
164 - def add_actor(self, actor):
165 """Add an actor to the VTK display""" 166 self.renderer.AddActor(actor) 167 self.actors.append(actor)
168
169 - def can_be_selected(self, actor, reference):
170 """Specify an actor that can be selected""" 171 self.picker.AddPickList(actor) 172 self.list_store.add(actor, reference)
173
174 - def remove_actor(self, actor):
175 """Remove an actor from the VTK display""" 176 self.renderer.RemoveActor(actor) 177 self.actors.remove(actor)
178
179 - def remove_from_selection(self, actor):
180 """Remove an actor that could be selected""" 181 self.picker.DeletePickList(actor) 182 self.list_store.remove(actor)
183
184 - def get_actor(self):
185 """Return an VTK actor reference from the mouse pointer position 186 or None""" 187 xpos, ypos = self.win_interactor.GetEventPosition() 188 self.picker.Pick(xpos, ypos, 0., self.renderer) 189 actor = self.picker.GetActor() 190 return actor
191
192 - def get_object(self, event):
193 """Return a Python instance reference or None 194 when the mouse pointer has moved. Note: C{event} is 195 not used for VTK, because the event is got from the 196 C{win_interactor} in that case.""" 197 actor = self.get_actor() 198 if actor == None: 199 return None 200 else: 201 return self.list_store.get_from(actor)
202
203 - def clean(self):
204 """Clean the VTK area""" 205 for actor in self.actors: 206 self.renderer.RemoveActor(actor) 207 for actor in self.list_store.actors: 208 self.picker.DeletePickList(actor)
209
210 - def remove_items(self):
211 """Remove the selected items by moving the mouse pointer""" 212 self.status_bar.write(self.mess_nothing) 213 self.clean_selected_items()
214
215 - def clean_selected_items(self):
216 """Remove the selected items from the list""" 217 if self.has_selected_item: 218 for idx in range(len(self.selected_items)): 219 item = self.selected_items.pop() 220 item.set_unselected() 221 self.has_selected_item = False
222
223 - def add_item(self, item):
224 """Add a selected item by the mouse pointer to the list""" 225 item.set_selected() 226 self.selected_items.append(item) 227 self.has_selected_item = True 228 self.status_bar.write(self.mess_actor % item.name)
229