ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PythonInterpreter.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include "PythonConfig.h"
11 #include <QObject>
12 
13 #include <memory>
14 
15 #undef slots
16 #include <pybind11/eval.h>
17 #include <pybind11/pybind11.h>
18 
19 class QListWidget;
20 class PythonConfig;
21 
32 class PythonInterpreter final : public QObject
33 {
34  Q_OBJECT
35 
36  public:
38  struct State
39  {
40  State();
41  pybind11::dict globals;
42  pybind11::object locals;
43  };
44 
45  public:
46  explicit PythonInterpreter(QObject *parent = nullptr);
47  bool isExecuting() const;
48  void initialize(const PythonConfig &config);
49  void finalize();
50  static bool IsInitialized();
51  const PythonConfigPaths &config() const;
52 
54  public Q_SLOTS:
55  bool executeFile(const std::string &filePath);
56  void executeCode(const std::string &code, QListWidget *output);
57  void executeCodeWithState(const std::string &code,
58  QListWidget *output,
60  void executeStatementWithState(const std::string &code,
61  QListWidget *output,
63  void executeFunction(const pybind11::object &function);
64 
65  Q_SIGNALS:
66  void executionStarted();
67  void executionFinished();
68 
69  private:
70  template <pybind11::eval_mode mode>
71  void executeCodeString(const std::string &code,
72  QListWidget *output,
74 
75  private:
76  bool m_isExecuting{false};
77 
78  PythonConfigPaths m_config;
79 
80 #ifdef Q_OS_UNIX
81  void *m_libPythonHandle{nullptr};
82 #endif
83 };
PythonConfigPaths::pythonPath
const wchar_t * pythonPath() const
Returns the pythonPath.
Definition: PythonConfig.cpp:158
QtCompat.h
PythonInterpreter::executionStarted
void executionStarted()
PythonInterpreter::executeStatementWithState
void executeStatementWithState(const std::string &code, QListWidget *output, PythonInterpreter::State &state)
Definition: PythonInterpreter.cpp:137
PythonVersion
constexpr Version PythonVersion(PY_MAJOR_VERSION, PY_MINOR_VERSION, PY_MICRO_VERSION)
Python Version the plugin was compiled against.
CVLog.h
PythonInterpreter::finalize
void finalize()
Definition: PythonInterpreter.cpp:298
CVLog::Error
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
PythonConfigPaths
Definition: PythonConfig.h:183
PythonInterpreter
Definition: PythonInterpreter.h:32
PyStdErrOutStreamRedirect
Definition: PythonStdErrOutRedirect.h:28
ccConsoleOutput
Redirects messages to ACloudViewer's console output.
Definition: Consoles.h:76
e
const double * e
Definition: essential_matrix_poly.h:10
PythonConfigPaths::pythonHome
const wchar_t * pythonHome() const
Returns the pythonHome.
Definition: PythonConfig.cpp:153
CreateGlobals
static py::dict CreateGlobals()
Definition: PythonInterpreter.cpp:37
PythonInterpreter::initialize
void initialize(const PythonConfig &config)
Definition: PythonInterpreter.cpp:173
PythonInterpreter::executeFunction
void executeFunction(const pybind11::object &function)
Definition: PythonInterpreter.cpp:150
PythonConfig.h
PythonConfig
Definition: PythonConfig.h:71
PythonInterpreter::executeCodeWithState
void executeCodeWithState(const std::string &code, QListWidget *output, PythonInterpreter::State &state)
Definition: PythonInterpreter.cpp:130
PythonInterpreter::State::State
State()
Definition: PythonInterpreter.cpp:46
CVTools::FromQString
static std::string FromQString(const QString &qs)
Definition: CVTools.cpp:100
Utilities.h
ecvColor::orange
constexpr Rgb orange(MAX, MAX/2, 0)
CVTools.h
plgPrint
PluginLogger< CVLog::LOG_STANDARD > plgPrint
Definition: Utilities.h:100
PythonInterpreter::executeFile
bool executeFile(const std::string &filePath)
Execution functions (and slots)
Definition: PythonInterpreter.cpp:52
PythonInterpreter::PythonInterpreter
PythonInterpreter(QObject *parent=nullptr)
Definition: PythonInterpreter.cpp:50
CVLog::Warning
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
PythonInterpreter::State::locals
pybind11::object locals
Definition: PythonInterpreter.h:42
plgWarning
PluginLogger< CVLog::LOG_WARNING > plgWarning
Definition: Utilities.h:102
PythonInterpreter::State::globals
pybind11::dict globals
Definition: PythonInterpreter.h:41
pybind11
Definition: ccGuiPythonInstance.h:15
ecvColor::red
constexpr Rgb red(MAX, 0, 0)
PythonInterpreter::IsInitialized
static bool IsInitialized()
Definition: PythonInterpreter.cpp:293
Version::versionMajor
uint16_t versionMajor
Definition: PythonConfig.h:58
PythonInterpreter::State
Variable state.
Definition: PythonInterpreter.h:38
PythonInterpreter.h
PythonInterpreter::executeCode
void executeCode(const std::string &code, QListWidget *output)
Definition: PythonInterpreter.cpp:144
PythonConfig::Type::System
@ System
ListWidgetConsole
Writes messages to the QListWidget given.
Definition: Consoles.h:120
PythonInterpreter::config
const PythonConfigPaths & config() const
Definition: PythonInterpreter.cpp:320
PythonInterpreter::executionFinished
void executionFinished()
Consoles.h
PythonStdErrOutRedirect.h
Version::versionMinor
uint16_t versionMinor
Definition: PythonConfig.h:59
PythonInterpreter::isExecuting
bool isExecuting() const
Definition: PythonInterpreter.cpp:315