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