File indexing completed on 2024-04-28 15:14:02

0001 /***************************************************************************
0002     File                 : SettingsDialog.cpp
0003     Project              : LabPlot
0004     --------------------------------------------------------------------
0005     Copyright            : (C) 2008-2020 Alexander Semke (alexander.semke@web.de)
0006     Description          : application settings dialog
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 #include "SettingsDialog.h"
0029 
0030 #include "MainWin.h"
0031 #include "SettingsGeneralPage.h"
0032 #include "SettingsDatasetsPage.h"
0033 // #include "SettingsWelcomePage.h"
0034 #include "SettingsSpreadsheetPage.h"
0035 #include "SettingsWorksheetPage.h"
0036 
0037 #include <QPushButton>
0038 #include <QDialogButtonBox>
0039 #include <QWindow>
0040 
0041 #include <KMessageBox>
0042 #include <KConfigGroup>
0043 #include <KSharedConfig>
0044 #include <KWindowConfig>
0045 #include <KI18n/KLocalizedString>
0046 
0047 #ifdef HAVE_KUSERFEEDBACK
0048 #include <KUserFeedback/FeedbackConfigWidget>
0049 #endif
0050 
0051 /**
0052  * \brief Settings dialog for Labplot.
0053  *
0054  * Contains the pages for general settings and view settings.
0055  *
0056  */
0057 SettingsDialog::SettingsDialog(QWidget* parent) : KPageDialog(parent) {
0058     setFaceType(List);
0059     setWindowTitle(i18nc("@title:window", "Preferences"));
0060     setWindowIcon(QIcon::fromTheme("preferences-other"));
0061     setAttribute(Qt::WA_DeleteOnClose);
0062 
0063     buttonBox()->addButton(QDialogButtonBox::Apply)->setEnabled(false);
0064     buttonBox()->addButton(QDialogButtonBox::RestoreDefaults);
0065     connect(buttonBox(), &QDialogButtonBox::clicked, this, &SettingsDialog::slotButtonClicked);
0066 
0067     m_generalPage = new SettingsGeneralPage(this);
0068     KPageWidgetItem* generalFrame = addPage(m_generalPage, i18n("General"));
0069     generalFrame->setIcon(QIcon::fromTheme("system-run"));
0070     connect(m_generalPage, &SettingsGeneralPage::settingsChanged, this, &SettingsDialog::changed);
0071 
0072     m_worksheetPage = new SettingsWorksheetPage(this);
0073     KPageWidgetItem* worksheetFrame = addPage(m_worksheetPage, i18n("Worksheet"));
0074     worksheetFrame->setIcon(QIcon::fromTheme(QLatin1String("labplot-worksheet")));
0075     connect(m_worksheetPage, &SettingsWorksheetPage::settingsChanged, this, &SettingsDialog::changed);
0076 
0077     m_spreadsheetPage = new SettingsSpreadsheetPage(this);
0078     KPageWidgetItem* spreadsheetFrame = addPage(m_spreadsheetPage, i18n("Spreadsheet"));
0079     spreadsheetFrame->setIcon(QIcon::fromTheme(QLatin1String("labplot-spreadsheet")));
0080     connect(m_spreadsheetPage, &SettingsSpreadsheetPage::settingsChanged, this, &SettingsDialog::changed);
0081 
0082     m_datasetsPage = new SettingsDatasetsPage(this);
0083     KPageWidgetItem* datasetsFrame = addPage(m_datasetsPage, i18n("Datasets"));
0084     datasetsFrame->setIcon(QIcon::fromTheme(QLatin1String("database-index")));
0085 
0086 //  m_welcomePage = new SettingsWelcomePage(this);
0087 //  KPageWidgetItem* welcomeFrame = addPage(m_welcomePage, i18n("Welcome Screen"));
0088 //  welcomeFrame->setIcon(QIcon::fromTheme(QLatin1String("database-index")));
0089 //  connect(m_welcomePage, &SettingsWelcomePage::resetWelcomeScreen, this, &SettingsDialog::resetWelcomeScreen);
0090 
0091 #ifdef HAVE_KUSERFEEDBACK
0092     auto* mainWin = static_cast<MainWin*>(parent);
0093     m_userFeedbackWidget = new KUserFeedback::FeedbackConfigWidget(this);
0094     m_userFeedbackWidget->setFeedbackProvider(&mainWin->userFeedbackProvider());
0095     connect(m_userFeedbackWidget, &KUserFeedback::FeedbackConfigWidget::configurationChanged, this, &SettingsDialog::changed);
0096 
0097     KPageWidgetItem* userFeedBackFrame = addPage(m_userFeedbackWidget, i18n("User Feedback"));
0098     userFeedBackFrame->setIcon(QIcon::fromTheme(QLatin1String("preferences-desktop-locale")));
0099 #endif
0100 
0101     //restore saved settings if available
0102     create(); // ensure there's a window created
0103     KConfigGroup conf(KSharedConfig::openConfig(), "SettingsDialog");
0104     if (conf.exists()) {
0105         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0106         resize(windowHandle()->size()); // workaround for QTBUG-40584
0107     } else
0108         resize(QSize(0, 0).expandedTo(minimumSize()));
0109 }
0110 
0111 SettingsDialog::~SettingsDialog() {
0112     KConfigGroup dialogConfig = KSharedConfig::openConfig()->group("SettingsDialog");
0113     KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
0114 }
0115 
0116 void SettingsDialog::slotButtonClicked(QAbstractButton* button) {
0117     if ((button == buttonBox()->button(QDialogButtonBox::Ok)) || (button == buttonBox()->button(QDialogButtonBox::Apply))) {
0118         if (m_changed) {
0119             applySettings();
0120             setWindowTitle(i18nc("@title:window", "Preferences"));
0121             buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
0122         }
0123     } else if (button == buttonBox()->button(QDialogButtonBox::RestoreDefaults)) {
0124         const QString text(i18n("All settings will be reset to default values. Do you want to continue?"));
0125         if (KMessageBox::questionYesNo(this, text) == KMessageBox::Yes) {
0126             restoreDefaults();
0127             setWindowTitle(i18nc("@title:window", "Preferences"));
0128             buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
0129         }
0130     }
0131 }
0132 
0133 void SettingsDialog::changed() {
0134     m_changed = true;
0135     setWindowTitle(i18nc("@title:window", "Preferences    [Changed]"));
0136     buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
0137 }
0138 
0139 void SettingsDialog::applySettings() {
0140     m_changed = false;
0141     m_generalPage->applySettings();
0142     m_worksheetPage->applySettings();
0143     m_spreadsheetPage->applySettings();
0144     KSharedConfig::openConfig()->sync();
0145 
0146 #ifdef HAVE_KUSERFEEDBACK
0147     auto* mainWin = static_cast<MainWin*>(parent());
0148     mainWin->userFeedbackProvider().setTelemetryMode(m_userFeedbackWidget->telemetryMode());
0149     mainWin->userFeedbackProvider().setSurveyInterval(m_userFeedbackWidget->surveyInterval());
0150 #endif
0151 
0152     emit settingsChanged();
0153 }
0154 
0155 void SettingsDialog::restoreDefaults() {
0156     m_changed = false;
0157     m_generalPage->restoreDefaults();
0158     m_worksheetPage->restoreDefaults();
0159     m_spreadsheetPage->restoreDefaults();
0160 }