Commit 9cbf791e35f212fdf2b10ddf976636fb6581c7e9

Authored by Thiago Franco de Moraes
1 parent 91ea38bc
Exists in interactor_style

Created a new interactor style to handle the editor

invesalius/data/styles.py
@@ -138,17 +138,6 @@ class CrossInteractorStyle(DefaultInteractorStyle): @@ -138,17 +138,6 @@ class CrossInteractorStyle(DefaultInteractorStyle):
138 138
139 iren.Render() 139 iren.Render()
140 140
141 - def get_coordinate_cursor(self):  
142 - # Find position  
143 - x, y, z = self.picker.GetPickPosition()  
144 - bounds = self.slice_actor.GetBounds()  
145 - if bounds[0] == bounds[1]:  
146 - x = bounds[0]  
147 - elif bounds[2] == bounds[3]:  
148 - y = bounds[2]  
149 - elif bounds[4] == bounds[5]:  
150 - z = bounds[4]  
151 - return x, y, z  
152 141
153 def calcultate_scroll_position(self, position): 142 def calcultate_scroll_position(self, position):
154 # Based in the given coord (x, y, z), returns a list with the scroll positions for each 143 # Based in the given coord (x, y, z), returns a list with the scroll positions for each
@@ -444,6 +433,145 @@ class ChangeSliceInteractorStyle(DefaultInteractorStyle): @@ -444,6 +433,145 @@ class ChangeSliceInteractorStyle(DefaultInteractorStyle):
444 self.last_position = position[1] 433 self.last_position = position[1]
445 434
446 435
  436 +class EditorInteractorStyle(DefaultInteractorStyle):
  437 + def __init__(self, viewer):
  438 + DefaultInteractorStyle.__init__(self, viewer)
  439 +
  440 + self.viewer = viewer
  441 + self.orientation = self.viewer.orientation
  442 +
  443 + self.picker = vtk.vtkWorldPointPicker()
  444 +
  445 + self.AddObserver("EnterEvent", self.OnEnterInteractor)
  446 + self.AddObserver("LeaveEvent", self.OnLeaveInteractor)
  447 +
  448 + self.AddObserver("LeftButtonPressEvent", self.OnBrushClick)
  449 + self.AddObserver("LeftButtonReleaseEvent", self.OnBrushClick)
  450 + self.AddObserver("MouseMoveEvent", self.OnBrushMove)
  451 +
  452 + def OnEnterInteractor(self, obj, evt):
  453 + if (self.viewer.slice_.buffer_slices[self.orientation].mask is None):
  454 + return
  455 + self.viewer.slice_data.cursor.Show()
  456 + self.viewer.interactor.SetCursor(wx.StockCursor(wx.CURSOR_BLANK))
  457 + self.viewer.interactor.Render()
  458 +
  459 + def OnLeaveInteractor(self, obj, evt):
  460 + self.viewer.slice_data.cursor.Show(0)
  461 + self.viewer.interactor.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
  462 + self.viewer.interactor.Render()
  463 +
  464 + def OnBrushClick(self, obj, evt):
  465 + if (self.viewer.slice_.buffer_slices[self.orientation].mask is None):
  466 + return
  467 +
  468 + viewer = self.viewer
  469 + iren = viewer.interactor
  470 +
  471 + viewer._set_editor_cursor_visibility(1)
  472 +
  473 + mouse_x, mouse_y = iren.GetEventPosition()
  474 + render = iren.FindPokedRenderer(mouse_x, mouse_y)
  475 + slice_data = viewer.get_slice_data(render)
  476 +
  477 + # TODO: Improve!
  478 + #for i in self.slice_data_list:
  479 + #i.cursor.Show(0)
  480 + slice_data.cursor.Show()
  481 +
  482 + self.picker.Pick(mouse_x, mouse_y, 0, render)
  483 +
  484 + coord = self.get_coordinate_cursor()
  485 + position = slice_data.actor.GetInput().FindPoint(coord)
  486 +
  487 + if position != -1:
  488 + coord = slice_data.actor.GetInput().GetPoint(position)
  489 +
  490 + slice_data.cursor.SetPosition(coord)
  491 + cursor = slice_data.cursor
  492 + radius = cursor.radius
  493 +
  494 + if position < 0:
  495 + position = viewer.calculate_matrix_position(coord)
  496 +
  497 + viewer.slice_.edit_mask_pixel(viewer._brush_cursor_op, cursor.GetPixels(),
  498 + position, radius, viewer.orientation)
  499 + viewer._flush_buffer = True
  500 +
  501 + # TODO: To create a new function to reload images to viewer.
  502 + viewer.OnScrollBar()
  503 +
  504 + def OnBrushMove(self, obj, evt):
  505 + if (self.viewer.slice_.buffer_slices[self.orientation].mask is None):
  506 + return
  507 +
  508 + viewer = self.viewer
  509 + iren = viewer.interactor
  510 +
  511 + viewer._set_editor_cursor_visibility(1)
  512 +
  513 + mouse_x, mouse_y = iren.GetEventPosition()
  514 + render = iren.FindPokedRenderer(mouse_x, mouse_y)
  515 + slice_data = viewer.get_slice_data(render)
  516 +
  517 + # TODO: Improve!
  518 + #for i in self.slice_data_list:
  519 + #i.cursor.Show(0)
  520 +
  521 + self.picker.Pick(mouse_x, mouse_y, 0, render)
  522 +
  523 + #if (self.pick.GetViewProp()):
  524 + #self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_BLANK))
  525 + #else:
  526 + #self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
  527 +
  528 + coord = self.get_coordinate_cursor()
  529 + position = viewer.slice_data.actor.GetInput().FindPoint(coord)
  530 +
  531 + # when position == -1 the cursos is not over the image, so is not
  532 + # necessary to set the cursor position to world coordinate center of
  533 + # pixel from slice image.
  534 + if position != -1:
  535 + coord = slice_data.actor.GetInput().GetPoint(position)
  536 + slice_data.cursor.SetPosition(coord)
  537 + #self.__update_cursor_position(slice_data, coord)
  538 +
  539 + if (self.left_pressed):
  540 + cursor = slice_data.cursor
  541 + position = slice_data.actor.GetInput().FindPoint(coord)
  542 + radius = cursor.radius
  543 +
  544 + if position < 0:
  545 + position = viewer.calculate_matrix_position(coord)
  546 +
  547 + viewer.slice_.edit_mask_pixel(viewer._brush_cursor_op, cursor.GetPixels(),
  548 + position, radius, self.orientation)
  549 + # TODO: To create a new function to reload images to viewer.
  550 + viewer.OnScrollBar(update3D=False)
  551 +
  552 + else:
  553 + viewer.interactor.Render()
  554 +
  555 + def OnBrushRelease(self, evt, obj):
  556 + if (self.viewer.slice_.buffer_slices[self.orientation].mask is None):
  557 + return
  558 +
  559 + self.viewer.slice_.apply_slice_buffer_to_mask(self.orientation)
  560 + self.viewer_flush_buffer = False
  561 +
  562 + def get_coordinate_cursor(self):
  563 + # Find position
  564 + x, y, z = self.picker.GetPickPosition()
  565 + bounds = self.viewer.slice_data.actor.GetBounds()
  566 + if bounds[0] == bounds[1]:
  567 + x = bounds[0]
  568 + elif bounds[2] == bounds[3]:
  569 + y = bounds[2]
  570 + elif bounds[4] == bounds[5]:
  571 + z = bounds[4]
  572 + return x, y, z
  573 +
  574 +
447 class ViewerStyle: 575 class ViewerStyle:
448 def __init__(self): 576 def __init__(self):
449 self.interactor = None 577 self.interactor = None
invesalius/data/viewer_slice.py
@@ -175,6 +175,9 @@ class Viewer(wx.Panel): @@ -175,6 +175,9 @@ class Viewer(wx.Panel):
175 interactor.SetInteractorStyle(style) 175 interactor.SetInteractorStyle(style)
176 176
177 def SetInteractorStyle(self, state): 177 def SetInteractorStyle(self, state):
  178 + self.__set_cross_visibility(0)
  179 + self.on_wl = False
  180 + self.wl_text.Hide()
178 if state == const.STATE_DEFAULT: 181 if state == const.STATE_DEFAULT:
179 style = styles.DefaultInteractorStyle(self) 182 style = styles.DefaultInteractorStyle(self)
180 self.style = style 183 self.style = style
@@ -247,6 +250,13 @@ class Viewer(wx.Panel): @@ -247,6 +250,13 @@ class Viewer(wx.Panel):
247 self.interactor.SetInteractorStyle(style) 250 self.interactor.SetInteractorStyle(style)
248 self.interactor.Render() 251 self.interactor.Render()
249 252
  253 + elif state == const.SLICE_STATE_EDITOR:
  254 + style = styles.EditorInteractorStyle(self)
  255 +
  256 + self.style = style
  257 + self.interactor.SetInteractorStyle(style)
  258 + self.interactor.Render()
  259 +
250 else: 260 else:
251 self.state = state 261 self.state = state
252 action = { 262 action = {
@@ -278,7 +288,7 @@ class Viewer(wx.Panel): @@ -278,7 +288,7 @@ class Viewer(wx.Panel):
278 self.on_wl = False 288 self.on_wl = False
279 self.wl_text.Hide() 289 self.wl_text.Hide()
280 290
281 - self.__set_editor_cursor_visibility(0) 291 + self._set_editor_cursor_visibility(0)
282 292
283 # Bind method according to current mode 293 # Bind method according to current mode
284 if(state == const.STATE_ZOOM_SL): 294 if(state == const.STATE_ZOOM_SL):
@@ -654,7 +664,7 @@ class Viewer(wx.Panel): @@ -654,7 +664,7 @@ class Viewer(wx.Panel):
654 if (self.slice_.buffer_slices[self.orientation].mask is None): 664 if (self.slice_.buffer_slices[self.orientation].mask is None):
655 return 665 return
656 666
657 - self.__set_editor_cursor_visibility(1) 667 + self._set_editor_cursor_visibility(1)
658 668
659 mouse_x, mouse_y = self.interactor.GetEventPosition() 669 mouse_x, mouse_y = self.interactor.GetEventPosition()
660 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y) 670 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
@@ -691,7 +701,7 @@ class Viewer(wx.Panel): @@ -691,7 +701,7 @@ class Viewer(wx.Panel):
691 if (self.slice_.buffer_slices[self.orientation].mask is None): 701 if (self.slice_.buffer_slices[self.orientation].mask is None):
692 return 702 return
693 703
694 - self.__set_editor_cursor_visibility(1) 704 + self._set_editor_cursor_visibility(1)
695 705
696 mouse_x, mouse_y = self.interactor.GetEventPosition() 706 mouse_x, mouse_y = self.interactor.GetEventPosition()
697 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y) 707 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
@@ -1166,7 +1176,7 @@ class Viewer(wx.Panel): @@ -1166,7 +1176,7 @@ class Viewer(wx.Panel):
1166 def __set_cross_visibility(self, visibility): 1176 def __set_cross_visibility(self, visibility):
1167 self.cross_actor.SetVisibility(visibility) 1177 self.cross_actor.SetVisibility(visibility)
1168 1178
1169 - def __set_editor_cursor_visibility(self, visibility): 1179 + def _set_editor_cursor_visibility(self, visibility):
1170 for slice_data in self.slice_data_list: 1180 for slice_data in self.slice_data_list:
1171 slice_data.cursor.actor.SetVisibility(visibility) 1181 slice_data.cursor.actor.SetVisibility(visibility)
1172 1182