]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/dolphinsettingsdialog.cpp
Fix build with Qt 5.14 RC
[dolphin.git] / src / settings / dolphinsettingsdialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinsettingsdialog.h"
22
23 #include "dolphin_generalsettings.h"
24 #include "dolphinmainwindow.h"
25 #include "general/generalsettingspage.h"
26 #include "navigation/navigationsettingspage.h"
27 #include "services/servicessettingspage.h"
28 #include "startup/startupsettingspage.h"
29 #include "trash/trashsettingspage.h"
30 #include "viewmodes/viewsettingspage.h"
31
32 #include <KAuthorized>
33 #include <KLocalizedString>
34 #include <KWindowConfig>
35 #include <KMessageBox>
36
37 #include <QCloseEvent>
38 #include <QPushButton>
39
40 DolphinSettingsDialog::DolphinSettingsDialog(const QUrl& url, QWidget* parent) :
41 KPageDialog(parent),
42 m_pages(),
43 m_unsavedChanges(false)
44
45 {
46 const QSize minSize = minimumSize();
47 setMinimumSize(QSize(540, minSize.height()));
48
49 setFaceType(List);
50 setWindowTitle(i18nc("@title:window", "Dolphin Preferences"));
51 QDialogButtonBox* box = new QDialogButtonBox(QDialogButtonBox::Ok
52 | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
53 box->button(QDialogButtonBox::Apply)->setEnabled(false);
54 box->button(QDialogButtonBox::Ok)->setDefault(true);
55 setButtonBox(box);
56
57 connect(box->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
58 connect(box->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
59 connect(box->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &DolphinSettingsDialog::restoreDefaults);
60
61 // General
62 GeneralSettingsPage* generalSettingsPage = new GeneralSettingsPage(url, this);
63 KPageWidgetItem* generalSettingsFrame = addPage(generalSettingsPage,
64 i18nc("@title:group General settings", "General"));
65 generalSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("system-file-manager")));
66 connect(generalSettingsPage, &GeneralSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
67
68 // Startup
69 StartupSettingsPage* startupSettingsPage = new StartupSettingsPage(url, this);
70 KPageWidgetItem* startupSettingsFrame = addPage(startupSettingsPage,
71 i18nc("@title:group", "Startup"));
72 startupSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-launch-feedback")));
73 connect(startupSettingsPage, &StartupSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
74
75 // View Modes
76 ViewSettingsPage* viewSettingsPage = new ViewSettingsPage(this);
77 KPageWidgetItem* viewSettingsFrame = addPage(viewSettingsPage,
78 i18nc("@title:group", "View Modes"));
79 viewSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-icons")));
80 connect(viewSettingsPage, &ViewSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
81
82 // Navigation
83 NavigationSettingsPage* navigationSettingsPage = new NavigationSettingsPage(this);
84 KPageWidgetItem* navigationSettingsFrame = addPage(navigationSettingsPage,
85 i18nc("@title:group", "Navigation"));
86 navigationSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-navigation")));
87 connect(navigationSettingsPage, &NavigationSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
88
89 // Services
90 ServicesSettingsPage* servicesSettingsPage = new ServicesSettingsPage(this);
91 KPageWidgetItem* servicesSettingsFrame = addPage(servicesSettingsPage,
92 i18nc("@title:group", "Services"));
93 servicesSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-system-services")));
94 connect(servicesSettingsPage, &ServicesSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
95
96 // Trash
97 auto* trashSettingsPage = createTrashSettingsPage(this);
98 if (trashSettingsPage) {
99 KPageWidgetItem* trashSettingsFrame = addPage(trashSettingsPage,
100 i18nc("@title:group", "Trash"));
101 trashSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("user-trash")));
102 connect(trashSettingsPage, &TrashSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
103 }
104
105 m_pages.append(generalSettingsPage);
106 m_pages.append(startupSettingsPage);
107 m_pages.append(viewSettingsPage);
108 m_pages.append(navigationSettingsPage);
109 m_pages.append(servicesSettingsPage);
110 if (trashSettingsPage) {
111 m_pages.append(trashSettingsPage);
112 }
113
114 const KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "SettingsDialog");
115 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
116 }
117
118 DolphinSettingsDialog::~DolphinSettingsDialog()
119 {
120 KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "SettingsDialog");
121 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
122 }
123
124 void DolphinSettingsDialog::enableApply()
125 {
126 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
127 m_unsavedChanges = true;
128 }
129
130 void DolphinSettingsDialog::applySettings()
131 {
132 foreach (SettingsPageBase* page, m_pages) {
133 page->applySettings();
134 }
135
136 emit settingsChanged();
137
138 GeneralSettings* settings = GeneralSettings::self();
139 if (settings->modifiedStartupSettings()) {
140 // Reset the modified startup settings hint. The changed startup settings
141 // have been applied already due to emitting settingsChanged().
142 settings->setModifiedStartupSettings(false);
143 settings->save();
144 }
145 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
146 m_unsavedChanges = false;
147 }
148
149 void DolphinSettingsDialog::restoreDefaults()
150 {
151 foreach (SettingsPageBase* page, m_pages) {
152 page->restoreDefaults();
153 }
154 }
155
156 void DolphinSettingsDialog::closeEvent(QCloseEvent* event)
157 {
158 if (!m_unsavedChanges) {
159 event->accept();
160 return;
161 }
162
163 const auto response = KMessageBox::warningYesNoCancel(this,
164 i18n("You have unsaved changes. Do you want to apply the changes or discard them?"),
165 i18n("Warning"),
166 KStandardGuiItem::save(),
167 KStandardGuiItem::discard(),
168 KStandardGuiItem::cancel());
169 switch (response) {
170 case KMessageBox::Yes:
171 applySettings();
172 Q_FALLTHROUGH();
173 case KMessageBox::No:
174 event->accept();
175 break;
176 case KMessageBox::Cancel:
177 event->ignore();
178 break;
179 default:
180 break;
181 }
182 }
183
184
185 SettingsPageBase *DolphinSettingsDialog::createTrashSettingsPage(QWidget *parent)
186 {
187 if (!KAuthorized::authorizeControlModule(QStringLiteral("kcmtrash.desktop"))) {
188 return nullptr;
189 }
190
191 return new TrashSettingsPage(parent);
192 }