2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "folderstabssettingspage.h"
8 #include "dolphinmainwindow.h"
9 #include "dolphinviewcontainer.h"
12 #include <KLocalizedString>
13 #include <KMessageBox>
14 #include <KProtocolManager>
16 #include <QButtonGroup>
18 #include <QFileDialog>
19 #include <QFormLayout>
20 #include <QGridLayout>
21 #include <QHBoxLayout>
24 #include <QPushButton>
25 #include <QRadioButton>
26 #include <QSpacerItem>
28 FoldersTabsSettingsPage::FoldersTabsSettingsPage(QWidget
*parent
)
29 : SettingsPageBase(parent
)
30 , m_homeUrlBoxLayoutContainer(nullptr)
31 , m_buttonBoxLayoutContainer(nullptr)
32 , m_homeUrlRadioButton(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)
43 QFormLayout
*topLayout
= new QFormLayout(this);
46 m_rememberOpenedTabsRadioButton
= new QRadioButton(i18nc("@option:radio Show on startup", "Folders, tabs, and window state from last time"), this);
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);
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
);
61 QPushButton
*selectHomeUrlButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
62 homeUrlBoxLayout
->addWidget(selectHomeUrlButton
);
64 #ifndef QT_NO_ACCESSIBILITY
65 selectHomeUrlButton
->setAccessibleName(i18nc("@action:button", "Select Home Location"));
68 connect(selectHomeUrlButton
, &QPushButton::clicked
, this, &FoldersTabsSettingsPage::selectHomeUrl
);
70 m_buttonBoxLayoutContainer
= new QWidget(this);
71 QHBoxLayout
*buttonBoxLayout
= new QHBoxLayout(m_buttonBoxLayoutContainer
);
72 buttonBoxLayout
->setContentsMargins(0, 0, 0, 0);
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
);
81 QButtonGroup
*initialViewGroup
= new QButtonGroup(this);
82 initialViewGroup
->addButton(m_rememberOpenedTabsRadioButton
);
83 initialViewGroup
->addButton(m_homeUrlRadioButton
);
85 topLayout
->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton
);
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);
94 topLayout
->addRow(QString(), startInLocationLayout
);
96 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
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
);
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
);
110 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
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
);
122 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
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
);
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."));
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
);
144 updateInitialViewOptions();
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
);
150 connect(m_splitView
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
151 connect(m_filterBar
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
153 connect(m_openExternallyCalledFolderInNewTab
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
154 connect(m_showFullPathInTitlebar
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
156 connect(m_useTabForSplitViewSwitch
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::changed
);
157 connect(m_closeActiveSplitView
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::changed
);
159 connect(m_openNewTabAfterCurrentTab
, &QRadioButton::toggled
, this, &FoldersTabsSettingsPage::changed
);
160 connect(m_openNewTabAfterLastTab
, &QRadioButton::toggled
, this, &FoldersTabsSettingsPage::changed
);
163 FoldersTabsSettingsPage::~FoldersTabsSettingsPage()
167 void FoldersTabsSettingsPage::applySettings()
169 GeneralSettings
*settings
= GeneralSettings::self();
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
));
180 showSetDefaultDirectoryError();
184 showSetDefaultDirectoryError();
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();
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());
201 settings
->setOpenNewTabAfterLastTab(m_openNewTabAfterLastTab
->isChecked());
206 void FoldersTabsSettingsPage::restoreDefaults()
208 GeneralSettings
*settings
= GeneralSettings::self();
209 settings
->useDefaults(true);
211 settings
->useDefaults(false);
214 void FoldersTabsSettingsPage::slotSettingsChanged()
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);
221 // Enable and disable home URL controls appropriately
222 updateInitialViewOptions();
226 void FoldersTabsSettingsPage::updateInitialViewOptions()
228 m_homeUrlBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
229 m_buttonBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
232 void FoldersTabsSettingsPage::selectHomeUrl()
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();
242 void FoldersTabsSettingsPage::useCurrentLocation()
244 m_homeUrl
->setText(m_url
.toDisplayString(QUrl::PreferLocalFile
));
247 void FoldersTabsSettingsPage::useDefaultLocation()
249 m_homeUrl
->setText(QDir::homePath());
252 void FoldersTabsSettingsPage::loadSettings()
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());
263 m_useTabForSplitViewSwitch
->setChecked(GeneralSettings::useTabForSwitchingSplitView());
264 m_closeActiveSplitView
->setChecked(GeneralSettings::closeActiveSplitView());
266 m_openNewTabAfterLastTab
->setChecked(GeneralSettings::openNewTabAfterLastTab());
267 m_openNewTabAfterCurrentTab
->setChecked(!m_openNewTabAfterLastTab
->isChecked());
270 void FoldersTabsSettingsPage::showSetDefaultDirectoryError()
272 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
275 #include "moc_folderstabssettingspage.cpp"