-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
89 lines (76 loc) · 2.36 KB
/
mainwindow.cpp
File metadata and controls
89 lines (76 loc) · 2.36 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QTimer>
#include "abstractserial.h"
#include "config.h"
#include <limits>
MainWindow::MainWindow(QIODevice *ioDev, QWidget *parent) :
QMainWindow(parent),
ioDev(ioDev),
ui(new Ui::MainWindow)
{
this->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
ui->setupUi(this);
connect(ioDev, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(ioDev, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
readWaitTimer = new QTimer(this);
readWaitTimer->setInterval(STB_NEXTCHAR_INTERVAL);
readWaitTimer->setSingleShot(true);
connect(readWaitTimer, SIGNAL(timeout()), this, SLOT(onReadyRead()));
onReadyRead();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::printChat(ChatterType type, QString data) {
data = data.trimmed();
if(!data.length()) return;
char colors[][10] = { "blue", "red", "green" };
char chatterName[][10] = {"Recieve", "System", "Send"};
ui->chatBoxTextEdit->append(
QString("<font color='%1'>[%2.%5] %3 : </font>%4\n")
.arg(colors[type], QTime::currentTime().toString(), chatterName[type], data)
.arg(QTime::currentTime().msec())
);
}
void MainWindow::processData(QString data) {
if(data.isEmpty()) return;
printChat(Recieve, data);
readWaitTimer->stop();
}
void MainWindow::onReadyRead() {
static QByteArray buffer = "";
QByteArray b = ioDev->peek(100);
save+=b;
buffer += b;
if(b.length()) readWaitTimer->start();
else {
processData(buffer);
buffer = "";
}
}
void MainWindow::onBytesWritten(qint64 numBytes) {
printChat(System, QString("Wrote %1 bytes").arg(numBytes));
}
void MainWindow::sendTextboxData() {
QString data = ui->chatlineEdit->text()+'\n';
sendData(data);
}
void MainWindow::sendData(QString data) {
if(ioDev->write(data.toAscii()) == -1) {
statusBar()->showMessage("Can't send data!");
printChat(System, "Can't send data :o");
} else {
printChat(Send, data);
}
}
void MainWindow::on_disconPushButton_clicked()
{
ioDev->close();
}
void MainWindow::on_sendPushButton_clicked()
{
sendTextboxData();
}