Slicer  4.11
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
AnnotationsSubjectHierarchyPlugin.py
Go to the documentation of this file.
1 import vtk, qt, ctk, slicer
2 import logging
3 from AbstractScriptedSubjectHierarchyPlugin import *
4 
6  """ Scripted subject hierarchy plugin for the Annotations module.
7 
8  This is also an example for scripted plugins, so includes all possible methods.
9  The methods that are not needed (i.e. the default implementation in
10  qSlicerSubjectHierarchyAbstractPlugin is satisfactory) can simply be
11  omitted in plugins created based on this one.
12 
13  The plugin registers itself on creation, but needs to be initialized from the
14  module or application as follows:
15  from SubjectHierarchyPlugins import AnnotationsSubjectHierarchyPlugin
16  scriptedPlugin = slicer.qSlicerSubjectHierarchyScriptedPlugin(None)
17  scriptedPlugin.setPythonSource(AnnotationsSubjectHierarchyPlugin.filePath)
18  """
19 
20  # Necessary static member to be able to set python source to scripted subject hierarchy plugin
21  filePath = __file__
22 
23  def __init__(self, scriptedPlugin):
24  scriptedPlugin.name = 'Annotations'
25  AbstractScriptedSubjectHierarchyPlugin.__init__(self, scriptedPlugin)
26 
27  def canAddNodeToSubjectHierarchy(self, node, parentItemID):
28  if node is not None:
29  if node.IsA("vtkMRMLAnnotationROINode") or node.IsA("vtkMRMLAnnotationRulerNode"):
30  return 1.0
31  return 0.0
32 
33  def canOwnSubjectHierarchyItem(self, itemID):
34  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
35  shNode = pluginHandlerSingleton.subjectHierarchyNode()
36  associatedNode = shNode.GetItemDataNode(itemID)
37  # ROI or Ruler
38  if associatedNode is not None:
39  if associatedNode.IsA("vtkMRMLAnnotationROINode") or associatedNode.IsA("vtkMRMLAnnotationRulerNode"):
40  return 1.0
41  return 0.0
42 
43  def roleForPlugin(self):
44  return "Annotation"
45 
46  def helpText(self):
47  # return ("<p style=\" margin-top:4px; margin-bottom:1px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
48  # "<span style=\" font-family:'sans-serif'; font-size:9pt; font-weight:600; color:#000000;\">"
49  # "SegmentEditor module subject hierarchy help text"
50  # "</span>"
51  # "</p>"
52  # "<p style=\" margin-top:0px; margin-bottom:11px; margin-left:26px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
53  # "<span style=\" font-family:'sans-serif'; font-size:9pt; color:#000000;\">"
54  # "This is how you can add help text to the subject hierarchy module help box via a python scripted plugin."
55  # "</span>"
56  # "</p>\n")
57  return ""
58 
59  def icon(self, itemID):
60  import os
61  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
62  shNode = pluginHandlerSingleton.subjectHierarchyNode()
63  associatedNode = shNode.GetItemDataNode(itemID);
64  if associatedNode is not None:
65  # ROI
66  if associatedNode.IsA("vtkMRMLAnnotationROINode"):
67  roiIconPath = os.path.join(os.path.dirname(__file__), '../Resources/Icons/AnnotationROI.png')
68  if os.path.exists(roiIconPath):
69  return qt.QIcon(roiIconPath)
70  # Ruler
71  if associatedNode.IsA("vtkMRMLAnnotationRulerNode"):
72  rulerIconPath = os.path.join(os.path.dirname(__file__), '../Resources/Icons/AnnotationDistance.png')
73  if os.path.exists(rulerIconPath):
74  return qt.QIcon(rulerIconPath)
75  # Item unknown by plugin
76  return qt.QIcon()
77 
78  def visibilityIcon(self, visible):
79  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
80  return pluginHandlerSingleton.pluginByName('Default').visibilityIcon(visible)
81 
82  def editProperties(self, itemID):
83  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
84  pluginHandlerSingleton.pluginByName('Default').editProperties(itemID)
85 
87  return []
88 
90  return []
91 
92  def showContextMenuActionsForItem(self, itemID):
93  pass
94 
96  """ Important note:
97  In order to use view menus in scripted plugins, it needs to be registered differently,
98  so that the Python API can be fully built by the time this function is called.
99 
100  The following changes are necessary:
101  1. Remove or comment out the following line from constructor
102  AbstractScriptedSubjectHierarchyPlugin.__init__(self, scriptedPlugin)
103  2. In addition to the initialization where the scripted plugin is instantialized and
104  the source set, the plugin also needs to be registered manually:
105  pluginHandler = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
106  pluginHandler.registerPlugin(scriptedPlugin)
107  """
108  return []
109 
110  def showViewContextMenuActionsForItem(self, itemID, eventData):
111  pass
112 
113  def tooltip(self, itemID):
114  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
115  tooltip = pluginHandlerSingleton.pluginByName('Default').tooltip(itemID)
116  return str(tooltip)
117 
118  def setDisplayVisibility(self, itemID, visible):
119  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
120  pluginHandlerSingleton.pluginByName('Default').setDisplayVisibility(itemID, visible)
121 
122  def getDisplayVisibility(self, itemID):
123  pluginHandlerSingleton = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
124  return pluginHandlerSingleton.pluginByName('Default').getDisplayVisibility(itemID)