File indexing completed on 2024-05-12 07:41:27

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