]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
60c9f21f56160659a8fd17a25c32d22713812ccf
[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 applyButton->setEnabled(false);
200 connect(applyButton, &QPushButton::clicked, this, &ViewPropertiesDialog::slotApply);
201 connect(this, &ViewPropertiesDialog::isDirtyChanged, applyButton, [applyButton](bool isDirty) {
202 applyButton->setEnabled(isDirty);
203 });
204
205 const KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
206 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
207
208 loadSettings();
209 }
210
211 ViewPropertiesDialog::~ViewPropertiesDialog()
212 {
213 m_isDirty = false;
214 delete m_viewProps;
215 m_viewProps = nullptr;
216
217 KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "ViewPropertiesDialog");
218 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
219 }
220
221 void ViewPropertiesDialog::accept()
222 {
223 applyViewProperties();
224 QDialog::accept();
225 }
226
227 void ViewPropertiesDialog::slotApply()
228 {
229 applyViewProperties();
230 markAsDirty(false);
231 }
232
233 void ViewPropertiesDialog::slotViewModeChanged(int index)
234 {
235 const QVariant itemData = m_viewMode->itemData(index);
236 const DolphinView::Mode viewMode = static_cast<DolphinView::Mode>(itemData.toInt());
237 m_viewProps->setViewMode(viewMode);
238 markAsDirty(true);
239 }
240
241 void ViewPropertiesDialog::slotSortingChanged(int index)
242 {
243 const QByteArray role = m_sorting->itemData(index).toByteArray();
244 m_viewProps->setSortRole(role);
245 markAsDirty(true);
246 }
247
248 void ViewPropertiesDialog::slotSortOrderChanged(int index)
249 {
250 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
251 m_viewProps->setSortOrder(sortOrder);
252 markAsDirty(true);
253 }
254
255 void ViewPropertiesDialog::slotGroupedSortingChanged()
256 {
257 m_viewProps->setGroupedSorting(m_showInGroups->isChecked());
258 markAsDirty(true);
259 }
260
261 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
262 {
263 const bool foldersFirst = m_sortFoldersFirst->isChecked();
264 m_viewProps->setSortFoldersFirst(foldersFirst);
265 markAsDirty(true);
266 }
267
268 void ViewPropertiesDialog::slotShowPreviewChanged()
269 {
270 const bool show = m_previewsShown->isChecked();
271 m_viewProps->setPreviewsShown(show);
272 markAsDirty(true);
273 }
274
275 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
276 {
277 const bool show = m_showHiddenFiles->isChecked();
278 m_viewProps->setHiddenFilesShown(show);
279 markAsDirty(true);
280 }
281
282 void ViewPropertiesDialog::markAsDirty(bool isDirty)
283 {
284 if (m_isDirty != isDirty) {
285 m_isDirty = isDirty;
286 emit isDirtyChanged(isDirty);
287 }
288 }
289
290 void ViewPropertiesDialog::configureAdditionalInfo()
291 {
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);
303 }
304
305 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, visibleRoles);
306 if (dialog->exec() == QDialog::Accepted) {
307 m_viewProps->setVisibleRoles(dialog->visibleRoles());
308 markAsDirty(true);
309 }
310 delete dialog;
311 }
312
313 void ViewPropertiesDialog::applyViewProperties()
314 {
315 // if nothing changed in the dialog, we have nothing to apply
316 if (!m_isDirty) {
317 return;
318 }
319
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) {
324 return;
325 }
326
327 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
328 m_dolphinView->url(),
329 *m_viewProps);
330 info->setAttribute(Qt::WA_DeleteOnClose);
331 info->setWindowModality(Qt::NonModal);
332 info->show();
333 }
334
335 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
336
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());
340 if (useAsDefault) {
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());
345
346 GeneralSettings::setGlobalViewProps(true);
347 ViewProperties defaultProps(m_dolphinView->url());
348 defaultProps.setDirProperties(*m_viewProps);
349 defaultProps.save();
350 GeneralSettings::setGlobalViewProps(false);
351 }
352
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) {
356 return;
357 }
358
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());
363 settings->save();
364 }
365
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());
374
375 m_viewProps->save();
376
377 markAsDirty(false);
378 }
379
380 void ViewPropertiesDialog::loadSettings()
381 {
382 // Load view mode
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;
387 default: break;
388 }
389
390 // Load sort order and sorting
391 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
392 m_sortOrder->setCurrentIndex(sortOrderIndex);
393
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()) {
398 sortRoleIndex = i;
399 break;
400 }
401 }
402 m_sorting->setCurrentIndex(sortRoleIndex);
403
404 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
405
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());
410 markAsDirty(false);
411 }
412