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_previewsShown(nullptr),
48 m_showInGroups(nullptr),
49 m_showHiddenFiles(nullptr),
50 m_applyToCurrentFolder(nullptr),
51 m_applyToSubFolders(nullptr),
52 m_applyToAllFolders(nullptr),
53 m_useAsDefault(nullptr)
55 Q_ASSERT(dolphinView
);
56 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
58 setWindowTitle(i18nc("@title:window", "View Display Style"));
60 const QUrl
& url
= dolphinView
->url();
61 m_viewProps
= new ViewProperties(url
);
62 m_viewProps
->setAutoSaveEnabled(false);
64 auto layout
= new QFormLayout(this);
65 // Otherwise the dialog won't resize when we collapse the KCollapsibleGroupBox.
66 layout
->setSizeConstraint(QLayout::SetFixedSize
);
68 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
69 m_viewMode
= new QComboBox();
70 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-icons")), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
71 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-details")), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
72 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-tree")), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
74 m_sortOrder
= new QComboBox();
75 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
76 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
78 m_sorting
= new QComboBox();
79 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
80 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
81 m_sorting
->addItem(info
.translation
, info
.role
);
84 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
85 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
86 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
87 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
89 auto additionalInfoBox
= new KCollapsibleGroupBox();
90 additionalInfoBox
->setTitle(i18nc("@title:group", "Additional Information"));
91 auto innerLayout
= new QVBoxLayout(additionalInfoBox
);
94 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
95 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
96 if (useDefaultRoles
) {
97 // Using the details view without any additional information (-> additional column)
98 // makes no sense and leads to a usability problem as no viewport area is available
99 // anymore. Hence as fallback provide at least a size and date column.
100 visibleRoles
.clear();
101 visibleRoles
.append("text");
102 visibleRoles
.append("size");
103 visibleRoles
.append("modificationtime");
104 m_viewProps
->setVisibleRoles(visibleRoles
);
108 bool indexingEnabled
= false;
110 Baloo::IndexerConfig config
;
111 indexingEnabled
= config
.fileIndexingEnabled();
114 m_listWidget
= new QListWidget();
115 connect(m_listWidget
, &QListWidget::itemChanged
, this, &ViewPropertiesDialog::slotItemChanged
);
116 m_listWidget
->setSelectionMode(QAbstractItemView::NoSelection
);
117 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
118 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
119 QListWidgetItem
* item
= new QListWidgetItem(info
.translation
, m_listWidget
);
120 item
->setCheckState(visibleRoles
.contains(info
.role
) ? Qt::Checked
: Qt::Unchecked
);
122 const bool enable
= ((!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
123 (info
.requiresBaloo
) ||
124 (info
.requiresIndexer
&& indexingEnabled
)) && info
.role
!= "text";
127 item
->setFlags(item
->flags() & ~Qt::ItemIsEnabled
);
130 QLabel
* additionalViewOptionsLabel
= new QLabel(i18n("Choose what to see on each file or folder:"));
131 innerLayout
->addWidget(additionalViewOptionsLabel
);
132 innerLayout
->addWidget(m_listWidget
);
135 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
136 sortingLayout
->setContentsMargins(0, 0, 0, 0);
137 sortingLayout
->addWidget(m_sortOrder
);
138 sortingLayout
->addWidget(m_sorting
);
140 layout
->addRow(i18nc("@label:listbox", "View mode:"), m_viewMode
);
141 layout
->addRow(i18nc("@label:listbox", "Sorting:"), sortingLayout
);
143 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
145 layout
->addRow(i18n("View options:"), m_sortFoldersFirst
);
146 layout
->addRow(QString(), m_previewsShown
);
147 layout
->addRow(QString(), m_showInGroups
);
148 layout
->addRow(QString(), m_showHiddenFiles
);
150 connect(m_viewMode
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
151 this, &ViewPropertiesDialog::slotViewModeChanged
);
152 connect(m_sorting
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
153 this, &ViewPropertiesDialog::slotSortingChanged
);
154 connect(m_sortOrder
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
155 this, &ViewPropertiesDialog::slotSortOrderChanged
);
156 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
157 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
158 connect(m_previewsShown
, &QCheckBox::clicked
,
159 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
160 connect(m_showInGroups
, &QCheckBox::clicked
,
161 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
162 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
163 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
165 // Only show the following settings if the view properties are remembered
166 // for each directory:
167 if (!useGlobalViewProps
) {
168 // create 'Apply View Properties To' group
169 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
171 m_applyToCurrentFolder
->setChecked(true);
172 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
173 "Current folder and sub-folders"));
174 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
177 QButtonGroup
* applyGroup
= new QButtonGroup(this);
178 applyGroup
->addButton(m_applyToCurrentFolder
);
179 applyGroup
->addButton(m_applyToSubFolders
);
180 applyGroup
->addButton(m_applyToAllFolders
);
182 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
184 layout
->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder
);
185 layout
->addRow(QString(), m_applyToSubFolders
);
186 layout
->addRow(QString(), m_applyToAllFolders
);
187 layout
->addRow(QString(), m_applyToAllFolders
);
189 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
190 layout
->addRow(QString(), m_useAsDefault
);
192 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
193 this, &ViewPropertiesDialog::markAsDirty
);
194 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
195 this, &ViewPropertiesDialog::markAsDirty
);
196 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
197 this, &ViewPropertiesDialog::markAsDirty
);
198 connect(m_useAsDefault
, &QCheckBox::clicked
,
199 this, &ViewPropertiesDialog::markAsDirty
);
202 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
204 layout
->addRow(additionalInfoBox
);
206 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
207 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
208 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
209 layout
->addWidget(buttonBox
);
211 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
212 okButton
->setShortcut(Qt::CTRL
+ Qt::Key_Return
);
213 okButton
->setDefault(true);
215 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
216 applyButton
->setEnabled(false);
217 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
218 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
219 applyButton
->setEnabled(isDirty
);
222 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
223 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
228 ViewPropertiesDialog::~ViewPropertiesDialog()
232 m_viewProps
= nullptr;
234 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
235 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
238 void ViewPropertiesDialog::accept()
240 applyViewProperties();
244 void ViewPropertiesDialog::slotApply()
246 applyViewProperties();
250 void ViewPropertiesDialog::slotViewModeChanged(int index
)
252 const QVariant itemData
= m_viewMode
->itemData(index
);
253 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
254 m_viewProps
->setViewMode(viewMode
);
258 void ViewPropertiesDialog::slotSortingChanged(int index
)
260 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
261 m_viewProps
->setSortRole(role
);
265 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
267 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
268 m_viewProps
->setSortOrder(sortOrder
);
272 void ViewPropertiesDialog::slotGroupedSortingChanged()
274 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
278 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
280 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
281 m_viewProps
->setSortFoldersFirst(foldersFirst
);
285 void ViewPropertiesDialog::slotShowPreviewChanged()
287 const bool show
= m_previewsShown
->isChecked();
288 m_viewProps
->setPreviewsShown(show
);
292 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
294 const bool show
= m_showHiddenFiles
->isChecked();
295 m_viewProps
->setHiddenFilesShown(show
);
299 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
305 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
307 if (m_isDirty
!= isDirty
) {
309 Q_EMIT
isDirtyChanged(isDirty
);
313 void ViewPropertiesDialog::applyViewProperties()
315 // if nothing changed in the dialog, we have nothing to apply
320 // Update visible roles.
322 QList
<QByteArray
> visibleRoles
;
324 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
325 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
326 const QListWidgetItem
* item
= m_listWidget
->item(index
);
327 if (item
->checkState() == Qt::Checked
) {
328 visibleRoles
.append(info
.role
);
333 m_viewProps
->setVisibleRoles(visibleRoles
);
336 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
337 if (applyToSubFolders
) {
338 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
339 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
343 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
344 m_dolphinView
->url(),
346 info
->setAttribute(Qt::WA_DeleteOnClose
);
347 info
->setWindowModality(Qt::NonModal
);
351 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
353 // If the user selected 'Apply To All Folders' the view properties implicitly
354 // are also used as default for new folders.
355 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
357 // For directories where no .directory file is available, the .directory
358 // file stored for the global view properties is used as fallback. To update
359 // this file we temporary turn on the global view properties mode.
360 Q_ASSERT(!GeneralSettings::globalViewProps());
362 GeneralSettings::setGlobalViewProps(true);
363 ViewProperties
defaultProps(m_dolphinView
->url());
364 defaultProps
.setDirProperties(*m_viewProps
);
366 GeneralSettings::setGlobalViewProps(false);
369 if (applyToAllFolders
) {
370 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
371 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
375 // Updating the global view properties time stamp in the general settings makes
376 // all existing viewproperties invalid, as they have a smaller time stamp.
377 GeneralSettings
* settings
= GeneralSettings::self();
378 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
382 m_dolphinView
->setMode(m_viewProps
->viewMode());
383 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
384 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
385 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
386 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
387 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
388 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
389 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
396 void ViewPropertiesDialog::loadSettings()
399 switch (m_viewProps
->viewMode()) {
400 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
401 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
402 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
406 // Load sort order and sorting
407 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
408 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
410 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
411 int sortRoleIndex
= 0;
412 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
413 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
418 m_sorting
->setCurrentIndex(sortRoleIndex
);
420 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
422 // Load show preview, show in groups and show hidden files settings
423 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
424 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
425 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());