]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
a1f9718feaceccf70faee9e6784457a7d1cea332
[dolphin.git] / src / settings / viewpropertiesdialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2018 by Elvis Angelaccio <elvis.angelaccio@kde.org> *
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 "dolphin_generalsettings.h"
24 #include "dolphin_iconsmodesettings.h"
25 #include "global.h"
26 #include "kitemviews/kfileitemmodel.h"
27 #include "viewpropsprogressinfo.h"
28 #include "views/dolphinview.h"
29
30 #include <KCollapsibleGroupBox>
31 #include <KLocalizedString>
32 #include <KMessageBox>
33 #include <KWindowConfig>
34
35 #ifdef HAVE_BALOO
36 #include <Baloo/IndexerConfig>
37 #endif
38
39 #include <QButtonGroup>
40 #include <QCheckBox>
41 #include <QComboBox>
42 #include <QFormLayout>
43 #include <QLabel>
44 #include <QListWidget>
45 #include <QPushButton>
46 #include <QRadioButton>
47 #include <QSpacerItem>
48
49 #include <views/viewproperties.h>
50
51 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
52 QDialog(dolphinView),
53 m_isDirty(false),
54 m_dolphinView(dolphinView),
55 m_viewProps(nullptr),
56 m_viewMode(nullptr),
57 m_sortOrder(nullptr),
58 m_sorting(nullptr),
59 m_sortFoldersFirst(nullptr),
60 m_previewsShown(nullptr),
61 m_showInGroups(nullptr),
62 m_showHiddenFiles(nullptr),
63 m_applyToCurrentFolder(nullptr),
64 m_applyToSubFolders(nullptr),
65 m_applyToAllFolders(nullptr),
66 m_useAsDefault(nullptr)
67 {
68 Q_ASSERT(dolphinView);
69 const bool useGlobalViewProps = GeneralSettings::globalViewProps();
70
71 setWindowTitle(i18nc("@title:window", "View Properties"));
72
73 const QUrl& url = dolphinView->url();
74 m_viewProps = new ViewProperties(url);
75 m_viewProps->setAutoSaveEnabled(false);
76
77 auto layout = new QFormLayout(this);
78 // Otherwise the dialog won't resize when we collapse the KCollapsibleGroupBox.
79 layout->setSizeConstraint(QLayout::SetFixedSize);
80 setLayout(layout);
81
82 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
83 m_viewMode = new QComboBox();
84 m_viewMode->addItem(QIcon::fromTheme(QStringLiteral("view-list-icons")), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView);
85 m_viewMode->addItem(QIcon::fromTheme(QStringLiteral("view-list-details")), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView);
86 m_viewMode->addItem(QIcon::fromTheme(QStringLiteral("view-list-tree")), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView);
87
88 m_sortOrder = new QComboBox();
89 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
90 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Descending"));
91
92 m_sorting = new QComboBox();
93 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
94 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
95 m_sorting->addItem(info.translation, info.role);
96 }
97
98 m_sortFoldersFirst = new QCheckBox(i18nc("@option:check", "Show folders first"));
99 m_previewsShown = new QCheckBox(i18nc("@option:check", "Show preview"));
100 m_showInGroups = new QCheckBox(i18nc("@option:check", "Show in groups"));
101 m_showHiddenFiles = new QCheckBox(i18nc("@option:check", "Show hidden files"));
102
103 auto additionalInfoBox = new KCollapsibleGroupBox();
104 additionalInfoBox->setTitle(i18nc("@title:group", "Additional Information"));
105 auto innerLayout = new QVBoxLayout();
106
107 {
108 QList<QByteArray> visibleRoles = m_viewProps->visibleRoles();
109 const bool useDefaultRoles = (m_viewProps->viewMode() == DolphinView::DetailsView) && visibleRoles.isEmpty();
110 if (useDefaultRoles) {
111 // Using the details view without any additional information (-> additional column)
112 // makes no sense and leads to a usability problem as no viewport area is available
113 // anymore. Hence as fallback provide at least a size and date column.
114 visibleRoles.clear();
115 visibleRoles.append("text");
116 visibleRoles.append("size");
117 visibleRoles.append("modificationtime");
118 m_viewProps->setVisibleRoles(visibleRoles);
119 }
120
121 // Add checkboxes
122 bool indexingEnabled = false;
123 #ifdef HAVE_BALOO
124 Baloo::IndexerConfig config;
125 indexingEnabled = config.fileIndexingEnabled();
126 #endif
127
128 m_listWidget = new QListWidget();
129 connect(m_listWidget, &QListWidget::itemChanged, this, &ViewPropertiesDialog::slotItemChanged);
130 m_listWidget->setSelectionMode(QAbstractItemView::NoSelection);
131 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
132 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
133 QListWidgetItem* item = new QListWidgetItem(info.translation, m_listWidget);
134 item->setCheckState(visibleRoles.contains(info.role) ? Qt::Checked : Qt::Unchecked);
135
136 const bool enable = ((!info.requiresBaloo && !info.requiresIndexer) ||
137 (info.requiresBaloo) ||
138 (info.requiresIndexer && indexingEnabled)) && info.role != "text";
139
140 if (!enable) {
141 item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
142 }
143 }
144 QLabel* additionalViewOptionsLabel = new QLabel(i18n("Choose what to see on each file or folder:"));
145 innerLayout->addWidget(additionalViewOptionsLabel);
146 innerLayout->addWidget(m_listWidget);
147 }
148
149 additionalInfoBox->setLayout(innerLayout);
150
151 QHBoxLayout* sortingLayout = new QHBoxLayout();
152 sortingLayout->setContentsMargins(0, 0, 0, 0);
153 sortingLayout->addWidget(m_sortOrder);
154 sortingLayout->addWidget(m_sorting);
155
156 layout->addRow(i18nc("@label:listbox", "View mode:"), m_viewMode);
157 layout->addRow(i18nc("@label:listbox", "Sorting:"), sortingLayout);
158
159 layout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
160
161 layout->addRow(i18n("View options:"), m_sortFoldersFirst);
162 layout->addRow(QString(), m_previewsShown);
163 layout->addRow(QString(), m_showInGroups);
164 layout->addRow(QString(), m_showHiddenFiles);
165
166 connect(m_viewMode, QOverload<int>::of(&QComboBox::currentIndexChanged),
167 this, &ViewPropertiesDialog::slotViewModeChanged);
168 connect(m_sorting, QOverload<int>::of(&QComboBox::currentIndexChanged),
169 this, &ViewPropertiesDialog::slotSortingChanged);
170 connect(m_sortOrder, QOverload<int>::of(&QComboBox::currentIndexChanged),
171 this, &ViewPropertiesDialog::slotSortOrderChanged);
172 connect(m_sortFoldersFirst, &QCheckBox::clicked,
173 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged);
174 connect(m_previewsShown, &QCheckBox::clicked,
175 this, &ViewPropertiesDialog::slotShowPreviewChanged);
176 connect(m_showInGroups, &QCheckBox::clicked,
177 this, &ViewPropertiesDialog::slotGroupedSortingChanged);
178 connect(m_showHiddenFiles, &QCheckBox::clicked,
179 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged);
180
181 // Only show the following settings if the view properties are remembered
182 // for each directory:
183 if (!useGlobalViewProps) {
184 // create 'Apply View Properties To' group
185 m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
186 "Current folder"));
187 m_applyToCurrentFolder->setChecked(true);
188 m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
189 "Current folder and sub-folders"));
190 m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
191 "All folders"));
192
193 QButtonGroup* applyGroup = new QButtonGroup(this);
194 applyGroup->addButton(m_applyToCurrentFolder);
195 applyGroup->addButton(m_applyToSubFolders);
196 applyGroup->addButton(m_applyToAllFolders);
197
198 layout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
199
200 layout->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder);
201 layout->addRow(QString(), m_applyToSubFolders);
202 layout->addRow(QString(), m_applyToAllFolders);
203 layout->addRow(QString(), m_applyToAllFolders);
204
205 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
206 layout->addRow(QString(), m_useAsDefault);
207
208 connect(m_applyToCurrentFolder, &QRadioButton::clicked,
209 this, &ViewPropertiesDialog::markAsDirty);
210 connect(m_applyToSubFolders, &QRadioButton::clicked,
211 this, &ViewPropertiesDialog::markAsDirty);
212 connect(m_applyToAllFolders, &QRadioButton::clicked,
213 this, &ViewPropertiesDialog::markAsDirty);
214 connect(m_useAsDefault, &QCheckBox::clicked,
215 this, &ViewPropertiesDialog::markAsDirty);
216 }
217
218 layout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
219
220 layout->addRow(additionalInfoBox);
221
222 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply, this);
223 connect(buttonBox, &QDialogButtonBox::accepted, this, &ViewPropertiesDialog::accept);
224 connect(buttonBox, &QDialogButtonBox::rejected, this, &ViewPropertiesDialog::reject);
225 layout->addWidget(buttonBox);
226
227 auto okButton = buttonBox->button(QDialogButtonBox::Ok);
228 okButton->setShortcut(Qt::CTRL + Qt::Key_Return);
229 okButton->setDefault(true);
230
231 auto applyButton = buttonBox->button(QDialogButtonBox::Apply);
232 applyButton->setEnabled(false);
233 connect(applyButton, &QPushButton::clicked, this, &ViewPropertiesDialog::slotApply);
234 connect(this, &ViewPropertiesDialog::isDirtyChanged, applyButton, [applyButton](bool isDirty) {
235 applyButton->setEnabled(isDirty);
236 });
237
238 const KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
239 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
240
241 loadSettings();
242 }
243
244 ViewPropertiesDialog::~ViewPropertiesDialog()
245 {
246 m_isDirty = false;
247 delete m_viewProps;
248 m_viewProps = nullptr;
249
250 KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
251 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
252 }
253
254 void ViewPropertiesDialog::accept()
255 {
256 applyViewProperties();
257 QDialog::accept();
258 }
259
260 void ViewPropertiesDialog::slotApply()
261 {
262 applyViewProperties();
263 markAsDirty(false);
264 }
265
266 void ViewPropertiesDialog::slotViewModeChanged(int index)
267 {
268 const QVariant itemData = m_viewMode->itemData(index);
269 const DolphinView::Mode viewMode = static_cast<DolphinView::Mode>(itemData.toInt());
270 m_viewProps->setViewMode(viewMode);
271 markAsDirty(true);
272 }
273
274 void ViewPropertiesDialog::slotSortingChanged(int index)
275 {
276 const QByteArray role = m_sorting->itemData(index).toByteArray();
277 m_viewProps->setSortRole(role);
278 markAsDirty(true);
279 }
280
281 void ViewPropertiesDialog::slotSortOrderChanged(int index)
282 {
283 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
284 m_viewProps->setSortOrder(sortOrder);
285 markAsDirty(true);
286 }
287
288 void ViewPropertiesDialog::slotGroupedSortingChanged()
289 {
290 m_viewProps->setGroupedSorting(m_showInGroups->isChecked());
291 markAsDirty(true);
292 }
293
294 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
295 {
296 const bool foldersFirst = m_sortFoldersFirst->isChecked();
297 m_viewProps->setSortFoldersFirst(foldersFirst);
298 markAsDirty(true);
299 }
300
301 void ViewPropertiesDialog::slotShowPreviewChanged()
302 {
303 const bool show = m_previewsShown->isChecked();
304 m_viewProps->setPreviewsShown(show);
305 markAsDirty(true);
306 }
307
308 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
309 {
310 const bool show = m_showHiddenFiles->isChecked();
311 m_viewProps->setHiddenFilesShown(show);
312 markAsDirty(true);
313 }
314
315 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem *item)
316 {
317 Q_UNUSED(item)
318 markAsDirty(true);
319 }
320
321 void ViewPropertiesDialog::markAsDirty(bool isDirty)
322 {
323 if (m_isDirty != isDirty) {
324 m_isDirty = isDirty;
325 emit isDirtyChanged(isDirty);
326 }
327 }
328
329 void ViewPropertiesDialog::applyViewProperties()
330 {
331 // if nothing changed in the dialog, we have nothing to apply
332 if (!m_isDirty) {
333 return;
334 }
335
336 // Update visible roles.
337 {
338 QList<QByteArray> visibleRoles;
339 int index = 0;
340 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
341 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
342 const QListWidgetItem* item = m_listWidget->item(index);
343 if (item->checkState() == Qt::Checked) {
344 visibleRoles.append(info.role);
345 }
346 ++index;
347 }
348
349 m_viewProps->setVisibleRoles(visibleRoles);
350 }
351
352 const bool applyToSubFolders = m_applyToSubFolders && m_applyToSubFolders->isChecked();
353 if (applyToSubFolders) {
354 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
355 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
356 return;
357 }
358
359 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
360 m_dolphinView->url(),
361 *m_viewProps);
362 info->setAttribute(Qt::WA_DeleteOnClose);
363 info->setWindowModality(Qt::NonModal);
364 info->show();
365 }
366
367 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
368
369 // If the user selected 'Apply To All Folders' the view properties implicitly
370 // are also used as default for new folders.
371 const bool useAsDefault = applyToAllFolders || (m_useAsDefault && m_useAsDefault->isChecked());
372 if (useAsDefault) {
373 // For directories where no .directory file is available, the .directory
374 // file stored for the global view properties is used as fallback. To update
375 // this file we temporary turn on the global view properties mode.
376 Q_ASSERT(!GeneralSettings::globalViewProps());
377
378 GeneralSettings::setGlobalViewProps(true);
379 ViewProperties defaultProps(m_dolphinView->url());
380 defaultProps.setDirProperties(*m_viewProps);
381 defaultProps.save();
382 GeneralSettings::setGlobalViewProps(false);
383 }
384
385 if (applyToAllFolders) {
386 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
387 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
388 return;
389 }
390
391 // Updating the global view properties time stamp in the general settings makes
392 // all existing viewproperties invalid, as they have a smaller time stamp.
393 GeneralSettings* settings = GeneralSettings::self();
394 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
395 settings->save();
396 }
397
398 m_dolphinView->setMode(m_viewProps->viewMode());
399 m_dolphinView->setSortRole(m_viewProps->sortRole());
400 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
401 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
402 m_dolphinView->setGroupedSorting(m_viewProps->groupedSorting());
403 m_dolphinView->setVisibleRoles(m_viewProps->visibleRoles());
404 m_dolphinView->setPreviewsShown(m_viewProps->previewsShown());
405 m_dolphinView->setHiddenFilesShown(m_viewProps->hiddenFilesShown());
406
407 m_viewProps->save();
408
409 markAsDirty(false);
410 }
411
412 void ViewPropertiesDialog::loadSettings()
413 {
414 // Load view mode
415 switch (m_viewProps->viewMode()) {
416 case DolphinView::IconsView: m_viewMode->setCurrentIndex(0); break;
417 case DolphinView::CompactView: m_viewMode->setCurrentIndex(1); break;
418 case DolphinView::DetailsView: m_viewMode->setCurrentIndex(2); break;
419 default: break;
420 }
421
422 // Load sort order and sorting
423 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
424 m_sortOrder->setCurrentIndex(sortOrderIndex);
425
426 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
427 int sortRoleIndex = 0;
428 for (int i = 0; i < rolesInfo.count(); ++i) {
429 if (rolesInfo[i].role == m_viewProps->sortRole()) {
430 sortRoleIndex = i;
431 break;
432 }
433 }
434 m_sorting->setCurrentIndex(sortRoleIndex);
435
436 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
437
438 // Load show preview, show in groups and show hidden files settings
439 m_previewsShown->setChecked(m_viewProps->previewsShown());
440 m_showInGroups->setChecked(m_viewProps->groupedSorting());
441 m_showHiddenFiles->setChecked(m_viewProps->hiddenFilesShown());
442 markAsDirty(false);
443 }
444