]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
First step to introduce dynamic roles
[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 "views/rolesaccessor.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 <Nepomuk/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 RolesAccessor& rolesAccessor = RolesAccessor::instance();
111 const QList<QByteArray> roles = rolesAccessor.roles();
112 foreach (const QByteArray& role, roles) {
113 m_sorting->addItem(rolesAccessor.translation(role), role);
114 }
115
116 m_sortFoldersFirst = new QCheckBox(i18nc("@option:check", "Show folders first"));
117 m_previewsShown = new QCheckBox(i18nc("@option:check", "Show preview"));
118 m_showInGroups = new QCheckBox(i18nc("@option:check", "Show in groups"));
119 m_showHiddenFiles = new QCheckBox(i18nc("@option:check", "Show hidden files"));
120
121 m_additionalInfo = new QPushButton(i18nc("@action:button", "Additional Information"));
122
123 QHBoxLayout* sortingLayout = new QHBoxLayout();
124 sortingLayout->setMargin(0);
125 sortingLayout->addWidget(m_sortOrder);
126 sortingLayout->addWidget(m_sorting);
127 sortingBox->setLayout(sortingLayout);
128
129 QGridLayout* propsGridLayout = new QGridLayout(propsGrid);
130 propsGridLayout->addWidget(viewModeLabel, 0, 0, Qt::AlignRight);
131 propsGridLayout->addWidget(m_viewMode, 0, 1);
132 propsGridLayout->addWidget(sortingLabel, 1, 0, Qt::AlignRight);
133 propsGridLayout->addWidget(sortingBox, 1, 1);
134
135 QVBoxLayout* propsBoxLayout = new QVBoxLayout(propsBox);
136 propsBoxLayout->addWidget(propsGrid);
137 propsBoxLayout->addWidget(m_sortFoldersFirst);
138 propsBoxLayout->addWidget(m_previewsShown);
139 propsBoxLayout->addWidget(m_showInGroups);
140 propsBoxLayout->addWidget(m_showHiddenFiles);
141 propsBoxLayout->addWidget(m_additionalInfo);
142
143 topLayout->addWidget(propsBox);
144
145 connect(m_viewMode, SIGNAL(currentIndexChanged(int)),
146 this, SLOT(slotViewModeChanged(int)));
147 connect(m_sorting, SIGNAL(currentIndexChanged(int)),
148 this, SLOT(slotSortingChanged(int)));
149 connect(m_sortOrder, SIGNAL(currentIndexChanged(int)),
150 this, SLOT(slotSortOrderChanged(int)));
151 connect(m_additionalInfo, SIGNAL(clicked()),
152 this, SLOT(configureAdditionalInfo()));
153 connect(m_sortFoldersFirst, SIGNAL(clicked()),
154 this, SLOT(slotSortFoldersFirstChanged()));
155 connect(m_previewsShown, SIGNAL(clicked()),
156 this, SLOT(slotShowPreviewChanged()));
157 connect(m_showInGroups, SIGNAL(clicked()),
158 this, SLOT(slotGroupedSortingChanged()));
159 connect(m_showHiddenFiles, SIGNAL(clicked()),
160 this, SLOT(slotShowHiddenFilesChanged()));
161
162 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
163 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
164
165 // Only show the following settings if the view properties are remembered
166 // for each directory:
167 if (!useGlobalViewProps) {
168 // create 'Apply View Properties To' group
169 QGroupBox* applyBox = new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main);
170
171 m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
172 "Current folder"), applyBox);
173 m_applyToCurrentFolder->setChecked(true);
174 m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
175 "Current folder including all sub-folders"), applyBox);
176 m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
177 "All folders"), applyBox);
178
179 QButtonGroup* applyGroup = new QButtonGroup(this);
180 applyGroup->addButton(m_applyToCurrentFolder);
181 applyGroup->addButton(m_applyToSubFolders);
182 applyGroup->addButton(m_applyToAllFolders);
183
184 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
185 applyBoxLayout->addWidget(m_applyToCurrentFolder);
186 applyBoxLayout->addWidget(m_applyToSubFolders);
187 applyBoxLayout->addWidget(m_applyToAllFolders);
188
189 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main);
190
191 topLayout->addWidget(applyBox);
192 topLayout->addWidget(m_useAsDefault);
193
194 connect(m_applyToCurrentFolder, SIGNAL(clicked(bool)),
195 this, SLOT(markAsDirty(bool)));
196 connect(m_applyToSubFolders, SIGNAL(clicked(bool)),
197 this, SLOT(markAsDirty(bool)));
198 connect(m_applyToAllFolders, SIGNAL(clicked(bool)),
199 this, SLOT(markAsDirty(bool)));
200 connect(m_useAsDefault, SIGNAL(clicked(bool)),
201 this, SLOT(markAsDirty(bool)));
202 }
203
204 main->setLayout(topLayout);
205 setMainWidget(main);
206
207 const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
208 "ViewPropertiesDialog");
209 restoreDialogSize(dialogConfig);
210
211 loadSettings();
212 }
213
214 ViewPropertiesDialog::~ViewPropertiesDialog()
215 {
216 m_isDirty = false;
217 delete m_viewProps;
218 m_viewProps = 0;
219
220 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
221 "ViewPropertiesDialog");
222 saveDialogSize(dialogConfig, KConfigBase::Persistent);
223 }
224
225 void ViewPropertiesDialog::slotOk()
226 {
227 applyViewProperties();
228 accept();
229 }
230
231 void ViewPropertiesDialog::slotApply()
232 {
233 applyViewProperties();
234 markAsDirty(false);
235 }
236
237 void ViewPropertiesDialog::slotViewModeChanged(int index)
238 {
239 const QVariant itemData = m_viewMode->itemData(index);
240 const DolphinView::Mode viewMode = static_cast<DolphinView::Mode>(itemData.toInt());
241 m_viewProps->setViewMode(viewMode);
242 markAsDirty(true);
243 }
244
245 void ViewPropertiesDialog::slotSortingChanged(int index)
246 {
247 const QByteArray role = m_sorting->itemData(index).toByteArray();
248 m_viewProps->setSortRole(role);
249 markAsDirty(true);
250 }
251
252 void ViewPropertiesDialog::slotSortOrderChanged(int index)
253 {
254 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
255 m_viewProps->setSortOrder(sortOrder);
256 markAsDirty(true);
257 }
258
259 void ViewPropertiesDialog::slotGroupedSortingChanged()
260 {
261 m_viewProps->setGroupedSorting(m_showInGroups->isChecked());
262 markAsDirty(true);
263 }
264
265 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
266 {
267 const bool foldersFirst = m_sortFoldersFirst->isChecked();
268 m_viewProps->setSortFoldersFirst(foldersFirst);
269 markAsDirty(true);
270 }
271
272 void ViewPropertiesDialog::slotShowPreviewChanged()
273 {
274 const bool show = m_previewsShown->isChecked();
275 m_viewProps->setPreviewsShown(show);
276 markAsDirty(true);
277 }
278
279 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
280 {
281 const bool show = m_showHiddenFiles->isChecked();
282 m_viewProps->setHiddenFilesShown(show);
283 markAsDirty(true);
284 }
285
286 void ViewPropertiesDialog::markAsDirty(bool isDirty)
287 {
288 m_isDirty = isDirty;
289 enableButtonApply(isDirty);
290 }
291
292 void ViewPropertiesDialog::configureAdditionalInfo()
293 {
294 QList<QByteArray> visibleRoles = m_viewProps->visibleRoles();
295 const bool useDefaultRoles = (m_viewProps->viewMode() == DolphinView::DetailsView) && visibleRoles.isEmpty();
296 if (useDefaultRoles) {
297 // Using the details view without any additional information (-> additional column)
298 // makes no sense and leads to a usability problem as no viewport area is available
299 // anymore. Hence as fallback provide at least a size and date column.
300 visibleRoles.clear();
301 visibleRoles.append("name");
302 visibleRoles.append("size");
303 visibleRoles.append("date");
304 m_viewProps->setVisibleRoles(visibleRoles);
305 }
306
307 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, visibleRoles);
308 if (dialog->exec() == QDialog::Accepted) {
309 m_viewProps->setVisibleRoles(dialog->visibleRoles());
310 markAsDirty(true);
311 }
312 delete dialog;
313 }
314
315 void ViewPropertiesDialog::applyViewProperties()
316 {
317 // if nothing changed in the dialog, we have nothing to apply
318 if (!m_isDirty) {
319 return;
320 }
321
322 const bool applyToSubFolders = m_applyToSubFolders && m_applyToSubFolders->isChecked();
323 if (applyToSubFolders) {
324 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
325 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
326 return;
327 }
328
329 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
330 m_dolphinView->url(),
331 *m_viewProps);
332 info->setAttribute(Qt::WA_DeleteOnClose);
333 info->setWindowModality(Qt::NonModal);
334 info->show();
335 }
336
337 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
338
339 // If the user selected 'Apply To All Folders' the view properties implicitely
340 // are also used as default for new folders.
341 const bool useAsDefault = applyToAllFolders || (m_useAsDefault && m_useAsDefault->isChecked());
342 if (useAsDefault) {
343 // For directories where no .directory file is available, the .directory
344 // file stored for the global view properties is used as fallback. To update
345 // this file we temporary turn on the global view properties mode.
346 Q_ASSERT(!GeneralSettings::globalViewProps());
347
348 GeneralSettings::setGlobalViewProps(true);
349 ViewProperties defaultProps(m_dolphinView->url());
350 defaultProps.setDirProperties(*m_viewProps);
351 defaultProps.save();
352 GeneralSettings::setGlobalViewProps(false);
353 }
354
355 if (applyToAllFolders) {
356 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
357 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
358 return;
359 }
360
361 // Updating the global view properties time stamp in the general settings makes
362 // all existing viewproperties invalid, as they have a smaller time stamp.
363 GeneralSettings* settings = GeneralSettings::self();
364 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
365 settings->writeConfig();
366 }
367
368 m_dolphinView->setMode(m_viewProps->viewMode());
369 m_dolphinView->setSortRole(m_viewProps->sortRole());
370 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
371 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
372 m_dolphinView->setGroupedSorting(m_viewProps->groupedSorting());
373 m_dolphinView->setVisibleRoles(m_viewProps->visibleRoles());
374 m_dolphinView->setPreviewsShown(m_viewProps->previewsShown());
375 m_dolphinView->setHiddenFilesShown(m_viewProps->hiddenFilesShown());
376
377 m_viewProps->save();
378
379 markAsDirty(false);
380 }
381
382 void ViewPropertiesDialog::loadSettings()
383 {
384 // Load view mode
385 switch (m_viewProps->viewMode()) {
386 case DolphinView::IconsView: m_viewMode->setCurrentIndex(0); break;
387 case DolphinView::CompactView: m_viewMode->setCurrentIndex(1); break;
388 case DolphinView::DetailsView: m_viewMode->setCurrentIndex(2); break;
389 default: break;
390 }
391
392 // Load sort order and sorting
393 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
394 m_sortOrder->setCurrentIndex(sortOrderIndex);
395
396 const QList<QByteArray> roles = RolesAccessor::instance().roles();
397 const int sortRoleIndex = roles.indexOf(m_viewProps->sortRole());
398 m_sorting->setCurrentIndex(sortRoleIndex);
399
400 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
401
402 // Load show preview, show in groups and show hidden files settings
403 m_previewsShown->setChecked(m_viewProps->previewsShown());
404 m_showInGroups->setChecked(m_viewProps->groupedSorting());
405 m_showHiddenFiles->setChecked(m_viewProps->hiddenFilesShown());
406 markAsDirty(false);
407 }
408
409 #include "viewpropertiesdialog.moc"