Changeset View
Changeset View
Standalone View
Standalone View
src/Gui/SettingsDialog/SettingsDialog.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2019 David Redondo <kde@david-redondo.de> | ||||
2 | * Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org> | 3 | * Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org> | ||
3 | * | 4 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | 5 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU Lesser General Public License as published by | 6 | * it under the terms of the GNU Lesser General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | 7 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | 8 | * (at your option) any later version. | ||
8 | * | 9 | * | ||
9 | * This program is distributed in the hope that it will be useful, | 10 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | 13 | * GNU General Public License for more details. | ||
13 | * | 14 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public License | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
15 | * along with this program; if not, write to the Free Software | 16 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 18 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 19 | */ | ||
19 | 20 | | |||
20 | #include "SettingsDialog.h" | 21 | #include "SettingsDialog.h" | ||
21 | 22 | | |||
22 | #include "GeneralOptionsPage.h" | 23 | #include "GeneralOptionsPage.h" | ||
23 | #include "SaveOptionsPage.h" | 24 | #include "SaveOptionsPage.h" | ||
25 | #include "settings.h" | ||||
24 | #include "ShortcutsOptionsPage.h" | 26 | #include "ShortcutsOptionsPage.h" | ||
25 | 27 | | |||
26 | #include <KLocalizedString> | 28 | #include <KLocalizedString> | ||
27 | #include <KShortcutWidget> | 29 | #include <KShortcutWidget> | ||
28 | 30 | | |||
29 | #include <QIcon> | 31 | #include <QIcon> | ||
30 | #include <QMessageBox> | 32 | #include <QMessageBox> | ||
31 | 33 | | |||
32 | SettingsDialog::SettingsDialog(QWidget *parent) : | 34 | SettingsDialog::SettingsDialog(QWidget *parent) : | ||
33 | KPageDialog(parent) | 35 | KConfigDialog(parent, QStringLiteral("settings"), Settings::self()) | ||
34 | { | 36 | { | ||
35 | // set up window options and geometry | 37 | addPage(new GeneralOptionsPage(this), Settings::self(), | ||
36 | setWindowTitle(i18nc("@title:window", "Configure")); | 38 | i18n("General"), QStringLiteral("spectacle")); | ||
37 | setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); | 39 | addPage(new SaveOptionsPage(this), Settings::self(), | ||
38 | resize(600, 550); | 40 | i18n("Save"), QStringLiteral("document-save")); | ||
39 | setFaceType(List); | 41 | mShortcutsPage = new ShortcutsOptionsPage(this); | ||
40 | 42 | addPage(mShortcutsPage, i18n("Shortcuts"), QStringLiteral("preferences-desktop-keyboard")); | |||
41 | // init all pages | 43 | connect(mShortcutsPage, &ShortcutsOptionsPage::shortCutsChanged, this, [this] { | ||
42 | QMetaObject::invokeMethod(this, "initPages", Qt::QueuedConnection); | 44 | updateButtons(); | ||
45 | }); | ||||
46 | resize(600, 590); | ||||
47 | connect(this, &KConfigDialog::currentPageChanged, this, &SettingsDialog::updateButtons); | ||||
43 | } | 48 | } | ||
44 | 49 | | |||
45 | void SettingsDialog::initPages() | 50 | bool SettingsDialog::hasChanged() | ||
46 | { | 51 | { | ||
47 | KPageWidgetItem *generalOptions = new KPageWidgetItem(new GeneralOptionsPage(this), i18n("General")); | 52 | return mShortcutsPage->isModified() || KConfigDialog::hasChanged(); | ||
48 | generalOptions->setHeader(i18n("General")); | | |||
49 | generalOptions->setIcon(QIcon::fromTheme(QStringLiteral("spectacle"))); | | |||
50 | addPage(generalOptions); | | |||
51 | mPages.insert(generalOptions); | | |||
52 | | ||||
53 | KPageWidgetItem *saveOptions = new KPageWidgetItem(new SaveOptionsPage(this), i18n("Save")); | | |||
54 | saveOptions->setHeader(i18n("Save")); | | |||
55 | saveOptions->setIcon(QIcon::fromTheme(QStringLiteral("document-save"))); | | |||
56 | addPage(saveOptions); | | |||
57 | mPages.insert(saveOptions); | | |||
58 | | ||||
59 | KPageWidgetItem *shortcutOptions = new KPageWidgetItem(new ShortcutsOptionsPage(this), i18n("Shortcuts")); | | |||
60 | shortcutOptions->setHeader(i18n("Shortcuts")); | | |||
61 | shortcutOptions->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-keyboard"))); | | |||
62 | addPage(shortcutOptions); | | |||
63 | mPages.insert(shortcutOptions); | | |||
64 | | ||||
65 | connect(this, &SettingsDialog::currentPageChanged, this, &SettingsDialog::onPageChanged); | | |||
66 | } | 53 | } | ||
67 | 54 | | |||
68 | void SettingsDialog::accept() | 55 | bool SettingsDialog::isDefault() | ||
69 | { | 56 | { | ||
70 | for (auto page : qAsConst(mPages)) { | 57 | return currentPage()->name() != i18n("Shortcuts") && KConfigDialog::isDefault(); | ||
71 | SettingsPage *pageWidget = dynamic_cast<SettingsPage *>(page->widget()); | | |||
72 | if (pageWidget) { | | |||
73 | pageWidget->saveChanges(); | | |||
74 | } | 58 | } | ||
59 | | ||||
60 | void SettingsDialog::updateSettings() | ||||
61 | { | ||||
62 | KConfigDialog::updateSettings(); | ||||
63 | mShortcutsPage->saveChanges(); | ||||
75 | } | 64 | } | ||
76 | 65 | | |||
77 | done(QDialog::Accepted); | 66 | void SettingsDialog::updateWidgets() | ||
67 | { | ||||
68 | KConfigDialog::updateWidgets(); | ||||
69 | mShortcutsPage->resetChanges(); | ||||
78 | } | 70 | } | ||
79 | 71 | | |||
80 | void SettingsDialog::onPageChanged(KPageWidgetItem *current, KPageWidgetItem *before) | 72 | void SettingsDialog::updateWidgetsDefault() | ||
81 | { | 73 | { | ||
82 | Q_UNUSED(current); | 74 | KConfigDialog::updateWidgetsDefault(); | ||
75 | mShortcutsPage->defaults(); | ||||
76 | } | ||||
77 | | ||||
78 | | ||||
79 | | ||||
83 | 80 | | |||
84 | SettingsPage *pageWidget = dynamic_cast<SettingsPage *>(before->widget()); | | |||
85 | if (pageWidget && (pageWidget->changesMade())) { | | |||
86 | QMessageBox::StandardButton response = QMessageBox::question(this, i18n("Apply Unsaved Changes"), | | |||
87 | i18n("You have made changes to the settings in this tab. Do you want to apply those changes?")); | | |||
88 | 81 | | |||
89 | if (response == QMessageBox::Yes) { | | |||
90 | pageWidget->saveChanges(); | | |||
91 | } else { | | |||
92 | pageWidget->resetChanges(); | | |||
93 | } | | |||
94 | } | | |||
95 | } | |