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