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