]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
First step of cleaning up the view properties: inheriting of viewproperties does...
[dolphin.git] / src / viewpropertiesdialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "viewpropertiesdialog.h"
22
23 #include <klocale.h>
24 #include <kiconloader.h>
25 #include <kmessagebox.h>
26 #include <assert.h>
27
28 #include <QCheckBox>
29 #include <QComboBox>
30 #include <QGridLayout>
31 #include <QGroupBox>
32 #include <QLabel>
33 #include <QRadioButton>
34 #include <QVBoxLayout>
35
36 #include "viewproperties.h"
37 #include "dolphinview.h"
38
39 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
40 KDialog(dolphinView),
41 m_isDirty(false),
42 m_dolphinView(dolphinView)
43 {
44 assert(dolphinView != 0);
45
46 setCaption(i18n("View Properties"));
47 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
48
49 const KUrl& url = dolphinView->url();
50 m_viewProps = new ViewProperties(url);
51 m_viewProps->setAutoSaveEnabled(false);
52
53 QWidget* main = new QWidget();
54 QVBoxLayout* topLayout = new QVBoxLayout();
55
56 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
57 QGroupBox* propsBox = new QGroupBox(i18n("Properties"), main);
58
59 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
60 m_viewMode = new QComboBox(propsBox);
61 m_viewMode->addItem(SmallIcon("view_icon"), i18n("Icons"));
62 m_viewMode->addItem(SmallIcon("view_text"), i18n("Details"));
63 m_viewMode->addItem(SmallIcon("gvdirpart"), i18n("Previews"));
64 const int index = static_cast<int>(m_viewProps->viewMode());
65 m_viewMode->setCurrentIndex(index);
66
67 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
68 m_sorting = new QComboBox(propsBox);
69 m_sorting->addItem("By Name");
70 m_sorting->addItem("By Size");
71 m_sorting->addItem("By Date");
72 int sortingIdx = 0;
73 switch (m_viewProps->sorting()) {
74 case DolphinView::SortByName: sortingIdx = 0; break;
75 case DolphinView::SortBySize: sortingIdx = 1; break;
76 case DolphinView::SortByDate: sortingIdx = 2; break;
77 default: break;
78 }
79 m_sorting->setCurrentIndex(sortingIdx);
80
81 QLabel* sortOrderLabel = new QLabel(i18n("Sort order:"), propsBox);
82 m_sortOrder = new QComboBox(propsBox);
83 m_sortOrder->addItem(i18n("Ascending"));
84 m_sortOrder->addItem(i18n("Descending"));
85 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::Ascending) ? 0 : 1;
86 m_sortOrder->setCurrentIndex(sortOrderIdx);
87
88 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
89 m_showHiddenFiles->setChecked(m_viewProps->isShowHiddenFilesEnabled());
90
91 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
92 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
93 propsBoxLayout->addWidget(m_viewMode, 0, 1);
94 propsBoxLayout->addWidget(m_sorting, 1, 1);
95 propsBoxLayout->addWidget(sortingLabel, 1, 0);
96 propsBoxLayout->addWidget(m_sorting, 1, 1);
97 propsBoxLayout->addWidget(sortOrderLabel, 2, 0);
98 propsBoxLayout->addWidget(m_sortOrder, 2, 1);
99 propsBoxLayout->addWidget(m_showHiddenFiles, 3, 0);
100
101 // create 'Apply view properties to:' group
102 QGroupBox* buttonBox = new QGroupBox(i18n("Apply view properties to:"), main);
103 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), buttonBox);
104 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), buttonBox);
105 m_applyToAllFolders = new QRadioButton(i18n("All folders"), buttonBox);
106
107 QVBoxLayout* buttonBoxLayout = new QVBoxLayout();
108 buttonBoxLayout->addWidget(m_applyToCurrentFolder);
109 buttonBoxLayout->addWidget(m_applyToSubFolders);
110 buttonBoxLayout->addWidget(m_applyToAllFolders);
111 buttonBox->setLayout(buttonBoxLayout);
112
113 m_applyToCurrentFolder->setChecked(true);
114
115 topLayout->addWidget(propsBox);
116 topLayout->addWidget(buttonBox);
117
118 connect(m_viewMode, SIGNAL(activated(int)),
119 this, SLOT(slotViewModeChanged(int)));
120 connect(m_sorting, SIGNAL(activated(int)),
121 this, SLOT(slotSortingChanged(int)));
122 connect(m_sortOrder, SIGNAL(activated(int)),
123 this, SLOT(slotSortOrderChanged(int)));
124 connect(m_showHiddenFiles, SIGNAL(clicked()),
125 this, SLOT(slotShowHiddenFilesChanged()));
126 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
127 this, SLOT(slotApplyToCurrentFolder()));
128 connect(m_applyToSubFolders, SIGNAL(clicked()),
129 this, SLOT(slotApplyToSubFolders()));
130 connect(m_applyToAllFolders, SIGNAL(clicked()),
131 this, SLOT(slotApplyToAllFolders()));
132
133 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
134 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
135
136 main->setLayout(topLayout);
137 setMainWidget(main);
138 }
139
140 ViewPropertiesDialog::~ViewPropertiesDialog()
141 {
142 m_isDirty = false;
143 delete m_viewProps;
144 m_viewProps = 0;
145 }
146
147 void ViewPropertiesDialog::slotOk()
148 {
149 applyViewProperties();
150 accept();
151 }
152
153 void ViewPropertiesDialog::slotApply()
154 {
155 applyViewProperties();
156 }
157
158 void ViewPropertiesDialog::slotViewModeChanged(int index)
159 {
160 assert((index >= 0) && (index <= 2));
161 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
162 m_isDirty = true;
163 }
164
165 void ViewPropertiesDialog::slotSortingChanged(int index)
166 {
167 DolphinView::Sorting sorting = DolphinView::SortByName;
168 switch (index) {
169 case 1: sorting = DolphinView::SortBySize; break;
170 case 2: sorting = DolphinView::SortByDate; break;
171 default: break;
172 }
173 m_viewProps->setSorting(sorting);
174 m_isDirty = true;
175 }
176
177 void ViewPropertiesDialog::slotSortOrderChanged(int index)
178 {
179 Qt::SortOrder sortOrder = (index == 0) ? Qt::Ascending : Qt::Descending;
180 m_viewProps->setSortOrder(sortOrder);
181 m_isDirty = true;
182 }
183
184 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
185 {
186 const bool show = m_showHiddenFiles->isChecked();
187 m_viewProps->setShowHiddenFilesEnabled(show);
188 m_isDirty = true;
189 }
190
191 void ViewPropertiesDialog::slotApplyToCurrentFolder()
192 {
193 m_isDirty = true;
194 }
195
196 void ViewPropertiesDialog::slotApplyToSubFolders()
197 {
198 //m_viewProps->setValidForSubDirs(true);
199 m_isDirty = true;
200 }
201
202 void ViewPropertiesDialog::slotApplyToAllFolders()
203 {
204 m_isDirty = true;
205 }
206
207 void ViewPropertiesDialog::applyViewProperties()
208 {
209 if (m_applyToAllFolders->isChecked()) {
210 if (m_isDirty) {
211 const QString text(i18n("The view properties of all folders will be replaced. Do you want to continue?"));
212 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
213 return;
214 }
215 }
216
217 ViewProperties props(QDir::homePath());
218 props.setViewMode(m_viewProps->viewMode());
219 props.setSorting(m_viewProps->sorting());
220 props.setSortOrder(m_viewProps->sortOrder());
221 props.setShowHiddenFilesEnabled(m_viewProps->isShowHiddenFilesEnabled());
222 //props.setValidForSubDirs(true);
223 }
224 else if (m_applyToSubFolders->isChecked() && m_isDirty) {
225 const QString text(i18n("The view properties of all sub folders will be replaced. Do you want to continue?"));
226 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
227 return;
228 }
229 }
230
231 m_viewProps->save();
232 m_dolphinView->setViewProperties(*m_viewProps);
233 m_isDirty = false;
234 }
235
236 #include "viewpropertiesdialog.moc"