]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
Qt::Ascending -> Qt::AscendingOrder
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "viewpropertiesdialog.h"
22 #include "viewpropsprogressinfo.h"
23 #include "dolphinview.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphin_generalsettings.h"
27 #include "viewproperties.h"
28
29 #include <kcomponentdata.h>
30 #include <klocale.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
33 #include <kmessagebox.h>
34 #include <kstandarddirs.h>
35 #include <kurl.h>
36
37 #include <QButtonGroup>
38 #include <QCheckBox>
39 #include <QComboBox>
40 #include <QGridLayout>
41 #include <QGroupBox>
42 #include <QLabel>
43 #include <QRadioButton>
44 #include <QVBoxLayout>
45
46 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
47 KDialog(dolphinView),
48 m_isDirty(false),
49 m_dolphinView(dolphinView),
50 m_viewProps(0),
51 m_viewMode(0),
52 m_sorting(0),
53 m_sortOrder(0),
54 m_additionalInfo(0),
55 m_showPreview(0),
56 m_showHiddenFiles(0),
57 m_applyToCurrentFolder(0),
58 m_applyToSubFolders(0),
59 m_useAsDefault(0)
60 {
61 Q_ASSERT(dolphinView != 0);
62 const bool useGlobalViewProps = DolphinSettings::instance().generalSettings()->globalViewProps();
63
64 setCaption(i18n("View Properties"));
65 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
66
67 const KUrl& url = dolphinView->url();
68 m_viewProps = new ViewProperties(url);
69 m_viewProps->setAutoSaveEnabled(false);
70
71 QWidget* main = new QWidget();
72 QVBoxLayout* topLayout = new QVBoxLayout();
73
74 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
75 QWidget* propsBox = main;
76 if (!useGlobalViewProps) {
77 propsBox = new QGroupBox(i18n("Properties"), main);
78 }
79
80 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
81 m_viewMode = new QComboBox(propsBox);
82 m_viewMode->addItem(KIcon("view-icon"), i18n("Icons"));
83 m_viewMode->addItem(KIcon("fileview-text"), i18n("Details"));
84 m_viewMode->addItem(KIcon("view-tree"), i18n("Column"));
85 const int index = static_cast<int>(m_viewProps->viewMode());
86 m_viewMode->setCurrentIndex(index);
87
88 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
89 QWidget* sortingBox = new QWidget(propsBox);
90
91 m_sortOrder = new QComboBox(sortingBox);
92 m_sortOrder->addItem(i18n("Ascending"));
93 m_sortOrder->addItem(i18n("Descending"));
94 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
95 m_sortOrder->setCurrentIndex(sortOrderIdx);
96
97 m_sorting = new QComboBox(sortingBox);
98 m_sorting->addItem("By Name");
99 m_sorting->addItem("By Size");
100 m_sorting->addItem("By Date");
101 m_sorting->addItem("By Permissions");
102 m_sorting->addItem("By Owner");
103 m_sorting->addItem("By Group");
104 m_sorting->setCurrentIndex(m_viewProps->sorting());
105
106 QHBoxLayout* sortingLayout = new QHBoxLayout();
107 sortingLayout->setMargin(0);
108 sortingLayout->addWidget(m_sortOrder);
109 sortingLayout->addWidget(m_sorting);
110 sortingBox->setLayout(sortingLayout);
111
112 QLabel* additionalInfoLabel = new QLabel(i18n("Additional information:"), propsBox);
113 m_additionalInfo = new QComboBox(propsBox);
114 m_additionalInfo->addItem(i18n("No Information"), KFileItemDelegate::NoInformation);
115 m_additionalInfo->addItem(i18n("Type"), KFileItemDelegate::FriendlyMimeType);
116 m_additionalInfo->addItem(i18n("Size"), KFileItemDelegate::Size);
117 m_additionalInfo->addItem(i18n("Date"), KFileItemDelegate::ModificationTime);
118 const int addInfoIndex = m_additionalInfo->findData(m_viewProps->additionalInfo());
119 m_additionalInfo->setCurrentIndex(addInfoIndex);
120 m_additionalInfo->setEnabled(m_viewProps->viewMode() == DolphinView::IconsView);
121
122 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
123 m_showPreview->setChecked(m_viewProps->showPreview());
124
125 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
126 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
127
128 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
129 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
130 propsBoxLayout->addWidget(m_viewMode, 0, 1);
131 propsBoxLayout->addWidget(sortingLabel, 1, 0);
132 propsBoxLayout->addWidget(sortingBox, 1, 1);
133 propsBoxLayout->addWidget(additionalInfoLabel, 2, 0);
134 propsBoxLayout->addWidget(m_additionalInfo, 2, 1);
135 propsBoxLayout->addWidget(m_showPreview, 3, 0);
136 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
137
138 topLayout->addWidget(propsBox);
139
140 connect(m_viewMode, SIGNAL(activated(int)),
141 this, SLOT(slotViewModeChanged(int)));
142 connect(m_sorting, SIGNAL(activated(int)),
143 this, SLOT(slotSortingChanged(int)));
144 connect(m_sortOrder, SIGNAL(activated(int)),
145 this, SLOT(slotSortOrderChanged(int)));
146 connect(m_additionalInfo, SIGNAL(activated(int)),
147 this, SLOT(slotAdditionalInfoChanged(int)));
148 connect(m_showPreview, SIGNAL(clicked()),
149 this, SLOT(slotShowPreviewChanged()));
150 connect(m_showHiddenFiles, SIGNAL(clicked()),
151 this, SLOT(slotShowHiddenFilesChanged()));
152
153 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
154 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
155
156 // Only show the following settings if the view properties are remembered
157 // for each directory:
158 if (!useGlobalViewProps) {
159 // create 'Apply view properties to:' group
160 QGroupBox* applyBox = new QGroupBox(i18n("Apply view properties to:"), main);
161
162 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), applyBox);
163 m_applyToCurrentFolder->setChecked(true);
164 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), applyBox);
165 m_applyToAllFolders = new QRadioButton(i18n("All folders"),applyBox);
166
167 QButtonGroup* applyGroup = new QButtonGroup(this);
168 applyGroup->addButton(m_applyToCurrentFolder);
169 applyGroup->addButton(m_applyToSubFolders);
170 applyGroup->addButton(m_applyToAllFolders);
171
172 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
173 applyBoxLayout->addWidget(m_applyToCurrentFolder);
174 applyBoxLayout->addWidget(m_applyToSubFolders);
175 applyBoxLayout->addWidget(m_applyToAllFolders);
176
177 m_useAsDefault = new QCheckBox(i18n("Use as default for new folders"), main);
178
179 topLayout->addWidget(applyBox);
180 topLayout->addWidget(m_useAsDefault);
181
182 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
183 this, SLOT(markAsDirty()));
184 connect(m_applyToSubFolders, SIGNAL(clicked()),
185 this, SLOT(markAsDirty()));
186 connect(m_applyToAllFolders, SIGNAL(clicked()),
187 this, SLOT(markAsDirty()));
188 connect(m_useAsDefault, SIGNAL(clicked()),
189 this, SLOT(markAsDirty()));
190 }
191
192 main->setLayout(topLayout);
193 setMainWidget(main);
194 }
195
196 ViewPropertiesDialog::~ViewPropertiesDialog()
197 {
198 m_isDirty = false;
199 delete m_viewProps;
200 m_viewProps = 0;
201 }
202
203 void ViewPropertiesDialog::slotOk()
204 {
205 applyViewProperties();
206 accept();
207 }
208
209 void ViewPropertiesDialog::slotApply()
210 {
211 applyViewProperties();
212 }
213
214 void ViewPropertiesDialog::slotViewModeChanged(int index)
215 {
216 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
217 m_isDirty = true;
218
219 m_additionalInfo->setEnabled(m_viewProps->viewMode() == DolphinView::IconsView);
220 }
221
222 void ViewPropertiesDialog::slotSortingChanged(int index)
223 {
224 const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
225 m_viewProps->setSorting(sorting);
226 m_isDirty = true;
227 }
228
229 void ViewPropertiesDialog::slotSortOrderChanged(int index)
230 {
231 Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
232 m_viewProps->setSortOrder(sortOrder);
233 m_isDirty = true;
234 }
235
236 void ViewPropertiesDialog::slotAdditionalInfoChanged(int index)
237 {
238 KFileItemDelegate::AdditionalInformation info = KFileItemDelegate::NoInformation;
239 switch (index) {
240 case 1: info = KFileItemDelegate::FriendlyMimeType; break;
241 case 2: info = KFileItemDelegate::Size; break;
242 case 3: info = KFileItemDelegate::ModificationTime; break;
243 default: break;
244 }
245 m_viewProps->setAdditionalInfo(info);
246 m_isDirty = true;
247 }
248
249 void ViewPropertiesDialog::slotShowPreviewChanged()
250 {
251 const bool show = m_showPreview->isChecked();
252 m_viewProps->setShowPreview(show);
253 m_isDirty = true;
254 }
255
256 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
257 {
258 const bool show = m_showHiddenFiles->isChecked();
259 m_viewProps->setShowHiddenFiles(show);
260 m_isDirty = true;
261 }
262
263 void ViewPropertiesDialog::markAsDirty()
264 {
265 m_isDirty = true;
266 }
267
268 void ViewPropertiesDialog::applyViewProperties()
269 {
270 const bool applyToSubFolders = m_isDirty &&
271 (m_applyToSubFolders != 0) &&
272 m_applyToSubFolders->isChecked();
273 if (applyToSubFolders) {
274 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
275 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
276 return;
277 }
278
279 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
280 m_dolphinView->url(),
281 *m_viewProps);
282 info->setWindowModality(Qt::NonModal);
283 info->show();
284 }
285
286 const bool applyToAllFolders = m_isDirty &&
287 (m_applyToAllFolders != 0) &&
288 m_applyToAllFolders->isChecked();
289 if (applyToAllFolders) {
290 const QString text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
291 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
292 return;
293 }
294
295 // Updating the global view properties time stamp in the general settings makes
296 // all existing viewproperties invalid, as they have a smaller time stamp.
297 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
298 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
299
300 // This is also a good chance to make a cleanup of all mirrored view properties:
301 const KUrl mirroredDir = ViewProperties::mirroredDirectory();
302 KIO::NetAccess::del(mirroredDir, this);
303 }
304
305 m_viewProps->save();
306
307 m_dolphinView->setMode(m_viewProps->viewMode());
308 m_dolphinView->setSorting(m_viewProps->sorting());
309 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
310 m_dolphinView->setAdditionalInfo(m_viewProps->additionalInfo());
311 m_dolphinView->setShowPreview(m_viewProps->showPreview());
312 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
313
314 m_isDirty = false;
315
316 if (m_useAsDefault->isChecked()) {
317 // For directories where no .directory file is available, the .directory
318 // file stored for the global view properties is used as fallback. To update
319 // this file we temporary turn on the global view properties mode.
320 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
321 Q_ASSERT(!settings->globalViewProps());
322
323 settings->setGlobalViewProps(true);
324 ViewProperties defaultProps(m_dolphinView->url());
325 defaultProps.setDirProperties(*m_viewProps);
326 defaultProps.save();
327 settings->setGlobalViewProps(false);
328 }
329 }
330
331 #include "viewpropertiesdialog.moc"