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