ctkAbstractPythonManager.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkAbstractPythonManager_h
  15. #define __ctkAbstractPythonManager_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QList>
  19. #include <QStringList>
  20. #include <QVariant>
  21. // PythonQt includes
  22. #include <PythonQtPythonInclude.h> // For PyObject
  23. // CTK includes
  24. #include "ctkScriptingPythonCoreExport.h"
  25. class ctkAbstractPythonManagerPrivate;
  26. class PythonQtForeignWrapperFactory;
  27. class PythonQtObjectPtr;
  28. /// \ingroup Scripting_Python_Core
  29. class CTK_SCRIPTING_PYTHON_CORE_EXPORT ctkAbstractPythonManager : public QObject
  30. {
  31. Q_OBJECT
  32. public:
  33. typedef QObject Superclass;
  34. ctkAbstractPythonManager(QObject* _parent=NULL);
  35. virtual ~ctkAbstractPythonManager();
  36. /// Calling this function after mainContext() has been called at least once is a no-op.
  37. /// If not overridden calling this function, the default initialization flags are
  38. /// PythonQt::IgnoreSiteModule and PythonQt::RedirectStdOut.
  39. /// \sa PythonQt::InitFlags
  40. void setInitializationFlags(int flags);
  41. /// \sa setInitializationFlags
  42. int initializationFlags()const;
  43. /// Initialize python context considering the initializationFlags.
  44. /// Return \a True if python has been successfully initialized.
  45. /// \sa setInitializationFlags, mainContext, isPythonInitialized
  46. /// \sa preInitialization, executeInitializationScripts, pythonPreInitialized, pythonInitialized
  47. bool initialize();
  48. /// Return a reference to the python main context.
  49. /// Calling this function implicitly call initialize() if it hasn't been done.
  50. PythonQtObjectPtr mainContext();
  51. void addObjectToPythonMain(const QString& name, QObject* obj);
  52. void addWrapperFactory(PythonQtForeignWrapperFactory* factory);
  53. void registerPythonQtDecorator(QObject* decorator);
  54. void registerClassForPythonQt(const QMetaObject* metaobject);
  55. void registerCPPClassForPythonQt(const char* name);
  56. /// \sa PythonQt::systemExitExceptionHandlerEnabled
  57. bool systemExitExceptionHandlerEnabled()const;
  58. /// \sa PythonQt::setSystemExitExceptionHandlerEnabled
  59. void setSystemExitExceptionHandlerEnabled(bool value);
  60. /// This enum maps to Py_eval_input, Py_file_input and Py_single_input
  61. /// \see http://docs.python.org/c-api/veryhigh.html#Py_eval_input
  62. /// \see http://docs.python.org/c-api/veryhigh.html#Py_file_input
  63. /// \see http://docs.python.org/c-api/veryhigh.html#Py_single_input
  64. enum ExecuteStringMode
  65. {
  66. EvalInput = 0,
  67. FileInput,
  68. SingleInput
  69. };
  70. /// Execute a python of python code (can be multiple lines separated with newline)
  71. /// and return the result as a QVariant.
  72. Q_INVOKABLE QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
  73. /// Gets the value of the variable looking in the __main__ module.
  74. /// If the variable is not found returns a default initialized QVariant.
  75. QVariant getVariable(const QString& varName);
  76. /// Execute a python script with the given filename.
  77. Q_INVOKABLE void executeFile(const QString& filename);
  78. /// Set function that is initialized after preInitialization and before executeInitializationScripts
  79. /// \sa preInitialization executeInitializationScripts
  80. void setInitializationFunction(void (*initFunction)());
  81. /// Given a python variable name, lookup its attributes and return them in a string list.
  82. /// By default the attributes are looked up from \c __main__.
  83. /// If the argument \c appendParenthesis is set to True, "()" will be appended to attributes
  84. /// being Python callable.
  85. QStringList pythonAttributes(const QString& pythonVariableName,
  86. const QString& module = QLatin1String("__main__"),
  87. bool appendParenthesis = false) const;
  88. /// Given a string of the form "<modulename1>[.<modulenameN>...]" containing modules, return the final module as a PyObject*
  89. static PyObject* pythonModule(const QString &module);
  90. /// Given a string of the form "<modulename1>[.<modulenameN>...].correspondingObject, return the final object as a PyObject*
  91. /// \sa pythonModule
  92. static PyObject* pythonObject(const QString& variableNameAndFunction);
  93. /// Returns True if python is initialized
  94. /// \sa pythonInitialized
  95. bool isPythonInitialized()const;
  96. /// Returns True if a python error occured.
  97. /// \sa PythonQt::hadError()
  98. bool pythonErrorOccured()const;
  99. /// Reset error flag
  100. /// \sa PythonQt::clearError()
  101. void resetErrorFlag();
  102. Q_SIGNALS:
  103. /// This signal is emitted after python is pre-initialized. Observers can listen
  104. /// for this signal to handle additional initialization steps.
  105. /// \sa preInitialization
  106. void pythonPreInitialized();
  107. /// This signal is emitted after python is initialized and scripts are executed
  108. /// \sa preInitialization
  109. /// \sa executeScripts
  110. void pythonInitialized();
  111. //! emitted when both custom SystemExit exception handler is enabled and a SystemExit
  112. //! exception is raised.
  113. //! \sa setSystemExitExceptionHandlerEnabled(bool), PythonQt::systemExitExceptionRaised(int)
  114. void systemExitExceptionRaised(int exitCode);
  115. protected Q_SLOTS:
  116. void printStderr(const QString&);
  117. void printStdout(const QString&);
  118. protected:
  119. void initPythonQt(int flags);
  120. virtual QStringList pythonPaths();
  121. /// Overload this function to load Decorator and pythonQt wrapper at initialization time
  122. virtual void preInitialization();
  123. /// Overload this function to execute script at initialization time
  124. virtual void executeInitializationScripts();
  125. protected:
  126. QScopedPointer<ctkAbstractPythonManagerPrivate> d_ptr;
  127. private:
  128. Q_DECLARE_PRIVATE(ctkAbstractPythonManager);
  129. Q_DISABLE_COPY(ctkAbstractPythonManager);
  130. };
  131. #endif