forked from ZedThree/pyxpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_widget.py
More file actions
103 lines (69 loc) · 3.04 KB
/
Copy pathconsole_widget.py
File metadata and controls
103 lines (69 loc) · 3.04 KB
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
"""
A Python console custom widget for Qt Designer.
Origin: Merging of
https://qt.gitorious.org/pyside/pyside-examples/source/d4e4c7fdf71ab52083e49ffdea1b7daeff6c8d8d:examples/designer/plugins/widgets/pythonconsolewidget.py
http://www.nullege.com/codes/show/src@e@r@err-1.7.1@errbot@backends@graphic.py/32/PySide.QtGui.QPlainTextEdit
Modified 2014 Ben Dudson <benjamin.dudson@york.ac.uk>
* Returns the command string without executing it
* PySide rather than Qt calls
Copyright (C) 2006 David Boddie <david@boddie.org.uk>
Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from PySide import QtCore, QtGui
class ConsoleWidget(QtGui.QLineEdit):
"""ConsoleWidget
Provides a custom widget to accept Python expressions and emit output
to other components via a custom signal.
"""
# This signal is emitted when a command is entered
commandEntered = QtCore.Signal(str)
def __init__(self, parent=None):
QtGui.QLineEdit.__init__(self, parent)
self.history = []
self.current = -1
self.returnPressed.connect(self.execute)
def keyReleaseEvent(self, event):
if event.type() == QtCore.QEvent.KeyRelease:
if event.key() == QtCore.Qt.Key_Up:
current = max(0, self.current - 1)
if 0 <= current < len(self.history):
self.setText(self.history[current])
self.current = current
event.accept()
elif event.key() == QtCore.Qt.Key_Down:
current = min(len(self.history), self.current + 1)
if 0 <= current < len(self.history):
self.setText(self.history[current])
else:
self.clear()
self.current = current
event.accept()
def execute(self):
expression = self.text()
# Clear the line edit, append the expression to the
# history, and update the current command index.
self.clear()
self.history.append(expression)
self.current = len(self.history)
# Emit the text
self.commandEntered.emit(expression)
if __name__ == "__main__":
import sys
def runCommand(expr):
print(expr)
app = QtGui.QApplication(sys.argv)
widget = ConsoleWidget()
widget.commandEntered.connect(runCommand)
widget.show()
sys.exit(app.exec_())