File indexing completed on 2024-05-12 15:27:51

0001 /***************************************************************************
0002 File                 : MQTTConnectionManagerDialog.cpp
0003 Project              : LabPlot
0004 Description          : widget for managing MQTT connections
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2018 Ferencz Kovacs (kferike98@gmail.com)
0007 
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 
0029 #include "MQTTConnectionManagerDialog.h"
0030 #include "MQTTConnectionManagerWidget.h"
0031 
0032 #include <KLocalizedString>
0033 #include <KSharedConfig>
0034 #include <KWindowConfig>
0035 
0036 #include <QDialogButtonBox>
0037 #include <QWindow>
0038 
0039 /*!
0040     \class MQTTConnectionManagerDialog
0041     \brief dialog for managing MQTT connections
0042 
0043     \ingroup kdefrontend
0044 */
0045 MQTTConnectionManagerDialog::MQTTConnectionManagerDialog(QWidget* parent, const QString& conn, bool changed) : QDialog(parent),
0046     mainWidget(new MQTTConnectionManagerWidget(this, conn)),
0047     m_initialConnectionChanged(changed),
0048     m_initialConnection(conn) {
0049 
0050     setWindowIcon(QIcon::fromTheme("labplot-MQTT"));
0051     setWindowTitle(i18nc("@title:window", "MQTT Connections"));
0052 
0053     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0054 
0055     auto* layout = new QVBoxLayout(this);
0056     layout->addWidget(mainWidget);
0057     layout->addWidget(m_buttonBox);
0058 
0059     connect(mainWidget, &MQTTConnectionManagerWidget::changed, this, &MQTTConnectionManagerDialog::changed);
0060     connect(m_buttonBox->button(QDialogButtonBox::Ok),&QPushButton::clicked, this, &MQTTConnectionManagerDialog::save);
0061     connect(m_buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &MQTTConnectionManagerDialog::close);
0062     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0063     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0064 
0065     //restore saved settings if available
0066     create(); // ensure there's a window created
0067     KConfigGroup conf(KSharedConfig::openConfig(), "MQTTConnectionManagerDialog");
0068     if (conf.exists()) {
0069         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0070         resize(windowHandle()->size()); // workaround for QTBUG-40584
0071     } else
0072         resize(QSize(0, 0).expandedTo(minimumSize()));
0073 }
0074 
0075 /*!
0076  * \brief returns the selected connection of the mainWidget
0077  */
0078 QString MQTTConnectionManagerDialog::connection() const {
0079     return mainWidget->connection();
0080 }
0081 
0082 /*!
0083  * \brief Returns whether the initial connection has been changed
0084  * \return m_initialConnectionChanged
0085  */
0086 bool MQTTConnectionManagerDialog::initialConnectionChanged() const
0087 {
0088     return m_initialConnectionChanged;
0089 }
0090 
0091 MQTTConnectionManagerDialog::~MQTTConnectionManagerDialog() {
0092     //save current settings
0093     KConfigGroup conf(KSharedConfig::openConfig(), "MQTTConnectionManagerDialog");
0094     KWindowConfig::saveWindowSize(windowHandle(), conf);
0095 }
0096 
0097 /*!
0098  * \brief Called when the user changes any setting in mainWidget
0099  */
0100 void MQTTConnectionManagerDialog::changed() {
0101     setWindowTitle(i18nc("@title:window", "MQTT Connections  [Changed]"));
0102 
0103     //set true if initial connection was changed
0104     if (mainWidget->connection() == m_initialConnection)
0105         m_initialConnectionChanged = true;
0106 
0107     if (mainWidget->checkConnections()) {
0108         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0109         m_changed = true;
0110     } else {
0111         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0112     }
0113 }
0114 
0115 /*!
0116  * \brief Saves the settings for the mainWidget
0117  */
0118 void MQTTConnectionManagerDialog::save() {
0119     //ok-button was clicked, save the connections if they were changed
0120     if (m_changed)
0121         mainWidget->saveConnections();
0122 }