소스 검색

Review initialization process of PythonManager

Initialization is split into two steps:
 1) preInitialization and 2) executeInitializationScripts

The signals pythonPreInitialized and pythonInitialized are respectively
sent after the first and second one.

Function setInitializationFunction allows also to set a function that
should be initialized in between the two steps described above.
Jean-Christophe Fillion-Robin 14 년 전
부모
커밋
4907ae9b7f

+ 6 - 2
Applications/ctkSimplePythonShell/ctkSimplePythonManager.cpp.in

@@ -98,13 +98,17 @@ void ctkSimplePythonManager::preInitialization()
   
   // Add object to python interpreter context
   this->addObjectToPythonMain("_ctkSimplePythonShellInstance", qApp);
+}
 
+//-----------------------------------------------------------------------------
+void ctkSimplePythonManager::executeInitializationScripts()
+{
   QString self_dir = QFileInfo(qApp->applicationFilePath()).absolutePath();
 
   QString initFile = self_dir;
-#if defined(CMAKE_INTDIR)
+  #if defined(CMAKE_INTDIR)
   initFile.append("/..");
-#endif
+  #endif
   initFile.append("/Python/ctkSimplePythonShell.py");
 
   Q_ASSERT(QFile::exists(initFile));

+ 1 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonManager.h

@@ -19,6 +19,7 @@ protected:
 
   virtual QStringList pythonPaths();
   virtual void preInitialization();
+  virtual void executeInitializationScripts();
 
 };
 

+ 18 - 1
Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

@@ -82,7 +82,7 @@ void PythonQt_init_QtXmlPatterns(PyObject*);
 //-----------------------------------------------------------------------------
 ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
 {
-
+  this->InitFunction = 0;
 }
 
 //-----------------------------------------------------------------------------
@@ -175,7 +175,13 @@ void ctkAbstractPythonManager::initPythonQt()
   _mainContext.evalScript(initCode.join("\n"));
 
   this->preInitialization();
+  if (this->InitFunction)
+    {
+    (*this->InitFunction)();
+    }
+  emit this->pythonPreInitialized();
 
+  this->executeInitializationScripts();
   emit this->pythonInitialized();
 }
 
@@ -191,6 +197,11 @@ void ctkAbstractPythonManager::preInitialization()
 }
 
 //-----------------------------------------------------------------------------
+void ctkAbstractPythonManager::executeInitializationScripts()
+{
+}
+
+//-----------------------------------------------------------------------------
 void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
 {
   PythonQt::self()->addDecorators(decorator);
@@ -231,6 +242,12 @@ void ctkAbstractPythonManager::executeFile(const QString& filename)
 }
 
 //-----------------------------------------------------------------------------
+void ctkAbstractPythonManager::setInitializationFunction(void (*initFunction)())
+{
+  this->InitFunction = initFunction;
+}
+
+//-----------------------------------------------------------------------------
 void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
 {
   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();

+ 19 - 5
Libs/Scripting/Python/Core/ctkAbstractPythonManager.h

@@ -46,25 +46,31 @@ public:
   void registerClassForPythonQt(const QMetaObject* metaobject);
   void registerCPPClassForPythonQt(const char* name);
 
-  ///
   /// Execute a python of python code (can be multiple lines separated with newline)
   /// and return the result as a QVariant.
   QVariant executeString(const QString& code);
 
-  ///
   /// Gets the value of the variable looking in the __main__ module.
   /// If the variable is not found returns a default initialized QVariant.
   QVariant getVariable(const QString& varName);
 
-  ///
   /// Execute a python script with the given filename.
   void executeFile(const QString& filename);
 
+  /// Set function that is initialized after preInitialization and before executeInitializationScripts
+  /// \sa preInitialization executeInitializationScripts
+  void setInitializationFunction(void (*initFunction)());
+
 signals:
 
-  ///
-  /// This signal is emitted after python is initialized.  Observers can listen
+  /// This signal is emitted after python is pre-initialized. Observers can listen
   /// for this signal to handle additional initialization steps.
+  /// \sa preInitialization
+  void pythonPreInitialized();
+
+  /// This signal is emitted after python is initialized and scripts are executed
+  /// \sa preInitialization
+  /// \sa executeScripts
   void pythonInitialized();
 
 protected slots:
@@ -76,7 +82,15 @@ protected:
   void initPythonQt();
 
   virtual QStringList     pythonPaths();
+
+  /// Overload this function to load Decorator and pythonQt wrapper at initialization time
   virtual void            preInitialization();
 
+  /// Overload this function to execute script at initialization time
+  virtual void            executeInitializationScripts();
+
+private:
+  void (*InitFunction)();
+
 };
 #endif