]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/interface/folderstabssettingspage.cpp
Make FoldersTabsSettingsPage fully usable for blind users
[dolphin.git] / src / settings / interface / folderstabssettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "folderstabssettingspage.h"
8 #include "dolphinmainwindow.h"
9 #include "dolphinviewcontainer.h"
10 #include "global.h"
11
12 #include <KLocalizedString>
13 #include <KMessageBox>
14 #include <KProtocolManager>
15
16 #include <QButtonGroup>
17 #include <QCheckBox>
18 #include <QFileDialog>
19 #include <QFormLayout>
20 #include <QGridLayout>
21 #include <QHBoxLayout>
22 #include <QLabel>
23 #include <QLineEdit>
24 #include <QPushButton>
25 #include <QRadioButton>
26 #include <QSpacerItem>
27
28 FoldersTabsSettingsPage::FoldersTabsSettingsPage(QWidget *parent)
29 : SettingsPageBase(parent)
30 , m_homeUrlBoxLayoutContainer(nullptr)
31 , m_buttonBoxLayoutContainer(nullptr)
32 , m_homeUrlRadioButton(nullptr)
33 , m_homeUrl(nullptr)
34 , m_rememberOpenedTabsRadioButton(nullptr)
35 , m_openNewTabAfterLastTab(nullptr)
36 , m_openNewTabAfterCurrentTab(nullptr)
37 , m_splitView(nullptr)
38 , m_filterBar(nullptr)
39 , m_showFullPathInTitlebar(nullptr)
40 , m_openExternallyCalledFolderInNewTab(nullptr)
41 , m_useTabForSplitViewSwitch(nullptr)
42 {
43 QFormLayout *topLayout = new QFormLayout(this);
44
45 // Show on startup
46 m_rememberOpenedTabsRadioButton = new QRadioButton(i18nc("@option:radio Show on startup", "Folders, tabs, and window state from last time"), this);
47
48 // create 'Home URL' editor
49 m_homeUrlBoxLayoutContainer = new QWidget(this);
50 m_homeUrlRadioButton = new QRadioButton(m_homeUrlBoxLayoutContainer);
51 m_homeUrlRadioButton->setAccessibleName(i18nc("@option:radio", "Show home location on startup"));
52 QHBoxLayout *homeUrlBoxLayout = new QHBoxLayout(m_homeUrlBoxLayoutContainer);
53 homeUrlBoxLayout->setContentsMargins(0, 0, 0, 0);
54
55 m_homeUrl = new QLineEdit();
56 m_homeUrl->setClearButtonEnabled(true);
57 // i18n: For entering the absolute path to a user-specified home folder. Default: /home/userName/
58 m_homeUrl->setPlaceholderText(i18nc("@info:placeholder", "Enter home location path"));
59 homeUrlBoxLayout->addWidget(m_homeUrl);
60
61 QPushButton *selectHomeUrlButton = new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
62 homeUrlBoxLayout->addWidget(selectHomeUrlButton);
63
64 #ifndef QT_NO_ACCESSIBILITY
65 selectHomeUrlButton->setAccessibleName(i18nc("@action:button", "Select Home Location"));
66 #endif
67
68 connect(selectHomeUrlButton, &QPushButton::clicked, this, &FoldersTabsSettingsPage::selectHomeUrl);
69
70 m_buttonBoxLayoutContainer = new QWidget(this);
71 QHBoxLayout *buttonBoxLayout = new QHBoxLayout(m_buttonBoxLayoutContainer);
72 buttonBoxLayout->setContentsMargins(0, 0, 0, 0);
73
74 QPushButton *useCurrentButton = new QPushButton(i18nc("@action:button", "Use Current Location"));
75 buttonBoxLayout->addWidget(useCurrentButton);
76 connect(useCurrentButton, &QPushButton::clicked, this, &FoldersTabsSettingsPage::useCurrentLocation);
77 QPushButton *useDefaultButton = new QPushButton(i18nc("@action:button", "Use Default Location"));
78 buttonBoxLayout->addWidget(useDefaultButton);
79 connect(useDefaultButton, &QPushButton::clicked, this, &FoldersTabsSettingsPage::useDefaultLocation);
80
81 QButtonGroup *initialViewGroup = new QButtonGroup(this);
82 initialViewGroup->addButton(m_rememberOpenedTabsRadioButton);
83 initialViewGroup->addButton(m_homeUrlRadioButton);
84
85 topLayout->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton);
86
87 QGridLayout *startInLocationLayout = new QGridLayout();
88 startInLocationLayout->setHorizontalSpacing(0);
89 startInLocationLayout->setContentsMargins(0, 0, 0, 0);
90 startInLocationLayout->addWidget(m_homeUrlRadioButton, 0, 0);
91 startInLocationLayout->addWidget(m_homeUrlBoxLayoutContainer, 0, 1);
92 startInLocationLayout->addWidget(m_buttonBoxLayoutContainer, 1, 1);
93
94 topLayout->addRow(QString(), startInLocationLayout);
95
96 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
97 // Opening Folders
98 auto *openingFoldersLabel = new QLabel{i18nc("@label:checkbox", "Opening Folders:")};
99 m_openExternallyCalledFolderInNewTab = new QCheckBox(i18nc("@option:check Opening Folders", "Keep a single Dolphin window, opening new folders in tabs"));
100 openingFoldersLabel->setAccessibleName(m_openExternallyCalledFolderInNewTab->text());
101 topLayout->addRow(openingFoldersLabel, m_openExternallyCalledFolderInNewTab);
102 // Window
103 auto *windowLabel = new QLabel{i18nc("@label:checkbox", "Window:")};
104 m_showFullPathInTitlebar = new QCheckBox(i18nc("@option:check Startup Settings", "Show full path in title bar"));
105 windowLabel->setAccessibleName(m_showFullPathInTitlebar->text());
106 topLayout->addRow(windowLabel, m_showFullPathInTitlebar);
107 m_filterBar = new QCheckBox(i18nc("@option:check Window Startup Settings", "Show filter bar"));
108 topLayout->addRow(QString(), m_filterBar);
109
110 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
111
112 // Tabs properties
113 m_openNewTabAfterCurrentTab = new QRadioButton(i18nc("option:radio", "After current tab"));
114 m_openNewTabAfterLastTab = new QRadioButton(i18nc("option:radio", "At end of tab bar"));
115 QButtonGroup *tabsBehaviorGroup = new QButtonGroup(this);
116 tabsBehaviorGroup->addButton(m_openNewTabAfterCurrentTab);
117 tabsBehaviorGroup->addButton(m_openNewTabAfterLastTab);
118 topLayout->addRow(i18nc("@title:group", "Open new tabs: "), m_openNewTabAfterCurrentTab);
119 topLayout->addRow(QString(), m_openNewTabAfterLastTab);
120
121 // Split Views
122 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
123
124 // 'Switch between panes of split views with tab key'
125 auto *splitViewLabel = new QLabel{i18nc("@title:group", "Split view: ")};
126 m_useTabForSplitViewSwitch = new QCheckBox(i18nc("option:check split view panes", "Switch between views with Tab key"));
127 splitViewLabel->setAccessibleName(m_useTabForSplitViewSwitch->text());
128 topLayout->addRow(splitViewLabel, m_useTabForSplitViewSwitch);
129
130 // 'Close active pane when turning off split view'
131 m_closeActiveSplitView = new QCheckBox(i18nc("option:check", "Turning off split view closes the view in focus"));
132 topLayout->addRow(QString(), m_closeActiveSplitView);
133 m_closeActiveSplitView->setToolTip(
134 i18n("When unchecked, the opposite view will be closed. The Close icon always illustrates which view (left or right) will be closed."));
135
136 // 'Begin in split view mode'
137 auto *newWindowsLabel = new QLabel{i18n("New windows:")};
138 m_splitView = new QCheckBox(i18nc("@option:check Startup Settings", "Begin in split view mode"));
139 newWindowsLabel->setAccessibleName(m_splitView->text());
140 topLayout->addRow(newWindowsLabel, m_splitView);
141
142 loadSettings();
143
144 updateInitialViewOptions();
145
146 connect(m_homeUrl, &QLineEdit::textChanged, this, &FoldersTabsSettingsPage::slotSettingsChanged);
147 connect(m_rememberOpenedTabsRadioButton, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
148 connect(m_homeUrlRadioButton, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
149
150 connect(m_splitView, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
151 connect(m_filterBar, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
152
153 connect(m_openExternallyCalledFolderInNewTab, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
154 connect(m_showFullPathInTitlebar, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
155
156 connect(m_useTabForSplitViewSwitch, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::changed);
157 connect(m_closeActiveSplitView, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::changed);
158
159 connect(m_openNewTabAfterCurrentTab, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::changed);
160 connect(m_openNewTabAfterLastTab, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::changed);
161 }
162
163 FoldersTabsSettingsPage::~FoldersTabsSettingsPage()
164 {
165 }
166
167 void FoldersTabsSettingsPage::applySettings()
168 {
169 GeneralSettings *settings = GeneralSettings::self();
170
171 settings->setUseTabForSwitchingSplitView(m_useTabForSplitViewSwitch->isChecked());
172 settings->setCloseActiveSplitView(m_closeActiveSplitView->isChecked());
173 const QUrl url(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
174 if (url.isValid() && KProtocolManager::supportsListing(url)) {
175 KIO::StatJob *job = KIO::stat(url, KIO::StatJob::SourceSide, KIO::StatDetail::StatBasic, KIO::JobFlag::HideProgressInfo);
176 connect(job, &KJob::result, this, [this, settings, url](KJob *job) {
177 if (job->error() == 0 && qobject_cast<KIO::StatJob *>(job)->statResult().isDir()) {
178 settings->setHomeUrl(url.toDisplayString(QUrl::PreferLocalFile));
179 } else {
180 showSetDefaultDirectoryError();
181 }
182 });
183 } else {
184 showSetDefaultDirectoryError();
185 }
186
187 // Remove saved state if "remember open tabs" has been turned off
188 if (!m_rememberOpenedTabsRadioButton->isChecked()) {
189 KConfigGroup windowState{KSharedConfig::openConfig(QStringLiteral("dolphinrc")), QStringLiteral("WindowState")};
190 if (windowState.exists()) {
191 windowState.deleteGroup();
192 }
193 }
194
195 settings->setRememberOpenedTabs(m_rememberOpenedTabsRadioButton->isChecked());
196 settings->setSplitView(m_splitView->isChecked());
197 settings->setFilterBar(m_filterBar->isChecked());
198 settings->setOpenExternallyCalledFolderInNewTab(m_openExternallyCalledFolderInNewTab->isChecked());
199 settings->setShowFullPathInTitlebar(m_showFullPathInTitlebar->isChecked());
200
201 settings->setOpenNewTabAfterLastTab(m_openNewTabAfterLastTab->isChecked());
202
203 settings->save();
204 }
205
206 void FoldersTabsSettingsPage::restoreDefaults()
207 {
208 GeneralSettings *settings = GeneralSettings::self();
209 settings->useDefaults(true);
210 loadSettings();
211 settings->useDefaults(false);
212 }
213
214 void FoldersTabsSettingsPage::slotSettingsChanged()
215 {
216 // Provide a hint that the startup settings have been changed. This allows the views
217 // to apply the startup settings only if they have been explicitly changed by the user
218 // (see bug #254947).
219 GeneralSettings::setModifiedStartupSettings(true);
220
221 // Enable and disable home URL controls appropriately
222 updateInitialViewOptions();
223 Q_EMIT changed();
224 }
225
226 void FoldersTabsSettingsPage::updateInitialViewOptions()
227 {
228 m_homeUrlBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
229 m_buttonBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
230 }
231
232 void FoldersTabsSettingsPage::selectHomeUrl()
233 {
234 const QUrl homeUrl(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
235 QUrl url = QFileDialog::getExistingDirectoryUrl(this, QString(), homeUrl);
236 if (!url.isEmpty()) {
237 m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
238 slotSettingsChanged();
239 }
240 }
241
242 void FoldersTabsSettingsPage::useCurrentLocation()
243 {
244 m_homeUrl->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
245 }
246
247 void FoldersTabsSettingsPage::useDefaultLocation()
248 {
249 m_homeUrl->setText(QDir::homePath());
250 }
251
252 void FoldersTabsSettingsPage::loadSettings()
253 {
254 const QUrl url(Dolphin::homeUrl());
255 m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
256 m_rememberOpenedTabsRadioButton->setChecked(GeneralSettings::rememberOpenedTabs());
257 m_homeUrlRadioButton->setChecked(!GeneralSettings::rememberOpenedTabs());
258 m_splitView->setChecked(GeneralSettings::splitView());
259 m_filterBar->setChecked(GeneralSettings::filterBar());
260 m_showFullPathInTitlebar->setChecked(GeneralSettings::showFullPathInTitlebar());
261 m_openExternallyCalledFolderInNewTab->setChecked(GeneralSettings::openExternallyCalledFolderInNewTab());
262
263 m_useTabForSplitViewSwitch->setChecked(GeneralSettings::useTabForSwitchingSplitView());
264 m_closeActiveSplitView->setChecked(GeneralSettings::closeActiveSplitView());
265
266 m_openNewTabAfterLastTab->setChecked(GeneralSettings::openNewTabAfterLastTab());
267 m_openNewTabAfterCurrentTab->setChecked(!m_openNewTabAfterLastTab->isChecked());
268 }
269
270 void FoldersTabsSettingsPage::showSetDefaultDirectoryError()
271 {
272 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
273 }
274
275 #include "moc_folderstabssettingspage.cpp"