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