File indexing completed on 2025-09-14 03:42:58

0001 /*
0002     File                 : MQTTConnectionManagerDialog.cpp
0003     Project              : LabPlot
0004     Description          : widget for managing MQTT connections
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2018 Ferencz Kovacs <kferike98@gmail.com>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "MQTTConnectionManagerDialog.h"
0011 #include "MQTTConnectionManagerWidget.h"
0012 #include "backend/core/Settings.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <KWindowConfig>
0017 
0018 #include <QDialogButtonBox>
0019 #include <QWindow>
0020 
0021 /*!
0022     \class MQTTConnectionManagerDialog
0023     \brief dialog for managing MQTT connections
0024 
0025     \ingroup kdefrontend
0026 */
0027 MQTTConnectionManagerDialog::MQTTConnectionManagerDialog(QWidget* parent, const QString& conn, bool changed)
0028     : QDialog(parent)
0029     , mainWidget(new MQTTConnectionManagerWidget(this, conn))
0030     , m_initialConnectionChanged(changed)
0031     , m_initialConnection(conn) {
0032     setWindowIcon(QIcon::fromTheme(QStringLiteral("labplot-MQTT")));
0033     setWindowTitle(i18nc("@title:window", "MQTT Connections"));
0034 
0035     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0036 
0037     auto* layout = new QVBoxLayout(this);
0038     layout->addWidget(mainWidget);
0039     layout->addWidget(m_buttonBox);
0040 
0041     connect(mainWidget, &MQTTConnectionManagerWidget::changed, this, &MQTTConnectionManagerDialog::changed);
0042     connect(m_buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &MQTTConnectionManagerDialog::save);
0043     connect(m_buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &MQTTConnectionManagerDialog::close);
0044     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0045     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0046 
0047     // restore saved settings if available
0048     create(); // ensure there's a window created
0049     KConfigGroup conf = Settings::group(QStringLiteral("MQTTConnectionManagerDialog"));
0050     if (conf.exists()) {
0051         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0052         resize(windowHandle()->size()); // workaround for QTBUG-40584
0053     } else
0054         resize(QSize(0, 0).expandedTo(minimumSize()));
0055 }
0056 
0057 /*!
0058  * \brief returns the selected connection of the mainWidget
0059  */
0060 QString MQTTConnectionManagerDialog::connection() const {
0061     return mainWidget->connection();
0062 }
0063 
0064 /*!
0065  * \brief Returns whether the initial connection has been changed
0066  * \return m_initialConnectionChanged
0067  */
0068 bool MQTTConnectionManagerDialog::initialConnectionChanged() const {
0069     return m_initialConnectionChanged;
0070 }
0071 
0072 MQTTConnectionManagerDialog::~MQTTConnectionManagerDialog() {
0073     // save current settings
0074     KConfigGroup conf = Settings::group(QStringLiteral("MQTTConnectionManagerDialog"));
0075     KWindowConfig::saveWindowSize(windowHandle(), conf);
0076 }
0077 
0078 /*!
0079  * \brief Called when the user changes any setting in mainWidget
0080  */
0081 void MQTTConnectionManagerDialog::changed() {
0082     setWindowTitle(i18nc("@title:window", "MQTT Connections  [Changed]"));
0083 
0084     // set true if initial connection was changed
0085     if (mainWidget->connection() == m_initialConnection)
0086         m_initialConnectionChanged = true;
0087 
0088     if (mainWidget->checkConnections()) {
0089         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0090         m_changed = true;
0091     } else {
0092         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0093     }
0094 }
0095 
0096 /*!
0097  * \brief Saves the settings for the mainWidget
0098  */
0099 void MQTTConnectionManagerDialog::save() {
0100     // ok-button was clicked, save the connections if they were changed
0101     if (m_changed)
0102         mainWidget->saveConnections();
0103 }