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"
25 #include "kitemviews/kfileitemmodel.h"
26 #include "viewpropsprogressinfo.h"
27 #include "views/dolphinview.h"
29 #include <KCollapsibleGroupBox>
30 #include <KLocalizedString>
31 #include <KMessageBox>
32 #include <KWindowConfig>
35 #include <Baloo/IndexerConfig>
38 #include <QButtonGroup>
41 #include <QGridLayout>
44 #include <QListWidget>
45 #include <QPushButton>
46 #include <QRadioButton>
48 #include <views/viewproperties.h>
50 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
53 m_dolphinView(dolphinView
),
58 m_sortFoldersFirst(nullptr),
59 m_previewsShown(nullptr),
60 m_showInGroups(nullptr),
61 m_showHiddenFiles(nullptr),
62 m_applyToCurrentFolder(nullptr),
63 m_applyToSubFolders(nullptr),
64 m_applyToAllFolders(nullptr),
65 m_useAsDefault(nullptr)
67 Q_ASSERT(dolphinView
);
68 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
70 setWindowTitle(i18nc("@title:window", "View Properties"));
72 const QUrl
& url
= dolphinView
->url();
73 m_viewProps
= new ViewProperties(url
);
74 m_viewProps
->setAutoSaveEnabled(false);
76 auto layout
= new QVBoxLayout(this);
77 // Otherwise the dialog won't resize when we collapse the KCollapsibleGroupBox.
78 layout
->setSizeConstraint(QLayout::SetFixedSize
);
81 auto propsGrid
= new QWidget(this);
82 layout
->addWidget(propsGrid
);
84 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
85 QWidget
* propsBox
= this;
86 if (!useGlobalViewProps
) {
87 propsBox
= new QGroupBox(i18nc("@title:group", "Properties"), this);
88 layout
->addWidget(propsBox
);
91 QLabel
* viewModeLabel
= new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid
);
92 m_viewMode
= new QComboBox(propsGrid
);
93 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-icons")), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
94 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-details")), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
95 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-tree")), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
97 QLabel
* sortingLabel
= new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid
);
98 QWidget
* sortingBox
= new QWidget(propsGrid
);
100 m_sortOrder
= new QComboBox(sortingBox
);
101 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
102 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
104 m_sorting
= new QComboBox(sortingBox
);
105 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
106 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
107 m_sorting
->addItem(info
.translation
, info
.role
);
110 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
111 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
112 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
113 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
115 auto additionalInfoBox
= new KCollapsibleGroupBox();
116 additionalInfoBox
->setTitle(i18nc("@title:group", "Additional Information Shown"));
117 auto innerLayout
= new QVBoxLayout();
120 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
121 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
122 if (useDefaultRoles
) {
123 // Using the details view without any additional information (-> additional column)
124 // makes no sense and leads to a usability problem as no viewport area is available
125 // anymore. Hence as fallback provide at least a size and date column.
126 visibleRoles
.clear();
127 visibleRoles
.append("text");
128 visibleRoles
.append("size");
129 visibleRoles
.append("modificationtime");
130 m_viewProps
->setVisibleRoles(visibleRoles
);
134 bool indexingEnabled
= false;
136 Baloo::IndexerConfig config
;
137 indexingEnabled
= config
.fileIndexingEnabled();
140 m_listWidget
= new QListWidget();
141 connect(m_listWidget
, &QListWidget::itemChanged
, this, &ViewPropertiesDialog::slotItemChanged
);
142 m_listWidget
->setSelectionMode(QAbstractItemView::NoSelection
);
143 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
144 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
145 QListWidgetItem
* item
= new QListWidgetItem(info
.translation
, m_listWidget
);
146 item
->setCheckState(visibleRoles
.contains(info
.role
) ? Qt::Checked
: Qt::Unchecked
);
148 const bool enable
= ((!info
.requiresBaloo
&& !info
.requiresIndexer
) ||
149 (info
.requiresBaloo
) ||
150 (info
.requiresIndexer
&& indexingEnabled
)) && info
.role
!= "text";
153 item
->setFlags(item
->flags() & ~Qt::ItemIsEnabled
);
156 innerLayout
->addWidget(m_listWidget
);
159 additionalInfoBox
->setLayout(innerLayout
);
161 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
162 sortingLayout
->setMargin(0);
163 sortingLayout
->addWidget(m_sortOrder
);
164 sortingLayout
->addWidget(m_sorting
);
165 sortingBox
->setLayout(sortingLayout
);
167 QGridLayout
* propsGridLayout
= new QGridLayout(propsGrid
);
168 propsGridLayout
->addWidget(viewModeLabel
, 0, 0, Qt::AlignRight
);
169 propsGridLayout
->addWidget(m_viewMode
, 0, 1);
170 propsGridLayout
->addWidget(sortingLabel
, 1, 0, Qt::AlignRight
);
171 propsGridLayout
->addWidget(sortingBox
, 1, 1);
173 QVBoxLayout
* propsBoxLayout
= propsBox
== this ? layout
: new QVBoxLayout(propsBox
);
174 propsBoxLayout
->addWidget(propsGrid
);
175 propsBoxLayout
->addWidget(m_sortFoldersFirst
);
176 propsBoxLayout
->addWidget(m_previewsShown
);
177 propsBoxLayout
->addWidget(m_showInGroups
);
178 propsBoxLayout
->addWidget(m_showHiddenFiles
);
179 propsBoxLayout
->addWidget(additionalInfoBox
);
181 connect(m_viewMode
, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged
),
182 this, &ViewPropertiesDialog::slotViewModeChanged
);
183 connect(m_sorting
, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged
),
184 this, &ViewPropertiesDialog::slotSortingChanged
);
185 connect(m_sortOrder
, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged
),
186 this, &ViewPropertiesDialog::slotSortOrderChanged
);
187 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
188 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
189 connect(m_previewsShown
, &QCheckBox::clicked
,
190 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
191 connect(m_showInGroups
, &QCheckBox::clicked
,
192 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
193 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
194 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
196 // Only show the following settings if the view properties are remembered
197 // for each directory:
198 if (!useGlobalViewProps
) {
199 // create 'Apply View Properties To' group
200 QGroupBox
* applyBox
= new QGroupBox(i18nc("@title:group", "Apply View Properties To"), this);
201 layout
->addWidget(applyBox
);
203 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
204 "Current folder"), applyBox
);
205 m_applyToCurrentFolder
->setChecked(true);
206 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
207 "Current folder including all sub-folders"), applyBox
);
208 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
209 "All folders"), applyBox
);
211 QButtonGroup
* applyGroup
= new QButtonGroup(this);
212 applyGroup
->addButton(m_applyToCurrentFolder
);
213 applyGroup
->addButton(m_applyToSubFolders
);
214 applyGroup
->addButton(m_applyToAllFolders
);
216 QVBoxLayout
* applyBoxLayout
= new QVBoxLayout(applyBox
);
217 applyBoxLayout
->addWidget(m_applyToCurrentFolder
);
218 applyBoxLayout
->addWidget(m_applyToSubFolders
);
219 applyBoxLayout
->addWidget(m_applyToAllFolders
);
221 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use these view properties as default"), this);
222 layout
->addWidget(m_useAsDefault
);
224 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
225 this, &ViewPropertiesDialog::markAsDirty
);
226 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
227 this, &ViewPropertiesDialog::markAsDirty
);
228 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
229 this, &ViewPropertiesDialog::markAsDirty
);
230 connect(m_useAsDefault
, &QCheckBox::clicked
,
231 this, &ViewPropertiesDialog::markAsDirty
);
234 layout
->addStretch();
236 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
237 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
238 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
239 layout
->addWidget(buttonBox
);
241 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
242 okButton
->setShortcut(Qt::CTRL
+ Qt::Key_Return
);
243 okButton
->setDefault(true);
245 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
246 applyButton
->setEnabled(false);
247 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
248 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
249 applyButton
->setEnabled(isDirty
);
252 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
253 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
258 ViewPropertiesDialog::~ViewPropertiesDialog()
262 m_viewProps
= nullptr;
264 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
265 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
268 void ViewPropertiesDialog::accept()
270 applyViewProperties();
274 void ViewPropertiesDialog::slotApply()
276 applyViewProperties();
280 void ViewPropertiesDialog::slotViewModeChanged(int index
)
282 const QVariant itemData
= m_viewMode
->itemData(index
);
283 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
284 m_viewProps
->setViewMode(viewMode
);
288 void ViewPropertiesDialog::slotSortingChanged(int index
)
290 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
291 m_viewProps
->setSortRole(role
);
295 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
297 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
298 m_viewProps
->setSortOrder(sortOrder
);
302 void ViewPropertiesDialog::slotGroupedSortingChanged()
304 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
308 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
310 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
311 m_viewProps
->setSortFoldersFirst(foldersFirst
);
315 void ViewPropertiesDialog::slotShowPreviewChanged()
317 const bool show
= m_previewsShown
->isChecked();
318 m_viewProps
->setPreviewsShown(show
);
322 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
324 const bool show
= m_showHiddenFiles
->isChecked();
325 m_viewProps
->setHiddenFilesShown(show
);
329 void ViewPropertiesDialog::slotItemChanged(QListWidgetItem
*item
)
335 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
337 if (m_isDirty
!= isDirty
) {
339 emit
isDirtyChanged(isDirty
);
343 void ViewPropertiesDialog::applyViewProperties()
345 // if nothing changed in the dialog, we have nothing to apply
350 // Update visible roles.
352 QList
<QByteArray
> visibleRoles
;
354 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
355 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
356 const QListWidgetItem
* item
= m_listWidget
->item(index
);
357 if (item
->checkState() == Qt::Checked
) {
358 visibleRoles
.append(info
.role
);
363 m_viewProps
->setVisibleRoles(visibleRoles
);
366 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
367 if (applyToSubFolders
) {
368 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
369 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
373 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
374 m_dolphinView
->url(),
376 info
->setAttribute(Qt::WA_DeleteOnClose
);
377 info
->setWindowModality(Qt::NonModal
);
381 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
383 // If the user selected 'Apply To All Folders' the view properties implicitely
384 // are also used as default for new folders.
385 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
387 // For directories where no .directory file is available, the .directory
388 // file stored for the global view properties is used as fallback. To update
389 // this file we temporary turn on the global view properties mode.
390 Q_ASSERT(!GeneralSettings::globalViewProps());
392 GeneralSettings::setGlobalViewProps(true);
393 ViewProperties
defaultProps(m_dolphinView
->url());
394 defaultProps
.setDirProperties(*m_viewProps
);
396 GeneralSettings::setGlobalViewProps(false);
399 if (applyToAllFolders
) {
400 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
401 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
405 // Updating the global view properties time stamp in the general settings makes
406 // all existing viewproperties invalid, as they have a smaller time stamp.
407 GeneralSettings
* settings
= GeneralSettings::self();
408 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
412 m_dolphinView
->setMode(m_viewProps
->viewMode());
413 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
414 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
415 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
416 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
417 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
418 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
419 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
426 void ViewPropertiesDialog::loadSettings()
429 switch (m_viewProps
->viewMode()) {
430 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
431 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
432 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
436 // Load sort order and sorting
437 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
438 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
440 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
441 int sortRoleIndex
= 0;
442 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
443 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
448 m_sorting
->setCurrentIndex(sortRoleIndex
);
450 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
452 // Load show preview, show in groups and show hidden files settings
453 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
454 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
455 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());