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 <KComponentData>
34 #include <KIconLoader>
35 #include <KIO/NetAccess>
36 #include <KMessageBox>
37 #include <KStandardDirs>
42 #include <QButtonGroup>
44 #include <QGridLayout>
48 #include <QPushButton>
49 #include <QRadioButton>
52 #include <views/viewproperties.h>
54 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
57 m_dolphinView(dolphinView
),
62 m_sortFoldersFirst(0),
67 m_applyToCurrentFolder(0),
68 m_applyToSubFolders(0),
69 m_applyToAllFolders(0),
72 Q_ASSERT(dolphinView
);
73 const bool useGlobalViewProps
= GeneralSettings::globalViewProps();
75 setCaption(i18nc("@title:window", "View Properties"));
76 setButtons(KDialog::Ok
| KDialog::Cancel
| KDialog::Apply
);
78 const KUrl
& url
= dolphinView
->url();
79 m_viewProps
= new ViewProperties(url
);
80 m_viewProps
->setAutoSaveEnabled(false);
82 QWidget
* main
= new QWidget();
83 QVBoxLayout
* topLayout
= new QVBoxLayout();
85 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
86 QWidget
* propsBox
= main
;
87 if (!useGlobalViewProps
) {
88 propsBox
= new QGroupBox(i18nc("@title:group", "Properties"), main
);
91 QWidget
* propsGrid
= new QWidget();
93 QLabel
* viewModeLabel
= new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid
);
94 m_viewMode
= new KComboBox(propsGrid
);
95 m_viewMode
->addItem(QIcon::fromTheme("view-list-icons"), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView
);
96 m_viewMode
->addItem(QIcon::fromTheme("view-list-details"), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView
);
97 m_viewMode
->addItem(QIcon::fromTheme("view-list-tree"), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView
);
99 QLabel
* sortingLabel
= new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid
);
100 QWidget
* sortingBox
= new QWidget(propsGrid
);
102 m_sortOrder
= new KComboBox(sortingBox
);
103 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
104 m_sortOrder
->addItem(i18nc("@item:inlistbox Sort", "Descending"));
106 m_sorting
= new KComboBox(sortingBox
);
107 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
108 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
109 m_sorting
->addItem(info
.translation
, info
.role
);
112 m_sortFoldersFirst
= new QCheckBox(i18nc("@option:check", "Show folders first"));
113 m_previewsShown
= new QCheckBox(i18nc("@option:check", "Show preview"));
114 m_showInGroups
= new QCheckBox(i18nc("@option:check", "Show in groups"));
115 m_showHiddenFiles
= new QCheckBox(i18nc("@option:check", "Show hidden files"));
117 m_additionalInfo
= new QPushButton(i18nc("@action:button", "Additional Information"));
119 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
120 sortingLayout
->setMargin(0);
121 sortingLayout
->addWidget(m_sortOrder
);
122 sortingLayout
->addWidget(m_sorting
);
123 sortingBox
->setLayout(sortingLayout
);
125 QGridLayout
* propsGridLayout
= new QGridLayout(propsGrid
);
126 propsGridLayout
->addWidget(viewModeLabel
, 0, 0, Qt::AlignRight
);
127 propsGridLayout
->addWidget(m_viewMode
, 0, 1);
128 propsGridLayout
->addWidget(sortingLabel
, 1, 0, Qt::AlignRight
);
129 propsGridLayout
->addWidget(sortingBox
, 1, 1);
131 QVBoxLayout
* propsBoxLayout
= new QVBoxLayout(propsBox
);
132 propsBoxLayout
->addWidget(propsGrid
);
133 propsBoxLayout
->addWidget(m_sortFoldersFirst
);
134 propsBoxLayout
->addWidget(m_previewsShown
);
135 propsBoxLayout
->addWidget(m_showInGroups
);
136 propsBoxLayout
->addWidget(m_showHiddenFiles
);
137 propsBoxLayout
->addWidget(m_additionalInfo
);
139 topLayout
->addWidget(propsBox
);
141 connect(m_viewMode
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
142 this, &ViewPropertiesDialog::slotViewModeChanged
);
143 connect(m_sorting
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
144 this, &ViewPropertiesDialog::slotSortingChanged
);
145 connect(m_sortOrder
, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged
),
146 this, &ViewPropertiesDialog::slotSortOrderChanged
);
147 connect(m_additionalInfo
, &QPushButton::clicked
,
148 this, &ViewPropertiesDialog::configureAdditionalInfo
);
149 connect(m_sortFoldersFirst
, &QCheckBox::clicked
,
150 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged
);
151 connect(m_previewsShown
, &QCheckBox::clicked
,
152 this, &ViewPropertiesDialog::slotShowPreviewChanged
);
153 connect(m_showInGroups
, &QCheckBox::clicked
,
154 this, &ViewPropertiesDialog::slotGroupedSortingChanged
);
155 connect(m_showHiddenFiles
, &QCheckBox::clicked
,
156 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged
);
158 connect(this, &ViewPropertiesDialog::okClicked
, this, &ViewPropertiesDialog::slotOk
);
159 connect(this, &ViewPropertiesDialog::applyClicked
, this, &ViewPropertiesDialog::slotApply
);
161 // Only show the following settings if the view properties are remembered
162 // for each directory:
163 if (!useGlobalViewProps
) {
164 // create 'Apply View Properties To' group
165 QGroupBox
* applyBox
= new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main
);
167 m_applyToCurrentFolder
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
168 "Current folder"), applyBox
);
169 m_applyToCurrentFolder
->setChecked(true);
170 m_applyToSubFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
171 "Current folder including all sub-folders"), applyBox
);
172 m_applyToAllFolders
= new QRadioButton(i18nc("@option:radio Apply View Properties To",
173 "All folders"), applyBox
);
175 QButtonGroup
* applyGroup
= new QButtonGroup(this);
176 applyGroup
->addButton(m_applyToCurrentFolder
);
177 applyGroup
->addButton(m_applyToSubFolders
);
178 applyGroup
->addButton(m_applyToAllFolders
);
180 QVBoxLayout
* applyBoxLayout
= new QVBoxLayout(applyBox
);
181 applyBoxLayout
->addWidget(m_applyToCurrentFolder
);
182 applyBoxLayout
->addWidget(m_applyToSubFolders
);
183 applyBoxLayout
->addWidget(m_applyToAllFolders
);
185 m_useAsDefault
= new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main
);
187 topLayout
->addWidget(applyBox
);
188 topLayout
->addWidget(m_useAsDefault
);
190 connect(m_applyToCurrentFolder
, &QRadioButton::clicked
,
191 this, &ViewPropertiesDialog::markAsDirty
);
192 connect(m_applyToSubFolders
, &QRadioButton::clicked
,
193 this, &ViewPropertiesDialog::markAsDirty
);
194 connect(m_applyToAllFolders
, &QRadioButton::clicked
,
195 this, &ViewPropertiesDialog::markAsDirty
);
196 connect(m_useAsDefault
, &QCheckBox::clicked
,
197 this, &ViewPropertiesDialog::markAsDirty
);
200 main
->setLayout(topLayout
);
203 const KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
204 "ViewPropertiesDialog");
205 restoreDialogSize(dialogConfig
);
210 ViewPropertiesDialog::~ViewPropertiesDialog()
216 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
217 "ViewPropertiesDialog");
218 saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
221 void ViewPropertiesDialog::slotOk()
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
)
285 enableButtonApply(isDirty
);
288 void ViewPropertiesDialog::configureAdditionalInfo()
290 QList
<QByteArray
> visibleRoles
= m_viewProps
->visibleRoles();
291 const bool useDefaultRoles
= (m_viewProps
->viewMode() == DolphinView::DetailsView
) && visibleRoles
.isEmpty();
292 if (useDefaultRoles
) {
293 // Using the details view without any additional information (-> additional column)
294 // makes no sense and leads to a usability problem as no viewport area is available
295 // anymore. Hence as fallback provide at least a size and date column.
296 visibleRoles
.clear();
297 visibleRoles
.append("text");
298 visibleRoles
.append("size");
299 visibleRoles
.append("date");
300 m_viewProps
->setVisibleRoles(visibleRoles
);
303 QPointer
<AdditionalInfoDialog
> dialog
= new AdditionalInfoDialog(this, visibleRoles
);
304 if (dialog
->exec() == QDialog::Accepted
) {
305 m_viewProps
->setVisibleRoles(dialog
->visibleRoles());
311 void ViewPropertiesDialog::applyViewProperties()
313 // if nothing changed in the dialog, we have nothing to apply
318 const bool applyToSubFolders
= m_applyToSubFolders
&& m_applyToSubFolders
->isChecked();
319 if (applyToSubFolders
) {
320 const QString
text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
321 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
325 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
326 m_dolphinView
->url(),
328 info
->setAttribute(Qt::WA_DeleteOnClose
);
329 info
->setWindowModality(Qt::NonModal
);
333 const bool applyToAllFolders
= m_applyToAllFolders
&& m_applyToAllFolders
->isChecked();
335 // If the user selected 'Apply To All Folders' the view properties implicitely
336 // are also used as default for new folders.
337 const bool useAsDefault
= applyToAllFolders
|| (m_useAsDefault
&& m_useAsDefault
->isChecked());
339 // For directories where no .directory file is available, the .directory
340 // file stored for the global view properties is used as fallback. To update
341 // this file we temporary turn on the global view properties mode.
342 Q_ASSERT(!GeneralSettings::globalViewProps());
344 GeneralSettings::setGlobalViewProps(true);
345 ViewProperties
defaultProps(m_dolphinView
->url());
346 defaultProps
.setDirProperties(*m_viewProps
);
348 GeneralSettings::setGlobalViewProps(false);
351 if (applyToAllFolders
) {
352 const QString
text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
353 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
357 // Updating the global view properties time stamp in the general settings makes
358 // all existing viewproperties invalid, as they have a smaller time stamp.
359 GeneralSettings
* settings
= GeneralSettings::self();
360 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
361 settings
->writeConfig();
364 m_dolphinView
->setMode(m_viewProps
->viewMode());
365 m_dolphinView
->setSortRole(m_viewProps
->sortRole());
366 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
367 m_dolphinView
->setSortFoldersFirst(m_viewProps
->sortFoldersFirst());
368 m_dolphinView
->setGroupedSorting(m_viewProps
->groupedSorting());
369 m_dolphinView
->setVisibleRoles(m_viewProps
->visibleRoles());
370 m_dolphinView
->setPreviewsShown(m_viewProps
->previewsShown());
371 m_dolphinView
->setHiddenFilesShown(m_viewProps
->hiddenFilesShown());
378 void ViewPropertiesDialog::loadSettings()
381 switch (m_viewProps
->viewMode()) {
382 case DolphinView::IconsView
: m_viewMode
->setCurrentIndex(0); break;
383 case DolphinView::CompactView
: m_viewMode
->setCurrentIndex(1); break;
384 case DolphinView::DetailsView
: m_viewMode
->setCurrentIndex(2); break;
388 // Load sort order and sorting
389 const int sortOrderIndex
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
390 m_sortOrder
->setCurrentIndex(sortOrderIndex
);
392 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
393 int sortRoleIndex
= 0;
394 for (int i
= 0; i
< rolesInfo
.count(); ++i
) {
395 if (rolesInfo
[i
].role
== m_viewProps
->sortRole()) {
400 m_sorting
->setCurrentIndex(sortRoleIndex
);
402 m_sortFoldersFirst
->setChecked(m_viewProps
->sortFoldersFirst());
404 // Load show preview, show in groups and show hidden files settings
405 m_previewsShown
->setChecked(m_viewProps
->previewsShown());
406 m_showInGroups
->setChecked(m_viewProps
->groupedSorting());
407 m_showHiddenFiles
->setChecked(m_viewProps
->hiddenFilesShown());