Debian Codegraph @

src/Gui/PythonConsole.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #ifndef GUI_PYTHONCONSOLE_H
24 #define GUI_PYTHONCONSOLE_H
25
26 #include <Python.h>
27 #include <QTimer>
28 #include "PythonEditor.h"
29
30
31 class QPlainTextEdit;
32 class QPushButton;
33
34 namespace Gui
35 {
36
37 /**
38 * This class implements an interactive Python interpreter.
39 * @author Werner Mayer
40 */
41 class GuiExport InteractiveInterpreter
42 {
43 public:
44 InteractiveInterpreter();
45 ~InteractiveInterpreter();
46
47 bool isOccupied() const;
48 bool interrupt() const;
49
50 bool push(const char*);
51 int compileCommand(const char*) const;
52 bool hasPendingInput() const;
53 void setBuffer(const QStringList&);
54 QStringList getBuffer() const;
55 void clearBuffer();
56
57 private:
58 bool runSource(const char*) const;
59 PyObject* compile(const char*) const;
60 void runCode(PyCodeObject*) const;
61 void setPrompt();
62
63 private:
64 struct InteractiveInterpreterP* d;
65 };
66
67 /**
68 * This class implements the history for the Python console.
69 * @author Werner Mayer
70 */
71 class GuiExport ConsoleHistory
72 {
73 public:
74 ConsoleHistory();
75 ~ConsoleHistory();
76
77 void first();
78 bool more();
79 bool next();
80 bool prev(const QString& prefix = QString());
81 bool isEmpty() const;
82 const QString& value() const;
83 void append(const QString& inputLine);
84 const QStringList& values() const;
85 void restart();
86 void markScratch();
87 void doScratch();
88
89 private:
90 QStringList _history;
91 QStringList::ConstIterator _it;
92 int _scratchBegin;
93 QString _prefix;
94 };
95
96 /**
97 * Python text console with syntax highlighting.
98 * @author Werner Mayer
99 */
100 class PythonConsoleHighlighter;
101 class GuiExport PythonConsole: public PythonTextEditor
102 {
103 Q_OBJECT
104
105 public:
106 enum Prompt
107 {
108 Complete = 0,
109 Incomplete = 1,
110 Flush = 2,
111 Special = 3
112 };
113
114 explicit PythonConsole(QWidget* parent = nullptr);
115 ~PythonConsole() override;
116
117 void OnChange(Base::Subject<const char*>& rCaller, const char* rcReason) override;
118 void printStatement(const QString& cmd);
119 QString readline();
120 int getInputStringPosition() override;
121 QString getInputString() override;
122
123 public Q_SLOTS:
124 void onSaveHistoryAs();
125 void onInsertFileName();
126 void onCopyHistory();
127 void onCopyCommand();
128 void onClearConsole();
129 void onFlush();
130
131 private Q_SLOTS:
132 void visibilityChanged(bool visible);
133
134 protected:
135 void keyPressEvent(QKeyEvent* e) override;
136 void showEvent(QShowEvent* e) override;
137 void dropEvent(QDropEvent* e) override;
138 void dragEnterEvent(QDragEnterEvent* e) override;
139 void dragMoveEvent(QDragMoveEvent* e) override;
140 void changeEvent(QEvent* e) override;
141 void mouseReleaseEvent(QMouseEvent* e) override;
142
143 void overrideCursor(const QString& txt);
144
145 /** Pops up the context menu with some extensions */
146 void contextMenuEvent(QContextMenuEvent* e) override;
147 bool canInsertFromMimeData(const QMimeData* source) const override;
148 QMimeData* createMimeDataFromSelection() const override;
149 void insertFromMimeData(const QMimeData* source) override;
150 QTextCursor inputBegin() const;
151
152 private:
153 void runSource(const QString&);
154 bool isComment(const QString&) const;
155 void printPrompt(Prompt);
156 void insertPythonOutput(const QString&);
157 void insertPythonError(const QString&);
158 void runSourceFromMimeData(const QString&);
159 void appendOutput(const QString&, int);
160 void loadHistory() const;
161 void saveHistory() const;
162 void flushOutput();
163
164 Q_SIGNALS:
165 void pendingSource();
166
167 private:
168 struct PythonConsoleP* d;
169
170 PythonConsoleHighlighter* pythonSyntax {nullptr};
171 QString* _sourceDrain {nullptr};
172 QString _historyFile;
173 QTimer* flusher {nullptr};
174
175 friend class PythonStdout;
176 friend class PythonStderr;
177 };
178
179 /**
180 * Syntax highlighter for Python console.
181 * @author Werner Mayer
182 */
183 class GuiExport PythonConsoleHighlighter: public PythonSyntaxHighlighter
184 {
185 public:
186 explicit PythonConsoleHighlighter(QObject* parent);
187 ~PythonConsoleHighlighter() override;
188
189 void highlightBlock(const QString& text) override;
190
191 protected:
192 void colorChanged(const QString& type, const QColor& col) override;
193 };
194
195 } // namespace Gui
196
197 #endif // GUI_PYTHONCONSOLE_H