Slicer 4.2
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
slicerqt.py
Go to the documentation of this file.
00001 import vtk, qt, ctk
00002 import slicer
00003 
00004 from slicer.util import *
00005 import slicer.cli
00006 
00007 #
00008 # loadSlicerRCFile - Let's not add this function to 'slicer.util' so that 
00009 # the global dictionary of the main context is passed to execfile().
00010 #
00011 
00012 def loadSlicerRCFile():
00013   """If it exists, execute slicer resource script '~/.slicerrc.py'"""
00014   import os
00015   if os.environ.has_key('SLICERRC'):
00016     rcfile = os.environ['SLICERRC']
00017   else:
00018     import os.path
00019     rcfile = os.path.expanduser( '~/.slicerrc.py' )
00020 
00021   if os.path.isfile( rcfile ):
00022     print 'Loading Slicer RC file [%s]' % ( rcfile )
00023     execfile( rcfile )
00024 
00025 #
00026 # Internal
00027 #
00028 
00029 class _Internal():
00030 
00031   def __init__( self ):
00032     import imp
00033 
00034     # Set attribute 'slicer.app'
00035     setattr( slicer, 'app', _qSlicerCoreApplicationInstance )
00036 
00037     # Listen factory and module manager to update slicer.{modules, moduleNames} when appropriate
00038     moduleManager = slicer.app.moduleManager()
00039 
00040     # if the qSlicerApplication is only minimally initialized, the factoryManager does not exist.
00041     # this would be the case if f.e. a commandline module wants to use qSlicerApplication for tcl
00042     # access but without all the managers.
00043     # this is not the default behavior.
00044     if hasattr( moduleManager, 'factoryManager' ):
00045       factoryManager = moduleManager.factoryManager()
00046       factoryManager.connect( 'allModulesRegistered()', self.setSlicerModuleNames )
00047       moduleManager.connect( 'moduleLoaded(qSlicerAbstractCoreModule*)', self.setSlicerModules )
00048 
00049     # Add module 'slicer.moduleNames'
00050     _moduleNames = imp.new_module( 'moduleNames' )
00051     setattr( slicer, _moduleNames.__name__, _moduleNames )
00052 
00053     # Add module 'slicer.modules'
00054     _modules = imp.new_module( 'modules' )
00055     setattr( slicer, _modules.__name__, _modules )
00056 
00057     # Retrieve current instance of the scene and set 'slicer.mrmlScene'
00058     setattr( slicer, 'mrmlScene', slicer.app.mrmlScene() )
00059 
00060     # HACK - Since qt.QTimer.singleShot is both a property and a static method, the property
00061     # is wrapped in python and prevent the call to the convenient static method having
00062     # the same name. To fix the problem, let's overwrite it's value.
00063     # Ideally this should be fixed in PythonQt itself.
00064     def _singleShot( msec, receiverOrCallable, member=None ):
00065       """Calls either a python function or a slot after a given time interval."""
00066       # Add 'moduleManager' as parent to prevent the premature destruction of the timer.
00067       # Doing so, we ensure that the QTimer will be deleted before PythonQt is cleanup.
00068       # Indeed, the moduleManager is destroyed before the pythonManager.
00069       timer = qt.QTimer( slicer.app.moduleManager() )
00070       timer.setSingleShot( True )
00071       if callable( receiverOrCallable ):
00072         timer.connect( "timeout()", receiverOrCallable )
00073       else:
00074         timer.connect( "timeout()", receiverOrCallable, member )
00075       timer.start( msec )
00076 
00077     qt.QTimer.singleShot = staticmethod( _singleShot )
00078 
00079   def setSlicerModuleNames( self ):
00080     """Add module names as attributes of module slicer.moduleNames"""
00081     for name in moduleNames():
00082       setattr( slicer.moduleNames, name, name )
00083 
00084   def setSlicerModules( self, module ):
00085     """Add modules as attributes of module slicer.modules"""
00086     setattr( slicer.modules, module.name, module )
00087 
00088 
00089 _internalInstance = _Internal()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines