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 Properties"));
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 connect(m_viewMode
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
167 this, &ViewPropertiesDialog::slotViewModeChanged
);
168 connect(m_sorting
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
169 this, &ViewPropertiesDialog::slotSortingChanged
);
170 connect(m_sortOrder
, QOverload
<int>::of(&QComboBox::currentIndexChanged
),
171 this, &ViewPropertiesDialog::slotSortOrderChanged
);
172 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
173 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
174 connect(m_previewsShown
, &QCheckBox::clicked
,
175 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
176 connect(m_showInGroups
, &QCheckBox::clicked
,
177 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
178 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
179 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
181 // Only show the following settings if the view properties are remembered
182 // for each directory:
183 if (!useGlobalViewProps
) {
184 // create 'Apply View Properties To' group
185 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
187 m_applyToCurrentFolder
->setChecked(true);
188 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
189 "Current folder and sub-folders"));
190 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
193 QButtonGroup
* applyGroup
= new QButtonGroup(this);
194 applyGroup
->addButton(m_applyToCurrentFolder
);
195 applyGroup
->addButton(m_applyToSubFolders
);
196 applyGroup
->addButton(m_applyToAllFolders
);
198 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
200 layout
->addRow(i18nc("@title:group", "Apply to:"), m_applyToCurrentFolder
);
201 layout
->addRow(QString(), m_applyToSubFolders
);
202 layout
->addRow(QString(), m_applyToAllFolders
);
203 layout
->addRow(QString(), m_applyToAllFolders
);
205 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use as default view settings"), this);
206 layout
->addRow(QString(), m_useAsDefault
);
208 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
209 this, &ViewPropertiesDialog::markAsDirty
);
210 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
211 this, &ViewPropertiesDialog::markAsDirty
);
212 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
213 this, &ViewPropertiesDialog::markAsDirty
);
214 connect(m_useAsDefault
, &QCheckBox::clicked
,
215 this, &ViewPropertiesDialog::markAsDirty
);
218 layout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
220 layout
->addRow(additionalInfoBox
);
222 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
223 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
224 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
225 layout
->addWidget(buttonBox
);
227 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
228 okButton
->setShortcut(Qt::CTRL
+ Qt::Key_Return
);
229 okButton
->setDefault(true);
231 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
232 applyButton
->setEnabled(false);
233 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
234 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
235 applyButton
->setEnabled(isDirty
);
238 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
239 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
244 ViewPropertiesDialog::~ViewPropertiesDialog()
248 m_viewProps
= nullptr;
250 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
251 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
254 void ViewPropertiesDialog::accept()
256 applyViewProperties();
260 void ViewPropertiesDialog::slotApply()
262 applyViewProperties();
266 void ViewPropertiesDialog::slotViewModeChanged(int index
)
268 const QVariant itemData
= m_viewMode
->itemData(index
);
269 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
270 m_viewProps
->setViewMode(viewMode
);
274 void ViewPropertiesDialog::slotSortingChanged(int index
)
276 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
277 m_viewProps
->setSortRole(role
);
281 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
283 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
284 m_viewProps
->setSortOrder(sortOrder
);
288 void ViewPropertiesDialog::slotGroupedSortingChanged()
290 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
294 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
296 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
297 m_viewProps
->setSortFoldersFirst(foldersFirst
);
301 void ViewPropertiesDialog::slotShowPreviewChanged()
303 const bool show
= m_previewsShown
->isChecked();
304 m_viewProps
->setPreviewsShown(show
);
308 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
310 const bool show
= m_showHiddenFiles
->isChecked();
311 m_viewProps
->setHiddenFilesShown(show
);
315 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
321 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
323 if (m_isDirty
!= isDirty
) {
325 emit
isDirtyChanged(isDirty
);
329 void ViewPropertiesDialog::applyViewProperties()
331 // if nothing changed in the dialog, we have nothing to apply
336 // Update visible roles.
338 QList
<QByteArray
> visibleRoles
;
340 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
341 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
342 const QListWidgetItem
* item
= m_listWidget
->item(index
);
343 if (item
->checkState() == Qt::Checked
) {
344 visibleRoles
.append(info
.role
);
349 m_viewProps
->setVisibleRoles(visibleRoles
);
352 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
353 if (applyToSubFolders
) {
354 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
355 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
359 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
360 m_dolphinView
->url(),
362 info
->setAttribute(Qt::WA_DeleteOnClose
);
363 info
->setWindowModality(Qt::NonModal
);
367 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
369 // If the user selected 'Apply To All Folders' the view properties implicitly
370 // are also used as default for new folders.
371 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
373 // For directories where no .directory file is available, the .directory
374 // file stored for the global view properties is used as fallback. To update
375 // this file we temporary turn on the global view properties mode.
376 Q_ASSERT(!GeneralSettings::globalViewProps());
378 GeneralSettings::setGlobalViewProps(true);
379 ViewProperties
defaultProps(m_dolphinView
->url());
380 defaultProps
.setDirProperties(*m_viewProps
);
382 GeneralSettings::setGlobalViewProps(false);
385 if (applyToAllFolders
) {
386 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
387 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
391 // Updating the global view properties time stamp in the general settings makes
392 // all existing viewproperties invalid, as they have a smaller time stamp.
393 GeneralSettings
* settings
= GeneralSettings::self();
394 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
398 m_dolphinView
->setMode(m_viewProps
->viewMode());
399 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
400 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
401 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
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());
438 // Load show preview, show in groups and show hidden files settings
439 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
440 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
441 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());