slicerqt-with-tcl.py 3.15 KB
Newer Older
1
2
3
4
5
6
7
8

#
# This script should be sourced after slicerqt.py
#

def tcl(cmd):
  global _tpycl
  try:
9
    import os
10
    os.environ['Slicer_HOME']
11
12
  except KeyError:
    home = os.path.dirname(os.path.dirname(sys.executable))
13
    os.environ['Slicer_HOME'] = home
14
15
16
17
18
19
20
  try:
    _tpycl
  except NameError:
    # no tcl yet, so first bring in the adapters, then the actual code
    import tpycl
    _tpycl = tpycl.tpycl()
    _tpycl.tcl_eval("""
21
        set dir $::env(Slicer_HOME)/lib/Slicer3/SlicerBaseGUI/Tcl
22
23
24
25
26
27
28
        source $dir/Slicer3Adapters.tcl
        ::Slicer3Adapters::Initialize
      """)
    
    # TODO: not clear why package require is not working 
    # - instead, source the pkgIndex directly
    _tpycl.tcl_eval("""
29
        set dir $::env(Slicer_HOME)/lib/Slicer3/SlicerBaseGUI/Tcl
30
31
32
33
34
        source $dir/pkgIndex.tcl
      """)

  return _tpycl.tcl_eval(cmd)

35
class _sliceWidget(object):
36
  """ an empty class that can be instanced as a place to store 
37
  references to sliceWidget components
38
39
40
41
  """
  def __init__(self):
    pass

42
def registerScriptedDisplayableManagers(sliceWidget):
43
44
  """ called from qSlicerLayoutManager::createSliceView
      after it creates python wrappers for the vtk parts of
45
      the sliceWidget
46
47
  """
  # create an instance of the adapter class
48
  sliceGUIName = 'sliceGUI%s' % sliceWidget
49
50
51
52
53
54
55
56
  tcl('::Slicer3Adapters::SliceGUI %s' % sliceGUIName)
  # create procs for the elements and set them in the adapter
  #    leave out interactor and renderWindow,
  #    since you can get these from the InteractorStyle
  # TODO ('cornerAnnotation', 'SetCornerAnnotation'),
  for key,method in (
      ('sliceLogic', 'SetLogic'),
      ('interactorStyle', 'SetInteractorStyle')):
57
    instName = 'slicer.sliceWidget%s_%s' % (sliceWidget, key)
58
59
60
61
    evalString = '%s.GetClassName()' % instName
    instClass = eval(evalString)
    # creat a proc that represents the instance
    tcl('set procName [::tpycl::uniqueInstanceName %s]' % instClass)
62
    tclCmd = 'proc $procName {args} {::tpycl::methodCaller slicer.sliceWidget%s_%s $args}' % (sliceWidget, key)
63
64
65
66
    tcl(tclCmd)
    # set the new tcl instance into the sliceGUI instance for this slice
    tcl('%s %s $procName' % (sliceGUIName, method))
    #
67
      
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  sWidget = tcl('SliceSWidget #auto %s' % sliceGUIName)
  tcl('%s configure -calculateAnnotations 0' % sWidget)
  tcl('''
    # turn off event processing for all swidget instances
    foreach sw [itcl::find objects -isa SWidget] {
      $sw configure -enabled false
    }
    # enable the specific instances that are working well currently (low overhead)
    set enabledSWidget {SliceSWidget FiducialsSWidget VolumeDisplaySWidget}
    foreach esw $enabledSWidget {
      foreach sw [itcl::find objects -isa $esw] {
        $sw configure -enabled true
      }
    }
    # tell the SliceSWidget to create instances of only specific widget types
    foreach sw [itcl::find objects -isa SliceSWidget] {
      $sw configure -swidgetTypes  {}
    }
  ''')
87
88
89

if __name__ == "__main__":

90
  # Initialize global slicer.sliceWidgets dict
91
92
  # -- it gets populated in qSlicerLayoutManagerPrivate::createSliceView
  #    and then used by the scripted code that needs to access the slice views
93
  slicer.sliceWidgets = {}