-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtopicwindow.cpp
149 lines (115 loc) · 4.58 KB
/
topicwindow.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
#include "topicwindow.h"
#include "ui_topicwindow.h"
#include <iostream>
#include <QScrollBar>
// --- CONSTRUCTOR ---
TopicWindow::TopicWindow(QWidget *parent) : QWidget(parent), ui(new Ui::TopicWindow) {
ui->setupUi(this);
// Connections.
connect(ui->publishButton, SIGNAL(pressed()), this, SLOT(publishTopic()));
connect(ui->publishLineEdit, SIGNAL(returnPressed()), this, SLOT(publishTopic()));
connect(ui->subscribeCheckBox, SIGNAL(toggled(bool)), this, SLOT(subscriptionStatus(bool)));
// Defaults
ui->subscribeCheckBox->setChecked(false);
ui->subscribePlainTextEdit->document()->setMaximumBlockCount(100); // Limit paragraphs.
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
ui->subscribePlainTextEdit->setFont(font);
this->setFixedWidth(320);
}
// --- SET TOPIC ---
void TopicWindow::setTopic(QString topic) {
this->topic = topic.toStdString();
setWindowTitle(topic);
}
// --- DECONSTRUCTOR ---
TopicWindow::~TopicWindow() {
delete ui;
}
// --- PUBLISH TOPIC ---
void TopicWindow::publishTopic() {
std::string message = ui->publishLineEdit->text().toStdString();
// Parse any binary meta characters in the message string.
// These start with \x and are followed by two digits (0-9, a-f, A-F).
int idx = 0;
uint outpos = 0;
std::string out;
while ((idx = message.find("\\x", idx)) != std::string::npos) {
// Read the next two characters into a string for conversion.
// Copy the part that will remain unchanged into the output string.
out += message.substr(outpos, (idx - outpos));
QString hex = QString::fromStdString(message.substr((idx + 2), 2));
bool ok;
quint8 tint = (quint8) hex.toUInt(&ok, 16);
//cout << "Index: " << idx << ", tInt: " << tint << endl;
// if !ok, copy the four characters verbatim.
if (!ok) {
out += message.substr(idx, 4);
}
else {
out += std::string((const char*) &tint, 1);
}
// Update indices.
outpos = idx + 4;
idx += 4;
}
//cout << "Outpos: " << outpos << ", MsgLength: " << message.length() << endl;
//cout << "Out: " << out << endl;
//cout << "End: " << message.substr(outpos) << endl;
if (out.length() < 1) { out = message; }
else if (message.length() > outpos) { out += message.substr(outpos); }
uint8_t qos = ui->qosSpinBox->value();
bool retain = ui->retainCheckbox->isChecked();
emit newMessage(topic, out, qos, retain);
}
// --- SUBSCRIPTION STATUS ---
void TopicWindow::subscriptionStatus(bool status) {
if (status) { emit addSubscription(topic); }
else { emit removeSubscription(topic); }
}
// --- RECEIVE MESSAGE ---
// Called when a new message is available.
void TopicWindow::receiveMessage(std::string message) {
// Debug
std::cout << "TopicWindow: Received new message: " << message << std::endl;
// Format to hex-editor style text, with 8 hex bytes & 8 ASCII characters per line.
QString text;
uint i = 0;
while (i < message.length()) {
std::string temp;
if ((i + 8) < message.length()) { temp = message.substr(i, 8); }
else { temp = message.substr(i); }
QString line;
for (uint j = 0; j < temp.length(); ++j) {
QString l = QString::number((uint) temp[j], 16);
if (l.length() == 1) { line += "0" + l; }
else { line += l; }
line += " ";
}
if (line.length() < 24) {
// Add padding.
while (line.length() < 24) { line += "\u2007"; } // using U+2007 FIGURE SPACE
}
text += line;
text += "\u2007\u2007\u2007\u2007";
text += QString::fromStdString(temp);
text += "\n";
// Debug
std::cout << "Adding line: " << line.toStdString() << std::endl;
i += 8;
}
// Scroll to bottom after inserting text if we are already at the bottom, else leave the scrollbar where it is.
if (ui->subscribePlainTextEdit->verticalScrollBar()->value() == ui->subscribePlainTextEdit->verticalScrollBar()->maximum()) {
ui->subscribePlainTextEdit->insertPlainText(text);
ui->subscribePlainTextEdit->verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMaximum);
}
else {
ui->subscribePlainTextEdit->insertPlainText(text);
}
}
// --- CLOSE EVENT ---
// Handle the window being closed by the user.
void TopicWindow::closeEvent(QCloseEvent *event) {
emit windowClosing(topic);
event->accept();
}