-
Notifications
You must be signed in to change notification settings - Fork 10
/
console.cpp
123 lines (109 loc) · 3.1 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
#include "console.h"
Console::Console(QWidget *parent) :
QPlainTextEdit(parent)
{
prompt = "redis> ";
QPalette p = palette();
p.setColor(QPalette::Base, Qt::black);
p.setColor(QPalette::Text, Qt::green);
setPalette(p);
history = new QStringList;
historyPos = 0;
insertPrompt(false);
isLocked = false;
}
void Console::keyPressEvent(QKeyEvent *event)
{
if(isLocked)
return;
if(event->key() >= 0x20 && event->key() <= 0x7e
&& (event->modifiers() == Qt::NoModifier || event->modifiers() == Qt::ShiftModifier))
QPlainTextEdit::keyPressEvent(event);
if(event->key() == Qt::Key_Backspace
&& event->modifiers() == Qt::NoModifier
&& textCursor().positionInBlock() > prompt.length())
QPlainTextEdit::keyPressEvent(event);
if(event->key() == Qt::Key_Return && event->modifiers() == Qt::NoModifier)
onEnter();
if(event->key() == Qt::Key_Up && event->modifiers() == Qt::NoModifier)
historyBack();
if(event->key() == Qt::Key_Down && event->modifiers() == Qt::NoModifier)
historyForward();
QString cmd = textCursor().block().text().mid(prompt.length());
emit onChange(cmd);
}
void Console::mousePressEvent(QMouseEvent *)
{
setFocus();
}
void Console::mouseDoubleClickEvent(QMouseEvent *){}
void Console::contextMenuEvent(QContextMenuEvent *){}
void Console::onEnter()
{
if(textCursor().positionInBlock() == prompt.length())
{
insertPrompt();
return;
}
QString cmd = textCursor().block().text().mid(prompt.length());
isLocked = true;
historyAdd(cmd);
emit onCommand(cmd);
}
void Console::output(QString s)
{
textCursor().insertBlock();
QTextCharFormat format;
format.setForeground(Qt::white);
textCursor().setBlockCharFormat(format);
textCursor().insertText(s);
insertPrompt();
isLocked = false;
}
void Console::insertPrompt(bool insertNewBlock)
{
if(insertNewBlock)
textCursor().insertBlock();
QTextCharFormat format;
format.setForeground(Qt::green);
textCursor().setBlockCharFormat(format);
textCursor().insertText(prompt);
scrollDown();
}
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++;
}