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"
12 #include "kitemviews/kfileitemmodel.h"
13 #include "viewpropsprogressinfo.h"
14 #include "views/dolphinview.h"
16 #include <KCollapsibleGroupBox>
17 #include <KLocalizedString>
18 #include <KMessageBox>
19 #include <KWindowConfig>
22 #include <Baloo/IndexerConfig>
25 #include <QButtonGroup>
28 #include <QFormLayout>
30 #include <QListWidget>
31 #include <QPushButton>
32 #include <QRadioButton>
33 #include <QSpacerItem>
35 #include <views/viewproperties.h>
37 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
40 m_dolphinView(dolphinView
),
45 m_sortFoldersFirst(nullptr),
46 m_sortHiddenLast(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_sortHiddenLast
= new QCheckBox(i18nc("@option:check", "Show hidden files last"));
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(additionalInfoBox
);
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 for (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 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
137 sortingLayout
->setContentsMargins(0, 0, 0, 0);
138 sortingLayout
->addWidget(m_sortOrder
);
139 sortingLayout
->addWidget(m_sorting
);
141 layout
->addRow(i18nc("@label:listbox", "View mode:"), m_viewMode
);
142 layout
->addRow(i18nc("@label:listbox", "Sorting:"), sortingLayout
);
144 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
146 layout
->addRow(i18n("View options:"), m_sortFoldersFirst
);
147 layout
->addRow(QString(), m_previewsShown
);
148 layout
->addRow(QString(), m_showInGroups
);
149 layout
->addRow(QString(), m_showHiddenFiles
);
150 layout
->addRow(QString(), m_sortHiddenLast
);
152 connect(m_viewMode
, &QComboBox::currentIndexChanged
,
153 this, &ViewPropertiesDialog::slotViewModeChanged
);
154 connect(m_sorting
, &QComboBox::currentIndexChanged
,
155 this, &ViewPropertiesDialog::slotSortingChanged
);
156 connect(m_sortOrder
, &QComboBox::currentIndexChanged
,
157 this, &ViewPropertiesDialog::slotSortOrderChanged
);
158 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
159 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
160 connect(m_sortHiddenLast
, &QCheckBox::clicked
,
161 this, &ViewPropertiesDialog::slotSortHiddenLastChanged
);
162 connect(m_previewsShown
, &QCheckBox::clicked
,
163 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
164 connect(m_showInGroups
, &QCheckBox::clicked
,
165 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
166 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
167 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
169 // Only show the following settings if the view properties are remembered
170 // for each directory:
171 if (!useGlobalViewProps
) {
172 // create 'Apply View Properties To' group
173 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
175 m_applyToCurrentFolder
->setChecked(true);
176 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
177 "Current folder and sub-folders"));
178 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
181 QButtonGroup
* applyGroup
= new QButtonGroup(this);
182 applyGroup
->addButton(m_applyToCurrentFolder
);
183 applyGroup
->addButton(m_applyToSubFolders
);
184 applyGroup
->addButton(m_applyToAllFolders
);
186 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
188 layout
->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder
);
189 layout
->addRow(QString(), m_applyToSubFolders
);
190 layout
->addRow(QString(), m_applyToAllFolders
);
191 layout
->addRow(QString(), m_applyToAllFolders
);
193 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
194 layout
->addRow(QString(), m_useAsDefault
);
196 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
197 this, &ViewPropertiesDialog::markAsDirty
);
198 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
199 this, &ViewPropertiesDialog::markAsDirty
);
200 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
201 this, &ViewPropertiesDialog::markAsDirty
);
202 connect(m_useAsDefault
, &QCheckBox::clicked
,
203 this, &ViewPropertiesDialog::markAsDirty
);
206 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
208 layout
->addRow(additionalInfoBox
);
210 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
211 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
212 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
213 layout
->addWidget(buttonBox
);
215 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
216 okButton
->setShortcut(Qt::CTRL
| Qt::Key_Return
);
217 okButton
->setDefault(true);
219 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
220 applyButton
->setEnabled(false);
221 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
222 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
223 applyButton
->setEnabled(isDirty
);
226 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
227 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
232 ViewPropertiesDialog::~ViewPropertiesDialog()
236 m_viewProps
= nullptr;
238 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
239 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
242 void ViewPropertiesDialog::accept()
244 applyViewProperties();
248 void ViewPropertiesDialog::slotApply()
250 applyViewProperties();
254 void ViewPropertiesDialog::slotViewModeChanged(int index
)
256 const QVariant itemData
= m_viewMode
->itemData(index
);
257 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
258 m_viewProps
->setViewMode(viewMode
);
262 void ViewPropertiesDialog::slotSortingChanged(int index
)
264 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
265 m_viewProps
->setSortRole(role
);
269 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
271 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
272 m_viewProps
->setSortOrder(sortOrder
);
276 void ViewPropertiesDialog::slotGroupedSortingChanged()
278 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
282 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
284 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
285 m_viewProps
->setSortFoldersFirst(foldersFirst
);
289 void ViewPropertiesDialog::slotSortHiddenLastChanged()
291 const bool hiddenLast
= m_sortHiddenLast
->isChecked();
292 m_viewProps
->setSortHiddenLast(hiddenLast
);
296 void ViewPropertiesDialog::slotShowPreviewChanged()
298 const bool show
= m_previewsShown
->isChecked();
299 m_viewProps
->setPreviewsShown(show
);
303 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
305 const bool show
= m_showHiddenFiles
->isChecked();
306 m_viewProps
->setHiddenFilesShown(show
);
310 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
316 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
318 if (m_isDirty
!= isDirty
) {
320 Q_EMIT
isDirtyChanged(isDirty
);
324 void ViewPropertiesDialog::applyViewProperties()
326 // if nothing changed in the dialog, we have nothing to apply
331 // Update visible roles.
333 QList
<QByteArray
> visibleRoles
;
335 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
336 for (const KFileItemModel::RoleInfo
& info
: rolesInfo
) {
337 const QListWidgetItem
* item
= m_listWidget
->item(index
);
338 if (item
->checkState() == Qt::Checked
) {
339 visibleRoles
.append(info
.role
);
344 m_viewProps
->setVisibleRoles(visibleRoles
);
347 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
348 if (applyToSubFolders
) {
349 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
350 if (KMessageBox::questionYesNo(this, text
, {},
351 KStandardGuiItem::cont(),
352 KStandardGuiItem::cancel()) == KMessageBox::No
) {
356 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
357 m_dolphinView
->url(),
359 info
->setAttribute(Qt::WA_DeleteOnClose
);
360 info
->setWindowModality(Qt::NonModal
);
364 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
366 // If the user selected 'Apply To All Folders' the view properties implicitly
367 // are also used as default for new folders.
368 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
370 // For directories where no .directory file is available, the .directory
371 // file stored for the global view properties is used as fallback. To update
372 // this file we temporary turn on the global view properties mode.
373 Q_ASSERT(!GeneralSettings::globalViewProps());
375 GeneralSettings::setGlobalViewProps(true);
376 ViewProperties
defaultProps(m_dolphinView
->url());
377 defaultProps
.setDirProperties(*m_viewProps
);
379 GeneralSettings::setGlobalViewProps(false);
382 if (applyToAllFolders
) {
383 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
384 if (KMessageBox::questionYesNo(this, text
, {},
385 KStandardGuiItem::cont(),
386 KStandardGuiItem::cancel()) == KMessageBox::No
) {
390 // Updating the global view properties time stamp in the general settings makes
391 // all existing viewproperties invalid, as they have a smaller time stamp.
392 GeneralSettings
* settings
= GeneralSettings::self();
393 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
397 m_dolphinView
->setViewMode(m_viewProps
->viewMode());
398 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
399 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
400 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
401 m_dolphinView
->setSortHiddenLast(m_viewProps
->sortHiddenLast());
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());
412 void ViewPropertiesDialog::loadSettings()
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;
422 // Load sort order and sorting
423 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
424 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
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()) {
434 m_sorting
->setCurrentIndex(sortRoleIndex
);
436 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
437 m_sortHiddenLast
->setChecked(m_viewProps
->sortHiddenLast());
439 // Load show preview, show in groups and show hidden files settings
440 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
441 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
442 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());