ctkAbstractPythonManager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 PythonQtObjectPtr;
  23. class CTK_SCRIPTING_PYTHON_CORE_EXPORT ctkAbstractPythonManager : public QObject
  24. {
  25. Q_OBJECT
  26. public:
  27. typedef QObject Superclass;
  28. ctkAbstractPythonManager(QObject* _parent=NULL);
  29. ~ctkAbstractPythonManager();
  30. PythonQtObjectPtr mainContext();
  31. void addObjectToPythonMain(const QString& name, QObject* obj);
  32. void registerPythonQtDecorator(QObject* decorator);
  33. void registerClassForPythonQt(const QMetaObject* metaobject);
  34. void registerCPPClassForPythonQt(const char* name);
  35. /// This enum maps to Py_eval_input, Py_file_input and Py_single_input
  36. /// \see http://docs.python.org/c-api/veryhigh.html#Py_eval_input
  37. /// \see http://docs.python.org/c-api/veryhigh.html#Py_file_input
  38. /// \see http://docs.python.org/c-api/veryhigh.html#Py_single_input
  39. enum ExecuteStringMode
  40. {
  41. EvalInput = 0,
  42. FileInput,
  43. SingleInput
  44. };
  45. /// Execute a python of python code (can be multiple lines separated with newline)
  46. /// and return the result as a QVariant.
  47. QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
  48. /// Gets the value of the variable looking in the __main__ module.
  49. /// If the variable is not found returns a default initialized QVariant.
  50. QVariant getVariable(const QString& varName);
  51. /// Execute a python script with the given filename.
  52. void executeFile(const QString& filename);
  53. /// Set function that is initialized after preInitialization and before executeInitializationScripts
  54. /// \sa preInitialization executeInitializationScripts
  55. void setInitializationFunction(void (*initFunction)());
  56. /// Given a python variable name, lookup its attributes and return them in a string list.
  57. /// By default the attributes are looked up from \c __main__.
  58. /// If the argument \c appendParenthesis is set to True, "()" will be appended to attributes
  59. /// being Python callable.
  60. QStringList pythonAttributes(const QString& pythonVariableName,
  61. const QString& module = QLatin1String("__main__"),
  62. bool appendParenthesis = false) const;
  63. /// Returns True if python is initialized
  64. /// \sa pythonInitialized
  65. bool isPythonInitialized()const;
  66. signals:
  67. /// This signal is emitted after python is pre-initialized. Observers can listen
  68. /// for this signal to handle additional initialization steps.
  69. /// \sa preInitialization
  70. void pythonPreInitialized();
  71. /// This signal is emitted after python is initialized and scripts are executed
  72. /// \sa preInitialization
  73. /// \sa executeScripts
  74. void pythonInitialized();
  75. protected slots:
  76. void printStderr(const QString&);
  77. void printStdout(const QString&);
  78. protected:
  79. void initPythonQt();
  80. virtual QStringList pythonPaths();
  81. /// Overload this function to load Decorator and pythonQt wrapper at initialization time
  82. virtual void preInitialization();
  83. /// Overload this function to execute script at initialization time
  84. virtual void executeInitializationScripts();
  85. private:
  86. void (*InitFunction)();
  87. };
  88. #endif