File indexing completed on 2024-05-12 15:28:20

0001 /***************************************************************************
0002 File                 : MQTTWillSettingsWidget.cpp
0003 Project              : LabPlot
0004 Description          : widget for managing MQTT connection's will settings
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2018 by Ferencz Kovacs (kferike98@gmail.com)
0007 Copyright            : (C) 2018 Fabian Kristof (fkristofszabolcs@gmail.com)
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 #include "MQTTWillSettingsWidget.h"
0029 
0030 /*!
0031     \class MQTTWillSettingsWidget
0032     \brief Widget for managing MQTT connection's will settings
0033 
0034     \ingroup kdefrontend
0035  */
0036 MQTTWillSettingsWidget::MQTTWillSettingsWidget(QWidget* parent, const MQTTClient::MQTTWill& will, const QVector<QString>& topics) : QWidget(parent), m_will(will) {
0037     ui.setupUi(this);
0038     ui.leWillUpdateInterval->setValidator(new QIntValidator(2, 1000000));
0039 
0040     connect(ui.cbWillMessageType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MQTTWillSettingsWidget::willMessageTypeChanged);
0041     connect(ui.cbWillUpdate, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MQTTWillSettingsWidget::willUpdateTypeChanged);
0042 
0043     connect(ui.chbEnabled, &QCheckBox::stateChanged, this, &MQTTWillSettingsWidget::enableWillSettings);
0044     connect(ui.chbWillRetain, &QCheckBox::stateChanged, [this](int state) {
0045         m_will.willRetain = (state == Qt::Checked);
0046     });
0047     connect(ui.leWillOwnMessage, &QLineEdit::textChanged, [this](const QString& text) {
0048         m_will.willOwnMessage = text;
0049     });
0050     connect(ui.leWillUpdateInterval, &QLineEdit::textChanged, [this](const QString& text) {
0051         m_will.willTimeInterval = text.toInt();
0052     });
0053     connect(ui.lwWillStatistics, &QListWidget::itemChanged, [this](QListWidgetItem* item) {
0054         m_statisticsType = static_cast<MQTTClient::WillStatisticsType>(ui.lwWillStatistics->row(item));
0055     });
0056     connect(ui.cbWillQoS, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int index) {
0057         m_will.willQoS = index;
0058     });
0059     connect(ui.cbWillTopic, QOverload<const QString&>::of(&QComboBox::currentTextChanged), [this](const QString& text) {
0060         m_will.willTopic = text;
0061     });
0062     connect(ui.bApply, &QPushButton::clicked, this, &MQTTWillSettingsWidget::applyClicked);
0063 
0064     loadSettings(will, topics);
0065 }
0066 
0067 MQTTClient::MQTTWill MQTTWillSettingsWidget::will() const {
0068     return m_will;
0069 }
0070 
0071 MQTTClient::WillStatisticsType MQTTWillSettingsWidget::statisticsType() const {
0072     return m_statisticsType;
0073 }
0074 
0075 /*!
0076  *\brief called when the selected will message type is changed,
0077  *       shows the options for the selected message type, hides the irrelevant ones
0078  *
0079  * \param type the selected will message type
0080  */
0081 void MQTTWillSettingsWidget::willMessageTypeChanged(int index) {
0082     m_will.willMessageType = static_cast<MQTTClient::WillMessageType>(index);
0083     if (m_will.willMessageType == MQTTClient::WillMessageType::OwnMessage) {
0084         ui.leWillOwnMessage->show();
0085         ui.lWillOwnMessage->show();
0086         ui.lWillStatistics->hide();
0087         ui.lwWillStatistics->hide();
0088     } else if (m_will.willMessageType == MQTTClient::WillMessageType::LastMessage) {
0089         ui.leWillOwnMessage->hide();
0090         ui.lWillOwnMessage->hide();
0091         ui.lWillStatistics->hide();
0092         ui.lwWillStatistics->hide();
0093     } else if (m_will.willMessageType == MQTTClient::WillMessageType::Statistics) {
0094         ui.lWillStatistics->show();
0095         ui.lwWillStatistics->show();
0096         ui.leWillOwnMessage->hide();
0097         ui.lWillOwnMessage->hide();
0098     }
0099 }
0100 
0101 /*!
0102  *\brief called when the selected update type for the will message is changed,
0103  *       shows the options for the selected update type, hides the irrelevant ones
0104  *
0105  * \param type the selected will update type
0106  */
0107 void MQTTWillSettingsWidget::willUpdateTypeChanged(int index) {
0108     m_will.willUpdateType = static_cast<MQTTClient::WillUpdateType>(index);
0109     if (m_will.willUpdateType == MQTTClient::WillUpdateType::TimePeriod) {
0110         ui.leWillUpdateInterval->show();
0111         ui.lWillUpdateInterval->show();
0112     } else if (m_will.willUpdateType == MQTTClient::WillUpdateType::OnClick) {
0113         ui.leWillUpdateInterval->hide();
0114         ui.lWillUpdateInterval->hide();
0115     }
0116 }
0117 
0118 /*!
0119  * \brief Updates the widget based on the will settings
0120  */
0121 void MQTTWillSettingsWidget::loadSettings(const MQTTClient::MQTTWill& will, const QVector<QString>& topics) {
0122     ui.chbEnabled->setChecked(will.enabled);
0123     enableWillSettings(will.enabled);
0124 
0125     ui.cbWillTopic->addItems(topics.toList());
0126     //Set back the initial value
0127     if (!will.willTopic.isEmpty())
0128         ui.cbWillTopic->setCurrentText(will.willTopic);
0129 
0130     int messageType = static_cast<int>(will.willMessageType);
0131     if (ui.cbWillMessageType->currentIndex() != messageType)
0132         ui.cbWillMessageType->setCurrentIndex(messageType);
0133     else
0134         willMessageTypeChanged(messageType);
0135 
0136     int updateType = static_cast<int>(will.willUpdateType);
0137     if (ui.cbWillUpdate->currentIndex() != updateType)
0138         ui.cbWillUpdate->setCurrentIndex(updateType);
0139     else
0140         willUpdateTypeChanged(updateType);
0141 
0142     ui.leWillOwnMessage->setText(will.willOwnMessage);
0143     ui.leWillUpdateInterval->setText(QString::number(will.willTimeInterval));
0144     ui.chbWillRetain->setChecked(will.willRetain);
0145 
0146     for (int i = 0; i < will.willStatistics.size(); ++i) {
0147         if (will.willStatistics[i])
0148             ui.lwWillStatistics->item(i)->setCheckState(Qt::Checked);
0149         else
0150             ui.lwWillStatistics->item(i)->setCheckState(Qt::Unchecked);
0151     }
0152 }
0153 
0154 void MQTTWillSettingsWidget::enableWillSettings(int state) {
0155     const bool enabled = m_will.enabled = (state == Qt::Checked);
0156     ui.lWillMessageType->setEnabled(enabled);
0157     ui.cbWillMessageType->setEnabled(enabled);
0158     ui.lWillOwnMessage->setEnabled(enabled);
0159     ui.leWillOwnMessage->setEnabled(enabled);
0160     ui.lWillStatistics->setEnabled(enabled);
0161     ui.lwWillStatistics->setEnabled(enabled);
0162     ui.lWillTopic->setEnabled(enabled);
0163     ui.cbWillTopic->setEnabled(enabled);
0164     ui.lWillQoS->setEnabled(enabled);
0165     ui.cbWillQoS->setEnabled(enabled);
0166     ui.lUseRetainMessage->setEnabled(enabled);
0167     ui.chbWillRetain->setEnabled(enabled);
0168     ui.lWillUpdateType->setEnabled(enabled);
0169     ui.cbWillUpdate->setEnabled(enabled);
0170     ui.lWillUpdateInterval->setEnabled(enabled);
0171     ui.leWillUpdateInterval->setEnabled(enabled);
0172 }