]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
Merge branch 'Applications/18.04'
[dolphin.git] / src / settings / viewpropertiesdialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "viewpropertiesdialog.h"
22
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"
29
30 #include <KComboBox>
31 #include <KLocalizedString>
32 #include <KMessageBox>
33 #include <KWindowConfig>
34
35 #include <QButtonGroup>
36 #include <QCheckBox>
37 #include <QGridLayout>
38 #include <QGroupBox>
39 #include <QLabel>
40 #include <QPushButton>
41 #include <QRadioButton>
42
43 #include <views/viewproperties.h>
44
45 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
46 QDialog(dolphinView),
47 m_isDirty(false),
48 m_dolphinView(dolphinView),
49 m_viewProps(nullptr),
50 m_viewMode(nullptr),
51 m_sortOrder(nullptr),
52 m_sorting(nullptr),
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)
62 {
63 Q_ASSERT(dolphinView);
64 const bool useGlobalViewProps = GeneralSettings::globalViewProps();
65
66 setWindowTitle(i18nc("@title:window", "View Properties"));
67 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
68
69 const QUrl& url = dolphinView->url();
70 m_viewProps = new ViewProperties(url);
71 m_viewProps->setAutoSaveEnabled(false);
72
73 auto layout = new QVBoxLayout(this);
74 setLayout(layout);
75
76 auto propsGrid = new QWidget(this);
77 layout->addWidget(propsGrid);
78
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);
84 }
85
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);
91
92 QLabel* sortingLabel = new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid);
93 QWidget* sortingBox = new QWidget(propsGrid);
94
95 m_sortOrder = new KComboBox(sortingBox);
96 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
97 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Descending"));
98
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);
103 }
104
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"));
109
110 m_additionalInfo = new QPushButton(i18nc("@action:button", "Additional Information"));
111
112 QHBoxLayout* sortingLayout = new QHBoxLayout();
113 sortingLayout->setMargin(0);
114 sortingLayout->addWidget(m_sortOrder);
115 sortingLayout->addWidget(m_sorting);
116 sortingBox->setLayout(sortingLayout);
117
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);
123
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);
131
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);
148
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);
155
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);
163
164 QButtonGroup* applyGroup = new QButtonGroup(this);
165 applyGroup->addButton(m_applyToCurrentFolder);
166 applyGroup->addButton(m_applyToSubFolders);
167 applyGroup->addButton(m_applyToAllFolders);
168
169 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
170 applyBoxLayout->addWidget(m_applyToCurrentFolder);
171 applyBoxLayout->addWidget(m_applyToSubFolders);
172 applyBoxLayout->addWidget(m_applyToAllFolders);
173
174 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use these view properties as default"), this);
175 layout->addWidget(m_useAsDefault);
176
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);
185 }
186
187 layout->addStretch();
188
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);
193
194 auto okButton = buttonBox->button(QDialogButtonBox::Ok);
195 okButton->setShortcut(Qt::CTRL + Qt::Key_Return);
196 okButton->setDefault(true);
197
198 auto applyButton = buttonBox->button(QDialogButtonBox::Apply);
199 connect(applyButton, &QPushButton::clicked, this, &ViewPropertiesDialog::slotApply);
200 connect(this, &ViewPropertiesDialog::isDirtyChanged, applyButton, [applyButton](bool isDirty) {
201 applyButton->setEnabled(isDirty);
202 });
203
204 const KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
205 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
206
207 loadSettings();
208 }
209
210 ViewPropertiesDialog::~ViewPropertiesDialog()
211 {
212 m_isDirty = false;
213 delete m_viewProps;
214 m_viewProps = nullptr;
215
216 KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
217 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
218 }
219
220 void ViewPropertiesDialog::accept()
221 {
222 applyViewProperties();
223 QDialog::accept();
224 }
225
226 void ViewPropertiesDialog::slotApply()
227 {
228 applyViewProperties();
229 markAsDirty(false);
230 }
231
232 void ViewPropertiesDialog::slotViewModeChanged(int index)
233 {
234 const QVariant itemData = m_viewMode->itemData(index);
235 const DolphinView::Mode viewMode = static_cast<DolphinView::Mode>(itemData.toInt());
236 m_viewProps->setViewMode(viewMode);
237 markAsDirty(true);
238 }
239
240 void ViewPropertiesDialog::slotSortingChanged(int index)
241 {
242 const QByteArray role = m_sorting->itemData(index).toByteArray();
243 m_viewProps->setSortRole(role);
244 markAsDirty(true);
245 }
246
247 void ViewPropertiesDialog::slotSortOrderChanged(int index)
248 {
249 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
250 m_viewProps->setSortOrder(sortOrder);
251 markAsDirty(true);
252 }
253
254 void ViewPropertiesDialog::slotGroupedSortingChanged()
255 {
256 m_viewProps->setGroupedSorting(m_showInGroups->isChecked());
257 markAsDirty(true);
258 }
259
260 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
261 {
262 const bool foldersFirst = m_sortFoldersFirst->isChecked();
263 m_viewProps->setSortFoldersFirst(foldersFirst);
264 markAsDirty(true);
265 }
266
267 void ViewPropertiesDialog::slotShowPreviewChanged()
268 {
269 const bool show = m_previewsShown->isChecked();
270 m_viewProps->setPreviewsShown(show);
271 markAsDirty(true);
272 }
273
274 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
275 {
276 const bool show = m_showHiddenFiles->isChecked();
277 m_viewProps->setHiddenFilesShown(show);
278 markAsDirty(true);
279 }
280
281 void ViewPropertiesDialog::markAsDirty(bool isDirty)
282 {
283 if (m_isDirty != isDirty) {
284 m_isDirty = isDirty;
285 emit isDirtyChanged(isDirty);
286 }
287 }
288
289 void ViewPropertiesDialog::configureAdditionalInfo()
290 {
291 QList<QByteArray> visibleRoles = m_viewProps->visibleRoles();
292 const bool useDefaultRoles = (m_viewProps->viewMode() == DolphinView::DetailsView) && visibleRoles.isEmpty();
293 if (useDefaultRoles) {
294 // Using the details view without any additional information (-> additional column)
295 // makes no sense and leads to a usability problem as no viewport area is available
296 // anymore. Hence as fallback provide at least a size and date column.
297 visibleRoles.clear();
298 visibleRoles.append("text");
299 visibleRoles.append("size");
300 visibleRoles.append("modificationtime");
301 m_viewProps->setVisibleRoles(visibleRoles);
302 }
303
304 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, visibleRoles);
305 if (dialog->exec() == QDialog::Accepted) {
306 m_viewProps->setVisibleRoles(dialog->visibleRoles());
307 markAsDirty(true);
308 }
309 delete dialog;
310 }
311
312 void ViewPropertiesDialog::applyViewProperties()
313 {
314 // if nothing changed in the dialog, we have nothing to apply
315 if (!m_isDirty) {
316 return;
317 }
318
319 const bool applyToSubFolders = m_applyToSubFolders && m_applyToSubFolders->isChecked();
320 if (applyToSubFolders) {
321 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
322 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
323 return;
324 }
325
326 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
327 m_dolphinView->url(),
328 *m_viewProps);
329 info->setAttribute(Qt::WA_DeleteOnClose);
330 info->setWindowModality(Qt::NonModal);
331 info->show();
332 }
333
334 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
335
336 // If the user selected 'Apply To All Folders' the view properties implicitely
337 // are also used as default for new folders.
338 const bool useAsDefault = applyToAllFolders || (m_useAsDefault && m_useAsDefault->isChecked());
339 if (useAsDefault) {
340 // For directories where no .directory file is available, the .directory
341 // file stored for the global view properties is used as fallback. To update
342 // this file we temporary turn on the global view properties mode.
343 Q_ASSERT(!GeneralSettings::globalViewProps());
344
345 GeneralSettings::setGlobalViewProps(true);
346 ViewProperties defaultProps(m_dolphinView->url());
347 defaultProps.setDirProperties(*m_viewProps);
348 defaultProps.save();
349 GeneralSettings::setGlobalViewProps(false);
350 }
351
352 if (applyToAllFolders) {
353 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
354 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
355 return;
356 }
357
358 // Updating the global view properties time stamp in the general settings makes
359 // all existing viewproperties invalid, as they have a smaller time stamp.
360 GeneralSettings* settings = GeneralSettings::self();
361 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
362 settings->save();
363 }
364
365 m_dolphinView->setMode(m_viewProps->viewMode());
366 m_dolphinView->setSortRole(m_viewProps->sortRole());
367 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
368 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
369 m_dolphinView->setGroupedSorting(m_viewProps->groupedSorting());
370 m_dolphinView->setVisibleRoles(m_viewProps->visibleRoles());
371 m_dolphinView->setPreviewsShown(m_viewProps->previewsShown());
372 m_dolphinView->setHiddenFilesShown(m_viewProps->hiddenFilesShown());
373
374 m_viewProps->save();
375
376 markAsDirty(false);
377 }
378
379 void ViewPropertiesDialog::loadSettings()
380 {
381 // Load view mode
382 switch (m_viewProps->viewMode()) {
383 case DolphinView::IconsView: m_viewMode->setCurrentIndex(0); break;
384 case DolphinView::CompactView: m_viewMode->setCurrentIndex(1); break;
385 case DolphinView::DetailsView: m_viewMode->setCurrentIndex(2); break;
386 default: break;
387 }
388
389 // Load sort order and sorting
390 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
391 m_sortOrder->setCurrentIndex(sortOrderIndex);
392
393 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
394 int sortRoleIndex = 0;
395 for (int i = 0; i < rolesInfo.count(); ++i) {
396 if (rolesInfo[i].role == m_viewProps->sortRole()) {
397 sortRoleIndex = i;
398 break;
399 }
400 }
401 m_sorting->setCurrentIndex(sortRoleIndex);
402
403 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
404
405 // Load show preview, show in groups and show hidden files settings
406 m_previewsShown->setChecked(m_viewProps->previewsShown());
407 m_showInGroups->setChecked(m_viewProps->groupedSorting());
408 m_showHiddenFiles->setChecked(m_viewProps->hiddenFilesShown());
409 markAsDirty(false);
410 }
411