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;
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 };
MouseEvent event
Simple history system for the REPL.
Definition: PythonRepl.h:24
bool empty() const
Definition: PythonRepl.cpp:263
const QString & newer()
Definition: PythonRepl.cpp:244
const QString & older()
Definition: PythonRepl.cpp:233
size_t size() const
Definition: PythonRepl.cpp:268
History()=default
void add(QString &&cmd)
Definition: PythonRepl.cpp:227
Class used by the REPL to handle key presses.
Definition: PythonRepl.h:45
KeyPressEater(PythonRepl *repl, QObject *parent=nullptr)
Definition: PythonRepl.cpp:135
bool eventFilter(QObject *obj, QEvent *event) override
Definition: PythonRepl.cpp:33
Homemade REPL (Read Print Eval Loop)
Definition: PythonRepl.h:59
PythonRepl & operator=(PythonRepl &&)=delete
PythonRepl & operator=(const PythonRepl &)=delete
PythonRepl(PythonInterpreter *interpreter, QMainWindow *parent=nullptr)
Definition: PythonRepl.cpp:137
void importNeededPackages()
Definition: PythonRepl.cpp:190
PythonRepl(PythonRepl &&)=delete
QListWidget * outputDisplay()
Definition: PythonRepl.cpp:156
void setupUI()
Definition: PythonRepl.cpp:161
QPlainTextEdit * codeEdit()
Definition: PythonRepl.cpp:151
PythonRepl(const PythonRepl &)=delete
void reset()
Definition: PythonRepl.cpp:183
~PythonRepl() noexcept override
Definition: PythonRepl.cpp:146
void executeCode(const QString &pythonCode)
Definition: PythonRepl.cpp:197