This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui_console.py
118 lines (105 loc) · 4.4 KB
/
gui_console.py
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
from PyQt4 import QtCore, QtGui
class ConsoleEdit(QtGui.QTextEdit):
def __init__(self):
QtGui.QTextEdit.__init__(self)
font = QtGui.QFont("courier new", 10)
font.setStyleHint(QtGui.QFont.TypeWriter)
self.document().setDefaultFont(font)
def displayPrompt(self):
"""
Displays the command prompt at the bottom of the buffer,
and moves cursor there.
"""
self.append(">>> ")
self.moveCursor(QtGui.QTextCursor.End)
self.promptBlockNumber = self.textCursor().blockNumber()
self.promptColumnNumber = self.textCursor().columnNumber()
self.promptPosition = self.textCursor().position()
def getCurrentCommand(self):
"""
Gets the current command written on the command prompt.
"""
# Select the command.
block = self.document().findBlockByNumber(self.promptBlockNumber)
return block.text().mid(self.promptColumnNumber)
def isInEditionZone(self):
return self.promptBlockNumber == self.textCursor().blockNumber() and \
self.promptColumnNumber <= self.textCursor().columnNumber()
def keyPressEvent(self, event):
# Run the command if enter was pressed.
if event.key() == QtCore.Qt.Key_Enter or event.key() == QtCore.Qt.Key_Return:
event.accept()
command = self.getCurrentCommand()
if len(command) > 0:
result = ""
try:
result = eval(str(command), globals(), locals())
#exec str(command) in globals(), locals()
except Exception as error:
result = str(error)
self.append(str(result));
self.moveCursor(QtGui.QTextCursor.End)
self.displayPrompt()
return
if event.key() == QtCore.Qt.Key_Backspace:
event.accept()
if not self.isInEditionZone():
self.moveCursor(QtGui.QTextCursor.End)
# First backspace just moves the cursor.
return
# If there is something to be deleted, delete it.
if self.promptColumnNumber < self.textCursor().columnNumber():
self.textCursor().deletePreviousChar()
return
if event.key() == QtCore.Qt.Key_Left:
event.accept()
if not self.isInEditionZone():
self.moveCursor(QtGui.QTextCursor.End)
# First backspace just moves the cursor.
return
if self.promptColumnNumber < self.textCursor().columnNumber():
mode = QtGui.QTextCursor.MoveAnchor
if event.modifiers() & QtCore.Qt.ShiftModifier:
mode = QtGui.QTextCursor.KeepAnchor
self.moveCursor(QtGui.QTextCursor.Left, mode)
return
if event.key() == QtCore.Qt.Key_Right:
event.accept()
if not self.isInEditionZone():
self.moveCursor(QtGui.QTextCursor.End)
# First backspace just moves the cursor.
return
mode = QtGui.QTextCursor.MoveAnchor
if event.modifiers() & QtCore.Qt.ShiftModifier:
mode = QtGui.QTextCursor.KeepAnchor
self.moveCursor(QtGui.QTextCursor.Right, mode)
return
if len(event.text()) > 0:
event.accept()
if not self.isInEditionZone():
self.moveCursor(QtGui.QTextCursor.End)
self.textCursor().insertText(event.text())
class Console(QtGui.QDockWidget):
"""
Console window used for advanced commands, debugging,
logging, and profiling.
"""
def __init__(self, parentWindow):
QtGui.QDockWidget.__init__(self, parentWindow)
self.setTitleBarWidget(QtGui.QWidget())
self.setAllowedAreas(QtCore.Qt.BottomDockWidgetArea)
self.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures)
self.setVisible(False)
# Add console window
self.consoleEdit = ConsoleEdit()
self.consoleEdit.displayPrompt()
self.setWidget(self.consoleEdit)
def setVisible(self, visible):
"""
Override to set proper focus.
"""
QtGui.QDockWidget.setVisible(self, visible)
if visible:
self.consoleEdit.setFocus(QtCore.Qt.OtherFocusReason)
def showMessage(self, message):
self.consoleEdit.append(message);