ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PythonRepl.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 <QMainWindow>
11 
12 #include <string>
13 
14 #include "PythonInterpreter.h"
15 
16 class PythonInterpreter;
17 class Ui_PythonREPL;
18 class QPlainTextEdit;
19 
20 class PythonRepl;
21 
23 class History final
24 {
25  public:
26  explicit History() = default;
27 
28  void add(QString &&cmd);
29 
30  size_t size() const;
31 
32  bool empty() const;
33 
34  const QString &older();
35 
36  const QString &newer();
37 
38  private:
39  QVector<QString> m_commands;
40  QVector<QString>::const_reverse_iterator m_current;
41 };
42 
44 class KeyPressEater final : public QObject
45 {
46  Q_OBJECT
47  public:
48  explicit KeyPressEater(PythonRepl *repl, QObject *parent = nullptr);
49 
50  protected:
51  bool eventFilter(QObject *obj, QEvent *event) override;
52 
53  private:
54  PythonRepl *m_repl{nullptr};
55 };
56 
58 class PythonRepl final : public QMainWindow
59 {
60  friend KeyPressEater;
61  Q_OBJECT
62 
63  public:
64  explicit PythonRepl(PythonInterpreter *interpreter, QMainWindow *parent = nullptr);
65 
66  void executeCode(const QString &pythonCode);
67 
68  PythonRepl(const PythonRepl &) = delete;
69  PythonRepl(PythonRepl &&) = delete;
70  PythonRepl &operator=(const PythonRepl &) = delete;
71  PythonRepl &operator=(PythonRepl &&) = delete;
72  ~PythonRepl() noexcept override;
73 
74  protected:
75  QPlainTextEdit *codeEdit();
76  QListWidget *outputDisplay();
77 
78  void reset();
79  void importNeededPackages();
80  void setupUI();
81 
82  private:
83  History m_history{};
84  std::string m_buf;
85  Ui_PythonREPL *m_ui{nullptr};
86  PythonInterpreter *m_interpreter{nullptr};
88 };
KeyPressEater::KeyPressEater
KeyPressEater(PythonRepl *repl, QObject *parent=nullptr)
Definition: PythonRepl.cpp:135
PythonRepl::~PythonRepl
~PythonRepl() noexcept override
Definition: PythonRepl.cpp:146
QtCompat.h
PythonInterpreter::executeStatementWithState
void executeStatementWithState(const std::string &code, QListWidget *output, PythonInterpreter::State &state)
Definition: PythonInterpreter.cpp:137
QTCOMPAT_FONTMETRICS_WIDTH
#define QTCOMPAT_FONTMETRICS_WIDTH(fm, text)
Definition: QtCompat.h:339
PythonRepl::setupUI
void setupUI()
Definition: PythonRepl.cpp:161
History::size
size_t size() const
Definition: PythonRepl.cpp:268
qtCompatSetTabStopWidth
void qtCompatSetTabStopWidth(QPlainTextEdit *edit, int width)
Definition: QtCompat.h:1010
PythonRepl::codeEdit
QPlainTextEdit * codeEdit()
Definition: PythonRepl.cpp:151
PythonRepl::outputDisplay
QListWidget * outputDisplay()
Definition: PythonRepl.cpp:156
width
int width
Definition: FileIOFactory.cpp:142
KeyPressEater
Class used by the REPL to handle key presses.
Definition: PythonRepl.h:44
height
int height
Definition: FileIOFactory.cpp:143
PythonInterpreter
Definition: PythonInterpreter.h:32
PythonHighlighter
Definition: PythonHighlighter.h:19
event
MouseEvent event
Definition: BitmapWindowSystem.cpp:61
continuationDots
const static QString continuationDots(QStringLiteral("... "))
History::History
History()=default
PythonRepl::operator=
PythonRepl & operator=(const PythonRepl &)=delete
PythonRepl::importNeededPackages
void importNeededPackages()
Definition: PythonRepl.cpp:190
History::add
void add(QString &&cmd)
Definition: PythonRepl.cpp:227
History
Simple history system for the REPL.
Definition: PythonRepl.h:23
PythonRepl
Homemade REPL (Read Print Eval Loop)
Definition: PythonRepl.h:58
PythonHighlighter.h
PythonRepl::reset
void reset()
Definition: PythonRepl.cpp:183
pybind11
Definition: ccGuiPythonInstance.h:15
History::empty
bool empty() const
Definition: PythonRepl.cpp:263
PythonInterpreter::State
Variable state.
Definition: PythonInterpreter.h:38
KeyPressEater::eventFilter
bool eventFilter(QObject *obj, QEvent *event) override
Definition: PythonRepl.cpp:33
PythonInterpreter.h
PythonRepl::PythonRepl
PythonRepl(PythonInterpreter *interpreter, QMainWindow *parent=nullptr)
Definition: PythonRepl.cpp:137
PythonRepl.h
PythonRepl::executeCode
void executeCode(const QString &pythonCode)
Definition: PythonRepl.cpp:197
replArrows
const static QString replArrows(QStringLiteral(">>> "))
History::newer
const QString & newer()
Definition: PythonRepl.cpp:244
PythonStdErrOutRedirect.h
History::older
const QString & older()
Definition: PythonRepl.cpp:233
PythonInterpreter::isExecuting
bool isExecuting() const
Definition: PythonInterpreter.cpp:315