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