]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/startup/startupsettingspage.cpp
b216181d882e51ea83142a63354f6f1b1ad05be5
[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 "global.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinviewcontainer.h"
25
26 #include "dolphin_generalsettings.h"
27
28 #include <KLocalizedString>
29 #include <QLineEdit>
30 #include <KMessageBox>
31
32 #include <QVBoxLayout>
33 #include <QCheckBox>
34 #include <QGroupBox>
35 #include <QLabel>
36 #include <QPushButton>
37 #include <QHBoxLayout>
38 #include <QFileDialog>
39
40 #include "views/dolphinview.h"
41
42 StartupSettingsPage::StartupSettingsPage(const QUrl& url, QWidget* parent) :
43 SettingsPageBase(parent),
44 m_url(url),
45 m_homeUrl(nullptr),
46 m_splitView(nullptr),
47 m_editableUrl(nullptr),
48 m_showFullPath(nullptr),
49 m_filterBar(nullptr),
50 m_showFullPathInTitlebar(nullptr)
51 {
52 QVBoxLayout* topLayout = new QVBoxLayout(this);
53 QWidget* vBox = new QWidget(this);
54 QVBoxLayout *vBoxLayout = new QVBoxLayout(vBox);
55 vBoxLayout->setMargin(0);
56 vBoxLayout->setAlignment(Qt::AlignTop);
57
58 // create 'Home URL' editor
59 QGroupBox* homeBox = new QGroupBox(i18nc("@title:group", "Home Folder"), vBox);
60 vBoxLayout->addWidget(homeBox);
61
62 QWidget* homeUrlBox = new QWidget(homeBox);
63 QHBoxLayout *homeUrlBoxLayout = new QHBoxLayout(homeUrlBox);
64 homeUrlBoxLayout->setMargin(0);
65
66 QLabel* homeUrlLabel = new QLabel(i18nc("@label:textbox", "Location:"), homeUrlBox);
67 homeUrlBoxLayout->addWidget(homeUrlLabel);
68 m_homeUrl = new QLineEdit(homeUrlBox);
69 homeUrlBoxLayout->addWidget(m_homeUrl);
70 m_homeUrl->setClearButtonEnabled(true);
71
72 QPushButton* selectHomeUrlButton = new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString(), homeUrlBox);
73 homeUrlBoxLayout->addWidget(selectHomeUrlButton);
74
75 #ifndef QT_NO_ACCESSIBILITY
76 selectHomeUrlButton->setAccessibleName(i18nc("@action:button", "Select Home Location"));
77 #endif
78
79 connect(selectHomeUrlButton, &QPushButton::clicked,
80 this, &StartupSettingsPage::selectHomeUrl);
81
82 QWidget* buttonBox = new QWidget(homeBox);
83 QHBoxLayout *buttonBoxLayout = new QHBoxLayout(buttonBox);
84 buttonBoxLayout->setMargin(0);
85
86 QPushButton* useCurrentButton = new QPushButton(i18nc("@action:button", "Use Current Location"), buttonBox);
87 buttonBoxLayout->addWidget(useCurrentButton);
88 connect(useCurrentButton, &QPushButton::clicked,
89 this, &StartupSettingsPage::useCurrentLocation);
90 QPushButton* useDefaultButton = new QPushButton(i18nc("@action:button", "Use Default Location"), buttonBox);
91 buttonBoxLayout->addWidget(useDefaultButton);
92 connect(useDefaultButton, &QPushButton::clicked,
93 this, &StartupSettingsPage::useDefaultLocation);
94
95 QVBoxLayout* homeBoxLayout = new QVBoxLayout(homeBox);
96 homeBoxLayout->addWidget(homeUrlBox);
97 homeBoxLayout->addWidget(buttonBox);
98
99 // create 'Split view', 'Show full path', 'Editable location' and 'Filter bar' checkboxes
100 m_splitView = new QCheckBox(i18nc("@option:check Startup Settings", "Split view mode"), vBox);
101 vBoxLayout->addWidget(m_splitView);
102 m_editableUrl = new QCheckBox(i18nc("@option:check Startup Settings", "Editable location bar"), vBox);
103 vBoxLayout->addWidget(m_editableUrl);
104 m_showFullPath = new QCheckBox(i18nc("@option:check Startup Settings", "Show full path inside location bar"), vBox);
105 vBoxLayout->addWidget(m_showFullPath);
106 m_filterBar = new QCheckBox(i18nc("@option:check Startup Settings", "Show filter bar"), vBox);
107 vBoxLayout->addWidget(m_filterBar);
108 m_showFullPathInTitlebar = new QCheckBox(i18nc("@option:check Startup Settings", "Show full path in title bar"), vBox);
109 vBoxLayout->addWidget(m_showFullPathInTitlebar);
110
111 // Add a dummy widget with no restriction regarding
112 // a vertical resizing. This assures that the dialog layout
113 // is not stretched vertically.
114 new QWidget(vBox);
115
116 topLayout->addWidget(vBox);
117
118 loadSettings();
119
120 connect(m_homeUrl, &QLineEdit::textChanged, this, &StartupSettingsPage::slotSettingsChanged);
121 connect(m_splitView, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
122 connect(m_editableUrl, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
123 connect(m_showFullPath, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
124 connect(m_filterBar, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
125 connect(m_showFullPathInTitlebar, &QCheckBox::toggled, this, &StartupSettingsPage::slotSettingsChanged);
126 }
127
128 StartupSettingsPage::~StartupSettingsPage()
129 {
130 }
131
132 void StartupSettingsPage::applySettings()
133 {
134 GeneralSettings* settings = GeneralSettings::self();
135
136 const QUrl url(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
137 KFileItem fileItem(url);
138 if ((url.isValid() && fileItem.isDir()) || (url.scheme() == QLatin1String("timeline"))) {
139 settings->setHomeUrl(url.toDisplayString(QUrl::PreferLocalFile));
140 } else {
141 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
142 }
143
144 settings->setSplitView(m_splitView->isChecked());
145 settings->setEditableUrl(m_editableUrl->isChecked());
146 settings->setShowFullPath(m_showFullPath->isChecked());
147 settings->setFilterBar(m_filterBar->isChecked());
148 settings->setShowFullPathInTitlebar(m_showFullPathInTitlebar->isChecked());
149
150 settings->save();
151 }
152
153 void StartupSettingsPage::restoreDefaults()
154 {
155 GeneralSettings* settings = GeneralSettings::self();
156 settings->useDefaults(true);
157 loadSettings();
158 settings->useDefaults(false);
159 }
160
161 void StartupSettingsPage::slotSettingsChanged()
162 {
163 // Provide a hint that the startup settings have been changed. This allows the views
164 // to apply the startup settings only if they have been explicitly changed by the user
165 // (see bug #254947).
166 GeneralSettings::setModifiedStartupSettings(true);
167 emit changed();
168 }
169
170 void StartupSettingsPage::selectHomeUrl()
171 {
172 const QUrl homeUrl(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
173 QUrl url = QFileDialog::getExistingDirectoryUrl(this, QString(), homeUrl);
174 if (!url.isEmpty()) {
175 m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
176 slotSettingsChanged();
177 }
178 }
179
180 void StartupSettingsPage::useCurrentLocation()
181 {
182 m_homeUrl->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
183 }
184
185 void StartupSettingsPage::useDefaultLocation()
186 {
187 m_homeUrl->setText(QDir::homePath());
188 }
189
190 void StartupSettingsPage::loadSettings()
191 {
192 const QUrl url(Dolphin::homeUrl());
193 m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
194 m_splitView->setChecked(GeneralSettings::splitView());
195 m_editableUrl->setChecked(GeneralSettings::editableUrl());
196 m_showFullPath->setChecked(GeneralSettings::showFullPath());
197 m_filterBar->setChecked(GeneralSettings::filterBar());
198 m_showFullPathInTitlebar->setChecked(GeneralSettings::showFullPathInTitlebar());
199 }