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