ctkAbstractPythonManager.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QDir>
00023 #include <QDebug>
00024 
00025 // CTK includes
00026 #include "ctkAbstractPythonManager.h"
00027 
00028 // PythonQT includes
00029 #include <PythonQt.h>
00030 
00031 // STD includes
00032 #include <csignal>
00033 
00034 //-----------------------------------------------------------------------------
00035 ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
00036 {
00037 
00038 }
00039 
00040 //-----------------------------------------------------------------------------
00041 ctkAbstractPythonManager::~ctkAbstractPythonManager()
00042 {
00043   PythonQt::cleanup();
00044 }
00045 
00046 //-----------------------------------------------------------------------------
00047 PythonQtObjectPtr ctkAbstractPythonManager::mainContext()
00048 {
00049   if (!PythonQt::self())
00050     {
00051     this->initPythonQt();
00052     }
00053   if (PythonQt::self())
00054     {
00055     return PythonQt::self()->getMainModule();
00056     }
00057   return PythonQtObjectPtr();
00058 }
00059 
00060 //-----------------------------------------------------------------------------
00061 void ctkAbstractPythonManager::initPythonQt()
00062 {
00063   PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
00064 
00065   // Python maps SIGINT (control-c) to its own handler.  We will remap it
00066   // to the default so that control-c works.
00067   #ifdef SIGINT
00068   signal(SIGINT, SIG_DFL);
00069   #endif
00070 
00071   PythonQtObjectPtr _mainContext = PythonQt::self()->getMainModule();
00072 
00073   this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
00074                 SLOT(printStdout(const QString&)));
00075   this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
00076                 SLOT(printStderr(const QString&)));
00077 
00078   QStringList initCode;
00079   initCode << "import sys";
00080   foreach (QString path, this->pythonPaths())
00081     {
00082     initCode << QString("sys.path.append('%1')").arg(QDir::fromNativeSeparators(path));
00083     }
00084 
00085   _mainContext.evalScript(initCode.join("\n"));
00086 
00087   this->preInitialization();
00088 
00089   emit this->pythonInitialized();
00090 }
00091 
00092 //-----------------------------------------------------------------------------
00093 QStringList ctkAbstractPythonManager::pythonPaths()
00094 {
00095   return QStringList();
00096 }
00097 
00098 //-----------------------------------------------------------------------------
00099 void ctkAbstractPythonManager::preInitialization()
00100 {
00101 }
00102 
00103 //-----------------------------------------------------------------------------
00104 void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
00105 {
00106   PythonQt::self()->addDecorators(decorator);
00107 }
00108 
00109 //-----------------------------------------------------------------------------
00110 void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
00111 {
00112   PythonQt::self()->registerClass(metaobject);
00113 }
00114 
00115 //-----------------------------------------------------------------------------
00116 void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
00117 {
00118   PythonQt::self()->registerCPPClass(name);
00119 }
00120 
00121 //-----------------------------------------------------------------------------
00122 QVariant ctkAbstractPythonManager::executeString(const QString& code)
00123 {
00124   QVariant ret;
00125   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
00126   if (main)
00127     {
00128     ret = main.evalScript(code, Py_single_input);
00129     }
00130   return ret;
00131 }
00132 
00133 //-----------------------------------------------------------------------------
00134 void ctkAbstractPythonManager::executeFile(const QString& filename)
00135 {
00136   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
00137   if (main)
00138     {
00139     main.evalFile(filename);
00140     }
00141 }
00142 
00143 //-----------------------------------------------------------------------------
00144 void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
00145 {
00146   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
00147   if (main && obj)
00148     {
00149     main.addObject(name, obj);
00150     }
00151 }
00152 
00153 //-----------------------------------------------------------------------------
00154 QVariant ctkAbstractPythonManager::getVariable(const QString& name)
00155 {
00156   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
00157   if (main)
00158     {
00159     return PythonQt::self()->getVariable(main, name);
00160     }
00161   return QVariant();
00162 }
00163 
00164 //-----------------------------------------------------------------------------
00165 void ctkAbstractPythonManager::printStdout(const QString& text)
00166 {
00167   std::cout << qPrintable(text);
00168 }
00169 
00170 //-----------------------------------------------------------------------------
00171 void ctkAbstractPythonManager::printStderr(const QString& text)
00172 {
00173   std::cout << qPrintable(text);
00174 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines