File indexing completed on 2025-07-13 03:33:01

0001 /*
0002     File                 : MQTTWillSettingsWidget.cpp
0003     Project              : LabPlot
0004     Description          : widget for managing MQTT connection's will settings
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2018 Ferencz Kovacs <kferike98@gmail.com>
0007     SPDX-FileCopyrightText: 2018 Fabian Kristof <fkristofszabolcs@gmail.com>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 #include "MQTTWillSettingsWidget.h"
0012 
0013 /*!
0014     \class MQTTWillSettingsWidget
0015     \brief Widget for managing MQTT connection's will settings
0016 
0017     \ingroup kdefrontend
0018  */
0019 MQTTWillSettingsWidget::MQTTWillSettingsWidget(QWidget* parent, const MQTTClient::MQTTWill& will, const QVector<QString>& topics)
0020     : QWidget(parent)
0021     , m_will(will) {
0022     ui.setupUi(this);
0023     ui.leWillUpdateInterval->setValidator(new QIntValidator(2, 1000000));
0024 
0025     connect(ui.cbWillMessageType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MQTTWillSettingsWidget::willMessageTypeChanged);
0026     connect(ui.cbWillUpdate, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MQTTWillSettingsWidget::willUpdateTypeChanged);
0027 
0028     connect(ui.chbEnabled, &QCheckBox::toggled, this, &MQTTWillSettingsWidget::enableWillSettings);
0029     connect(ui.chbWillRetain, &QCheckBox::toggled, [this](bool state) {
0030         m_will.willRetain = state;
0031     });
0032     connect(ui.leWillOwnMessage, &QLineEdit::textChanged, [this](const QString& text) {
0033         m_will.willOwnMessage = text;
0034     });
0035     connect(ui.leWillUpdateInterval, &QLineEdit::textChanged, [this](const QString& text) {
0036         m_will.willTimeInterval = text.toInt();
0037     });
0038     connect(ui.lwWillStatistics, &QListWidget::itemChanged, [this](QListWidgetItem* item) {
0039         m_statisticsType = static_cast<MQTTClient::WillStatisticsType>(ui.lwWillStatistics->row(item));
0040     });
0041     connect(ui.cbWillQoS, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int index) {
0042         m_will.willQoS = index;
0043     });
0044     connect(ui.cbWillTopic, QOverload<const QString&>::of(&QComboBox::currentTextChanged), [this](const QString& text) {
0045         m_will.willTopic = text;
0046     });
0047     connect(ui.bApply, &QPushButton::clicked, this, &MQTTWillSettingsWidget::applyClicked);
0048 
0049     loadSettings(will, topics);
0050 }
0051 
0052 MQTTClient::MQTTWill MQTTWillSettingsWidget::will() const {
0053     return m_will;
0054 }
0055 
0056 MQTTClient::WillStatisticsType MQTTWillSettingsWidget::statisticsType() const {
0057     return m_statisticsType;
0058 }
0059 
0060 /*!
0061  *\brief called when the selected will message type is changed,
0062  *       shows the options for the selected message type, hides the irrelevant ones
0063  *
0064  * \param type the selected will message type
0065  */
0066 void MQTTWillSettingsWidget::willMessageTypeChanged(int index) {
0067     m_will.willMessageType = static_cast<MQTTClient::WillMessageType>(index);
0068     if (m_will.willMessageType == MQTTClient::WillMessageType::OwnMessage) {
0069         ui.leWillOwnMessage->show();
0070         ui.lWillOwnMessage->show();
0071         ui.lWillStatistics->hide();
0072         ui.lwWillStatistics->hide();
0073     } else if (m_will.willMessageType == MQTTClient::WillMessageType::LastMessage) {
0074         ui.leWillOwnMessage->hide();
0075         ui.lWillOwnMessage->hide();
0076         ui.lWillStatistics->hide();
0077         ui.lwWillStatistics->hide();
0078     } else if (m_will.willMessageType == MQTTClient::WillMessageType::Statistics) {
0079         ui.lWillStatistics->show();
0080         ui.lwWillStatistics->show();
0081         ui.leWillOwnMessage->hide();
0082         ui.lWillOwnMessage->hide();
0083     }
0084 }
0085 
0086 /*!
0087  *\brief called when the selected update type for the will message is changed,
0088  *       shows the options for the selected update type, hides the irrelevant ones
0089  *
0090  * \param type the selected will update type
0091  */
0092 void MQTTWillSettingsWidget::willUpdateTypeChanged(int index) {
0093     m_will.willUpdateType = static_cast<MQTTClient::WillUpdateType>(index);
0094     if (m_will.willUpdateType == MQTTClient::WillUpdateType::TimePeriod) {
0095         ui.leWillUpdateInterval->show();
0096         ui.lWillUpdateInterval->show();
0097     } else if (m_will.willUpdateType == MQTTClient::WillUpdateType::OnClick) {
0098         ui.leWillUpdateInterval->hide();
0099         ui.lWillUpdateInterval->hide();
0100     }
0101 }
0102 
0103 /*!
0104  * \brief Updates the widget based on the will settings
0105  */
0106 void MQTTWillSettingsWidget::loadSettings(const MQTTClient::MQTTWill& will, const QVector<QString>& topics) {
0107     ui.chbEnabled->setChecked(will.enabled);
0108     enableWillSettings(will.enabled);
0109 
0110     ui.cbWillTopic->addItems(topics.toList());
0111     // Set back the initial value
0112     if (!will.willTopic.isEmpty())
0113         ui.cbWillTopic->setCurrentText(will.willTopic);
0114 
0115     int messageType = static_cast<int>(will.willMessageType);
0116     if (ui.cbWillMessageType->currentIndex() != messageType)
0117         ui.cbWillMessageType->setCurrentIndex(messageType);
0118     else
0119         willMessageTypeChanged(messageType);
0120 
0121     int updateType = static_cast<int>(will.willUpdateType);
0122     if (ui.cbWillUpdate->currentIndex() != updateType)
0123         ui.cbWillUpdate->setCurrentIndex(updateType);
0124     else
0125         willUpdateTypeChanged(updateType);
0126 
0127     ui.leWillOwnMessage->setText(will.willOwnMessage);
0128     ui.leWillUpdateInterval->setText(QString::number(will.willTimeInterval));
0129     ui.chbWillRetain->setChecked(will.willRetain);
0130 
0131     for (int i = 0; i < will.willStatistics.size(); ++i) {
0132         if (will.willStatistics[i])
0133             ui.lwWillStatistics->item(i)->setCheckState(Qt::Checked);
0134         else
0135             ui.lwWillStatistics->item(i)->setCheckState(Qt::Unchecked);
0136     }
0137 }
0138 
0139 void MQTTWillSettingsWidget::enableWillSettings(bool enabled) {
0140     m_will.enabled = enabled;
0141     ui.lWillMessageType->setEnabled(enabled);
0142     ui.cbWillMessageType->setEnabled(enabled);
0143     ui.lWillOwnMessage->setEnabled(enabled);
0144     ui.leWillOwnMessage->setEnabled(enabled);
0145     ui.lWillStatistics->setEnabled(enabled);
0146     ui.lwWillStatistics->setEnabled(enabled);
0147     ui.lWillTopic->setEnabled(enabled);
0148     ui.cbWillTopic->setEnabled(enabled);
0149     ui.lWillQoS->setEnabled(enabled);
0150     ui.cbWillQoS->setEnabled(enabled);
0151     ui.lUseRetainMessage->setEnabled(enabled);
0152     ui.chbWillRetain->setEnabled(enabled);
0153     ui.lWillUpdateType->setEnabled(enabled);
0154     ui.cbWillUpdate->setEnabled(enabled);
0155     ui.lWillUpdateInterval->setEnabled(enabled);
0156     ui.leWillUpdateInterval->setEnabled(enabled);
0157 }