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