/
console.cpp
237 lines (179 loc) · 5.85 KB
/
console.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "console.h"
#include <QtWidgets>
Console::Console(QWidget *parent)
: QTextEdit(parent) {
setFrameShape(QFrame::NoFrame);
viewport()->setCursor(Qt::ArrowCursor);
setFont(QFont("Lucida Console", 10));
setWordWrapMode(QTextOption::WrapAnywhere);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
history = new QStringList;
historyPos = 0;
locked = false;
}
void Console::setPrompt(QString prompt) {
this->prompt = prompt;
insertPrompt();
}
void Console::lock() {
locked = true;
}
void Console::unlock() {
locked = false;
}
void Console::output(QString str) {
textCursor().insertBlock();
textCursor().insertText(str);
scrollDown();
}
Console &Console::operator<<(const QString &str) {
output(str);
return *this;
}
void Console::insertBlock() {
textCursor().insertBlock();
}
void Console::insertPrompt() {
textCursor().insertText(prompt);
scrollDown();
locked = false;
}
void Console::setBaseColor(QColor c) {
QPalette p = palette();
p.setColor(QPalette::Base, c);
setPalette(p);
}
void Console::setTextColor(QColor c) {
QPalette p = palette();
p.setColor(QPalette::Text, c);
setPalette(p);
}
void Console::keyPressEvent(QKeyEvent *e) {
if (locked)
return;
if ((e->key() == Qt::Key_V && e->modifiers() == Qt::ControlModifier))
QTextEdit::keyPressEvent(e);
else if (e->key() >= Qt::Key_Space && e->key() <= Qt::Key_BraceRight && e->key() != Qt::Key_QuoteLeft) {
if (e->modifiers() == Qt::NoModifier || e->modifiers() == Qt::ShiftModifier)
QTextEdit::keyPressEvent(e);
}
else
switch (e->key()) {
case Qt::Key_Return:
onReturn();
break;
case Qt::Key_Up:
historyBack();
break;
case Qt::Key_Down:
historyForward();
break;
case Qt::Key_Backspace:
if (textCursor().positionInBlock() > prompt.length())
QTextEdit::keyPressEvent(e);
break;
case Qt::Key_End:
case Qt::Key_Delete:
QTextEdit::keyPressEvent(e);
break;
case Qt::Key_Left:
if (e->modifiers() == Qt::NoModifier) {
QTextEdit::keyPressEvent(e);
QTextCursor cursor = textCursor();
if (cursor.positionInBlock() < prompt.length())
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor);
setTextCursor(cursor);
} else if (e->modifiers() == Qt::ControlModifier) {
QTextEdit::keyPressEvent(e);
QTextCursor cursor = textCursor();
if (cursor.positionInBlock() < prompt.length())
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2);
setTextCursor(cursor);
}
break;
case Qt::Key_Right:
if (e->modifiers() == Qt::NoModifier || e->modifiers() == Qt::ControlModifier)
QTextEdit::keyPressEvent(e);
break;
case Qt::Key_Home: {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, prompt.length());
setTextCursor(cursor);
} break;
// case Qt::Key_PageUp:
// case Qt::Key_PageDown:
// {
// QTextEdit::keyPressEvent(e);
// QTextCursor cursor = textCursor();
// cursor.movePosition(QTextCursor::End);
// setTextCursor(cursor);
// } break;
case Qt::Key_PageUp: {
QScrollBar *vbar = verticalScrollBar();
vbar->setValue(vbar->value() - 15);
} break;
case Qt::Key_PageDown: {
QScrollBar *vbar = verticalScrollBar();
vbar->setValue(vbar->value() + 15);
} break;
// case Qt::Key_Escape:
// {
// QTextCursor cursor = textCursor();
// cursor.movePosition(QTextCursor::StartOfBlock);
// cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
// cursor.removeSelectedText();
// insertPrompt();
// } break;
default:
QWidget::keyPressEvent(e);
}
}
void Console::mousePressEvent(QMouseEvent *) {
setFocus();
}
void Console::mouseDoubleClickEvent(QMouseEvent *) {
}
void Console::contextMenuEvent(QContextMenuEvent *) {
}
void Console::onReturn() {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::EndOfBlock);
setTextCursor(cursor);
QString cmd = cursor.block().text().mid(prompt.length());
historyAdd(cmd);
emit command(cmd);
}
void Console::scrollDown() {
QScrollBar *vbar = verticalScrollBar();
vbar->setValue(vbar->maximum());
}
void Console::historyAdd(QString cmd) {
history->append(cmd);
historyPos = history->length();
}
void Console::historyBack() {
if (!historyPos)
return;
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(prompt + history->at(historyPos - 1));
setTextCursor(cursor);
historyPos--;
}
void Console::historyForward() {
if (historyPos == history->length())
return;
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
if (historyPos == history->length() - 1)
cursor.insertText(prompt);
else
cursor.insertText(prompt + history->at(historyPos + 1));
setTextCursor(cursor);
historyPos++;
}