]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/dolphinsettingsdialog.cpp
DolphinSettingsDialog: Move focus to Okay button last
[dolphin.git] / src / settings / dolphinsettingsdialog.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphinsettingsdialog.h"
8
9 #include "config-dolphin.h"
10 #include "contextmenu/contextmenusettingspage.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphinmainwindow.h"
13 #include "interface/interfacesettingspage.h"
14 #include "trash/trashsettingspage.h"
15 #include "viewmodes/viewsettingspage.h"
16 #if HAVE_KUSERFEEDBACK
17 #include "userfeedback/dolphinfeedbackprovider.h"
18 #include "userfeedback/userfeedbacksettingspage.h"
19 #endif
20
21 #include <KAuthorized>
22 #include <KLocalizedString>
23 #include <KMessageBox>
24 #include <KWindowConfig>
25
26 #include <kwidgetsaddons_version.h>
27
28 #include <QCloseEvent>
29 #include <QPushButton>
30
31 DolphinSettingsDialog::DolphinSettingsDialog(const QUrl &url, QWidget *parent, KActionCollection *actions)
32 : KPageDialog(parent)
33 , m_pages()
34 , m_unsavedChanges(false)
35
36 {
37 const QSize minSize = minimumSize();
38 setMinimumSize(QSize(540, minSize.height()));
39
40 setFaceType(List);
41 setWindowTitle(i18nc("@title:window", "Configure"));
42
43 // Interface
44 InterfaceSettingsPage *interfaceSettingsPage = new InterfaceSettingsPage(this);
45 KPageWidgetItem *interfaceSettingsFrame = addPage(interfaceSettingsPage, i18nc("@title:group Interface settings", "Interface"));
46 interfaceSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("system-file-manager")));
47 connect(interfaceSettingsPage, &InterfaceSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
48
49 // View
50 ViewSettingsPage *viewSettingsPage = new ViewSettingsPage(url, this);
51 KPageWidgetItem *viewSettingsFrame = addPage(viewSettingsPage, i18nc("@title:group", "View"));
52 viewSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-icons")));
53 connect(viewSettingsPage, &ViewSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
54
55 // Context Menu
56 auto contextMenuSettingsPage = new ContextMenuSettingsPage(this,
57 actions,
58 {QStringLiteral("add_to_places"),
59 QStringLiteral("sort"),
60 QStringLiteral("view_mode"),
61 QStringLiteral("open_in_new_tab"),
62 QStringLiteral("open_in_new_window"),
63 QStringLiteral("open_in_split_view"),
64 QStringLiteral("copy_location"),
65 QStringLiteral("duplicate"),
66 QStringLiteral("open_terminal_here"),
67 QStringLiteral("copy_to_inactive_split_view"),
68 QStringLiteral("move_to_inactive_split_view")});
69 KPageWidgetItem *contextMenuSettingsFrame = addPage(contextMenuSettingsPage, i18nc("@title:group", "Context Menu"));
70 contextMenuSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-menu-edit")));
71 connect(contextMenuSettingsPage, &ContextMenuSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
72
73 // Trash
74 SettingsPageBase *trashSettingsPage = nullptr;
75 #ifndef Q_OS_WIN
76 trashSettingsPage = createTrashSettingsPage(this);
77 #endif
78 if (trashSettingsPage) {
79 trashSettings = addPage(trashSettingsPage, i18nc("@title:group", "Trash"));
80 trashSettings->setIcon(QIcon::fromTheme(QStringLiteral("user-trash")));
81 connect(trashSettingsPage, &TrashSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
82 }
83
84 #if HAVE_KUSERFEEDBACK
85 // User Feedback
86 UserFeedbackSettingsPage *feedbackSettingsPage = nullptr;
87 if (DolphinFeedbackProvider::instance()->isEnabled()) {
88 feedbackSettingsPage = new UserFeedbackSettingsPage(this);
89 auto feedbackSettingsFrame = addPage(feedbackSettingsPage, i18nc("@title:group", "User Feedback"));
90 feedbackSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-locale")));
91 connect(feedbackSettingsPage, &UserFeedbackSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
92 }
93 #endif
94
95 m_pages.append(interfaceSettingsPage);
96 m_pages.append(viewSettingsPage);
97 m_pages.append(contextMenuSettingsPage);
98 if (trashSettingsPage) {
99 m_pages.append(trashSettingsPage);
100 }
101 #if HAVE_KUSERFEEDBACK
102 if (feedbackSettingsPage) {
103 m_pages.append(feedbackSettingsPage);
104 }
105 #endif
106
107 // Create the buttons last so they are also last in the keyboard Tab focus order.
108 QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
109 box->button(QDialogButtonBox::Apply)->setEnabled(false);
110 box->button(QDialogButtonBox::Ok)->setDefault(true);
111 setButtonBox(box);
112
113 connect(box->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
114 connect(box->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
115 connect(box->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &DolphinSettingsDialog::restoreDefaults);
116
117 const KConfigGroup dialogConfig(KSharedConfig::openStateConfig(), QStringLiteral("SettingsDialog"));
118 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
119 }
120
121 DolphinSettingsDialog::~DolphinSettingsDialog()
122 {
123 KConfigGroup dialogConfig(KSharedConfig::openStateConfig(), QStringLiteral("SettingsDialog"));
124 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
125 }
126
127 void DolphinSettingsDialog::enableApply()
128 {
129 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
130 m_unsavedChanges = true;
131 }
132
133 void DolphinSettingsDialog::applySettings()
134 {
135 for (SettingsPageBase *page : std::as_const(m_pages)) {
136 page->applySettings();
137 }
138
139 Q_EMIT settingsChanged();
140
141 GeneralSettings *settings = GeneralSettings::self();
142 if (settings->modifiedStartupSettings()) {
143 // Reset the modified startup settings hint. The changed startup settings
144 // have been applied already due to emitting settingsChanged().
145 settings->setModifiedStartupSettings(false);
146 settings->save();
147 }
148 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
149 m_unsavedChanges = false;
150 }
151
152 void DolphinSettingsDialog::restoreDefaults()
153 {
154 for (SettingsPageBase *page : std::as_const(m_pages)) {
155 page->restoreDefaults();
156 }
157 }
158
159 void DolphinSettingsDialog::closeEvent(QCloseEvent *event)
160 {
161 if (!m_unsavedChanges) {
162 event->accept();
163 return;
164 }
165
166 const auto response = KMessageBox::warningTwoActionsCancel(this,
167 i18n("You have unsaved changes. Do you want to apply the changes or discard them?"),
168 i18n("Warning"),
169 KStandardGuiItem::save(),
170 KStandardGuiItem::discard(),
171 KStandardGuiItem::cancel());
172 switch (response) {
173 case KMessageBox::PrimaryAction:
174 applySettings();
175 Q_FALLTHROUGH();
176 case KMessageBox::SecondaryAction:
177 event->accept();
178 break;
179 case KMessageBox::Cancel:
180 event->ignore();
181 break;
182 default:
183 break;
184 }
185 }
186
187 SettingsPageBase *DolphinSettingsDialog::createTrashSettingsPage(QWidget *parent)
188 {
189 if (!KAuthorized::authorizeControlModule(QStringLiteral("kcmtrash.desktop"))) {
190 return nullptr;
191 }
192
193 return new TrashSettingsPage(parent);
194 }
195
196 #include "moc_dolphinsettingsdialog.cpp"