]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
Merge branch 'KDE/4.9'
[dolphin.git] / src / settings / 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
23 #include "additionalinfodialog.h"
24 #include "kitemviews/kfileitemmodel.h"
25 #include "views/dolphinview.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphin_iconsmodesettings.h"
28 #include "viewpropsprogressinfo.h"
29
30 #include <config-nepomuk.h>
31 #ifdef HAVE_NEPOMUK
32 #include <Nepomuk2/ResourceManager>
33 #endif
34
35 #include <KComponentData>
36 #include <KLocale>
37 #include <KIconLoader>
38 #include <KIO/NetAccess>
39 #include <KMessageBox>
40 #include <KStandardDirs>
41 #include <KUrl>
42 #include <KComboBox>
43
44 #include <QAction>
45 #include <QButtonGroup>
46 #include <QCheckBox>
47 #include <QGridLayout>
48 #include <QGroupBox>
49 #include <QLabel>
50 #include <QMenu>
51 #include <QPushButton>
52 #include <QRadioButton>
53 #include <QBoxLayout>
54
55 #include <views/viewproperties.h>
56
57 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
58 KDialog(dolphinView),
59 m_isDirty(false),
60 m_dolphinView(dolphinView),
61 m_viewProps(0),
62 m_viewMode(0),
63 m_sortOrder(0),
64 m_sorting(0),
65 m_sortFoldersFirst(0),
66 m_previewsShown(0),
67 m_showInGroups(0),
68 m_showHiddenFiles(0),
69 m_additionalInfo(0),
70 m_applyToCurrentFolder(0),
71 m_applyToSubFolders(0),
72 m_applyToAllFolders(0),
73 m_useAsDefault(0)
74 {
75 Q_ASSERT(dolphinView);
76 const bool useGlobalViewProps = GeneralSettings::globalViewProps();
77
78 setCaption(i18nc("@title:window", "View Properties"));
79 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
80
81 const KUrl& url = dolphinView->url();
82 m_viewProps = new ViewProperties(url);
83 m_viewProps->setAutoSaveEnabled(false);
84
85 QWidget* main = new QWidget();
86 QVBoxLayout* topLayout = new QVBoxLayout();
87
88 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
89 QWidget* propsBox = main;
90 if (!useGlobalViewProps) {
91 propsBox = new QGroupBox(i18nc("@title:group", "Properties"), main);
92 }
93
94 QWidget* propsGrid = new QWidget();
95
96 QLabel* viewModeLabel = new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid);
97 m_viewMode = new KComboBox(propsGrid);
98 m_viewMode->addItem(KIcon("view-list-icons"), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView);
99 m_viewMode->addItem(KIcon("view-list-details"), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView);
100 m_viewMode->addItem(KIcon("view-list-tree"), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView);
101
102 QLabel* sortingLabel = new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid);
103 QWidget* sortingBox = new QWidget(propsGrid);
104
105 m_sortOrder = new KComboBox(sortingBox);
106 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
107 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Descending"));
108
109 m_sorting = new KComboBox(sortingBox);
110 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
111 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
112 m_sorting->addItem(info.translation, info.role);
113 }
114
115 m_sortFoldersFirst = new QCheckBox(i18nc("@option:check", "Show folders first"));
116 m_previewsShown = new QCheckBox(i18nc("@option:check", "Show preview"));
117 m_showInGroups = new QCheckBox(i18nc("@option:check", "Show in groups"));
118 m_showHiddenFiles = new QCheckBox(i18nc("@option:check", "Show hidden files"));
119
120 m_additionalInfo = new QPushButton(i18nc("@action:button", "Additional Information"));
121
122 QHBoxLayout* sortingLayout = new QHBoxLayout();
123 sortingLayout->setMargin(0);
124 sortingLayout->addWidget(m_sortOrder);
125 sortingLayout->addWidget(m_sorting);
126 sortingBox->setLayout(sortingLayout);
127
128 QGridLayout* propsGridLayout = new QGridLayout(propsGrid);
129 propsGridLayout->addWidget(viewModeLabel, 0, 0, Qt::AlignRight);
130 propsGridLayout->addWidget(m_viewMode, 0, 1);
131 propsGridLayout->addWidget(sortingLabel, 1, 0, Qt::AlignRight);
132 propsGridLayout->addWidget(sortingBox, 1, 1);
133
134 QVBoxLayout* propsBoxLayout = new QVBoxLayout(propsBox);
135 propsBoxLayout->addWidget(propsGrid);
136 propsBoxLayout->addWidget(m_sortFoldersFirst);
137 propsBoxLayout->addWidget(m_previewsShown);
138 propsBoxLayout->addWidget(m_showInGroups);
139 propsBoxLayout->addWidget(m_showHiddenFiles);
140 propsBoxLayout->addWidget(m_additionalInfo);
141
142 topLayout->addWidget(propsBox);
143
144 connect(m_viewMode, SIGNAL(currentIndexChanged(int)),
145 this, SLOT(slotViewModeChanged(int)));
146 connect(m_sorting, SIGNAL(currentIndexChanged(int)),
147 this, SLOT(slotSortingChanged(int)));
148 connect(m_sortOrder, SIGNAL(currentIndexChanged(int)),
149 this, SLOT(slotSortOrderChanged(int)));
150 connect(m_additionalInfo, SIGNAL(clicked()),
151 this, SLOT(configureAdditionalInfo()));
152 connect(m_sortFoldersFirst, SIGNAL(clicked()),
153 this, SLOT(slotSortFoldersFirstChanged()));
154 connect(m_previewsShown, SIGNAL(clicked()),
155 this, SLOT(slotShowPreviewChanged()));
156 connect(m_showInGroups, SIGNAL(clicked()),
157 this, SLOT(slotGroupedSortingChanged()));
158 connect(m_showHiddenFiles, SIGNAL(clicked()),
159 this, SLOT(slotShowHiddenFilesChanged()));
160
161 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
162 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
163
164 // Only show the following settings if the view properties are remembered
165 // for each directory:
166 if (!useGlobalViewProps) {
167 // create 'Apply View Properties To' group
168 QGroupBox* applyBox = new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main);
169
170 m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
171 "Current folder"), applyBox);
172 m_applyToCurrentFolder->setChecked(true);
173 m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
174 "Current folder including all sub-folders"), applyBox);
175 m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
176 "All folders"), applyBox);
177
178 QButtonGroup* applyGroup = new QButtonGroup(this);
179 applyGroup->addButton(m_applyToCurrentFolder);
180 applyGroup->addButton(m_applyToSubFolders);
181 applyGroup->addButton(m_applyToAllFolders);
182
183 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
184 applyBoxLayout->addWidget(m_applyToCurrentFolder);
185 applyBoxLayout->addWidget(m_applyToSubFolders);
186 applyBoxLayout->addWidget(m_applyToAllFolders);
187
188 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main);
189
190 topLayout->addWidget(applyBox);
191 topLayout->addWidget(m_useAsDefault);
192
193 connect(m_applyToCurrentFolder, SIGNAL(clicked(bool)),
194 this, SLOT(markAsDirty(bool)));
195 connect(m_applyToSubFolders, SIGNAL(clicked(bool)),
196 this, SLOT(markAsDirty(bool)));
197 connect(m_applyToAllFolders, SIGNAL(clicked(bool)),
198 this, SLOT(markAsDirty(bool)));
199 connect(m_useAsDefault, SIGNAL(clicked(bool)),
200 this, SLOT(markAsDirty(bool)));
201 }
202
203 main->setLayout(topLayout);
204 setMainWidget(main);
205
206 const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
207 "ViewPropertiesDialog");
208 restoreDialogSize(dialogConfig);
209
210 loadSettings();
211 }
212
213 ViewPropertiesDialog::~ViewPropertiesDialog()
214 {
215 m_isDirty = false;
216 delete m_viewProps;
217 m_viewProps = 0;
218
219 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
220 "ViewPropertiesDialog");
221 saveDialogSize(dialogConfig, KConfigBase::Persistent);
222 }
223
224 void ViewPropertiesDialog::slotOk()
225 {
226 applyViewProperties();
227 accept();
228 }
229
230 void ViewPropertiesDialog::slotApply()
231 {
232 applyViewProperties();
233 markAsDirty(false);
234 }
235
236 void ViewPropertiesDialog::slotViewModeChanged(int index)
237 {
238 const QVariant itemData = m_viewMode->itemData(index);
239 const DolphinView::Mode viewMode = static_cast<DolphinView::Mode>(itemData.toInt());
240 m_viewProps->setViewMode(viewMode);
241 markAsDirty(true);
242 }
243
244 void ViewPropertiesDialog::slotSortingChanged(int index)
245 {
246 const QByteArray role = m_sorting->itemData(index).toByteArray();
247 m_viewProps->setSortRole(role);
248 markAsDirty(true);
249 }
250
251 void ViewPropertiesDialog::slotSortOrderChanged(int index)
252 {
253 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
254 m_viewProps->setSortOrder(sortOrder);
255 markAsDirty(true);
256 }
257
258 void ViewPropertiesDialog::slotGroupedSortingChanged()
259 {
260 m_viewProps->setGroupedSorting(m_showInGroups->isChecked());
261 markAsDirty(true);
262 }
263
264 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
265 {
266 const bool foldersFirst = m_sortFoldersFirst->isChecked();
267 m_viewProps->setSortFoldersFirst(foldersFirst);
268 markAsDirty(true);
269 }
270
271 void ViewPropertiesDialog::slotShowPreviewChanged()
272 {
273 const bool show = m_previewsShown->isChecked();
274 m_viewProps->setPreviewsShown(show);
275 markAsDirty(true);
276 }
277
278 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
279 {
280 const bool show = m_showHiddenFiles->isChecked();
281 m_viewProps->setHiddenFilesShown(show);
282 markAsDirty(true);
283 }
284
285 void ViewPropertiesDialog::markAsDirty(bool isDirty)
286 {
287 m_isDirty = isDirty;
288 enableButtonApply(isDirty);
289 }
290
291 void ViewPropertiesDialog::configureAdditionalInfo()
292 {
293 QList<QByteArray> visibleRoles = m_viewProps->visibleRoles();
294 const bool useDefaultRoles = (m_viewProps->viewMode() == DolphinView::DetailsView) && visibleRoles.isEmpty();
295 if (useDefaultRoles) {
296 // Using the details view without any additional information (-> additional column)
297 // makes no sense and leads to a usability problem as no viewport area is available
298 // anymore. Hence as fallback provide at least a size and date column.
299 visibleRoles.clear();
300 visibleRoles.append("text");
301 visibleRoles.append("size");
302 visibleRoles.append("date");
303 m_viewProps->setVisibleRoles(visibleRoles);
304 }
305
306 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, visibleRoles);
307 if (dialog->exec() == QDialog::Accepted) {
308 m_viewProps->setVisibleRoles(dialog->visibleRoles());
309 markAsDirty(true);
310 }
311 delete dialog;
312 }
313
314 void ViewPropertiesDialog::applyViewProperties()
315 {
316 // if nothing changed in the dialog, we have nothing to apply
317 if (!m_isDirty) {
318 return;
319 }
320
321 const bool applyToSubFolders = m_applyToSubFolders && m_applyToSubFolders->isChecked();
322 if (applyToSubFolders) {
323 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
324 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
325 return;
326 }
327
328 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
329 m_dolphinView->url(),
330 *m_viewProps);
331 info->setAttribute(Qt::WA_DeleteOnClose);
332 info->setWindowModality(Qt::NonModal);
333 info->show();
334 }
335
336 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
337
338 // If the user selected 'Apply To All Folders' the view properties implicitely
339 // are also used as default for new folders.
340 const bool useAsDefault = applyToAllFolders || (m_useAsDefault && m_useAsDefault->isChecked());
341 if (useAsDefault) {
342 // For directories where no .directory file is available, the .directory
343 // file stored for the global view properties is used as fallback. To update
344 // this file we temporary turn on the global view properties mode.
345 Q_ASSERT(!GeneralSettings::globalViewProps());
346
347 GeneralSettings::setGlobalViewProps(true);
348 ViewProperties defaultProps(m_dolphinView->url());
349 defaultProps.setDirProperties(*m_viewProps);
350 defaultProps.save();
351 GeneralSettings::setGlobalViewProps(false);
352 }
353
354 if (applyToAllFolders) {
355 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
356 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
357 return;
358 }
359
360 // Updating the global view properties time stamp in the general settings makes
361 // all existing viewproperties invalid, as they have a smaller time stamp.
362 GeneralSettings* settings = GeneralSettings::self();
363 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
364 settings->writeConfig();
365 }
366
367 m_dolphinView->setMode(m_viewProps->viewMode());
368 m_dolphinView->setSortRole(m_viewProps->sortRole());
369 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
370 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
371 m_dolphinView->setGroupedSorting(m_viewProps->groupedSorting());
372 m_dolphinView->setVisibleRoles(m_viewProps->visibleRoles());
373 m_dolphinView->setPreviewsShown(m_viewProps->previewsShown());
374 m_dolphinView->setHiddenFilesShown(m_viewProps->hiddenFilesShown());
375
376 m_viewProps->save();
377
378 markAsDirty(false);
379 }
380
381 void ViewPropertiesDialog::loadSettings()
382 {
383 // Load view mode
384 switch (m_viewProps->viewMode()) {
385 case DolphinView::IconsView: m_viewMode->setCurrentIndex(0); break;
386 case DolphinView::CompactView: m_viewMode->setCurrentIndex(1); break;
387 case DolphinView::DetailsView: m_viewMode->setCurrentIndex(2); break;
388 default: break;
389 }
390
391 // Load sort order and sorting
392 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
393 m_sortOrder->setCurrentIndex(sortOrderIndex);
394
395 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
396 int sortRoleIndex = 0;
397 for (int i = 0; i < rolesInfo.count(); ++i) {
398 if (rolesInfo[i].role == m_viewProps->sortRole()) {
399 sortRoleIndex = i;
400 break;
401 }
402 }
403 m_sorting->setCurrentIndex(sortRoleIndex);
404
405 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
406
407 // Load show preview, show in groups and show hidden files settings
408 m_previewsShown->setChecked(m_viewProps->previewsShown());
409 m_showInGroups->setChecked(m_viewProps->groupedSorting());
410 m_showHiddenFiles->setChecked(m_viewProps->hiddenFilesShown());
411 markAsDirty(false);
412 }
413
414 #include "viewpropertiesdialog.moc"