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