2 import vtk, qt, ctk, slicer
4 from SegmentEditorEffects
import *
7 """ DrawEffect is a LabelEffect implementing the interactive draw 8 tool in the segment editor 12 scriptedEffect.name =
'Draw' 14 AbstractScriptedSegmentEditorLabelEffect.__init__(self, scriptedEffect)
17 import qSlicerSegmentationsEditorEffectsPythonQt
as effects
18 clonedEffect = effects.qSlicerSegmentEditorScriptedLabelEffect(
None)
19 clonedEffect.setPythonSource(__file__.replace(
'\\',
'/'))
23 iconPath = os.path.join(os.path.dirname(__file__),
'Resources/Icons/Draw.png')
24 if os.path.exists(iconPath):
25 return qt.QIcon(iconPath)
29 return """<html>Draw segment outline in slice viewers<br>. 30 <p><ul style="margin: 0"> 31 <li><b>Left-click:</b> add point.</li> 32 <li><b>Left-button drag-and-drop:</b> add multiple points.</li> 33 <li><b>x:</b> delete last point.</li> 34 <li><b>Right-click</b> or <b>a</b> or <b>enter:</b> apply outline.</li> 40 self.scriptedEffect.removeActor2D(sliceWidget, pipeline.actor)
50 if viewWidget.className() !=
"qMRMLSliceWidget":
53 pipeline = self.pipelineForWidget(viewWidget)
57 if eventId == vtk.vtkCommand.LeftButtonPressEvent:
59 confirmedEditingAllowed = self.scriptedEffect.confirmCurrentSegmentVisible()
60 if confirmedEditingAllowed == self.scriptedEffect.NotConfirmed
or confirmedEditingAllowed == self.scriptedEffect.ConfirmedWithDialog:
65 pipeline.actionState =
"drawing" 66 self.scriptedEffect.cursorOff(viewWidget)
67 xy = callerInteractor.GetEventPosition()
68 ras = self.xyToRas(xy, viewWidget)
69 pipeline.addPoint(ras)
71 elif eventId == vtk.vtkCommand.LeftButtonReleaseEvent:
72 pipeline.actionState =
"" 73 self.scriptedEffect.cursorOn(viewWidget)
74 elif eventId == vtk.vtkCommand.RightButtonPressEvent:
75 sliceNode = viewWidget.sliceLogic().GetSliceNode()
76 pipeline.lastInsertSliceNodeMTime = sliceNode.GetMTime()
77 elif eventId == vtk.vtkCommand.RightButtonReleaseEvent:
78 sliceNode = viewWidget.sliceLogic().GetSliceNode()
79 if abs(pipeline.lastInsertSliceNodeMTime - sliceNode.GetMTime()) < 2:
81 pipeline.actionState =
None 82 elif eventId == vtk.vtkCommand.MouseMoveEvent:
83 if pipeline.actionState ==
"drawing":
84 xy = callerInteractor.GetEventPosition()
85 ras = self.xyToRas(xy, viewWidget)
86 pipeline.addPoint(ras)
88 elif eventId == vtk.vtkCommand.KeyPressEvent:
89 key = callerInteractor.GetKeySym()
90 if key ==
'a' or key ==
'Return':
94 pipeline.deleteLastPoint()
99 pipeline.positionActors()
103 if callerViewNode
and callerViewNode.IsA(
'vtkMRMLSliceNode'):
107 logging.error(
'processViewNodeEvents: Invalid pipeline')
112 sliceLogic = viewWidget.sliceLogic()
114 currentSlice = sliceLogic.GetSliceOffset()
115 if pipeline.activeSlice:
116 offset = abs(currentSlice - pipeline.activeSlice)
119 pipeline.setLineMode(lineMode)
120 pipeline.positionActors()
127 pipeline =
DrawPipeline(self.scriptedEffect, sliceWidget)
130 renderer = self.scriptedEffect.renderer(sliceWidget)
132 logging.error(
"pipelineForWidget: Failed to get renderer!")
134 self.scriptedEffect.addActor2D(sliceWidget, pipeline.actor)
143 """ Visualization objects and pipeline for each slice view for drawing 160 actorProperty = self.
actor.GetProperty()
161 actorProperty.SetColor(1,1,0)
162 actorProperty.SetLineWidth(1)
166 polyData = vtk.vtkPolyData()
168 lines = vtk.vtkCellArray()
169 polyData.SetLines(lines)
177 currentSlice = sliceLogic.GetSliceOffset()
187 sliceNode = sliceLogic.GetSliceNode()
192 idList = vtk.vtkIdList()
193 idList.InsertNextId(p-1)
194 idList.InsertNextId(p)
195 self.
polyData.InsertNextCell(vtk.VTK_LINE, idList)
198 actorProperty = self.
actor.GetProperty()
200 actorProperty.SetLineStipplePattern(0xffff)
201 elif mode ==
"dashed":
202 actorProperty.SetLineStipplePattern(0xff00)
207 sliceNode = sliceLogic.GetSliceNode()
208 rasToXY = vtk.vtkTransform()
209 rasToXY.SetMatrix(sliceNode.GetXYToRAS())
218 lineExists = lines.GetNumberOfCells() > 0
221 idList = vtk.vtkIdList()
222 idList.InsertNextId(self.
polyData.GetNumberOfPoints()-1)
223 idList.InsertNextId(0)
224 self.
polyData.InsertNextCell(vtk.VTK_LINE, idList)
227 import vtkSegmentationCorePython
as vtkSegmentationCore
231 segmentationNode = self.
scriptedEffect.parameterSetNode().GetSegmentationNode()
237 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd)
249 pcount = self.
rasPoints.GetNumberOfPoints()
256 cellCount = self.
polyData.GetNumberOfCells()
258 self.
polyData.DeleteCell(cellCount - 1)
def pipelineForWidget(self, sliceWidget)
def setupOptionsFrame(self)
def __init__(self, scriptedEffect, sliceWidget)
def processViewNodeEvents(self, callerViewNode, eventId, viewWidget)
def deleteLastPoint(self)
def processInteractionEvents(self, callerInteractor, eventId, viewWidget)
def setLineMode(self, mode="solid")
def __init__(self, scriptedEffect)