ctkAbstractPythonManager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // CTK includes
  21. #include "ctkScriptingPythonCoreExport.h"
  22. class ctkAbstractPythonManagerPrivate;
  23. class PythonQtForeignWrapperFactory;
  24. class PythonQtObjectPtr;
  25. /// \ingroup Scripting_Python_Core
  26. class CTK_SCRIPTING_PYTHON_CORE_EXPORT ctkAbstractPythonManager : public QObject
  27. {
  28. Q_OBJECT
  29. public:
  30. typedef QObject Superclass;
  31. ctkAbstractPythonManager(QObject* _parent=NULL);
  32. virtual ~ctkAbstractPythonManager();
  33. /// Calling this function after mainContext() has been called at least once is a no-op.
  34. /// If not overridden calling this function, the default initialization flags are
  35. /// PythonQt::IgnoreSiteModule and PythonQt::RedirectStdOut.
  36. /// \sa PythonQt::InitFlags
  37. void setInitializationFlags(int flags);
  38. /// \sa setInitializationFlags
  39. int initializationFlags()const;
  40. /// Initialize python context considering the initializationFlags.
  41. /// Return \a True if python has been successfully initialized.
  42. /// \sa setInitializationFlags, mainContext, isPythonInitialized
  43. /// \sa preInitialization, executeInitializationScripts, pythonPreInitialized, pythonInitialized
  44. bool initialize();
  45. /// Return a reference to the python main context.
  46. /// Calling this function implicitly call initialize() if it hasn't been done.
  47. PythonQtObjectPtr mainContext();
  48. void addObjectToPythonMain(const QString& name, QObject* obj);
  49. void addWrapperFactory(PythonQtForeignWrapperFactory* factory);
  50. void registerPythonQtDecorator(QObject* decorator);
  51. void registerClassForPythonQt(const QMetaObject* metaobject);
  52. void registerCPPClassForPythonQt(const char* name);
  53. /// This enum maps to Py_eval_input, Py_file_input and Py_single_input
  54. /// \see http://docs.python.org/c-api/veryhigh.html#Py_eval_input
  55. /// \see http://docs.python.org/c-api/veryhigh.html#Py_file_input
  56. /// \see http://docs.python.org/c-api/veryhigh.html#Py_single_input
  57. enum ExecuteStringMode
  58. {
  59. EvalInput = 0,
  60. FileInput,
  61. SingleInput
  62. };
  63. /// Execute a python of python code (can be multiple lines separated with newline)
  64. /// and return the result as a QVariant.
  65. QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
  66. /// Gets the value of the variable looking in the __main__ module.
  67. /// If the variable is not found returns a default initialized QVariant.
  68. QVariant getVariable(const QString& varName);
  69. /// Execute a python script with the given filename.
  70. void executeFile(const QString& filename);
  71. /// Set function that is initialized after preInitialization and before executeInitializationScripts
  72. /// \sa preInitialization executeInitializationScripts
  73. void setInitializationFunction(void (*initFunction)());
  74. /// Given a python variable name, lookup its attributes and return them in a string list.
  75. /// By default the attributes are looked up from \c __main__.
  76. /// If the argument \c appendParenthesis is set to True, "()" will be appended to attributes
  77. /// being Python callable.
  78. QStringList pythonAttributes(const QString& pythonVariableName,
  79. const QString& module = QLatin1String("__main__"),
  80. bool appendParenthesis = false) const;
  81. /// Returns True if python is initialized
  82. /// \sa pythonInitialized
  83. bool isPythonInitialized()const;
  84. /// Returns True if a python error occured.
  85. /// \sa PythonQt::errorOccured()
  86. bool pythonErrorOccured()const;
  87. Q_SIGNALS:
  88. /// This signal is emitted after python is pre-initialized. Observers can listen
  89. /// for this signal to handle additional initialization steps.
  90. /// \sa preInitialization
  91. void pythonPreInitialized();
  92. /// This signal is emitted after python is initialized and scripts are executed
  93. /// \sa preInitialization
  94. /// \sa executeScripts
  95. void pythonInitialized();
  96. protected Q_SLOTS:
  97. void printStderr(const QString&);
  98. void printStdout(const QString&);
  99. protected:
  100. void initPythonQt(int flags);
  101. virtual QStringList pythonPaths();
  102. /// Overload this function to load Decorator and pythonQt wrapper at initialization time
  103. virtual void preInitialization();
  104. /// Overload this function to execute script at initialization time
  105. virtual void executeInitializationScripts();
  106. protected:
  107. QScopedPointer<ctkAbstractPythonManagerPrivate> d_ptr;
  108. private:
  109. Q_DECLARE_PRIVATE(ctkAbstractPythonManager);
  110. Q_DISABLE_COPY(ctkAbstractPythonManager);
  111. };
  112. #endif