]> cloud.milkyroute.net Git - dolphin.git/blob - src/startupsettingspage.cpp
increase the readability of the code
[dolphin.git] / src / startupsettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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 "dolphinsettings.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinview.h"
25 #include "dolphinviewcontainer.h"
26
27 #include "dolphin_generalsettings.h"
28
29 #include <kdialog.h>
30 #include <kfiledialog.h>
31 #include <klocale.h>
32 #include <klineedit.h>
33 #include <kmessagebox.h>
34 #include <kvbox.h>
35
36 #include <QCheckBox>
37 #include <QGroupBox>
38 #include <QLabel>
39 #include <QPushButton>
40 #include <QRadioButton>
41
42 StartupSettingsPage::StartupSettingsPage(DolphinMainWindow* mainWin, QWidget* parent) :
43 SettingsPageBase(parent),
44 m_mainWindow(mainWin),
45 m_homeUrl(0),
46 m_splitView(0),
47 m_editableUrl(0),
48 m_filterBar(0)
49 {
50 const int spacing = KDialog::spacingHint();
51
52 QVBoxLayout* topLayout = new QVBoxLayout(this);
53 KVBox* vBox = new KVBox(this);
54 vBox->setSpacing(spacing);
55
56 // create 'Home URL' editor
57 QGroupBox* homeBox = new QGroupBox(i18nc("@title:group", "Home Folder"), vBox);
58
59 KHBox* homeUrlBox = new KHBox(homeBox);
60 homeUrlBox->setSpacing(spacing);
61
62 new QLabel(i18nc("@label:textbox", "Location:"), homeUrlBox);
63 m_homeUrl = new KLineEdit(homeUrlBox);
64 m_homeUrl->setClearButtonShown(true);
65
66 QPushButton* selectHomeUrlButton = new QPushButton(KIcon("folder-open"), QString(), homeUrlBox);
67 connect(selectHomeUrlButton, SIGNAL(clicked()),
68 this, SLOT(selectHomeUrl()));
69
70 KHBox* buttonBox = new KHBox(homeBox);
71 buttonBox->setSpacing(spacing);
72
73 QPushButton* useCurrentButton = new QPushButton(i18nc("@action:button", "Use Current Location"), buttonBox);
74 connect(useCurrentButton, SIGNAL(clicked()),
75 this, SLOT(useCurrentLocation()));
76 QPushButton* useDefaultButton = new QPushButton(i18nc("@action:button", "Use Default Location"), buttonBox);
77 connect(useDefaultButton, SIGNAL(clicked()),
78 this, SLOT(useDefaultLocation()));
79
80 QVBoxLayout* homeBoxLayout = new QVBoxLayout(homeBox);
81 homeBoxLayout->addWidget(homeUrlBox);
82 homeBoxLayout->addWidget(buttonBox);
83
84 // create 'Split view', 'Editable location' and 'Filter bar' checkboxes
85 m_splitView = new QCheckBox(i18nc("@option:check Startup Settings", "Split view mode"), vBox);
86 m_editableUrl = new QCheckBox(i18nc("@option:check Startup Settings", "Editable location bar"), vBox);
87 m_filterBar = new QCheckBox(i18nc("@option:check Startup Settings", "Show filter bar"), vBox);
88 connect(m_splitView, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
89 connect(m_editableUrl, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
90 connect(m_filterBar, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
91
92 // Add a dummy widget with no restriction regarding
93 // a vertical resizing. This assures that the dialog layout
94 // is not stretched vertically.
95 new QWidget(vBox);
96
97 topLayout->addWidget(vBox);
98
99 loadSettings();
100
101 // it's important connecting 'textChanged' after loadSettings(), as loadSettings()
102 // invokes m_homeUrl->setText()
103 connect(m_homeUrl, SIGNAL(textChanged(const QString&)), this, SIGNAL(changed()));
104 }
105
106 StartupSettingsPage::~StartupSettingsPage()
107 {
108 }
109
110 void StartupSettingsPage::applySettings()
111 {
112 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
113
114 const KUrl url(m_homeUrl->text());
115 KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, url);
116 if (url.isValid() && fileItem.isDir()) {
117 settings->setHomeUrl(url.prettyUrl());
118 } else {
119 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid and will not be applied."));
120 }
121
122 settings->setSplitView(m_splitView->isChecked());
123 settings->setEditableUrl(m_editableUrl->isChecked());
124 settings->setFilterBar(m_filterBar->isChecked());
125 }
126
127 void StartupSettingsPage::restoreDefaults()
128 {
129 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
130 settings->setDefaults();
131 loadSettings();
132 }
133
134 void StartupSettingsPage::selectHomeUrl()
135 {
136 const QString homeUrl = m_homeUrl->text();
137 KUrl url = KFileDialog::getExistingDirectoryUrl(homeUrl, this);
138 if (!url.isEmpty()) {
139 m_homeUrl->setText(url.prettyUrl());
140 emit changed();
141 }
142 }
143
144 void StartupSettingsPage::useCurrentLocation()
145 {
146 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
147 m_homeUrl->setText(view->url().prettyUrl());
148 }
149
150 void StartupSettingsPage::useDefaultLocation()
151 {
152 m_homeUrl->setText(QDir::homePath());
153 }
154
155 void StartupSettingsPage::loadSettings()
156 {
157 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
158 m_homeUrl->setText(settings->homeUrl());
159 m_splitView->setChecked(settings->splitView());
160 m_editableUrl->setChecked(settings->editableUrl());
161 m_filterBar->setChecked(settings->filterBar());
162 }
163
164 #include "startupsettingspage.moc"