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