]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
Ported Dolphin away from KStandardDirs
[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 <KComponentData>
33 #include <KLocale>
34 #include <KIconLoader>
35 #include <KIO/NetAccess>
36 #include <KMessageBox>
37 #include <KUrl>
38 #include <KComboBox>
39
40 #include <QAction>
41 #include <QButtonGroup>
42 #include <QCheckBox>
43 #include <QGridLayout>
44 #include <QGroupBox>
45 #include <QLabel>
46 #include <QMenu>
47 #include <QPushButton>
48 #include <QRadioButton>
49 #include <QBoxLayout>
50
51 #include <views/viewproperties.h>
52
53 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
54 KDialog(dolphinView),
55 m_isDirty(false),
56 m_dolphinView(dolphinView),
57 m_viewProps(0),
58 m_viewMode(0),
59 m_sortOrder(0),
60 m_sorting(0),
61 m_sortFoldersFirst(0),
62 m_previewsShown(0),
63 m_showInGroups(0),
64 m_showHiddenFiles(0),
65 m_additionalInfo(0),
66 m_applyToCurrentFolder(0),
67 m_applyToSubFolders(0),
68 m_applyToAllFolders(0),
69 m_useAsDefault(0)
70 {
71 Q_ASSERT(dolphinView);
72 const bool useGlobalViewProps = GeneralSettings::globalViewProps();
73
74 setCaption(i18nc("@title:window", "View Properties"));
75 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
76
77 const KUrl& url = dolphinView->url();
78 m_viewProps = new ViewProperties(url);
79 m_viewProps->setAutoSaveEnabled(false);
80
81 QWidget* main = new QWidget();
82 QVBoxLayout* topLayout = new QVBoxLayout();
83
84 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
85 QWidget* propsBox = main;
86 if (!useGlobalViewProps) {
87 propsBox = new QGroupBox(i18nc("@title:group", "Properties"), main);
88 }
89
90 QWidget* propsGrid = new QWidget();
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 topLayout->addWidget(propsBox);
139
140 connect(m_viewMode, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged),
141 this, &ViewPropertiesDialog::slotViewModeChanged);
142 connect(m_sorting, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged),
143 this, &ViewPropertiesDialog::slotSortingChanged);
144 connect(m_sortOrder, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged),
145 this, &ViewPropertiesDialog::slotSortOrderChanged);
146 connect(m_additionalInfo, &QPushButton::clicked,
147 this, &ViewPropertiesDialog::configureAdditionalInfo);
148 connect(m_sortFoldersFirst, &QCheckBox::clicked,
149 this, &ViewPropertiesDialog::slotSortFoldersFirstChanged);
150 connect(m_previewsShown, &QCheckBox::clicked,
151 this, &ViewPropertiesDialog::slotShowPreviewChanged);
152 connect(m_showInGroups, &QCheckBox::clicked,
153 this, &ViewPropertiesDialog::slotGroupedSortingChanged);
154 connect(m_showHiddenFiles, &QCheckBox::clicked,
155 this, &ViewPropertiesDialog::slotShowHiddenFilesChanged);
156
157 connect(this, &ViewPropertiesDialog::okClicked, this, &ViewPropertiesDialog::slotOk);
158 connect(this, &ViewPropertiesDialog::applyClicked, this, &ViewPropertiesDialog::slotApply);
159
160 // Only show the following settings if the view properties are remembered
161 // for each directory:
162 if (!useGlobalViewProps) {
163 // create 'Apply View Properties To' group
164 QGroupBox* applyBox = new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main);
165
166 m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
167 "Current folder"), applyBox);
168 m_applyToCurrentFolder->setChecked(true);
169 m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
170 "Current folder including all sub-folders"), applyBox);
171 m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
172 "All folders"), applyBox);
173
174 QButtonGroup* applyGroup = new QButtonGroup(this);
175 applyGroup->addButton(m_applyToCurrentFolder);
176 applyGroup->addButton(m_applyToSubFolders);
177 applyGroup->addButton(m_applyToAllFolders);
178
179 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
180 applyBoxLayout->addWidget(m_applyToCurrentFolder);
181 applyBoxLayout->addWidget(m_applyToSubFolders);
182 applyBoxLayout->addWidget(m_applyToAllFolders);
183
184 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main);
185
186 topLayout->addWidget(applyBox);
187 topLayout->addWidget(m_useAsDefault);
188
189 connect(m_applyToCurrentFolder, &QRadioButton::clicked,
190 this, &ViewPropertiesDialog::markAsDirty);
191 connect(m_applyToSubFolders, &QRadioButton::clicked,
192 this, &ViewPropertiesDialog::markAsDirty);
193 connect(m_applyToAllFolders, &QRadioButton::clicked,
194 this, &ViewPropertiesDialog::markAsDirty);
195 connect(m_useAsDefault, &QCheckBox::clicked,
196 this, &ViewPropertiesDialog::markAsDirty);
197 }
198
199 main->setLayout(topLayout);
200 setMainWidget(main);
201
202 const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
203 "ViewPropertiesDialog");
204 restoreDialogSize(dialogConfig);
205
206 loadSettings();
207 }
208
209 ViewPropertiesDialog::~ViewPropertiesDialog()
210 {
211 m_isDirty = false;
212 delete m_viewProps;
213 m_viewProps = 0;
214
215 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
216 "ViewPropertiesDialog");
217 saveDialogSize(dialogConfig, KConfigBase::Persistent);
218 }
219
220 void ViewPropertiesDialog::slotOk()
221 {
222 applyViewProperties();
223 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 m_isDirty = isDirty;
284 enableButtonApply(isDirty);
285 }
286
287 void ViewPropertiesDialog::configureAdditionalInfo()
288 {
289 QList<QByteArray> visibleRoles = m_viewProps->visibleRoles();
290 const bool useDefaultRoles = (m_viewProps->viewMode() == DolphinView::DetailsView) && visibleRoles.isEmpty();
291 if (useDefaultRoles) {
292 // Using the details view without any additional information (-> additional column)
293 // makes no sense and leads to a usability problem as no viewport area is available
294 // anymore. Hence as fallback provide at least a size and date column.
295 visibleRoles.clear();
296 visibleRoles.append("text");
297 visibleRoles.append("size");
298 visibleRoles.append("date");
299 m_viewProps->setVisibleRoles(visibleRoles);
300 }
301
302 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, visibleRoles);
303 if (dialog->exec() == QDialog::Accepted) {
304 m_viewProps->setVisibleRoles(dialog->visibleRoles());
305 markAsDirty(true);
306 }
307 delete dialog;
308 }
309
310 void ViewPropertiesDialog::applyViewProperties()
311 {
312 // if nothing changed in the dialog, we have nothing to apply
313 if (!m_isDirty) {
314 return;
315 }
316
317 const bool applyToSubFolders = m_applyToSubFolders && m_applyToSubFolders->isChecked();
318 if (applyToSubFolders) {
319 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
320 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
321 return;
322 }
323
324 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
325 m_dolphinView->url(),
326 *m_viewProps);
327 info->setAttribute(Qt::WA_DeleteOnClose);
328 info->setWindowModality(Qt::NonModal);
329 info->show();
330 }
331
332 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
333
334 // If the user selected 'Apply To All Folders' the view properties implicitely
335 // are also used as default for new folders.
336 const bool useAsDefault = applyToAllFolders || (m_useAsDefault && m_useAsDefault->isChecked());
337 if (useAsDefault) {
338 // For directories where no .directory file is available, the .directory
339 // file stored for the global view properties is used as fallback. To update
340 // this file we temporary turn on the global view properties mode.
341 Q_ASSERT(!GeneralSettings::globalViewProps());
342
343 GeneralSettings::setGlobalViewProps(true);
344 ViewProperties defaultProps(m_dolphinView->url());
345 defaultProps.setDirProperties(*m_viewProps);
346 defaultProps.save();
347 GeneralSettings::setGlobalViewProps(false);
348 }
349
350 if (applyToAllFolders) {
351 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
352 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
353 return;
354 }
355
356 // Updating the global view properties time stamp in the general settings makes
357 // all existing viewproperties invalid, as they have a smaller time stamp.
358 GeneralSettings* settings = GeneralSettings::self();
359 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
360 settings->writeConfig();
361 }
362
363 m_dolphinView->setMode(m_viewProps->viewMode());
364 m_dolphinView->setSortRole(m_viewProps->sortRole());
365 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
366 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
367 m_dolphinView->setGroupedSorting(m_viewProps->groupedSorting());
368 m_dolphinView->setVisibleRoles(m_viewProps->visibleRoles());
369 m_dolphinView->setPreviewsShown(m_viewProps->previewsShown());
370 m_dolphinView->setHiddenFilesShown(m_viewProps->hiddenFilesShown());
371
372 m_viewProps->save();
373
374 markAsDirty(false);
375 }
376
377 void ViewPropertiesDialog::loadSettings()
378 {
379 // Load view mode
380 switch (m_viewProps->viewMode()) {
381 case DolphinView::IconsView: m_viewMode->setCurrentIndex(0); break;
382 case DolphinView::CompactView: m_viewMode->setCurrentIndex(1); break;
383 case DolphinView::DetailsView: m_viewMode->setCurrentIndex(2); break;
384 default: break;
385 }
386
387 // Load sort order and sorting
388 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
389 m_sortOrder->setCurrentIndex(sortOrderIndex);
390
391 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
392 int sortRoleIndex = 0;
393 for (int i = 0; i < rolesInfo.count(); ++i) {
394 if (rolesInfo[i].role == m_viewProps->sortRole()) {
395 sortRoleIndex = i;
396 break;
397 }
398 }
399 m_sorting->setCurrentIndex(sortRoleIndex);
400
401 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
402
403 // Load show preview, show in groups and show hidden files settings
404 m_previewsShown->setChecked(m_viewProps->previewsShown());
405 m_showInGroups->setChecked(m_viewProps->groupedSorting());
406 m_showHiddenFiles->setChecked(m_viewProps->hiddenFilesShown());
407 markAsDirty(false);
408 }
409