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