-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsubform.cpp
106 lines (93 loc) · 3.24 KB
/
subform.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
/**
* Copyright (C) 2013-2017 Feng Lee <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QListWidgetItem>
#include "subform.h"
#include "ui_subform.h"
SubForm::SubForm(QWidget *parent) :
MqttForm(parent),
ui(new Ui::SubForm)
{
ui->setupUi(this);
topics = new QMap<QString, QListWidgetItem *>();
// clicking listWidget item, loads it into topicLineEdit (for "quick" unsubscribing)
connect(ui->listWidget, &QListWidget::itemClicked, [&](QListWidgetItem * item) {ui->topicLineEdit->setText(item->text());});
ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
// hitting Return in topicLineEdit, will subscribe or unsubscribe, if possible
connect(ui->topicLineEdit, &QLineEdit::returnPressed,
[&]()
{if (ui->subButton->isEnabled())
onSubscribe();
else if (ui->unsubButton->isEnabled())
onUnsubscribe();
});
ui->subButton->setShortcut(QKeySequence(tr("Ctrl+S")));
ui->unsubButton->setShortcut(QKeySequence(tr("Ctrl+N")));
}
SubForm::~SubForm()
{
delete ui;
delete topics;
}
void SubForm::init(QMQTT::Client *client)
{
MqttForm::init(client);
connect(_client, SIGNAL(subscribed(const QString &)), this, SLOT(onSubscribed(const QString &)));
connect(_client, SIGNAL(unsubscribed(const QString &)), this, SLOT(onUnsubscribed(const QString &)));
}
void SubForm::onSubscribed(const QString &topic)
{
if(!topics->contains(topic)) {
QListWidgetItem *item = new QListWidgetItem(topic);
topics->insert(topic, item);
ui->listWidget->addItem(item);
}
clearLineEdit();
}
void SubForm::onUnsubscribed(const QString &topic)
{
if(topics->contains(topic)) {
QListWidgetItem *item = topics->take(topic);
ui->listWidget->removeItemWidget(item);
delete item;
}
clearLineEdit();
}
void SubForm::onTopicInput(QString topic) {
const bool topicAlreadSub = topics->contains(topic);
ui->subButton->setEnabled(!topic.isEmpty() && !topicAlreadSub);
ui->unsubButton->setEnabled(!topic.isEmpty() && topicAlreadSub);
}
void SubForm::onSubscribe()
{
QString topic = ui->topicLineEdit->text();
quint8 qos = ui->qosComboBox->currentIndex();
qDebug("Qos: %d", qos);
_client->subscribe(topic, qos);
}
void SubForm::onUnsubscribe()
{
QString topic = ui->topicLineEdit->text();
_client->unsubscribe(topic);
}
void SubForm::clearLineEdit()
{
ui->topicLineEdit->clear();
ui->subButton->setEnabled(false);
ui->unsubButton->setEnabled(false);
ui->topicLineEdit->setFocus();
ui->listWidget->clearSelection();
}