-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathterminal.cpp
189 lines (151 loc) · 5.36 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
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
#include "terminal.h"
#include "ui_terminal.h"
Terminal::Terminal(QWidget *parent) :
QDialog(parent),
ui(new Ui::Terminal)
{
ui->setupUi(this);
connect(ui->sendBtn, SIGNAL(clicked(bool)), this, SLOT(sendInput()));
connect(ui->sentCmdList, SIGNAL(clicked(QModelIndex)), this, SLOT(sentCmdListItemClicked()));
connect(ui->sentCmdList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(sentCmdListItemDoubleClicked()));
ui->cmdEdit->installEventFilter( this );
connect(ui->clearBtn, SIGNAL(clicked(bool)), this, SLOT(clear()));
loadHistory();
ui->sentCmdList->setCurrentRow( ui->sentCmdList->count() - 1 );
}
void Terminal::saveHistory()
{
QStringList itemListStrList;
for(int i=0; i<ui->sentCmdList->count(); i++)
itemListStrList << ui->sentCmdList->item(i)->text();
QSettings settings;
settings.setValue("terminal/history", itemListStrList );
}
void Terminal::loadHistory()
{
QSettings settings;
QStringList savedHistoryList = settings.value("terminal/history", QStringList()).toStringList();
foreach(QString str, savedHistoryList )
ui->sentCmdList->addItem( str );
}
void Terminal::clear()
{
ui->cmdEdit->clear();
ui->sentCmdList->clear();
ui->terminalBrowser->clear();
QSettings settings;
settings.setValue("terminal/history", QStringList() );
}
bool Terminal::eventFilter(QObject *obj, QEvent *e)
{
if(obj == ui->cmdEdit)
{
if(e->type() == QEvent::KeyPress)
{
QKeyEvent * keyEvent = static_cast<QKeyEvent*>(e);
if(keyEvent->key() == Qt::Key_Up)
{
if(ui->sentCmdList->count() > 0)
{
if(ui->cmdEdit->text().isEmpty() == false && ui->sentCmdList->currentRow() != 0)
{
ui->sentCmdList->setCurrentRow( ui->sentCmdList->currentRow() - 1 );
}
ui->cmdEdit->setText( ui->sentCmdList->currentItem()->text() );
}
return true;
} else if(keyEvent->key() == Qt::Key_Down)
{
if(ui->sentCmdList->count() > 0)
{
if(ui->cmdEdit->text().isEmpty() == false && ui->sentCmdList->currentRow() < ui->sentCmdList->count() - 1)
{
ui->sentCmdList->setCurrentRow( ui->sentCmdList->currentRow() + 1 );
}
ui->cmdEdit->setText( ui->sentCmdList->currentItem()->text() );
}
return true;
}
}
}
return QDialog::eventFilter( obj, e );
}
void Terminal::sentCmdListItemDoubleClicked()
{
ui->cmdEdit->setText( ui->sentCmdList->currentItem()->text() );
sendInput();
}
void Terminal::sentCmdListItemClicked()
{
ui->cmdEdit->setText( ui->sentCmdList->currentItem()->text() );
}
void Terminal::sendInput()
{
if(ui->cmdEdit->text().length() == 0)
return;
QByteArray dataToSend = ui->cmdEdit->text().toLatin1();
switch(ui->lineEndCombobox->currentIndex())
{
case NL_END: dataToSend.append('\n'); break;
case CR_END: dataToSend.append('\r'); break;
case BOTH_NL_CR_END: dataToSend.append("\n\r"); break;
default /*NO_END*/: break;
}
emit writeToSerial( dataToSend );
if(ui->sentCmdList->count() > 0)
{
if(ui->sentCmdList->item( ui->sentCmdList->count()-1 )->text() != ui->cmdEdit->text())
ui->sentCmdList->addItem( ui->cmdEdit->text() );
}
else
ui->sentCmdList->addItem( ui->cmdEdit->text() );
ui->sentCmdList->setCurrentRow( ui->sentCmdList->count()-1 );
ui->sentCmdList->scrollToBottom();
ui->cmdEdit->clear();
saveHistory();
}
Terminal::~Terminal()
{
delete ui;
}
void Terminal::appendOutput(QByteArray data)
{
appendOutput( QString(data) );
}
void Terminal::appendOutput(QString data)
{
// ui->terminalBrowser->append( data );
if(ui->filterPlotPacketsCheckBox->isChecked())
{
QString tempStr( data.mid(1,data.size()-4) );
// QRegExp rx_timeplot("[a-zA-Z0-9]+(\\|[a-zA-Z0-9 ]+\\|\\d{1,3},\\d{1,3},\\d{1,3}\\|(\\-*\\d+(\\.{0,1}\\d+)*|nan|inf))+");
// QRegExp rx_xy_plot("[a-zA-Z0-9]+\\|[a-zA-Z0-9 ]+\\|\\d{1,3},\\d{1,3},\\d{1,3}\\|(\\-*\\d+\\s\\-*\\d+\\s*)+");
if( Helper::validPacket(tempStr) )
{
return;
}
}
QTextCursor oldCursor = ui->terminalBrowser->textCursor();
int oldScrollValue = ui->terminalBrowser->verticalScrollBar()->value();
if(ui->TimeStampCheckBox->isChecked())
{
ui->terminalBrowser->moveCursor (QTextCursor::End);
ui->terminalBrowser->insertPlainText ( QDateTime::currentDateTime().toString("HH:mm:ss:zzz") + ": \t");
ui->terminalBrowser->moveCursor (QTextCursor::End);
}
if(ui->hexOutputCheckBox->isChecked())
{
data = QString( data.toLatin1().toHex() ) + "\n";
data = data.toUpper();
for (int i = 2; i <= data.size(); i+=3)
data.insert(i, ' ');
}
ui->terminalBrowser->moveCursor (QTextCursor::End);
ui->terminalBrowser->insertPlainText (data);
ui->terminalBrowser->moveCursor (QTextCursor::End);
if(ui->autoScrollCheckBox->isChecked() == false)
{
ui->terminalBrowser->setTextCursor( oldCursor );
ui->terminalBrowser->verticalScrollBar()->setValue( oldScrollValue );
}
}