2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
3 * SPDX-FileCopyrightText: 2018 Elvis Angelaccio <elvis.angelaccio@kde.org>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "viewpropertiesdialog.h"
10 #include "dolphin_generalsettings.h"
11 #include "dolphin_iconsmodesettings.h"
13 #include "kitemviews/kfileitemmodel.h"
14 #include "viewpropsprogressinfo.h"
15 #include "views/dolphinview.h"
17 #include <KCollapsibleGroupBox>
18 #include <KLocalizedString>
19 #include <KMessageBox>
20 #include <KWindowConfig>
23 #include <Baloo/IndexerConfig>
26 #include <QButtonGroup>
29 #include <QFormLayout>
31 #include <QListWidget>
32 #include <QPushButton>
33 #include <QRadioButton>
34 #include <QSpacerItem>
36 #include <views/viewproperties.h>
38 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
41 m_dolphinView(dolphinView
),
46 m_sortFoldersFirst(nullptr),
47 m_sortHiddenLast(nullptr),
48 m_previewsShown(nullptr),
49 m_showInGroups(nullptr),
50 m_showHiddenFiles(nullptr),
51 m_applyToCurrentFolder(nullptr),
52 m_applyToSubFolders(nullptr),
53 m_applyToAllFolders(nullptr),
54 m_useAsDefault(nullptr)
56 Q_ASSERT(dolphinView
);
57 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
59 setWindowTitle(i18nc("@title:window", "View Display Style"));
61 const QUrl
& url
= dolphinView
->url();
62 m_viewProps
= new ViewProperties(url
);
63 m_viewProps
->setAutoSaveEnabled(false);
65 auto layout
= new QFormLayout(this);
66 // Otherwise the dialog won't resize when we collapse the KCollapsibleGroupBox.
67 layout
->setSizeConstraint(QLayout::SetFixedSize
);
69 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
70 m_viewMode
= new QComboBox();
71 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-icons")), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
72 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-details")), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
73 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-tree")), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
75 m_sortOrder
= new QComboBox();
76 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
77 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
79 m_sorting
= new QComboBox();
80 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
81 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
82 m_sorting
->addItem(info
.translation
, info
.role
);
85 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
86 m_sortHiddenLast
= new QCheckBox(i18nc("@option:check", "Show hidden files last"));
87 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
88 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
89 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
91 auto additionalInfoBox
= new KCollapsibleGroupBox();
92 additionalInfoBox
->setTitle(i18nc("@title:group", "Additional Information"));
93 auto innerLayout
= new QVBoxLayout(additionalInfoBox
);
96 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
97 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
98 if (useDefaultRoles
) {
99 // Using the details view without any additional information (-> additional column)
100 // makes no sense and leads to a usability problem as no viewport area is available
101 // anymore. Hence as fallback provide at least a size and date column.
102 visibleRoles
.clear();
103 visibleRoles
.append("text");
104 visibleRoles
.append("size");
105 visibleRoles
.append("modificationtime");
106 m_viewProps
->setVisibleRoles(visibleRoles
);
110 bool indexingEnabled
= false;
112 Baloo::IndexerConfig config
;
113 indexingEnabled
= config
.fileIndexingEnabled();
116 m_listWidget
= new QListWidget();
117 connect(m_listWidget
, &QListWidget::itemChanged
, this, &ViewPropertiesDialog::slotItemChanged
);
118 m_listWidget
->setSelectionMode(QAbstractItemView::NoSelection
);
119 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
120 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
121 QListWidgetItem
* item
= new QListWidgetItem(info
.translation
, m_listWidget
);
122 item
->setCheckState(visibleRoles
.contains(info
.role
) ? Qt::Checked
: Qt::Unchecked
);
124 const bool enable
= ((!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
125 (info
.requiresBaloo
) ||
126 (info
.requiresIndexer
&& indexingEnabled
)) && info
.role
!= "text";
129 item
->setFlags(item
->flags() & ~Qt::ItemIsEnabled
);
132 QLabel
* additionalViewOptionsLabel
= new QLabel(i18n("Choose what to see on each file or folder:"));
133 innerLayout
->addWidget(additionalViewOptionsLabel
);
134 innerLayout
->addWidget(m_listWidget
);
137 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
138 sortingLayout
->setContentsMargins(0, 0, 0, 0);
139 sortingLayout
->addWidget(m_sortOrder
);
140 sortingLayout
->addWidget(m_sorting
);
142 layout
->addRow(i18nc("@label:listbox", "View mode:"), m_viewMode
);
143 layout
->addRow(i18nc("@label:listbox", "Sorting:"), sortingLayout
);
145 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
147 layout
->addRow(i18n("View options:"), m_sortFoldersFirst
);
148 layout
->addRow(QString(), m_previewsShown
);
149 layout
->addRow(QString(), m_showInGroups
);
150 layout
->addRow(QString(), m_showHiddenFiles
);
151 layout
->addRow(QString(), m_sortHiddenLast
);
153 connect(m_viewMode
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
154 this, &ViewPropertiesDialog::slotViewModeChanged
);
155 connect(m_sorting
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
156 this, &ViewPropertiesDialog::slotSortingChanged
);
157 connect(m_sortOrder
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
158 this, &ViewPropertiesDialog::slotSortOrderChanged
);
159 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
160 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
161 connect(m_sortHiddenLast
, &QCheckBox::clicked
,
162 this, &ViewPropertiesDialog::slotSortHiddenLastChanged
);
163 connect(m_previewsShown
, &QCheckBox::clicked
,
164 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
165 connect(m_showInGroups
, &QCheckBox::clicked
,
166 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
167 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
168 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
170 // Only show the following settings if the view properties are remembered
171 // for each directory:
172 if (!useGlobalViewProps
) {
173 // create 'Apply View Properties To' group
174 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
176 m_applyToCurrentFolder
->setChecked(true);
177 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
178 "Current folder and sub-folders"));
179 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
182 QButtonGroup
* applyGroup
= new QButtonGroup(this);
183 applyGroup
->addButton(m_applyToCurrentFolder
);
184 applyGroup
->addButton(m_applyToSubFolders
);
185 applyGroup
->addButton(m_applyToAllFolders
);
187 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
189 layout
->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder
);
190 layout
->addRow(QString(), m_applyToSubFolders
);
191 layout
->addRow(QString(), m_applyToAllFolders
);
192 layout
->addRow(QString(), m_applyToAllFolders
);
194 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
195 layout
->addRow(QString(), m_useAsDefault
);
197 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
198 this, &ViewPropertiesDialog::markAsDirty
);
199 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
200 this, &ViewPropertiesDialog::markAsDirty
);
201 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
202 this, &ViewPropertiesDialog::markAsDirty
);
203 connect(m_useAsDefault
, &QCheckBox::clicked
,
204 this, &ViewPropertiesDialog::markAsDirty
);
207 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
209 layout
->addRow(additionalInfoBox
);
211 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
212 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
213 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
214 layout
->addWidget(buttonBox
);
216 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
217 okButton
->setShortcut(Qt::CTRL
| Qt::Key_Return
);
218 okButton
->setDefault(true);
220 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
221 applyButton
->setEnabled(false);
222 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
223 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
224 applyButton
->setEnabled(isDirty
);
227 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
228 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
233 ViewPropertiesDialog::~ViewPropertiesDialog()
237 m_viewProps
= nullptr;
239 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
240 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
243 void ViewPropertiesDialog::accept()
245 applyViewProperties();
249 void ViewPropertiesDialog::slotApply()
251 applyViewProperties();
255 void ViewPropertiesDialog::slotViewModeChanged(int index
)
257 const QVariant itemData
= m_viewMode
->itemData(index
);
258 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
259 m_viewProps
->setViewMode(viewMode
);
263 void ViewPropertiesDialog::slotSortingChanged(int index
)
265 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
266 m_viewProps
->setSortRole(role
);
270 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
272 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
273 m_viewProps
->setSortOrder(sortOrder
);
277 void ViewPropertiesDialog::slotGroupedSortingChanged()
279 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
283 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
285 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
286 m_viewProps
->setSortFoldersFirst(foldersFirst
);
290 void ViewPropertiesDialog::slotSortHiddenLastChanged()
292 const bool hiddenLast
= m_sortHiddenLast
->isChecked();
293 m_viewProps
->setSortHiddenLast(hiddenLast
);
297 void ViewPropertiesDialog::slotShowPreviewChanged()
299 const bool show
= m_previewsShown
->isChecked();
300 m_viewProps
->setPreviewsShown(show
);
304 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
306 const bool show
= m_showHiddenFiles
->isChecked();
307 m_viewProps
->setHiddenFilesShown(show
);
311 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
317 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
319 if (m_isDirty
!= isDirty
) {
321 Q_EMIT
isDirtyChanged(isDirty
);
325 void ViewPropertiesDialog::applyViewProperties()
327 // if nothing changed in the dialog, we have nothing to apply
332 // Update visible roles.
334 QList
<QByteArray
> visibleRoles
;
336 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
337 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
338 const QListWidgetItem
* item
= m_listWidget
->item(index
);
339 if (item
->checkState() == Qt::Checked
) {
340 visibleRoles
.append(info
.role
);
345 m_viewProps
->setVisibleRoles(visibleRoles
);
348 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
349 if (applyToSubFolders
) {
350 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
351 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
355 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
356 m_dolphinView
->url(),
358 info
->setAttribute(Qt::WA_DeleteOnClose
);
359 info
->setWindowModality(Qt::NonModal
);
363 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
365 // If the user selected 'Apply To All Folders' the view properties implicitly
366 // are also used as default for new folders.
367 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
369 // For directories where no .directory file is available, the .directory
370 // file stored for the global view properties is used as fallback. To update
371 // this file we temporary turn on the global view properties mode.
372 Q_ASSERT(!GeneralSettings::globalViewProps());
374 GeneralSettings::setGlobalViewProps(true);
375 ViewProperties
defaultProps(m_dolphinView
->url());
376 defaultProps
.setDirProperties(*m_viewProps
);
378 GeneralSettings::setGlobalViewProps(false);
381 if (applyToAllFolders
) {
382 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
383 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
387 // Updating the global view properties time stamp in the general settings makes
388 // all existing viewproperties invalid, as they have a smaller time stamp.
389 GeneralSettings
* settings
= GeneralSettings::self();
390 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
394 m_dolphinView
->setMode(m_viewProps
->viewMode());
395 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
396 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
397 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
398 m_dolphinView
->setSortHiddenLast(m_viewProps
->sortHiddenLast());
399 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
400 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
401 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
402 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
409 void ViewPropertiesDialog::loadSettings()
412 switch (m_viewProps
->viewMode()) {
413 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
414 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
415 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
419 // Load sort order and sorting
420 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
421 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
423 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
424 int sortRoleIndex
= 0;
425 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
426 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
431 m_sorting
->setCurrentIndex(sortRoleIndex
);
433 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
434 m_sortHiddenLast
->setChecked(m_viewProps
->sortHiddenLast());
436 // Load show preview, show in groups and show hidden files settings
437 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
438 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
439 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());