]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/startup/startupsettingspage.cpp
Merge branch 'release/20.08' into master
[dolphin.git] / src / settings / startup / startupsettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2008 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "startupsettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphinmainwindow.h"
11 #include "dolphinviewcontainer.h"
12 #include "global.h"
13
14 #include <KLocalizedString>
15 #include <KMessageBox>
16
17 #include <QButtonGroup>
18 #include <QCheckBox>
19 #include <QFileDialog>
20 #include <QLineEdit>
21 #include <QPushButton>
22 #include <QRadioButton>
23 #include <QFormLayout>
24 #include <QGridLayout>
25 #include <QHBoxLayout>
26
27 StartupSettingsPage::StartupSettingsPage(const QUrl& url, QWidget* parent) :
28 SettingsPageBase(parent),
29 m_url(url),
30 m_homeUrl(nullptr),
31 m_homeUrlBoxLayoutContainer(nullptr),
32 m_buttonBoxLayoutContainer(nullptr),
33 m_rememberOpenedTabsRadioButton(nullptr),
34 m_homeUrlRadioButton(nullptr),
35 m_splitView(nullptr),
36 m_editableUrl(nullptr),
37 m_showFullPath(nullptr),
38 m_filterBar(nullptr),
39 m_showFullPathInTitlebar(nullptr),
40 m_openExternallyCalledFolderInNewTab(nullptr)
41 {
42 QFormLayout* topLayout = new QFormLayout(this);
43
44 m_rememberOpenedTabsRadioButton = new QRadioButton(i18nc("@option:radio Startup Settings", "Folders, tabs, and window state from last time"));
45 m_homeUrlRadioButton = new QRadioButton();
46 // HACK: otherwise the radio button has too much spacing in a grid layout
47 m_homeUrlRadioButton->setMaximumWidth(24);
48
49 QButtonGroup* initialViewGroup = new QButtonGroup(this);
50 initialViewGroup->addButton(m_rememberOpenedTabsRadioButton);
51 initialViewGroup->addButton(m_homeUrlRadioButton);
52
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,
71 this, &StartupSettingsPage::selectHomeUrl);
72
73 m_buttonBoxLayoutContainer = new QWidget(this);
74 QHBoxLayout* buttonBoxLayout = new QHBoxLayout(m_buttonBoxLayoutContainer);
75 buttonBoxLayout->setContentsMargins(0, 0, 0, 0);
76
77 QPushButton* useCurrentButton = new QPushButton(i18nc("@action:button", "Use Current Location"));
78 buttonBoxLayout->addWidget(useCurrentButton);
79 connect(useCurrentButton, &QPushButton::clicked,
80 this, &StartupSettingsPage::useCurrentLocation);
81 QPushButton* useDefaultButton = new QPushButton(i18nc("@action:button", "Use Default Location"));
82 buttonBoxLayout->addWidget(useDefaultButton);
83 connect(useDefaultButton, &QPushButton::clicked,
84 this, &StartupSettingsPage::useDefaultLocation);
85
86 QGridLayout* startInLocationLayout = new QGridLayout();
87 startInLocationLayout->setHorizontalSpacing(0);
88 startInLocationLayout->setContentsMargins(0, 0, 0, 0);
89 startInLocationLayout->addWidget(m_homeUrlRadioButton, 0, 0);
90 startInLocationLayout->addWidget(m_homeUrlBoxLayoutContainer, 0, 1);
91 startInLocationLayout->addWidget(m_buttonBoxLayoutContainer, 1, 1);
92
93 topLayout->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton);
94 topLayout->addRow(QString(), startInLocationLayout);
95
96
97 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
98
99 m_splitView = new QCheckBox(i18nc("@option:check Startup Settings", "Begin in split view mode"));
100 topLayout->addRow(i18n("New windows:"), m_splitView);
101 m_filterBar = new QCheckBox(i18nc("@option:check Startup Settings", "Show filter bar"));
102 topLayout->addRow(QString(), m_filterBar);
103 m_editableUrl = new QCheckBox(i18nc("@option:check Startup Settings", "Make location bar editable"));
104 topLayout->addRow(QString(), m_editableUrl);
105
106 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
107
108 m_openExternallyCalledFolderInNewTab = new QCheckBox(i18nc("@option:check Startup Settings", "Open new folders in tabs"));
109 topLayout->addRow(i18nc("@label:checkbox", "General:"), m_openExternallyCalledFolderInNewTab);
110 m_showFullPath = new QCheckBox(i18nc("@option:check Startup Settings", "Show full path inside location bar"));
111 topLayout->addRow(QString(), m_showFullPath);
112 m_showFullPathInTitlebar = new QCheckBox(i18nc("@option:check Startup Settings", "Show full path in title bar"));
113 topLayout->addRow(QString(), m_showFullPathInTitlebar);
114
115 loadSettings();
116
117 updateInitialViewOptions();
118
119 connect(m_homeUrl, &QLineEdit::textChanged, this, &StartupSettingsPage::slotSettingsChanged);
120 connect(m_rememberOpenedTabsRadioButton, &QRadioButton::toggled, this, &StartupSettingsPage::slotSettingsChanged);
121 connect(m_homeUrlRadioButton, &QRadioButton::toggled, this, &StartupSettingsPage::slotSettingsChanged);
122
123 connect(m_splitView, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
124 connect(m_editableUrl, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
125 connect(m_filterBar, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
126
127 connect(m_openExternallyCalledFolderInNewTab, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
128 connect(m_showFullPath, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
129 connect(m_showFullPathInTitlebar, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
130 }
131
132 StartupSettingsPage::~StartupSettingsPage()
133 {
134 }
135
136 void StartupSettingsPage::applySettings()
137 {
138 GeneralSettings* settings = GeneralSettings::self();
139
140 const QUrl url(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
141 KFileItem fileItem(url);
142 if ((url.isValid() && fileItem.isDir()) || (url.scheme() == QLatin1String("timeline"))) {
143 settings->setHomeUrl(url.toDisplayString(QUrl::PreferLocalFile));
144 } else {
145 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
146 }
147
148 // Remove saved state if "remember open tabs" has been turned off
149 if (!m_rememberOpenedTabsRadioButton->isChecked()) {
150 KConfigGroup windowState{KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "WindowState"};
151 if (windowState.exists()) {
152 windowState.deleteGroup();
153 }
154 }
155
156 settings->setRememberOpenedTabs(m_rememberOpenedTabsRadioButton->isChecked());
157 settings->setSplitView(m_splitView->isChecked());
158 settings->setEditableUrl(m_editableUrl->isChecked());
159 settings->setFilterBar(m_filterBar->isChecked());
160 settings->setOpenExternallyCalledFolderInNewTab(m_openExternallyCalledFolderInNewTab->isChecked());
161 settings->setShowFullPath(m_showFullPath->isChecked());
162 settings->setShowFullPathInTitlebar(m_showFullPathInTitlebar->isChecked());
163 settings->save();
164 }
165
166 void StartupSettingsPage::restoreDefaults()
167 {
168 GeneralSettings* settings = GeneralSettings::self();
169 settings->useDefaults(true);
170 loadSettings();
171 settings->useDefaults(false);
172 }
173
174 void StartupSettingsPage::slotSettingsChanged()
175 {
176 // Provide a hint that the startup settings have been changed. This allows the views
177 // to apply the startup settings only if they have been explicitly changed by the user
178 // (see bug #254947).
179 GeneralSettings::setModifiedStartupSettings(true);
180
181 // Enable and disable home URL controls appropriately
182 updateInitialViewOptions();
183 emit changed();
184 }
185
186 void StartupSettingsPage::updateInitialViewOptions()
187 {
188 m_homeUrlBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
189 m_buttonBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
190 }
191
192 void StartupSettingsPage::selectHomeUrl()
193 {
194 const QUrl homeUrl(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
195 QUrl url = QFileDialog::getExistingDirectoryUrl(this, QString(), homeUrl);
196 if (!url.isEmpty()) {
197 m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
198 slotSettingsChanged();
199 }
200 }
201
202 void StartupSettingsPage::useCurrentLocation()
203 {
204 m_homeUrl->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
205 }
206
207 void StartupSettingsPage::useDefaultLocation()
208 {
209 m_homeUrl->setText(QDir::homePath());
210 }
211
212 void StartupSettingsPage::loadSettings()
213 {
214 const QUrl url(Dolphin::homeUrl());
215 m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
216 m_rememberOpenedTabsRadioButton->setChecked(GeneralSettings::rememberOpenedTabs());
217 m_homeUrlRadioButton->setChecked(!GeneralSettings::rememberOpenedTabs());
218 m_splitView->setChecked(GeneralSettings::splitView());
219 m_editableUrl->setChecked(GeneralSettings::editableUrl());
220 m_showFullPath->setChecked(GeneralSettings::showFullPath());
221 m_filterBar->setChecked(GeneralSettings::filterBar());
222 m_showFullPathInTitlebar->setChecked(GeneralSettings::showFullPathInTitlebar());
223 m_openExternallyCalledFolderInNewTab->setChecked(GeneralSettings::openExternallyCalledFolderInNewTab());
224 }