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