qSlicerPythonManager.cxx 3.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*=auto=========================================================================

 Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) 
 All Rights Reserved.

 See Doc/copyright/copyright.txt
 or http://www.slicer.org/copyright/copyright.txt for details.

 Program:   3D Slicer

=========================================================================auto=*/

13
14
15
// PythonQT includes
#include <PythonQt.h>

16
17
18
// CTK includes
#include <ctkLogger.h>

19
20
21
22
23
// SlicerQt includes
#include "qSlicerPythonManager.h"
#include "qSlicerApplication.h"
#include "qSlicerBaseQTGUIPythonQtDecorators.h"

24
#ifdef Slicer_USE_PYTHONQT_WITH_TCL
25
// SlicerVTK includes
26
#include "vtkEventBroker.h"
27
#endif
28

29
30
31
// PythonQt wrapper initialization methods
void PythonQt_init_org_commontk_CTKWidgets(PyObject*);
void PythonQt_init_org_commontk_CTKVisualizationVTKWidgets(PyObject*);
32
void PythonQt_init_org_slicer_libs_qMRMLWidgets(PyObject*);
33

34
35
36
37
//--------------------------------------------------------------------------
static ctkLogger logger("org.slicer.base.qtgui.qSlicerPythonManager");
//--------------------------------------------------------------------------

38
39
40
41
42
43
44
45
46
//-----------------------------------------------------------------------------
qSlicerPythonManager::qSlicerPythonManager(QObject* _parent) : Superclass(_parent)
{

}

//-----------------------------------------------------------------------------
qSlicerPythonManager::~qSlicerPythonManager()
{
47
#ifdef Slicer_USE_PYTHONQT_WITH_TCL
48
49
  vtkEventBroker::GetInstance()->SetScriptHandler(0, 0);
#endif
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
}

//-----------------------------------------------------------------------------
QStringList qSlicerPythonManager::pythonPaths()
{  
  QStringList paths;  
  paths << Superclass::pythonPaths();
  
  return paths; 
}

//-----------------------------------------------------------------------------
void qSlicerPythonManager::preInitialization()
{
  Superclass::preInitialization();
65
66
67
68
  
  // Initialize wrappers
  PythonQt_init_org_commontk_CTKWidgets(0);
  PythonQt_init_org_commontk_CTKVisualizationVTKWidgets(0);
69
  PythonQt_init_org_slicer_libs_qMRMLWidgets(0);
70
71
72
73
74
75
76
77

  // Register decorators
  this->registerPythonQtDecorator(new qSlicerBaseQTGUIPythonQtDecorators(this));
  
  qSlicerApplication* app = qSlicerApplication::application();
  
  // Evaluate application script
  this->executeFile(app->slicerHome() + "/bin/Python/slicer/slicerqt.py");
78

79
#ifdef Slicer_USE_PYTHONQT_WITH_TCL
80
81
82
83
  // Evaluate application script specific to the TCL layer
  // Note that it should be sourced after slicerqt.py
  this->executeFile(app->slicerHome() + "/bin/Python/slicer/slicerqt-with-tcl.py");

84
  // -- event broker
85
86
  // - script handler to pass callback strings to the tcl interpeter
  // - synchronous mode so that redundant events do not get collapsed
87
  //   (this is compatible with standard vtk event behavior, but adds
88
89
90
  //   the ability to trace event execution and auto cleanup on Delete events).
  vtkEventBroker::GetInstance()->SetScriptHandler(qSlicerPythonManager::eventBrokerScriptHandler,
                                                  reinterpret_cast<void *>(this));
91
  vtkEventBroker::GetInstance()->SetEventModeToSynchronous();
92
  this->addVTKObjectToPythonMain("slicer.broker", vtkEventBroker::GetInstance());
93
#endif
94
95
96
97

}

//-----------------------------------------------------------------------------
98
void qSlicerPythonManager::eventBrokerScriptHandler(const char *script, void *clientData)
99
{
100
#ifdef Slicer_USE_PYTHONQT_WITH_TCL
101
102
103
  QString pythonScript = QString("global _tpycl; _tpycl.tcl_callback('%1')").arg(script);
  qSlicerPythonManager * self = reinterpret_cast<qSlicerPythonManager*>(clientData);
  Q_ASSERT(self);
104
  //logger.setTrace();
105
106
  logger.trace(QString("Running broker observation script: %1").arg(script));
  self->executeString(pythonScript);
107
108
109
#else
  Q_UNUSED(script);
  Q_UNUSED(clientData);
110
#endif
111
112
}