1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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 "additionalinfodialog.h"
24 #include "dolphin_generalsettings.h"
25 #include "dolphin_iconsmodesettings.h"
26 #include "kitemviews/kfileitemmodel.h"
27 #include "viewpropsprogressinfo.h"
28 #include "views/dolphinview.h"
31 #include <KLocalizedString>
32 #include <KMessageBox>
33 #include <KWindowConfig>
35 #include <QButtonGroup>
37 #include <QGridLayout>
40 #include <QPushButton>
41 #include <QRadioButton>
43 #include <views/viewproperties.h>
45 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
48 m_dolphinView(dolphinView
),
53 m_sortFoldersFirst(nullptr),
54 m_previewsShown(nullptr),
55 m_showInGroups(nullptr),
56 m_showHiddenFiles(nullptr),
57 m_additionalInfo(nullptr),
58 m_applyToCurrentFolder(nullptr),
59 m_applyToSubFolders(nullptr),
60 m_applyToAllFolders(nullptr),
61 m_useAsDefault(nullptr)
63 Q_ASSERT(dolphinView
);
64 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
66 setWindowTitle(i18nc("@title:window", "View Properties"));
67 setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
69 const QUrl
& url
= dolphinView
->url();
70 m_viewProps
= new ViewProperties(url
);
71 m_viewProps
->setAutoSaveEnabled(false);
73 auto layout
= new QVBoxLayout(this);
76 auto propsGrid
= new QWidget(this);
77 layout
->addWidget(propsGrid
);
79 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
80 QWidget
* propsBox
= this;
81 if (!useGlobalViewProps
) {
82 propsBox
= new QGroupBox(i18nc("@title:group", "Properties"), this);
83 layout
->addWidget(propsBox
);
86 QLabel
* viewModeLabel
= new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid
);
87 m_viewMode
= new KComboBox(propsGrid
);
88 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-icons")), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
89 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-details")), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
90 m_viewMode
->addItem(QIcon::fromTheme(QStringLiteral("view-list-tree")), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
92 QLabel
* sortingLabel
= new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid
);
93 QWidget
* sortingBox
= new QWidget(propsGrid
);
95 m_sortOrder
= new KComboBox(sortingBox
);
96 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
97 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
99 m_sorting
= new KComboBox(sortingBox
);
100 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
101 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
102 m_sorting
->addItem(info
.translation
, info
.role
);
105 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
106 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
107 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
108 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
110 m_additionalInfo
= new QPushButton(i18nc("@action:button", "Additional Information"));
112 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
113 sortingLayout
->setMargin(0);
114 sortingLayout
->addWidget(m_sortOrder
);
115 sortingLayout
->addWidget(m_sorting
);
116 sortingBox
->setLayout(sortingLayout
);
118 QGridLayout
* propsGridLayout
= new QGridLayout(propsGrid
);
119 propsGridLayout
->addWidget(viewModeLabel
, 0, 0, Qt::AlignRight
);
120 propsGridLayout
->addWidget(m_viewMode
, 0, 1);
121 propsGridLayout
->addWidget(sortingLabel
, 1, 0, Qt::AlignRight
);
122 propsGridLayout
->addWidget(sortingBox
, 1, 1);
124 QVBoxLayout
* propsBoxLayout
= propsBox
== this ? layout
: new QVBoxLayout(propsBox
);
125 propsBoxLayout
->addWidget(propsGrid
);
126 propsBoxLayout
->addWidget(m_sortFoldersFirst
);
127 propsBoxLayout
->addWidget(m_previewsShown
);
128 propsBoxLayout
->addWidget(m_showInGroups
);
129 propsBoxLayout
->addWidget(m_showHiddenFiles
);
130 propsBoxLayout
->addWidget(m_additionalInfo
);
132 connect(m_viewMode
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
133 this, &ViewPropertiesDialog::slotViewModeChanged
);
134 connect(m_sorting
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
135 this, &ViewPropertiesDialog::slotSortingChanged
);
136 connect(m_sortOrder
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
137 this, &ViewPropertiesDialog::slotSortOrderChanged
);
138 connect(m_additionalInfo
, &QPushButton::clicked
,
139 this, &ViewPropertiesDialog::configureAdditionalInfo
);
140 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
141 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
142 connect(m_previewsShown
, &QCheckBox::clicked
,
143 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
144 connect(m_showInGroups
, &QCheckBox::clicked
,
145 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
146 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
147 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
149 // Only show the following settings if the view properties are remembered
150 // for each directory:
151 if (!useGlobalViewProps
) {
152 // create 'Apply View Properties To' group
153 QGroupBox
* applyBox
= new QGroupBox(i18nc("@title:group", "Apply View Properties To"), this);
154 layout
->addWidget(applyBox
);
156 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
157 "Current folder"), applyBox
);
158 m_applyToCurrentFolder
->setChecked(true);
159 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
160 "Current folder including all sub-folders"), applyBox
);
161 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
162 "All folders"), applyBox
);
164 QButtonGroup
* applyGroup
= new QButtonGroup(this);
165 applyGroup
->addButton(m_applyToCurrentFolder
);
166 applyGroup
->addButton(m_applyToSubFolders
);
167 applyGroup
->addButton(m_applyToAllFolders
);
169 QVBoxLayout
* applyBoxLayout
= new QVBoxLayout(applyBox
);
170 applyBoxLayout
->addWidget(m_applyToCurrentFolder
);
171 applyBoxLayout
->addWidget(m_applyToSubFolders
);
172 applyBoxLayout
->addWidget(m_applyToAllFolders
);
174 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use these view properties as default"), this);
175 layout
->addWidget(m_useAsDefault
);
177 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
178 this, &ViewPropertiesDialog::markAsDirty
);
179 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
180 this, &ViewPropertiesDialog::markAsDirty
);
181 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
182 this, &ViewPropertiesDialog::markAsDirty
);
183 connect(m_useAsDefault
, &QCheckBox::clicked
,
184 this, &ViewPropertiesDialog::markAsDirty
);
187 layout
->addStretch();
189 auto buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
| QDialogButtonBox::Apply
, this);
190 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &ViewPropertiesDialog::accept
);
191 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &ViewPropertiesDialog::reject
);
192 layout
->addWidget(buttonBox
);
194 auto okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
195 okButton
->setShortcut(Qt::CTRL
+ Qt::Key_Return
);
196 okButton
->setDefault(true);
198 auto applyButton
= buttonBox
->button(QDialogButtonBox::Apply
);
199 applyButton
->setEnabled(false);
200 connect(applyButton
, &QPushButton::clicked
, this, &ViewPropertiesDialog::slotApply
);
201 connect(this, &ViewPropertiesDialog::isDirtyChanged
, applyButton
, [applyButton
](bool isDirty
) {
202 applyButton
->setEnabled(isDirty
);
205 const KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
206 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig
);
211 ViewPropertiesDialog::~ViewPropertiesDialog()
215 m_viewProps
= nullptr;
217 KConfigGroup
dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
218 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig
);
221 void ViewPropertiesDialog::accept()
223 applyViewProperties();
227 void ViewPropertiesDialog::slotApply()
229 applyViewProperties();
233 void ViewPropertiesDialog::slotViewModeChanged(int index
)
235 const QVariant itemData
= m_viewMode
->itemData(index
);
236 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
237 m_viewProps
->setViewMode(viewMode
);
241 void ViewPropertiesDialog::slotSortingChanged(int index
)
243 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
244 m_viewProps
->setSortRole(role
);
248 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
250 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
251 m_viewProps
->setSortOrder(sortOrder
);
255 void ViewPropertiesDialog::slotGroupedSortingChanged()
257 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
261 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
263 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
264 m_viewProps
->setSortFoldersFirst(foldersFirst
);
268 void ViewPropertiesDialog::slotShowPreviewChanged()
270 const bool show
= m_previewsShown
->isChecked();
271 m_viewProps
->setPreviewsShown(show
);
275 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
277 const bool show
= m_showHiddenFiles
->isChecked();
278 m_viewProps
->setHiddenFilesShown(show
);
282 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
284 if (m_isDirty
!= isDirty
) {
286 emit
isDirtyChanged(isDirty
);
290 void ViewPropertiesDialog::configureAdditionalInfo()
292 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
293 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
294 if (useDefaultRoles
) {
295 // Using the details view without any additional information (-> additional column)
296 // makes no sense and leads to a usability problem as no viewport area is available
297 // anymore. Hence as fallback provide at least a size and date column.
298 visibleRoles
.clear();
299 visibleRoles
.append("text");
300 visibleRoles
.append("size");
301 visibleRoles
.append("modificationtime");
302 m_viewProps
->setVisibleRoles(visibleRoles
);
305 QPointer
<AdditionalInfoDialog
> dialog
= new AdditionalInfoDialog(this, visibleRoles
);
306 if (dialog
->exec() == QDialog::Accepted
) {
307 m_viewProps
->setVisibleRoles(dialog
->visibleRoles());
313 void ViewPropertiesDialog::applyViewProperties()
315 // if nothing changed in the dialog, we have nothing to apply
320 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
321 if (applyToSubFolders
) {
322 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
323 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
327 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
328 m_dolphinView
->url(),
330 info
->setAttribute(Qt::WA_DeleteOnClose
);
331 info
->setWindowModality(Qt::NonModal
);
335 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
337 // If the user selected 'Apply To All Folders' the view properties implicitely
338 // are also used as default for new folders.
339 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
341 // For directories where no .directory file is available, the .directory
342 // file stored for the global view properties is used as fallback. To update
343 // this file we temporary turn on the global view properties mode.
344 Q_ASSERT(!GeneralSettings::globalViewProps());
346 GeneralSettings::setGlobalViewProps(true);
347 ViewProperties
defaultProps(m_dolphinView
->url());
348 defaultProps
.setDirProperties(*m_viewProps
);
350 GeneralSettings::setGlobalViewProps(false);
353 if (applyToAllFolders
) {
354 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
355 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
359 // Updating the global view properties time stamp in the general settings makes
360 // all existing viewproperties invalid, as they have a smaller time stamp.
361 GeneralSettings
* settings
= GeneralSettings::self();
362 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
366 m_dolphinView
->setMode(m_viewProps
->viewMode());
367 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
368 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
369 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
370 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
371 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
372 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
373 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
380 void ViewPropertiesDialog::loadSettings()
383 switch (m_viewProps
->viewMode()) {
384 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
385 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
386 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
390 // Load sort order and sorting
391 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
392 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
394 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
395 int sortRoleIndex
= 0;
396 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
397 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
402 m_sorting
->setCurrentIndex(sortRoleIndex
);
404 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
406 // Load show preview, show in groups and show hidden files settings
407 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
408 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
409 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());