HAL
python_console.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
29 
30 #include <QTextEdit>
31 #include <memory>
32 
33 class QLabel;
34 class QPushButton;
35 class QTimer;
36 
37 namespace hal
38 {
39  class PythonConsoleHistory;
40 
42  {
43  Q_OBJECT
44  int mCount;
45  QLabel* mLabel;
46  QPushButton* mAbortButton;
47  QTimer* mTimer;
48 
49  private Q_SLOTS:
50  void handleTimeout();
51  void handleAbortButton();
52  public:
54  void start();
55  void stop();
56  };
57 
65  {
66  Q_OBJECT
67 
68  public:
72  PythonConsole(QWidget* parent = nullptr);
74 
76 
77  protected:
90  void keyPressEvent(QKeyEvent* e) override;
91 
97  void mousePressEvent(QMouseEvent* event) override;
98 
99  public Q_SLOTS:
100  void handleThreadFinished();
101 
102  private Q_SLOTS:
103  void handleStyleChanged(int istyle);
104 
105  Q_SIGNALS:
106  void inputReceived(QString input);
107 
108  public:
114  virtual void handleStdout(const QString& output) override;
115 
121  virtual void handleError(const QString& output) override;
122 
126  virtual void clear() override;
127 
131  void displayPrompt();
132 
138  void interpretCommand();
139 
146 
152  void replaceCurrentCommand(const QString& new_command);
153 
159  void appendToCurrentCommand(const QString& new_command);
160 
166  bool selectionEditable();
167 
171  void handleUpKeyPressed();
172 
176  void handleDownKeyPressed();
177 
181  void handleTabKeyPressed();
182 
189  void insertAtEnd(const QString& text, QColor textColor);
190 
196  void setInputMode(bool state);
197 
199 
200  private:
201  QString mStandardPrompt;
202  QString mCompoundPrompt;
203  QString mInputPrompt;
204 
205  int mPromptBlockNumber;
206  int mPromptLength;
207  int mPromptEndPosition;
208  int mCompoundPromptEndPosition;
209 
210  PromptType mPromptType;
211  bool mInCompletion;
212 
213  QString mCurrentCompoundInput;
214  QString mCurrentInput;
215 
216  int mCurrentHistoryIndex;
217  int mCurrentCompleterIndex;
218 
219  std::shared_ptr<PythonConsoleHistory> mHistory;
220  PythonConsoleAbortThread* mAbortThreadWidget;
221 
222  bool isCompound() const { return mPromptType == Compound; }
223  bool isInputMode() const { return mPromptType == Input; }
224 
225  void keyPressEventInputMode(QKeyEvent* e);
226  };
227 }
PythonConsoleAbortThread(QWidget *parent=nullptr)
An interactable python console.
void mousePressEvent(QMouseEvent *event) override
PythonConsole(QWidget *parent=nullptr)
void keyPressEvent(QKeyEvent *e) override
void setInputMode(bool state)
void inputReceived(QString input)
virtual void clear() override
virtual void handleStdout(const QString &output) override
PythonConsoleAbortThread * abortThreadWidget()
void replaceCurrentCommand(const QString &new_command)
void insertAtEnd(const QString &text, QColor textColor)
virtual void handleError(const QString &output) override
void appendToCurrentCommand(const QString &new_command)
Interface for handling python outputs.
virtual bool event(QEvent *event) override
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
QColor textColor() const const