]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
Let the user chose whether view properties should be remembered for each directory...
[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 #include "viewpropsprogressinfo.h"
23
24 #include <klocale.h>
25 #include <kiconloader.h>
26 #include <kmessagebox.h>
27 #include <assert.h>
28
29 #include <QCheckBox>
30 #include <QComboBox>
31 #include <QGridLayout>
32 #include <QGroupBox>
33 #include <QLabel>
34 #include <QRadioButton>
35 #include <QVBoxLayout>
36
37 #include "viewproperties.h"
38 #include "dolphinview.h"
39
40 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
41 KDialog(dolphinView),
42 m_isDirty(false),
43 m_dolphinView(dolphinView),
44 m_viewProps(0),
45 m_viewMode(0),
46 m_sorting(0),
47 m_sortOrder(0),
48 m_showPreview(0),
49 m_showHiddenFiles(0),
50 m_applyToSubFolders(0),
51 m_useAsDefault(0)
52 {
53 assert(dolphinView != 0);
54
55 setCaption(i18n("View Properties"));
56 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
57
58 const KUrl& url = dolphinView->url();
59 m_viewProps = new ViewProperties(url);
60 m_viewProps->setAutoSaveEnabled(false);
61
62 QWidget* main = new QWidget();
63 QVBoxLayout* topLayout = new QVBoxLayout();
64
65 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
66 QGroupBox* propsBox = new QGroupBox(i18n("Properties"), main);
67
68 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
69 m_viewMode = new QComboBox(propsBox);
70 m_viewMode->addItem(SmallIcon("view_icon"), i18n("Icons"));
71 m_viewMode->addItem(SmallIcon("view_text"), i18n("Details"));
72 const int index = static_cast<int>(m_viewProps->viewMode());
73 m_viewMode->setCurrentIndex(index);
74
75 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
76 m_sorting = new QComboBox(propsBox);
77 m_sorting->addItem("By Name");
78 m_sorting->addItem("By Size");
79 m_sorting->addItem("By Date");
80 int sortingIdx = 0;
81 switch (m_viewProps->sorting()) {
82 case DolphinView::SortByName: sortingIdx = 0; break;
83 case DolphinView::SortBySize: sortingIdx = 1; break;
84 case DolphinView::SortByDate: sortingIdx = 2; break;
85 default: break;
86 }
87 m_sorting->setCurrentIndex(sortingIdx);
88
89 QLabel* sortOrderLabel = new QLabel(i18n("Sort order:"), propsBox);
90 m_sortOrder = new QComboBox(propsBox);
91 m_sortOrder->addItem(i18n("Ascending"));
92 m_sortOrder->addItem(i18n("Descending"));
93 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::Ascending) ? 0 : 1;
94 m_sortOrder->setCurrentIndex(sortOrderIdx);
95
96 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
97 m_showPreview->setChecked(m_viewProps->showPreview());
98
99 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
100 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
101
102 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
103 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
104 propsBoxLayout->addWidget(m_viewMode, 0, 1);
105 propsBoxLayout->addWidget(m_sorting, 1, 1);
106 propsBoxLayout->addWidget(sortingLabel, 1, 0);
107 propsBoxLayout->addWidget(m_sorting, 1, 1);
108 propsBoxLayout->addWidget(sortOrderLabel, 2, 0);
109 propsBoxLayout->addWidget(m_sortOrder, 2, 1);
110 propsBoxLayout->addWidget(m_showPreview, 3, 0);
111 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
112
113 m_applyToSubFolders = new QCheckBox(i18n("Apply changes to all sub folders"), main);
114 m_useAsDefault = new QCheckBox(i18n("Use as default"), main);
115
116 topLayout->addWidget(propsBox);
117 topLayout->addWidget(m_applyToSubFolders);
118 topLayout->addWidget(m_useAsDefault);
119
120 connect(m_viewMode, SIGNAL(activated(int)),
121 this, SLOT(slotViewModeChanged(int)));
122 connect(m_sorting, SIGNAL(activated(int)),
123 this, SLOT(slotSortingChanged(int)));
124 connect(m_sortOrder, SIGNAL(activated(int)),
125 this, SLOT(slotSortOrderChanged(int)));
126 connect(m_showPreview, SIGNAL(clicked()),
127 this, SLOT(slotShowPreviewChanged()));
128 connect(m_showHiddenFiles, SIGNAL(clicked()),
129 this, SLOT(slotShowHiddenFilesChanged()));
130
131 connect(m_applyToSubFolders, SIGNAL(clicked()),
132 this, SLOT(markAsDirty()));
133 connect(m_applyToSubFolders, SIGNAL(clicked()),
134 this, SLOT(markAsDirty()));
135
136 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
137 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
138
139 main->setLayout(topLayout);
140 setMainWidget(main);
141 }
142
143 ViewPropertiesDialog::~ViewPropertiesDialog()
144 {
145 m_isDirty = false;
146 delete m_viewProps;
147 m_viewProps = 0;
148 }
149
150 void ViewPropertiesDialog::slotOk()
151 {
152 applyViewProperties();
153 accept();
154 }
155
156 void ViewPropertiesDialog::slotApply()
157 {
158 applyViewProperties();
159 }
160
161 void ViewPropertiesDialog::slotViewModeChanged(int index)
162 {
163 assert((index >= 0) && (index <= 2));
164 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
165 m_isDirty = true;
166 }
167
168 void ViewPropertiesDialog::slotSortingChanged(int index)
169 {
170 DolphinView::Sorting sorting = DolphinView::SortByName;
171 switch (index) {
172 case 1: sorting = DolphinView::SortBySize; break;
173 case 2: sorting = DolphinView::SortByDate; break;
174 default: break;
175 }
176 m_viewProps->setSorting(sorting);
177 m_isDirty = true;
178 }
179
180 void ViewPropertiesDialog::slotSortOrderChanged(int index)
181 {
182 Qt::SortOrder sortOrder = (index == 0) ? Qt::Ascending : Qt::Descending;
183 m_viewProps->setSortOrder(sortOrder);
184 m_isDirty = true;
185 }
186
187 void ViewPropertiesDialog::slotShowPreviewChanged()
188 {
189 const bool show = m_showPreview->isChecked();
190 m_viewProps->setShowPreview(show);
191 m_isDirty = true;
192 }
193
194 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
195 {
196 const bool show = m_showHiddenFiles->isChecked();
197 m_viewProps->setShowHiddenFiles(show);
198 m_isDirty = true;
199 }
200
201 void ViewPropertiesDialog::markAsDirty()
202 {
203 m_isDirty = true;
204 }
205
206 void ViewPropertiesDialog::applyViewProperties()
207 {
208 if (m_applyToSubFolders->isChecked() && m_isDirty) {
209 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
210 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
211 return;
212 }
213
214 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
215 m_dolphinView->url(),
216 *m_viewProps);
217 info->setWindowModality(Qt::NonModal);
218 info->show();
219 }
220
221 m_viewProps->save();
222 m_dolphinView->setViewProperties(*m_viewProps);
223 m_isDirty = false;
224
225 // TODO: handle m_useAsDefault setting
226 }
227
228 #include "viewpropertiesdialog.moc"