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 "kitemviews/kfileitemmodel.h"
25 #include "views/dolphinview.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphin_iconsmodesettings.h"
28 #include "viewpropsprogressinfo.h"
30 #include <config-baloo.h>
32 #include <KLocalizedString>
33 #include <KIO/NetAccess>
34 #include <KMessageBox>
38 #include <QButtonGroup>
40 #include <QGridLayout>
43 #include <QPushButton>
44 #include <QRadioButton>
46 #include <views/viewproperties.h>
48 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
51 m_dolphinView(dolphinView
),
56 m_sortFoldersFirst(0),
61 m_applyToCurrentFolder(0),
62 m_applyToSubFolders(0),
63 m_applyToAllFolders(0),
66 Q_ASSERT(dolphinView
);
67 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
69 setCaption(i18nc("@title:window", "View Properties"));
70 setButtons(KDialog::Ok
| KDialog::Cancel
| KDialog::Apply
);
72 const KUrl
& url
= dolphinView
->url();
73 m_viewProps
= new ViewProperties(url
);
74 m_viewProps
->setAutoSaveEnabled(false);
76 QWidget
* main
= new QWidget();
77 QVBoxLayout
* topLayout
= new QVBoxLayout();
79 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
80 QWidget
* propsBox
= main
;
81 if (!useGlobalViewProps
) {
82 propsBox
= new QGroupBox(i18nc("@title:group", "Properties"), main
);
85 QWidget
* propsGrid
= new QWidget();
87 QLabel
* viewModeLabel
= new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid
);
88 m_viewMode
= new KComboBox(propsGrid
);
89 m_viewMode
->addItem(QIcon::fromTheme("view-list-icons"), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
90 m_viewMode
->addItem(QIcon::fromTheme("view-list-details"), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
91 m_viewMode
->addItem(QIcon::fromTheme("view-list-tree"), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
93 QLabel
* sortingLabel
= new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid
);
94 QWidget
* sortingBox
= new QWidget(propsGrid
);
96 m_sortOrder
= new KComboBox(sortingBox
);
97 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
98 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
100 m_sorting
= new KComboBox(sortingBox
);
101 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
102 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
103 m_sorting
->addItem(info
.translation
, info
.role
);
106 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
107 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
108 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
109 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
111 m_additionalInfo
= new QPushButton(i18nc("@action:button", "Additional Information"));
113 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
114 sortingLayout
->setMargin(0);
115 sortingLayout
->addWidget(m_sortOrder
);
116 sortingLayout
->addWidget(m_sorting
);
117 sortingBox
->setLayout(sortingLayout
);
119 QGridLayout
* propsGridLayout
= new QGridLayout(propsGrid
);
120 propsGridLayout
->addWidget(viewModeLabel
, 0, 0, Qt::AlignRight
);
121 propsGridLayout
->addWidget(m_viewMode
, 0, 1);
122 propsGridLayout
->addWidget(sortingLabel
, 1, 0, Qt::AlignRight
);
123 propsGridLayout
->addWidget(sortingBox
, 1, 1);
125 QVBoxLayout
* propsBoxLayout
= new QVBoxLayout(propsBox
);
126 propsBoxLayout
->addWidget(propsGrid
);
127 propsBoxLayout
->addWidget(m_sortFoldersFirst
);
128 propsBoxLayout
->addWidget(m_previewsShown
);
129 propsBoxLayout
->addWidget(m_showInGroups
);
130 propsBoxLayout
->addWidget(m_showHiddenFiles
);
131 propsBoxLayout
->addWidget(m_additionalInfo
);
133 topLayout
->addWidget(propsBox
);
135 connect(m_viewMode
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
136 this, &ViewPropertiesDialog::slotViewModeChanged
);
137 connect(m_sorting
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
138 this, &ViewPropertiesDialog::slotSortingChanged
);
139 connect(m_sortOrder
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
140 this, &ViewPropertiesDialog::slotSortOrderChanged
);
141 connect(m_additionalInfo
, &QPushButton::clicked
,
142 this, &ViewPropertiesDialog::configureAdditionalInfo
);
143 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
144 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
145 connect(m_previewsShown
, &QCheckBox::clicked
,
146 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
147 connect(m_showInGroups
, &QCheckBox::clicked
,
148 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
149 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
150 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
152 connect(this, &ViewPropertiesDialog::okClicked
, this, &ViewPropertiesDialog::slotOk
);
153 connect(this, &ViewPropertiesDialog::applyClicked
, this, &ViewPropertiesDialog::slotApply
);
155 // Only show the following settings if the view properties are remembered
156 // for each directory:
157 if (!useGlobalViewProps
) {
158 // create 'Apply View Properties To' group
159 QGroupBox
* applyBox
= new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main
);
161 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
162 "Current folder"), applyBox
);
163 m_applyToCurrentFolder
->setChecked(true);
164 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
165 "Current folder including all sub-folders"), applyBox
);
166 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
167 "All folders"), applyBox
);
169 QButtonGroup
* applyGroup
= new QButtonGroup(this);
170 applyGroup
->addButton(m_applyToCurrentFolder
);
171 applyGroup
->addButton(m_applyToSubFolders
);
172 applyGroup
->addButton(m_applyToAllFolders
);
174 QVBoxLayout
* applyBoxLayout
= new QVBoxLayout(applyBox
);
175 applyBoxLayout
->addWidget(m_applyToCurrentFolder
);
176 applyBoxLayout
->addWidget(m_applyToSubFolders
);
177 applyBoxLayout
->addWidget(m_applyToAllFolders
);
179 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main
);
181 topLayout
->addWidget(applyBox
);
182 topLayout
->addWidget(m_useAsDefault
);
184 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
185 this, &ViewPropertiesDialog::markAsDirty
);
186 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
187 this, &ViewPropertiesDialog::markAsDirty
);
188 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
189 this, &ViewPropertiesDialog::markAsDirty
);
190 connect(m_useAsDefault
, &QCheckBox::clicked
,
191 this, &ViewPropertiesDialog::markAsDirty
);
194 main
->setLayout(topLayout
);
197 const KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
198 "ViewPropertiesDialog");
199 restoreDialogSize(dialogConfig
);
204 ViewPropertiesDialog::~ViewPropertiesDialog()
210 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
211 "ViewPropertiesDialog");
212 saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
215 void ViewPropertiesDialog::slotOk()
217 applyViewProperties();
221 void ViewPropertiesDialog::slotApply()
223 applyViewProperties();
227 void ViewPropertiesDialog::slotViewModeChanged(int index
)
229 const QVariant itemData
= m_viewMode
->itemData(index
);
230 const DolphinView::Mode viewMode
= static_cast<DolphinView::Mode
>(itemData
.toInt());
231 m_viewProps
->setViewMode(viewMode
);
235 void ViewPropertiesDialog::slotSortingChanged(int index
)
237 const QByteArray role
= m_sorting
->itemData(index
).toByteArray();
238 m_viewProps
->setSortRole(role
);
242 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
244 const Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
245 m_viewProps
->setSortOrder(sortOrder
);
249 void ViewPropertiesDialog::slotGroupedSortingChanged()
251 m_viewProps
->setGroupedSorting(m_showInGroups
->isChecked());
255 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
257 const bool foldersFirst
= m_sortFoldersFirst
->isChecked();
258 m_viewProps
->setSortFoldersFirst(foldersFirst
);
262 void ViewPropertiesDialog::slotShowPreviewChanged()
264 const bool show
= m_previewsShown
->isChecked();
265 m_viewProps
->setPreviewsShown(show
);
269 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
271 const bool show
= m_showHiddenFiles
->isChecked();
272 m_viewProps
->setHiddenFilesShown(show
);
276 void ViewPropertiesDialog::markAsDirty(bool isDirty
)
279 enableButtonApply(isDirty
);
282 void ViewPropertiesDialog::configureAdditionalInfo()
284 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
285 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
286 if (useDefaultRoles
) {
287 // Using the details view without any additional information (-> additional column)
288 // makes no sense and leads to a usability problem as no viewport area is available
289 // anymore. Hence as fallback provide at least a size and date column.
290 visibleRoles
.clear();
291 visibleRoles
.append("text");
292 visibleRoles
.append("size");
293 visibleRoles
.append("date");
294 m_viewProps
->setVisibleRoles(visibleRoles
);
297 QPointer
<AdditionalInfoDialog
> dialog
= new AdditionalInfoDialog(this, visibleRoles
);
298 if (dialog
->exec() == QDialog::Accepted
) {
299 m_viewProps
->setVisibleRoles(dialog
->visibleRoles());
305 void ViewPropertiesDialog::applyViewProperties()
307 // if nothing changed in the dialog, we have nothing to apply
312 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
313 if (applyToSubFolders
) {
314 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
315 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
319 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
320 m_dolphinView
->url(),
322 info
->setAttribute(Qt::WA_DeleteOnClose
);
323 info
->setWindowModality(Qt::NonModal
);
327 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
329 // If the user selected 'Apply To All Folders' the view properties implicitely
330 // are also used as default for new folders.
331 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
333 // For directories where no .directory file is available, the .directory
334 // file stored for the global view properties is used as fallback. To update
335 // this file we temporary turn on the global view properties mode.
336 Q_ASSERT(!GeneralSettings::globalViewProps());
338 GeneralSettings::setGlobalViewProps(true);
339 ViewProperties
defaultProps(m_dolphinView
->url());
340 defaultProps
.setDirProperties(*m_viewProps
);
342 GeneralSettings::setGlobalViewProps(false);
345 if (applyToAllFolders
) {
346 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
347 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
351 // Updating the global view properties time stamp in the general settings makes
352 // all existing viewproperties invalid, as they have a smaller time stamp.
353 GeneralSettings
* settings
= GeneralSettings::self();
354 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
355 settings
->writeConfig();
358 m_dolphinView
->setMode(m_viewProps
->viewMode());
359 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
360 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
361 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
362 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
363 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
364 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
365 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
372 void ViewPropertiesDialog::loadSettings()
375 switch (m_viewProps
->viewMode()) {
376 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
377 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
378 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
382 // Load sort order and sorting
383 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
384 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
386 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
387 int sortRoleIndex
= 0;
388 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
389 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
394 m_sorting
->setCurrentIndex(sortRoleIndex
);
396 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
398 // Load show preview, show in groups and show hidden files settings
399 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
400 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
401 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());