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
);
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 foreach (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_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
87 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
88 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
90 auto additionalInfoBox
= new KCollapsibleGroupBox();
91 additionalInfoBox
->setTitle(i18nc("@title:group", "Additional Information"));
92 auto innerLayout
= new QVBoxLayout();
95 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
96 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
97 if (useDefaultRoles
) {
98 // Using the details view without any additional information (-> additional column)
99 // makes no sense and leads to a usability problem as no viewport area is available
100 // anymore. Hence as fallback provide at least a size and date column.
101 visibleRoles
.clear();
102 visibleRoles
.append("text");
103 visibleRoles
.append("size");
104 visibleRoles
.append("modificationtime");
105 m_viewProps
->setVisibleRoles(visibleRoles
);
109 bool indexingEnabled
= false;
111 Baloo::IndexerConfig config
;
112 indexingEnabled
= config
.fileIndexingEnabled();
115 m_listWidget
= new QListWidget();
116 connect(m_listWidget
, &QListWidget::itemChanged
, this, &ViewPropertiesDialog::slotItemChanged
);
117 m_listWidget
->setSelectionMode(QAbstractItemView::NoSelection
);
118 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
119 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
120 QListWidgetItem
* item
= new QListWidgetItem(info
.translation
, m_listWidget
);
121 item
->setCheckState(visibleRoles
.contains(info
.role
) ? Qt::Checked
: Qt::Unchecked
);
123 const bool enable
= ((!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
124 (info
.requiresBaloo
) ||
125 (info
.requiresIndexer
&& indexingEnabled
)) && info
.role
!= "text";
128 item
->setFlags(item
->flags() & ~Qt::ItemIsEnabled
);
131 QLabel
* additionalViewOptionsLabel
= new QLabel(i18n("Choose what to see on each file or folder:"));
132 innerLayout
->addWidget(additionalViewOptionsLabel
);
133 innerLayout
->addWidget(m_listWidget
);
136 additionalInfoBox
->setLayout(innerLayout
);
138 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
139 sortingLayout
->setContentsMargins(0, 0, 0, 0);
140 sortingLayout
->addWidget(m_sortOrder
);
141 sortingLayout
->addWidget(m_sorting
);
143 layout
->addRow(i18nc("@label:listbox", "View mode:"), m_viewMode
);
144 layout
->addRow(i18nc("@label:listbox", "Sorting:"), sortingLayout
);
146 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
148 layout
->addRow(i18n("View options:"), m_sortFoldersFirst
);
149 layout
->addRow(QString(), m_previewsShown
);
150 layout
->addRow(QString(), m_showInGroups
);
151 layout
->addRow(QString(), m_showHiddenFiles
);
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_previewsShown
, &QCheckBox::clicked
,
162 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
163 connect(m_showInGroups
, &QCheckBox::clicked
,
164 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
165 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
166 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
168 // Only show the following settings if the view properties are remembered
169 // for each directory:
170 if (!useGlobalViewProps
) {
171 // create 'Apply View Properties To' group
172 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
174 m_applyToCurrentFolder
->setChecked(true);
175 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
176 "Current folder and sub-folders"));
177 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
180 QButtonGroup
* applyGroup
= new QButtonGroup(this);
181 applyGroup
->addButton(m_applyToCurrentFolder
);
182 applyGroup
->addButton(m_applyToSubFolders
);
183 applyGroup
->addButton(m_applyToAllFolders
);
185 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
187 layout
->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder
);
188 layout
->addRow(QString(), m_applyToSubFolders
);
189 layout
->addRow(QString(), m_applyToAllFolders
);
190 layout
->addRow(QString(), m_applyToAllFolders
);
192 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
193 layout
->addRow(QString(), m_useAsDefault
);
195 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
196 this, &ViewPropertiesDialog::markAsDirty
);
197 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
198 this, &ViewPropertiesDialog::markAsDirty
);
199 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
200 this, &ViewPropertiesDialog::markAsDirty
);
201 connect(m_useAsDefault
, &QCheckBox::clicked
,
202 this, &ViewPropertiesDialog::markAsDirty
);
205 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
207 layout
->addRow(additionalInfoBox
);
209 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
210 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
211 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
212 layout
->addWidget(buttonBox
);
214 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
215 okButton
->setShortcut(Qt::CTRL
+ Qt::Key_Return
);
216 okButton
->setDefault(true);
218 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
219 applyButton
->setEnabled(false);
220 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
221 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
222 applyButton
->setEnabled(isDirty
);
225 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
226 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
231 ViewPropertiesDialog::~ViewPropertiesDialog()
235 m_viewProps
= nullptr;
237 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
238 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
241 void ViewPropertiesDialog::accept()
243 applyViewProperties();
247 void ViewPropertiesDialog::slotApply()
249 applyViewProperties();
253 void ViewPropertiesDialog::slotViewModeChanged(int index
)
255 const QVariant itemData
= m_viewMode
->itemData(index
);
256 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
257 m_viewProps
->setViewMode(viewMode
);
261 void ViewPropertiesDialog::slotSortingChanged(int index
)
263 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
264 m_viewProps
->setSortRole(role
);
268 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
270 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
271 m_viewProps
->setSortOrder(sortOrder
);
275 void ViewPropertiesDialog::slotGroupedSortingChanged()
277 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
281 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
283 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
284 m_viewProps
->setSortFoldersFirst(foldersFirst
);
288 void ViewPropertiesDialog::slotShowPreviewChanged()
290 const bool show
= m_previewsShown
->isChecked();
291 m_viewProps
->setPreviewsShown(show
);
295 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
297 const bool show
= m_showHiddenFiles
->isChecked();
298 m_viewProps
->setHiddenFilesShown(show
);
302 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
308 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
310 if (m_isDirty
!= isDirty
) {
312 emit
isDirtyChanged(isDirty
);
316 void ViewPropertiesDialog::applyViewProperties()
318 // if nothing changed in the dialog, we have nothing to apply
323 // Update visible roles.
325 QList
<QByteArray
> visibleRoles
;
327 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
328 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
329 const QListWidgetItem
* item
= m_listWidget
->item(index
);
330 if (item
->checkState() == Qt::Checked
) {
331 visibleRoles
.append(info
.role
);
336 m_viewProps
->setVisibleRoles(visibleRoles
);
339 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
340 if (applyToSubFolders
) {
341 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
342 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
346 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
347 m_dolphinView
->url(),
349 info
->setAttribute(Qt::WA_DeleteOnClose
);
350 info
->setWindowModality(Qt::NonModal
);
354 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
356 // If the user selected 'Apply To All Folders' the view properties implicitly
357 // are also used as default for new folders.
358 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
360 // For directories where no .directory file is available, the .directory
361 // file stored for the global view properties is used as fallback. To update
362 // this file we temporary turn on the global view properties mode.
363 Q_ASSERT(!GeneralSettings::globalViewProps());
365 GeneralSettings::setGlobalViewProps(true);
366 ViewProperties
defaultProps(m_dolphinView
->url());
367 defaultProps
.setDirProperties(*m_viewProps
);
369 GeneralSettings::setGlobalViewProps(false);
372 if (applyToAllFolders
) {
373 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
374 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
378 // Updating the global view properties time stamp in the general settings makes
379 // all existing viewproperties invalid, as they have a smaller time stamp.
380 GeneralSettings
* settings
= GeneralSettings::self();
381 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
385 m_dolphinView
->setMode(m_viewProps
->viewMode());
386 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
387 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
388 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
389 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
390 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
391 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
392 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
399 void ViewPropertiesDialog::loadSettings()
402 switch (m_viewProps
->viewMode()) {
403 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
404 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
405 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
409 // Load sort order and sorting
410 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
411 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
413 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
414 int sortRoleIndex
= 0;
415 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
416 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
421 m_sorting
->setCurrentIndex(sortRoleIndex
);
423 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
425 // Load show preview, show in groups and show hidden files settings
426 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
427 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
428 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());