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>
17 #include <QApplication>
19 #include <QButtonGroup>
21 #include <QFileDialog>
22 #include <QFormLayout>
23 #include <QGridLayout>
24 #include <QHBoxLayout>
27 #include <QPushButton>
28 #include <QRadioButton>
29 #include <QSpacerItem>
31 FoldersTabsSettingsPage::FoldersTabsSettingsPage(QWidget
*parent
)
32 : SettingsPageBase(parent
)
33 , m_homeUrlBoxLayoutContainer(nullptr)
34 , m_buttonBoxLayoutContainer(nullptr)
35 , m_homeUrlRadioButton(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)
46 QFormLayout
*topLayout
= new QFormLayout(this);
49 m_rememberOpenedTabsRadioButton
= new QRadioButton(i18nc("@option:radio Show on startup", "Folders, tabs, and window state from last time"), this);
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);
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
);
64 QPushButton
*selectHomeUrlButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
65 homeUrlBoxLayout
->addWidget(selectHomeUrlButton
);
67 #ifndef QT_NO_ACCESSIBILITY
68 selectHomeUrlButton
->setAccessibleName(i18nc("@action:button", "Select Home Location"));
71 connect(selectHomeUrlButton
, &QPushButton::clicked
, this, &FoldersTabsSettingsPage::selectHomeUrl
);
73 m_buttonBoxLayoutContainer
= new QWidget(this);
74 QHBoxLayout
*buttonBoxLayout
= new QHBoxLayout(m_buttonBoxLayoutContainer
);
75 buttonBoxLayout
->setContentsMargins(0, 0, 0, 0);
78 QPushButton
*useCurrentButton
= new QPushButton(i18nc("@action:button", "Use Current Location"));
79 buttonBoxLayout
->addWidget(useCurrentButton
);
80 connect(useCurrentButton
, &QPushButton::clicked
, this, &FoldersTabsSettingsPage::useCurrentLocation
);
82 QPushButton
*useDefaultButton
= new QPushButton(i18nc("@action:button", "Use Default Location"));
83 buttonBoxLayout
->addWidget(useDefaultButton
);
84 connect(useDefaultButton
, &QPushButton::clicked
, this, &FoldersTabsSettingsPage::useDefaultLocation
);
86 QButtonGroup
*initialViewGroup
= new QButtonGroup(this);
87 initialViewGroup
->addButton(m_rememberOpenedTabsRadioButton
);
88 initialViewGroup
->addButton(m_homeUrlRadioButton
);
90 topLayout
->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton
);
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);
99 topLayout
->addRow(QString(), startInLocationLayout
);
101 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
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
);
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
);
115 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
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
);
127 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
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
);
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."));
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
);
149 updateInitialViewOptions();
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
);
155 connect(m_splitView
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
156 connect(m_filterBar
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
158 connect(m_openExternallyCalledFolderInNewTab
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
159 connect(m_showFullPathInTitlebar
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::slotSettingsChanged
);
161 connect(m_useTabForSplitViewSwitch
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::changed
);
162 connect(m_closeActiveSplitView
, &QCheckBox::toggled
, this, &FoldersTabsSettingsPage::changed
);
164 connect(m_openNewTabAfterCurrentTab
, &QRadioButton::toggled
, this, &FoldersTabsSettingsPage::changed
);
165 connect(m_openNewTabAfterLastTab
, &QRadioButton::toggled
, this, &FoldersTabsSettingsPage::changed
);
168 FoldersTabsSettingsPage::~FoldersTabsSettingsPage()
172 void FoldersTabsSettingsPage::applySettings()
174 GeneralSettings
*settings
= GeneralSettings::self();
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
));
185 showSetDefaultDirectoryError();
189 showSetDefaultDirectoryError();
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();
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());
206 settings
->setOpenNewTabAfterLastTab(m_openNewTabAfterLastTab
->isChecked());
211 void FoldersTabsSettingsPage::restoreDefaults()
213 GeneralSettings
*settings
= GeneralSettings::self();
214 settings
->useDefaults(true);
216 settings
->useDefaults(false);
219 void FoldersTabsSettingsPage::slotSettingsChanged()
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);
226 // Enable and disable home URL controls appropriately
227 updateInitialViewOptions();
231 void FoldersTabsSettingsPage::updateInitialViewOptions()
233 m_homeUrlBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
234 m_buttonBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
237 void FoldersTabsSettingsPage::selectHomeUrl()
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();
247 void FoldersTabsSettingsPage::useCurrentLocation()
250 DolphinMainWindow
*mainWindow
= nullptr;
251 const auto topLevelsWidgets
= QApplication::topLevelWidgets();
252 for (const auto widget
: topLevelsWidgets
) {
253 mainWindow
= qobject_cast
<DolphinMainWindow
*>(widget
);
260 m_url
= mainWindow
->activeViewContainer()->url();
263 m_homeUrl
->setText(m_url
.toDisplayString(QUrl::PreferLocalFile
));
266 void FoldersTabsSettingsPage::useDefaultLocation()
268 m_homeUrl
->setText(QDir::homePath());
271 void FoldersTabsSettingsPage::loadSettings()
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());
282 m_useTabForSplitViewSwitch
->setChecked(GeneralSettings::useTabForSwitchingSplitView());
283 m_closeActiveSplitView
->setChecked(GeneralSettings::closeActiveSplitView());
285 m_openNewTabAfterLastTab
->setChecked(GeneralSettings::openNewTabAfterLastTab());
286 m_openNewTabAfterCurrentTab
->setChecked(!m_openNewTabAfterLastTab
->isChecked());
289 void FoldersTabsSettingsPage::showSetDefaultDirectoryError()
291 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
294 #include "moc_folderstabssettingspage.cpp"