1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2018 by Elvis Angelaccio <elvis.angelaccio@kde.org> *
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. *
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. *
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 ***************************************************************************/
21 #include "viewpropertiesdialog.h"
23 #include "dolphin_generalsettings.h"
24 #include "dolphin_iconsmodesettings.h"
26 #include "kitemviews/kfileitemmodel.h"
27 #include "viewpropsprogressinfo.h"
28 #include "views/dolphinview.h"
30 #include <KCollapsibleGroupBox>
31 #include <KLocalizedString>
32 #include <KMessageBox>
33 #include <KWindowConfig>
36 #include <Baloo/IndexerConfig>
39 #include <QButtonGroup>
42 #include <QFormLayout>
44 #include <QListWidget>
45 #include <QPushButton>
46 #include <QRadioButton>
47 #include <QSpacerItem>
49 #include <views/viewproperties.h>
51 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
54 m_dolphinView(dolphinView
),
59 m_sortFoldersFirst(nullptr),
60 m_previewsShown(nullptr),
61 m_showInGroups(nullptr),
62 m_showHiddenFiles(nullptr),
63 m_applyToCurrentFolder(nullptr),
64 m_applyToSubFolders(nullptr),
65 m_applyToAllFolders(nullptr),
66 m_useAsDefault(nullptr)
68 Q_ASSERT(dolphinView
);
69 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
71 setWindowTitle(i18nc("@title:window", "View Display Style"));
73 const QUrl
& url
= dolphinView
->url();
74 m_viewProps
= new ViewProperties(url
);
75 m_viewProps
->setAutoSaveEnabled(false);
77 auto layout
= new QFormLayout(this);
78 // Otherwise the dialog won't resize when we collapse the KCollapsibleGroupBox.
79 layout
->setSizeConstraint(QLayout::SetFixedSize
);
82 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
83 m_viewMode
= new QComboBox();
84 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-icons")), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
85 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-details")), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
86 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-tree")), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
88 m_sortOrder
= new QComboBox();
89 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
90 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
92 m_sorting
= new QComboBox();
93 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
94 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
95 m_sorting
->addItem(info
.translation
, info
.role
);
98 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
99 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
100 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
101 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
103 auto additionalInfoBox
= new KCollapsibleGroupBox();
104 additionalInfoBox
->setTitle(i18nc("@title:group", "Additional Information"));
105 auto innerLayout
= new QVBoxLayout();
108 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
109 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
110 if (useDefaultRoles
) {
111 // Using the details view without any additional information (-> additional column)
112 // makes no sense and leads to a usability problem as no viewport area is available
113 // anymore. Hence as fallback provide at least a size and date column.
114 visibleRoles
.clear();
115 visibleRoles
.append("text");
116 visibleRoles
.append("size");
117 visibleRoles
.append("modificationtime");
118 m_viewProps
->setVisibleRoles(visibleRoles
);
122 bool indexingEnabled
= false;
124 Baloo::IndexerConfig config
;
125 indexingEnabled
= config
.fileIndexingEnabled();
128 m_listWidget
= new QListWidget();
129 connect(m_listWidget
, &QListWidget::itemChanged
, this, &ViewPropertiesDialog::slotItemChanged
);
130 m_listWidget
->setSelectionMode(QAbstractItemView::NoSelection
);
131 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
132 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
133 QListWidgetItem
* item
= new QListWidgetItem(info
.translation
, m_listWidget
);
134 item
->setCheckState(visibleRoles
.contains(info
.role
) ? Qt::Checked
: Qt::Unchecked
);
136 const bool enable
= ((!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
137 (info
.requiresBaloo
) ||
138 (info
.requiresIndexer
&& indexingEnabled
)) && info
.role
!= "text";
141 item
->setFlags(item
->flags() & ~Qt::ItemIsEnabled
);
144 QLabel
* additionalViewOptionsLabel
= new QLabel(i18n("Choose what to see on each file or folder:"));
145 innerLayout
->addWidget(additionalViewOptionsLabel
);
146 innerLayout
->addWidget(m_listWidget
);
149 additionalInfoBox
->setLayout(innerLayout
);
151 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
152 sortingLayout
->setContentsMargins(0, 0, 0, 0);
153 sortingLayout
->addWidget(m_sortOrder
);
154 sortingLayout
->addWidget(m_sorting
);
156 layout
->addRow(i18nc("@label:listbox", "View mode:"), m_viewMode
);
157 layout
->addRow(i18nc("@label:listbox", "Sorting:"), sortingLayout
);
159 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
161 layout
->addRow(i18n("View options:"), m_sortFoldersFirst
);
162 layout
->addRow(QString(), m_previewsShown
);
163 layout
->addRow(QString(), m_showInGroups
);
164 layout
->addRow(QString(), m_showHiddenFiles
);
166 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
167 connect(m_viewMode
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
169 connect(m_viewMode
, QOverload
<int, const QString
&>::of(&QComboBox::currentIndexChanged
),
171 this, &ViewPropertiesDialog::slotViewModeChanged
);
172 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
173 connect(m_sorting
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
175 connect(m_sorting
, QOverload
<int, const QString
&>::of(&QComboBox::currentIndexChanged
),
177 this, &ViewPropertiesDialog::slotSortingChanged
);
178 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
179 connect(m_sortOrder
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
181 connect(m_sortOrder
, QOverload
<int, const QString
&>::of(&QComboBox::currentIndexChanged
),
183 this, &ViewPropertiesDialog::slotSortOrderChanged
);
184 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
185 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
186 connect(m_previewsShown
, &QCheckBox::clicked
,
187 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
188 connect(m_showInGroups
, &QCheckBox::clicked
,
189 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
190 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
191 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
193 // Only show the following settings if the view properties are remembered
194 // for each directory:
195 if (!useGlobalViewProps
) {
196 // create 'Apply View Properties To' group
197 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
199 m_applyToCurrentFolder
->setChecked(true);
200 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
201 "Current folder and sub-folders"));
202 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
205 QButtonGroup
* applyGroup
= new QButtonGroup(this);
206 applyGroup
->addButton(m_applyToCurrentFolder
);
207 applyGroup
->addButton(m_applyToSubFolders
);
208 applyGroup
->addButton(m_applyToAllFolders
);
210 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
212 layout
->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder
);
213 layout
->addRow(QString(), m_applyToSubFolders
);
214 layout
->addRow(QString(), m_applyToAllFolders
);
215 layout
->addRow(QString(), m_applyToAllFolders
);
217 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
218 layout
->addRow(QString(), m_useAsDefault
);
220 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
221 this, &ViewPropertiesDialog::markAsDirty
);
222 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
223 this, &ViewPropertiesDialog::markAsDirty
);
224 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
225 this, &ViewPropertiesDialog::markAsDirty
);
226 connect(m_useAsDefault
, &QCheckBox::clicked
,
227 this, &ViewPropertiesDialog::markAsDirty
);
230 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
232 layout
->addRow(additionalInfoBox
);
234 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
235 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
236 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
237 layout
->addWidget(buttonBox
);
239 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
240 okButton
->setShortcut(Qt::CTRL
+ Qt::Key_Return
);
241 okButton
->setDefault(true);
243 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
244 applyButton
->setEnabled(false);
245 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
246 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
247 applyButton
->setEnabled(isDirty
);
250 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
251 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
256 ViewPropertiesDialog::~ViewPropertiesDialog()
260 m_viewProps
= nullptr;
262 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
263 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
266 void ViewPropertiesDialog::accept()
268 applyViewProperties();
272 void ViewPropertiesDialog::slotApply()
274 applyViewProperties();
278 void ViewPropertiesDialog::slotViewModeChanged(int index
)
280 const QVariant itemData
= m_viewMode
->itemData(index
);
281 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
282 m_viewProps
->setViewMode(viewMode
);
286 void ViewPropertiesDialog::slotSortingChanged(int index
)
288 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
289 m_viewProps
->setSortRole(role
);
293 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
295 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
296 m_viewProps
->setSortOrder(sortOrder
);
300 void ViewPropertiesDialog::slotGroupedSortingChanged()
302 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
306 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
308 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
309 m_viewProps
->setSortFoldersFirst(foldersFirst
);
313 void ViewPropertiesDialog::slotShowPreviewChanged()
315 const bool show
= m_previewsShown
->isChecked();
316 m_viewProps
->setPreviewsShown(show
);
320 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
322 const bool show
= m_showHiddenFiles
->isChecked();
323 m_viewProps
->setHiddenFilesShown(show
);
327 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
333 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
335 if (m_isDirty
!= isDirty
) {
337 emit
isDirtyChanged(isDirty
);
341 void ViewPropertiesDialog::applyViewProperties()
343 // if nothing changed in the dialog, we have nothing to apply
348 // Update visible roles.
350 QList
<QByteArray
> visibleRoles
;
352 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
353 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
354 const QListWidgetItem
* item
= m_listWidget
->item(index
);
355 if (item
->checkState() == Qt::Checked
) {
356 visibleRoles
.append(info
.role
);
361 m_viewProps
->setVisibleRoles(visibleRoles
);
364 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
365 if (applyToSubFolders
) {
366 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
367 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
371 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
372 m_dolphinView
->url(),
374 info
->setAttribute(Qt::WA_DeleteOnClose
);
375 info
->setWindowModality(Qt::NonModal
);
379 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
381 // If the user selected 'Apply To All Folders' the view properties implicitly
382 // are also used as default for new folders.
383 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
385 // For directories where no .directory file is available, the .directory
386 // file stored for the global view properties is used as fallback. To update
387 // this file we temporary turn on the global view properties mode.
388 Q_ASSERT(!GeneralSettings::globalViewProps());
390 GeneralSettings::setGlobalViewProps(true);
391 ViewProperties
defaultProps(m_dolphinView
->url());
392 defaultProps
.setDirProperties(*m_viewProps
);
394 GeneralSettings::setGlobalViewProps(false);
397 if (applyToAllFolders
) {
398 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
399 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
403 // Updating the global view properties time stamp in the general settings makes
404 // all existing viewproperties invalid, as they have a smaller time stamp.
405 GeneralSettings
* settings
= GeneralSettings::self();
406 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
410 m_dolphinView
->setMode(m_viewProps
->viewMode());
411 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
412 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
413 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
414 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
415 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
416 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
417 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
424 void ViewPropertiesDialog::loadSettings()
427 switch (m_viewProps
->viewMode()) {
428 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
429 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
430 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
434 // Load sort order and sorting
435 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
436 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
438 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
439 int sortRoleIndex
= 0;
440 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
441 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
446 m_sorting
->setCurrentIndex(sortRoleIndex
);
448 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
450 // Load show preview, show in groups and show hidden files settings
451 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
452 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
453 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());