Commit 7e582d992adda167caecbf0a236b536909b7319e

Authored by tatiana
1 parent 267cdf77

ENH: Events of slice viewer work

Showing 1 changed file with 113 additions and 99 deletions   Show diff stats
invesalius/data/viewer_slice.py
@@ -103,6 +103,7 @@ class Viewer(wx.Panel): @@ -103,6 +103,7 @@ class Viewer(wx.Panel):
103 103
104 104
105 def OnContextMenu(self, evt): 105 def OnContextMenu(self, evt):
  106 + self.right_pressed = 0
106 self.PopupMenu(self.menu) 107 self.PopupMenu(self.menu)
107 108
108 def SetPopupMenu(self, menu): 109 def SetPopupMenu(self, menu):
@@ -151,13 +152,11 @@ class Viewer(wx.Panel): @@ -151,13 +152,11 @@ class Viewer(wx.Panel):
151 { 152 {
152 "MouseMoveEvent": self.OnCrossMove, 153 "MouseMoveEvent": self.OnCrossMove,
153 "LeftButtonPressEvent": self.OnCrossMouseClick, 154 "LeftButtonPressEvent": self.OnCrossMouseClick,
154 - "LeftButtonReleaseEvent": self.OnCrossMouseRelease  
155 }, 155 },
156 const.SLICE_STATE_EDITOR: 156 const.SLICE_STATE_EDITOR:
157 { 157 {
158 "MouseMoveEvent": self.OnBrushMove, 158 "MouseMoveEvent": self.OnBrushMove,
159 "LeftButtonPressEvent": self.OnBrushClick, 159 "LeftButtonPressEvent": self.OnBrushClick,
160 - "LeftButtonReleaseEvent": self.OnMouseRelease,  
161 "EnterEvent": self.OnEnterInteractor, 160 "EnterEvent": self.OnEnterInteractor,
162 "LeaveEvent": self.OnLeaveInteractor 161 "LeaveEvent": self.OnLeaveInteractor
163 }, 162 },
@@ -165,43 +164,42 @@ class Viewer(wx.Panel): @@ -165,43 +164,42 @@ class Viewer(wx.Panel):
165 { 164 {
166 "MouseMoveEvent": self.OnPanMove, 165 "MouseMoveEvent": self.OnPanMove,
167 "LeftButtonPressEvent": self.OnPanClick, 166 "LeftButtonPressEvent": self.OnPanClick,
168 - "LeftButtonReleaseEvent": self.OnReleaseLeftButton 167 + "LeftButtonReleaseEvent": self.OnVtkRightRelease
169 }, 168 },
170 const.STATE_SPIN: 169 const.STATE_SPIN:
171 { 170 {
172 "MouseMoveEvent": self.OnSpinMove, 171 "MouseMoveEvent": self.OnSpinMove,
173 "LeftButtonPressEvent": self.OnSpinClick, 172 "LeftButtonPressEvent": self.OnSpinClick,
174 - "LeftButtonReleaseEvent": self.OnReleaseLeftButton 173 + "LeftButtonReleaseEvent": self.OnVtkRightRelease
175 }, 174 },
176 const.STATE_ZOOM: 175 const.STATE_ZOOM:
177 { 176 {
178 - "MouseMoveEvent": self.OnZoomMove, 177 + "MouseMoveEvent": self.OnZoomMoveLeft,
179 "LeftButtonPressEvent": self.OnZoomLeftClick, 178 "LeftButtonPressEvent": self.OnZoomLeftClick,
180 - "LeftButtonReleaseEvent": self.OnReleaseLeftButton, 179 + "LeftButtonReleaseEvent": self.OnVtkRightRelease
181 }, 180 },
182 const.SLICE_STATE_SCROLL: 181 const.SLICE_STATE_SCROLL:
183 { 182 {
184 "MouseMoveEvent": self.OnChangeSliceMove, 183 "MouseMoveEvent": self.OnChangeSliceMove,
185 "LeftButtonPressEvent": self.OnChangeSliceClick, 184 "LeftButtonPressEvent": self.OnChangeSliceClick,
186 - "LeftButtonReleaseEvent": self.OnReleaseLeftButton  
187 }, 185 },
188 const.STATE_WL: 186 const.STATE_WL:
189 { 187 {
190 "MouseMoveEvent": self.OnWindowLevelMove, 188 "MouseMoveEvent": self.OnWindowLevelMove,
191 "LeftButtonPressEvent": self.OnWindowLevelClick, 189 "LeftButtonPressEvent": self.OnWindowLevelClick,
192 - "LeftButtonReleaseEvent": self.OnReleaseLeftButton,  
193 }, 190 },
194 const.STATE_DEFAULT: 191 const.STATE_DEFAULT:
195 { 192 {
196 - "MouseMoveEvent": self.OnZoomMove,  
197 - "RightButtonPressEvent": self.OnZoomLeftClick,  
198 - "RightButtonReleaseEvent": self.OnReleaseRightButton,  
199 } 193 }
200 } 194 }
201 195
202 # Bind method according to current mode 196 # Bind method according to current mode
203 if(state == const.STATE_ZOOM_SL): 197 if(state == const.STATE_ZOOM_SL):
204 style = vtk.vtkInteractorStyleRubberBandZoom() 198 style = vtk.vtkInteractorStyleRubberBandZoom()
  199 +
  200 + style.AddObserver("RightButtonPressEvent", self.QuitRubberBandZoom)
  201 + #style.AddObserver("RightButtonPressEvent", self.EnterRubberBandZoom)
  202 +
205 else: 203 else:
206 style = vtk.vtkInteractorStyleImage() 204 style = vtk.vtkInteractorStyleImage()
207 205
@@ -210,9 +208,18 @@ class Viewer(wx.Panel): @@ -210,9 +208,18 @@ class Viewer(wx.Panel):
210 # Bind event 208 # Bind event
211 style.AddObserver(event, 209 style.AddObserver(event,
212 action[state][event]) 210 action[state][event])
213 -  
214 - style.AddObserver("RightButtonPressEvent",self.OnRightClick)  
215 - style.AddObserver("RightButtonReleaseEvent", self.OnReleaseRightButton) 211 +
  212 + # Common to all styles
  213 + # Mouse Buttons' presses / releases
  214 + style.AddObserver("LeftButtonPressEvent", self.OnLeftClick)
  215 + style.AddObserver("LeftButtonReleaseEvent", self.OnReleaseLeftButton)
  216 + style.AddObserver("RightButtonPressEvent", self.OnRightClick)
  217 + style.AddObserver("RightButtonReleaseEvent", self.OnReleaseRightButton)
  218 +
  219 + # Zoom using right button
  220 + style.AddObserver("RightButtonPressEvent",self.OnZoomRightClick)
  221 + style.AddObserver("MouseMoveEvent", self.OnZoomMoveRight)
  222 + style.AddObserver("RightButtonReleaseEvent", self.OnVtkRightRelease)
216 223
217 if ((state == const.STATE_ZOOM) or (state == const.STATE_ZOOM_SL)): 224 if ((state == const.STATE_ZOOM) or (state == const.STATE_ZOOM_SL)):
218 self.interactor.Bind(wx.EVT_LEFT_DCLICK, self.OnUnZoom) 225 self.interactor.Bind(wx.EVT_LEFT_DCLICK, self.OnUnZoom)
@@ -222,15 +229,51 @@ class Viewer(wx.Panel): @@ -222,15 +229,51 @@ class Viewer(wx.Panel):
222 self.style = style 229 self.style = style
223 self.interactor.SetInteractorStyle(style) 230 self.interactor.SetInteractorStyle(style)
224 231
225 - #def OnMouseMove(self, evt, obj):  
226 - # if(self.left_pressed):  
227 -  
228 - # if(self.state == const.SLICE_STATE_CROSS):  
229 -  
230 - 232 +
  233 + def QuitRubberBandZoom(self, evt, obj):
  234 + style = vtk.vtkInteractorStyleImage()
  235 + self.interactor.SetInteractorStyle(style)
  236 + self.style = style
  237 +
  238 + style.AddObserver("LeftButtonPressEvent", self.EnterRubberBandZoom)
  239 +
  240 + # Zoom using right button
  241 + style.AddObserver("RightButtonPressEvent", self.OnRightClick)
  242 + style.AddObserver("RightButtonReleaseEvent", self.OnReleaseRightButton)
  243 + style.AddObserver("RightButtonPressEvent",self.OnZoomRightClick)
  244 + style.AddObserver("MouseMoveEvent", self.OnZoomMoveRight)
  245 + style.AddObserver("RightButtonReleaseEvent", self.OnReleaseRightButton)
  246 +
  247 + def EnterRubberBandZoom(self, evt, obj):
  248 + style = vtk.vtkInteractorStyleRubberBandZoom()
  249 + self.interactor.SetInteractorStyle(style)
  250 + self.style = style
  251 +
  252 + style.AddObserver("RightButtonPressEvent", self.QuitRubberBandZoom)
  253 +
231 def OnRightClick(self, evt, obj): 254 def OnRightClick(self, evt, obj):
  255 + print "OnRightClick"
232 self.right_pressed = 1 256 self.right_pressed = 1
233 - 257 +
  258 + def OnReleaseRightButton(self, evt, obj):
  259 + print "OnReleaseRightButton"
  260 + self.right_pressed = 0
  261 + ps.Publisher().sendMessage('Update slice viewer')
  262 +
  263 + def OnLeftClick(self, evt, obj):
  264 + print "OnLeftClick"
  265 + self.left_pressed = 1
  266 +
  267 + def OnZoomLeftClick(self, evt, obj):
  268 + print "OnZoomLeftClick"
  269 + evt.StartDolly()
  270 +
  271 + def OnReleaseLeftButton(self, evt, obj):
  272 + self.left_pressed = 0
  273 + ps.Publisher().sendMessage('Update slice viewer')
  274 +
  275 +
  276 +
234 277
235 def OnWindowLevelMove(self, evt, obj): 278 def OnWindowLevelMove(self, evt, obj):
236 if (self.left_pressed): 279 if (self.left_pressed):
@@ -255,15 +298,11 @@ class Viewer(wx.Panel): @@ -255,15 +298,11 @@ class Viewer(wx.Panel):
255 #Necessary update the slice plane in the volume case exists 298 #Necessary update the slice plane in the volume case exists
256 ps.Publisher().sendMessage('Render volume viewer') 299 ps.Publisher().sendMessage('Render volume viewer')
257 300
258 - elif(self.right_pressed):  
259 - self.OnZoomMove(evt, obj)  
260 -  
261 self.interactor.Render() 301 self.interactor.Render()
262 302
263 303
264 def OnWindowLevelClick(self, evt, obj): 304 def OnWindowLevelClick(self, evt, obj):
265 self.last_x, self.last_y = self.interactor.GetLastEventPosition() 305 self.last_x, self.last_y = self.interactor.GetLastEventPosition()
266 - self.left_pressed = 1  
267 306
268 def UpdateWindowLevelValue(self, pubsub_evt): 307 def UpdateWindowLevelValue(self, pubsub_evt):
269 window, level = pubsub_evt.data 308 window, level = pubsub_evt.data
@@ -290,11 +329,8 @@ class Viewer(wx.Panel): @@ -290,11 +329,8 @@ class Viewer(wx.Panel):
290 self.scroll.SetThumbPosition(self.acum_achange_slice) 329 self.scroll.SetThumbPosition(self.acum_achange_slice)
291 self.OnScrollBar() 330 self.OnScrollBar()
292 331
293 - elif(self.right_pressed):  
294 - self.OnZoomMove(evt, obj)  
295 332
296 def OnChangeSliceClick(self, evt, obj): 333 def OnChangeSliceClick(self, evt, obj):
297 - self.left_pressed = 1  
298 position = list(self.interactor.GetLastEventPosition()) 334 position = list(self.interactor.GetLastEventPosition())
299 self.acum_achange_slice = self.scroll.GetThumbPosition() 335 self.acum_achange_slice = self.scroll.GetThumbPosition()
300 self.last_position = position[1] 336 self.last_position = position[1]
@@ -303,22 +339,21 @@ class Viewer(wx.Panel): @@ -303,22 +339,21 @@ class Viewer(wx.Panel):
303 if (self.left_pressed): 339 if (self.left_pressed):
304 evt.Pan() 340 evt.Pan()
305 evt.OnRightButtonDown() 341 evt.OnRightButtonDown()
306 - elif(self.right_pressed):  
307 - self.OnZoomMove(evt, obj)  
308 - 342 +
309 def OnPanClick(self, evt, obj): 343 def OnPanClick(self, evt, obj):
310 - self.left_pressed = 1  
311 evt.StartPan() 344 evt.StartPan()
312 345
313 - def OnZoomMove(self, evt, obj):  
314 - if (self.left_pressed or self.right_pressed): 346 + def OnZoomMoveLeft(self, evt, obj):
  347 + if self.left_pressed:
  348 + print "OnZoomMoveLeft:"
315 evt.Dolly() 349 evt.Dolly()
316 evt.OnRightButtonDown() 350 evt.OnRightButtonDown()
317 351
318 - def OnZoomLeftClick(self, evt, obj):  
319 - self.left_pressed = 1  
320 - evt.StartDolly()  
321 - 352 + def OnVtkRightRelease(self, evt, obj):
  353 + print "On VTK"
  354 + evt.OnRightButtonUp()
  355 +
  356 +
322 def OnUnZoom(self, evt, obj = None): 357 def OnUnZoom(self, evt, obj = None):
323 mouse_x, mouse_y = self.interactor.GetLastEventPosition() 358 mouse_x, mouse_y = self.interactor.GetLastEventPosition()
324 ren = self.interactor.FindPokedRenderer(mouse_x, mouse_y) 359 ren = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
@@ -332,24 +367,10 @@ class Viewer(wx.Panel): @@ -332,24 +367,10 @@ class Viewer(wx.Panel):
332 if (self.left_pressed): 367 if (self.left_pressed):
333 evt.Spin() 368 evt.Spin()
334 evt.OnRightButtonDown() 369 evt.OnRightButtonDown()
335 - elif(self.right_pressed):  
336 - self.OnZoomMove(evt, obj)  
337 -  
338 370
339 def OnSpinClick(self, evt, obj): 371 def OnSpinClick(self, evt, obj):
340 - self.left_pressed = 1  
341 evt.StartSpin() 372 evt.StartSpin()
342 373
343 - def OnReleaseLeftButton(self, evt, obj):  
344 - self.left_pressed = 0  
345 - ps.Publisher().sendMessage('Update slice viewer')  
346 -  
347 - def OnReleaseRightButton(self, evt, obj):  
348 - self.right_pressed = 0  
349 - ps.Publisher().sendMessage('Update slice viewer')  
350 -  
351 -  
352 -  
353 def OnEnterInteractor(self, evt, obj): 374 def OnEnterInteractor(self, evt, obj):
354 #self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_BLANK)) 375 #self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_BLANK))
355 pass 376 pass
@@ -537,14 +558,8 @@ class Viewer(wx.Panel): @@ -537,14 +558,8 @@ class Viewer(wx.Panel):
537 self.interactor.Render() 558 self.interactor.Render()
538 #self.cursor = cursor 559 #self.cursor = cursor
539 560
540 - def OnMouseClick(self, evt, obj):  
541 - self.left_pressed = 1  
542 -  
543 - def OnMouseRelease(self, evt, obj):  
544 - self.left_pressed = 0  
545 561
546 def OnBrushClick(self, evt, obj): 562 def OnBrushClick(self, evt, obj):
547 - self.left_pressed = 1  
548 563
549 mouse_x, mouse_y = self.interactor.GetEventPosition() 564 mouse_x, mouse_y = self.interactor.GetEventPosition()
550 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y) 565 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
@@ -574,38 +589,34 @@ class Viewer(wx.Panel): @@ -574,38 +589,34 @@ class Viewer(wx.Panel):
574 589
575 def OnBrushMove(self, evt, obj): 590 def OnBrushMove(self, evt, obj):
576 591
577 - if(self.right_pressed):  
578 - self.OnZoomLeftClick(evt, obj)  
579 - self.OnZoomMove(evt, obj)  
580 - else:  
581 - mouse_x, mouse_y = self.interactor.GetEventPosition()  
582 - render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)  
583 - slice_data = self.get_slice_data(render)  
584 -  
585 - # TODO: Improve!  
586 - for i in self.slice_data_list:  
587 - i.cursor.Show(0)  
588 - slice_data.cursor.Show()  
589 -  
590 - self.pick.Pick(mouse_x, mouse_y, 0, render)  
591 - coord = self.get_coordinate_cursor()  
592 - slice_data.cursor.SetPosition(coord)  
593 - slice_data.cursor.SetEditionPosition(  
594 - self.get_coordinate_cursor_edition(slice_data))  
595 - self.__update_cursor_position(slice_data, coord)  
596 -  
597 - if self._brush_cursor_op == const.BRUSH_ERASE:  
598 - evt_msg = 'Erase mask pixel'  
599 - elif self._brush_cursor_op == const.BRUSH_DRAW:  
600 - evt_msg = 'Add mask pixel'  
601 - elif self._brush_cursor_op == const.BRUSH_THRESH:  
602 - evt_msg = 'Edit mask pixel'  
603 -  
604 - if (self.left_pressed):  
605 - pixels = itertools.ifilter(self.test_operation_position,  
606 - slice_data.cursor.GetPixels())  
607 - ps.Publisher().sendMessage(evt_msg, pixels)  
608 - ps.Publisher().sendMessage('Update slice viewer') 592 + mouse_x, mouse_y = self.interactor.GetEventPosition()
  593 + render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
  594 + slice_data = self.get_slice_data(render)
  595 +
  596 + # TODO: Improve!
  597 + for i in self.slice_data_list:
  598 + i.cursor.Show(0)
  599 + slice_data.cursor.Show()
  600 +
  601 + self.pick.Pick(mouse_x, mouse_y, 0, render)
  602 + coord = self.get_coordinate_cursor()
  603 + slice_data.cursor.SetPosition(coord)
  604 + slice_data.cursor.SetEditionPosition(
  605 + self.get_coordinate_cursor_edition(slice_data))
  606 + self.__update_cursor_position(slice_data, coord)
  607 +
  608 + if self._brush_cursor_op == const.BRUSH_ERASE:
  609 + evt_msg = 'Erase mask pixel'
  610 + elif self._brush_cursor_op == const.BRUSH_DRAW:
  611 + evt_msg = 'Add mask pixel'
  612 + elif self._brush_cursor_op == const.BRUSH_THRESH:
  613 + evt_msg = 'Edit mask pixel'
  614 +
  615 + if (self.left_pressed):
  616 + pixels = itertools.ifilter(self.test_operation_position,
  617 + slice_data.cursor.GetPixels())
  618 + ps.Publisher().sendMessage(evt_msg, pixels)
  619 + ps.Publisher().sendMessage('Update slice viewer')
609 620
610 self.interactor.Render() 621 self.interactor.Render()
611 622
@@ -627,9 +638,17 @@ class Viewer(wx.Panel): @@ -627,9 +638,17 @@ class Viewer(wx.Panel):
627 coord[1]) 638 coord[1])
628 ps.Publisher().sendMessage(('Set scroll position', 'AXIAL'), 639 ps.Publisher().sendMessage(('Set scroll position', 'AXIAL'),
629 coord[2]) 640 coord[2])
630 - elif(self.right_pressed):  
631 - self.OnZoomLeftClick(evt, obj)  
632 - self.OnZoomMove(evt, obj) 641 +
  642 + def OnZoomMoveRight(self, evt, obj):
  643 + if (self.right_pressed):
  644 + print "OnZoomMoveRight"
  645 + evt.Dolly()
  646 + evt.OnRightButtonDown()
  647 +
  648 + def OnZoomRightClick(self, evt, obj):
  649 + print "OnZoomRightClick"
  650 + evt.StartDolly()
  651 +
633 652
634 def OnCrossMouseClick(self, evt, obj): 653 def OnCrossMouseClick(self, evt, obj):
635 mouse_x, mouse_y = self.interactor.GetEventPosition() 654 mouse_x, mouse_y = self.interactor.GetEventPosition()
@@ -645,10 +664,7 @@ class Viewer(wx.Panel): @@ -645,10 +664,7 @@ class Viewer(wx.Panel):
645 coord[1]) 664 coord[1])
646 ps.Publisher().sendMessage(('Set scroll position', 'AXIAL'), 665 ps.Publisher().sendMessage(('Set scroll position', 'AXIAL'),
647 coord[2]) 666 coord[2])
648 - self.left_pressed = 1  
649 667
650 - def OnCrossMouseRelease(self, evt, obj):  
651 - self.left_pressed = 0  
652 668
653 def get_slice_data(self, render): 669 def get_slice_data(self, render):
654 for slice_data in self.slice_data_list: 670 for slice_data in self.slice_data_list:
@@ -776,7 +792,6 @@ class Viewer(wx.Panel): @@ -776,7 +792,6 @@ class Viewer(wx.Panel):
776 del slice_data 792 del slice_data
777 793
778 self.modes = []#['DEFAULT'] 794 self.modes = []#['DEFAULT']
779 - self.mouse_pressed = 0  
780 self.slice_data_list = [] 795 self.slice_data_list = []
781 self.layout = (1, 1) 796 self.layout = (1, 1)
782 self.orientation_texts = [] 797 self.orientation_texts = []
@@ -788,7 +803,6 @@ class Viewer(wx.Panel): @@ -788,7 +803,6 @@ class Viewer(wx.Panel):
788 803
789 def OnSetMode(self, pubsub_evt): 804 def OnSetMode(self, pubsub_evt):
790 state = pubsub_evt.data 805 state = pubsub_evt.data
791 - self.left_pressed = 0  
792 self.SetState(state) 806 self.SetState(state)
793 807
794 808
@@ -801,7 +815,7 @@ class Viewer(wx.Panel): @@ -801,7 +815,7 @@ class Viewer(wx.Panel):
801 self.scroll.Bind(wx.EVT_SCROLL_THUMBTRACK, self.OnScrollBarRelease) 815 self.scroll.Bind(wx.EVT_SCROLL_THUMBTRACK, self.OnScrollBarRelease)
802 #self.scroll.Bind(wx.EVT_SCROLL_ENDSCROLL, self.OnScrollBarRelease) 816 #self.scroll.Bind(wx.EVT_SCROLL_ENDSCROLL, self.OnScrollBarRelease)
803 self.interactor.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) 817 self.interactor.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
804 - self.interactor.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu) 818 + self.interactor.Bind(wx.EVT_RIGHT_UP, self.OnContextMenu)
805 self.Bind(wx.EVT_SIZE, self.OnSize) 819 self.Bind(wx.EVT_SIZE, self.OnSize)
806 820
807 def LoadImagedata(self, pubsub_evt): 821 def LoadImagedata(self, pubsub_evt):