Difference between revisions of "Events:CTK-Hackfest-2010/SlicerPythonQt"

From NAMIC Wiki
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
* All slots/property of QObject are automatically exposed to the python interpreter
 
* All slots/property of QObject are automatically exposed to the python interpreter
 
* Concept of decorator
 
* Concept of decorator
** Expose regualar cpp method of QObject  
+
** Expose regular cpp method of QObject  
 
** Expose object and method of pure cpp classes
 
** Expose object and method of pure cpp classes
 
* Complete wrapping of all Qt Classes
 
* Complete wrapping of all Qt Classes
* From PythonQt website: '''In contrast to PyQt  , PythonQt  is not a complete Python wrapper around the complete Qt functionality. So if you are looking for a way to write complete applications in Python using the Qt GUI, you should use PyQt.'''
+
* From PythonQt website: ''In contrast to PyQt  , PythonQt  is not a complete Python wrapper around the complete Qt functionality. So if you are looking for a way to write complete applications in Python using the Qt GUI, you should use PyQt.''
 
* See http://pythonqt.sourceforge.net/
 
* See http://pythonqt.sourceforge.net/
  
 
= Implementation =
 
= Implementation =
* CTK Layer  
+
== CTK Layer ==
** qCTKConsoleWidget  
+
* qCTKConsoleWidget  
** qCTKShellPython
+
* qCTKShellPython
** qCTKAbstractPythonManager:  
+
* qCTKAbstractPythonManager:  
*** registerPythonQtDecorator, registerClassForPythonQt, registerCPPClassForPythonQt, executeString, getVariable, executeFile, ...
+
** registerPythonQtDecorator, registerClassForPythonQt, registerCPPClassForPythonQt, executeString, getVariable, executeFile, ...
*** also virtual methods: pythonPaths and preInitialization
+
** also virtual methods: pythonPaths and preInitialization
  
* Concrete example
+
== Concrete example ==
** Slicer superbuild download and patch SVN PythonQt head
+
* Slicer superbuild download and patch SVN PythonQt head
** Slicer CMake script pre-compile all slicer .py class
+
* Slicer CMake script pre-compile all slicer .py class
** VTK all wrapped in python and exposed
+
* VTK all wrapped in python and exposed
** qSlicerPythonManager
+
* qSlicerPythonManager
 
<pre>
 
<pre>
 
void qSlicerPythonManager::preInitialization()
 
void qSlicerPythonManager::preInitialization()
Line 37: Line 37:
 
   this->executeFile(app->slicerHome() + "/bin/Python/slicer/slicerqt.py");
 
   this->executeFile(app->slicerHome() + "/bin/Python/slicer/slicerqt.py");
 
}
 
}
 +
</pre>
 +
 +
* logic.py
 +
<pre>
 +
""" This module loads all the classes from the vtkSlicerBaseLogic library into its
 +
namespace."""
 +
 +
import os
 +
 +
if os.name == 'posix':
 +
    from libSlicerBaseLogicPython import *
 +
else:
 +
    from SlicerBaseLogicPython import *
 +
</pre>
 +
 +
* slicerqt.py
 +
<pre>
 +
import slicer
 +
from slicer import vtk
 +
 +
def quit():
 +
  exit()
 +
   
 +
def exit():
 +
  app().quit()
 +
 +
def app():
 +
  return _qSlicerCoreApplicationInstance
 +
 +
def getModuleNames():
 +
  return app().moduleManager().factoryManager().moduleNames()
 +
 +
def getModule(moduleName):
 +
  module = app().moduleManager().module(moduleName);
 +
  if not module:
 +
    print "Could not find module with name '%s" % moduleName
 +
    return None
 +
  return module
 +
 +
def getModuleGui(moduleName):
 +
  module = getModule(moduleName)
 +
  if not module:
 +
    return None
 +
  widgetRepr = module.widgetRepresentation()
 +
  if not widgetRepr:
 +
    print "Could not find module widget representation with name '%s" % moduleName
 +
    return None
 +
  return widgetRepr
 
</pre>
 
</pre>

Latest revision as of 07:14, 8 March 2010

Home < Events:CTK-Hackfest-2010 < SlicerPythonQt

Overview

  • All slots/property of QObject are automatically exposed to the python interpreter
  • Concept of decorator
    • Expose regular cpp method of QObject
    • Expose object and method of pure cpp classes
  • Complete wrapping of all Qt Classes
  • From PythonQt website: In contrast to PyQt , PythonQt is not a complete Python wrapper around the complete Qt functionality. So if you are looking for a way to write complete applications in Python using the Qt GUI, you should use PyQt.
  • See http://pythonqt.sourceforge.net/

Implementation

CTK Layer

  • qCTKConsoleWidget
  • qCTKShellPython
  • qCTKAbstractPythonManager:
    • registerPythonQtDecorator, registerClassForPythonQt, registerCPPClassForPythonQt, executeString, getVariable, executeFile, ...
    • also virtual methods: pythonPaths and preInitialization

Concrete example

  • Slicer superbuild download and patch SVN PythonQt head
  • Slicer CMake script pre-compile all slicer .py class
  • VTK all wrapped in python and exposed
  • qSlicerPythonManager
void qSlicerPythonManager::preInitialization()
{
  Superclass::preInitialization();

  // Register decorators
  this->registerPythonQtDecorator(new qSlicerBaseQTBasePythonQtDecorators(this));

  qSlicerCoreApplication* app = qSlicerCoreApplication::application();

  // Add object to python interpreter context
  this->addObjectToPythonMain("_qSlicerCoreApplicationInstance", app);

  // Evaluate application script
  this->executeFile(app->slicerHome() + "/bin/Python/slicer/slicerqt.py");
}
  • logic.py
""" This module loads all the classes from the vtkSlicerBaseLogic library into its
namespace."""

import os

if os.name == 'posix':
    from libSlicerBaseLogicPython import *
else:
    from SlicerBaseLogicPython import *
  • slicerqt.py
import slicer
from slicer import vtk

def quit():
  exit()
    
def exit():
  app().quit()

def app():
  return _qSlicerCoreApplicationInstance

def getModuleNames():
  return app().moduleManager().factoryManager().moduleNames()

def getModule(moduleName):
  module = app().moduleManager().module(moduleName);
  if not module:
    print "Could not find module with name '%s" % moduleName
    return None
  return module

def getModuleGui(moduleName):
  module = getModule(moduleName)
  if not module:
    return None
  widgetRepr = module.widgetRepresentation()
  if not widgetRepr:
    print "Could not find module widget representation with name '%s" % moduleName
    return None
  return widgetRepr