Skip to content

Commit

Permalink
Add auto-scroll. Add MQTT # topic support.
Browse files Browse the repository at this point in the history
  • Loading branch information
MayaPosch committed Jan 2, 2018
1 parent df38ed0 commit 2ebeb23
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.o
MQTTCute
MQTTCute.exe
release/
debug/
moc_*
ui_*
Makefile
29 changes: 20 additions & 9 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,7 @@ void MainWindow::addTopic() {
}
}

// Open a new window in the MDI for this topic.
//QWidget* w = new QWidget;
//Ui::TopicWindow tw;
//tw.setupUi(w);

// Open a new window in the MDI for this topic.
TopicWindow* tw = new TopicWindow(this);
tw->setTopic(topic);
topicwindows.insert(std::pair<string, TopicWindow*>(topic.toStdString(), tw));
Expand All @@ -190,9 +186,9 @@ void MainWindow::addTopic() {
connect(tw, SIGNAL(newMessage(string,string)), this, SLOT(publishMessage(string,string)));
connect(tw, SIGNAL(addSubscription(string)), this, SLOT(addSubscription(string)));
connect(tw, SIGNAL(removeSubscription(string)), this, SLOT(removeSubscription(string)));
connect(tw, SIGNAL(windowClosing(string)), this, SLOT(windowClosing(string)));

QMdiSubWindow* sw = ui->mdiArea->addSubWindow(tw);
//sw->setWindowTitle(topic);
sw->show();
}

Expand All @@ -210,11 +206,25 @@ void MainWindow::receiveMessage(string topic, string message) {
// Check that we have a window with the appropriate topic.
map<string, TopicWindow*>::const_iterator it;
it = topicwindows.find(topic);
bool found = false;
if (it == topicwindows.end()) {
cerr << "MQTT: message received for unknown topic: " << topic << endl;
// Search for partial match (for MQTT # topics).
for (it = topicwindows.begin(); it != topicwindows.end(); ++it) {
if (it->first.compare(0, it->first.length() - 1, topic, 0, it->first.length() - 1) == 0) {
// Matching topic found. Send message.
it->second->receiveMessage(message);
found = true;
}
}

// Remove the subscription.
removeSubscription(topic);
// If no matching topics were found, try to unsubscribe.
// FIXME: If the subscription was from an old # topic, figure out a way to unsubscribe anyway.
if (!found) {
cerr << "MQTT: message received for unknown topic: " << topic << endl;

// Remove the subscription.
removeSubscription(topic);
}

return;
}
Expand Down Expand Up @@ -242,6 +252,7 @@ void MainWindow::removeSubscription(string topic) {
// --- WINDOW CLOSING ---
void MainWindow::windowClosing(string topic) {
// As the window with the specified topic is closing, we should clean up references to it.
cout << "Closing window with topic: " << topic << endl;
map<string, TopicWindow*>::iterator it;
it = topicwindows.find(topic);
if (it == topicwindows.end()) {
Expand Down
13 changes: 12 additions & 1 deletion topicwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@

using namespace std;

#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->subscribeTextEdit->document()->setMaximumBlockCount(100); // Limit paragraphs.
}


Expand Down Expand Up @@ -115,7 +119,14 @@ void TopicWindow::receiveMessage(string message) {
i += 8;
}

ui->subscribeTextEdit->insertPlainText(text);
// Scroll to bottom after inserting text if we are already at the bottom, else leave the scrollbar where it is.
if (ui->subscribeTextEdit->verticalScrollBar()->value() == ui->subscribeTextEdit->verticalScrollBar()->maximum()) {
ui->subscribeTextEdit->insertPlainText(text);
ui->subscribeTextEdit->verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMaximum);
}
else {
ui->subscribeTextEdit->insertPlainText(text);
}
}


Expand Down

0 comments on commit 2ebeb23

Please sign in to comment.