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:
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 };
void executeCode(const std::string &code, QListWidget *output)
void executeFunction(const pybind11::object &function)
void executeCodeWithState(const std::string &code, QListWidget *output, PythonInterpreter::State &state)
const PythonConfigPaths & config() const
void executionFinished()
PythonInterpreter(QObject *parent=nullptr)
static bool IsInitialized()
bool executeFile(const std::string &filePath)
Execution functions (and slots)
void executeStatementWithState(const std::string &code, QListWidget *output, PythonInterpreter::State &state)
void initialize(const PythonConfig &config)