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