-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terminal.cpp
101 lines (83 loc) · 2.03 KB
/
Terminal.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
#include "Terminal.h"
#include <QDebug>
Terminal::Terminal(QWidget *parent) : QTextEdit(parent)
{
this->setStyleSheet("background-color: black; color: white;");
}
//TODO: Implement text coloring
void Terminal::addText(QString text, bool incoming)
{
asciiText.append(text);
this->updateTerminal(asciiText);
emit textAdded(text);
}
//TODO: Implement text coloring
void Terminal::addText(QByteArray text, bool incoming)
{
asciiText.append(text);
this->updateTerminal(asciiText);
emit textAdded(text);
}
QString Terminal::getText()
{
return this->toPlainText();
}
QString Terminal::getFormattedText()
{
return this->toHtml();
}
void Terminal::enableEcho(bool enable)
{
this->echoEnabled = enable;
}
void Terminal::setDisplayMode(Terminal::DisplayMode mode)
{
this->displayMode = mode;
this->updateTerminal(this->asciiText);
}
void Terminal::keyPressEvent(QKeyEvent *e)
{
QString key = e->text();
char c = 0;
//Handle special keys
//All other keys transmit as is
if(e->key() == Qt::Key_Backspace){
//TODO: Fix backspace not working
//c = '\b';
}
else if( !key.isEmpty() ){ //Key could be a modifier (i.e. shift). Do not send this.
c = key.at(0).toLatin1();
}
//If converted correctly send the character
if(c){
emit textEnterred(c);
if(echoEnabled){
addText(key, false);
}
}
}
void Terminal::updateTerminal(QString text)
{
if(this->displayMode == ASCII){
this->setText(text);
}
else{
this->setText(asciiTextToHex(text));
}
}
QString Terminal::asciiTextToHex(QString text)
{
const static std::string asciiLookup = "0123456789ABCDEF";
//Replace each character with its hex equivalent and a space
QString hex;
for(QChar qc : text){
char c = qc.toLatin1();
char lower, higher;
lower = asciiLookup[c & 0x0F];
higher = asciiLookup[(c & 0xF0) >> 4];
hex += higher;
hex += lower;
hex += " ";
}
return hex;
}