]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
Use capitalized KDE includes
[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 "views/dolphinview.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphin_iconsmodesettings.h"
28 #include "viewpropsprogressinfo.h"
29
30 #include <config-nepomuk.h>
31 #ifdef HAVE_NEPOMUK
32 #include <nepomuk/resourcemanager.h>
33 #endif
34
35 #include <KComponentData>
36 #include <KLocale>
37 #include <KIconLoader>
38 #include <KIO/NetAccess>
39 #include <KMessageBox>
40 #include <KStandardDirs>
41 #include <KUrl>
42 #include <kcombobox.h>
43
44 #include <QAction>
45 #include <QButtonGroup>
46 #include <QCheckBox>
47 #include <QGridLayout>
48 #include <QGroupBox>
49 #include <QLabel>
50 #include <QMenu>
51 #include <QPushButton>
52 #include <QRadioButton>
53 #include <QBoxLayout>
54
55 #include <views/dolphinsortfilterproxymodel.h>
56 #include <views/viewproperties.h>
57
58 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
59 KDialog(dolphinView),
60 m_isDirty(false),
61 m_dolphinView(dolphinView),
62 m_viewProps(0),
63 m_viewMode(0),
64 m_sortOrder(0),
65 m_sorting(0),
66 m_sortFoldersFirst(0),
67 m_showPreview(0),
68 m_showInGroups(0),
69 m_showHiddenFiles(0),
70 m_additionalInfo(0),
71 m_applyToCurrentFolder(0),
72 m_applyToSubFolders(0),
73 m_applyToAllFolders(0),
74 m_useAsDefault(0)
75 {
76 Q_ASSERT(dolphinView != 0);
77 const bool useGlobalViewProps = DolphinSettings::instance().generalSettings()->globalViewProps();
78
79 setCaption(i18nc("@title:window", "View Properties"));
80 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
81
82 const KUrl& url = dolphinView->url();
83 m_viewProps = new ViewProperties(url);
84 m_viewProps->setAutoSaveEnabled(false);
85
86 QWidget* main = new QWidget();
87 QVBoxLayout* topLayout = new QVBoxLayout();
88
89 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
90 QWidget* propsBox = main;
91 if (!useGlobalViewProps) {
92 propsBox = new QGroupBox(i18nc("@title:group", "Properties"), main);
93 }
94
95 QWidget* propsGrid = new QWidget();
96
97 QLabel* viewModeLabel = new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid);
98 m_viewMode = new KComboBox(propsGrid);
99 m_viewMode->addItem(KIcon("view-list-icons"), i18nc("@item:inlistbox", "Icons"));
100 m_viewMode->addItem(KIcon("view-list-details"), i18nc("@item:inlistbox", "Details"));
101 m_viewMode->addItem(KIcon("view-file-columns"), i18nc("@item:inlistbox", "Column"));
102
103 QLabel* sortingLabel = new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid);
104 QWidget* sortingBox = new QWidget(propsGrid);
105
106 m_sortOrder = new KComboBox(sortingBox);
107 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
108 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Descending"));
109
110 m_sorting = new KComboBox(sortingBox);
111 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Name"));
112 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Size"));
113 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Date"));
114 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Permissions"));
115 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Owner"));
116 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Group"));
117 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Type"));
118 #ifdef HAVE_NEPOMUK
119 // TODO: Hided "sort by rating" and "sort by tags" as without caching the performance
120 // is too slow currently (Nepomuk will support caching in future releases).
121 //
122 // if (!Nepomuk::ResourceManager::instance()->init()) {
123 // m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Rating"));
124 // m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Tags"));
125 // }
126 #endif
127 m_sortFoldersFirst = new QCheckBox(i18nc("@option:check", "Show folders first"));
128 m_showPreview = new QCheckBox(i18nc("@option:check", "Show preview"));
129 m_showInGroups = new QCheckBox(i18nc("@option:check", "Show in groups"));
130 m_showHiddenFiles = new QCheckBox(i18nc("@option:check", "Show hidden files"));
131
132 m_additionalInfo = new QPushButton(i18nc("@action:button", "Additional Information"));
133
134 QHBoxLayout* sortingLayout = new QHBoxLayout();
135 sortingLayout->setMargin(0);
136 sortingLayout->addWidget(m_sortOrder);
137 sortingLayout->addWidget(m_sorting);
138 sortingBox->setLayout(sortingLayout);
139
140 QGridLayout* propsGridLayout = new QGridLayout(propsGrid);
141 propsGridLayout->addWidget(viewModeLabel, 0, 0, Qt::AlignRight);
142 propsGridLayout->addWidget(m_viewMode, 0, 1);
143 propsGridLayout->addWidget(sortingLabel, 1, 0, Qt::AlignRight);
144 propsGridLayout->addWidget(sortingBox, 1, 1);
145
146 QVBoxLayout* propsBoxLayout = new QVBoxLayout(propsBox);
147 propsBoxLayout->addWidget(propsGrid);
148 propsBoxLayout->addWidget(m_sortFoldersFirst);
149 propsBoxLayout->addWidget(m_showPreview);
150 propsBoxLayout->addWidget(m_showInGroups);
151 propsBoxLayout->addWidget(m_showHiddenFiles);
152 propsBoxLayout->addWidget(m_additionalInfo);
153
154 topLayout->addWidget(propsBox);
155
156 connect(m_viewMode, SIGNAL(currentIndexChanged(int)),
157 this, SLOT(slotViewModeChanged(int)));
158 connect(m_sorting, SIGNAL(currentIndexChanged(int)),
159 this, SLOT(slotSortingChanged(int)));
160 connect(m_sortOrder, SIGNAL(currentIndexChanged(int)),
161 this, SLOT(slotSortOrderChanged(int)));
162 connect(m_additionalInfo, SIGNAL(clicked()),
163 this, SLOT(configureAdditionalInfo()));
164 connect(m_sortFoldersFirst, SIGNAL(clicked()),
165 this, SLOT(slotSortFoldersFirstChanged()));
166 connect(m_showPreview, SIGNAL(clicked()),
167 this, SLOT(slotShowPreviewChanged()));
168 connect(m_showInGroups, SIGNAL(clicked()),
169 this, SLOT(slotCategorizedSortingChanged()));
170 connect(m_showHiddenFiles, SIGNAL(clicked()),
171 this, SLOT(slotShowHiddenFilesChanged()));
172
173 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
174 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
175
176 // Only show the following settings if the view properties are remembered
177 // for each directory:
178 if (!useGlobalViewProps) {
179 // create 'Apply View Properties To' group
180 QGroupBox* applyBox = new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main);
181
182 m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
183 "Current folder"), applyBox);
184 m_applyToCurrentFolder->setChecked(true);
185 m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
186 "Current folder including all sub-folders"), applyBox);
187 m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
188 "All folders"), applyBox);
189
190 QButtonGroup* applyGroup = new QButtonGroup(this);
191 applyGroup->addButton(m_applyToCurrentFolder);
192 applyGroup->addButton(m_applyToSubFolders);
193 applyGroup->addButton(m_applyToAllFolders);
194
195 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
196 applyBoxLayout->addWidget(m_applyToCurrentFolder);
197 applyBoxLayout->addWidget(m_applyToSubFolders);
198 applyBoxLayout->addWidget(m_applyToAllFolders);
199
200 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main);
201
202 topLayout->addWidget(applyBox);
203 topLayout->addWidget(m_useAsDefault);
204
205 connect(m_applyToCurrentFolder, SIGNAL(clicked(bool)),
206 this, SLOT(markAsDirty(bool)));
207 connect(m_applyToSubFolders, SIGNAL(clicked(bool)),
208 this, SLOT(markAsDirty(bool)));
209 connect(m_applyToAllFolders, SIGNAL(clicked(bool)),
210 this, SLOT(markAsDirty(bool)));
211 connect(m_useAsDefault, SIGNAL(clicked(bool)),
212 this, SLOT(markAsDirty(bool)));
213 }
214
215 main->setLayout(topLayout);
216 setMainWidget(main);
217
218 const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
219 "ViewPropertiesDialog");
220 restoreDialogSize(dialogConfig);
221
222 loadSettings();
223 }
224
225 ViewPropertiesDialog::~ViewPropertiesDialog()
226 {
227 m_isDirty = false;
228 delete m_viewProps;
229 m_viewProps = 0;
230
231 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
232 "ViewPropertiesDialog");
233 saveDialogSize(dialogConfig, KConfigBase::Persistent);
234 }
235
236 void ViewPropertiesDialog::slotOk()
237 {
238 applyViewProperties();
239 accept();
240 }
241
242 void ViewPropertiesDialog::slotApply()
243 {
244 applyViewProperties();
245 markAsDirty(false);
246 }
247
248 void ViewPropertiesDialog::slotViewModeChanged(int index)
249 {
250 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
251 markAsDirty(true);
252
253 const DolphinView::Mode mode = m_viewProps->viewMode();
254 m_showInGroups->setEnabled(mode == DolphinView::IconsView);
255 m_additionalInfo->setEnabled(mode != DolphinView::ColumnView);
256 }
257
258 void ViewPropertiesDialog::slotSortingChanged(int index)
259 {
260 const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
261 m_viewProps->setSorting(sorting);
262 markAsDirty(true);
263 }
264
265 void ViewPropertiesDialog::slotSortOrderChanged(int index)
266 {
267 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
268 m_viewProps->setSortOrder(sortOrder);
269 markAsDirty(true);
270 }
271
272 void ViewPropertiesDialog::slotCategorizedSortingChanged()
273 {
274 m_viewProps->setCategorizedSorting(m_showInGroups->isChecked());
275 markAsDirty(true);
276 }
277
278 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
279 {
280 const bool foldersFirst = m_sortFoldersFirst->isChecked();
281 m_viewProps->setSortFoldersFirst(foldersFirst);
282 markAsDirty(true);
283 }
284
285 void ViewPropertiesDialog::slotShowPreviewChanged()
286 {
287 const bool show = m_showPreview->isChecked();
288 m_viewProps->setShowPreview(show);
289 markAsDirty(true);
290 }
291
292 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
293 {
294 const bool show = m_showHiddenFiles->isChecked();
295 m_viewProps->setShowHiddenFiles(show);
296 markAsDirty(true);
297 }
298
299 void ViewPropertiesDialog::markAsDirty(bool isDirty)
300 {
301 m_isDirty = isDirty;
302 enableButtonApply(isDirty);
303 }
304
305 void ViewPropertiesDialog::configureAdditionalInfo()
306 {
307 KFileItemDelegate::InformationList info = m_viewProps->additionalInfo();
308 const bool useDefaultInfo = (m_viewProps->viewMode() == DolphinView::DetailsView) &&
309 (info.isEmpty() || info.contains(KFileItemDelegate::NoInformation));
310 if (useDefaultInfo) {
311 // Using the details view without any additional information (-> additional column)
312 // makes no sense and leads to a usability problem as no viewport area is available
313 // anymore. Hence as fallback provide at least a size and date column.
314 info.clear();
315 info.append(KFileItemDelegate::Size);
316 info.append(KFileItemDelegate::ModificationTime);
317 m_viewProps->setAdditionalInfo(info);
318 }
319
320 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, info);
321 if (dialog->exec() == QDialog::Accepted) {
322 m_viewProps->setAdditionalInfo(dialog->informationList());
323 markAsDirty(true);
324 }
325 delete dialog;
326 }
327
328 void ViewPropertiesDialog::applyViewProperties()
329 {
330 // if nothing changed in the dialog, we have nothing to apply
331 if (!m_isDirty) {
332 return;
333 }
334
335 const bool applyToSubFolders = (m_applyToSubFolders != 0) &&
336 m_applyToSubFolders->isChecked();
337 if (applyToSubFolders) {
338 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
339 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
340 return;
341 }
342
343 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
344 m_dolphinView->url(),
345 *m_viewProps);
346 info->setAttribute(Qt::WA_DeleteOnClose);
347 info->setWindowModality(Qt::NonModal);
348 info->show();
349 }
350
351 const bool applyToAllFolders = (m_applyToAllFolders != 0) &&
352 m_applyToAllFolders->isChecked();
353
354 // If the user selected 'Apply To All Folders' the view properties implicitely
355 // are also used as default for new folders.
356 const bool useAsDefault = applyToAllFolders ||
357 ((m_useAsDefault != 0) && m_useAsDefault->isChecked());
358 if (useAsDefault) {
359 // For directories where no .directory file is available, the .directory
360 // file stored for the global view properties is used as fallback. To update
361 // this file we temporary turn on the global view properties mode.
362 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
363 Q_ASSERT(!settings->globalViewProps());
364
365 settings->setGlobalViewProps(true);
366 ViewProperties defaultProps(m_dolphinView->url());
367 defaultProps.setDirProperties(*m_viewProps);
368 defaultProps.save();
369 settings->setGlobalViewProps(false);
370 }
371
372 if (applyToAllFolders) {
373 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
374 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
375 return;
376 }
377
378 // Updating the global view properties time stamp in the general settings makes
379 // all existing viewproperties invalid, as they have a smaller time stamp.
380 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
381 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
382 }
383
384 m_dolphinView->setMode(m_viewProps->viewMode());
385 m_dolphinView->setSorting(m_viewProps->sorting());
386 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
387 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
388 m_dolphinView->setCategorizedSorting(m_viewProps->categorizedSorting());
389 m_dolphinView->setAdditionalInfo(m_viewProps->additionalInfo());
390 m_dolphinView->setShowPreview(m_viewProps->showPreview());
391 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
392
393 m_viewProps->save();
394
395 markAsDirty(false);
396 }
397
398 void ViewPropertiesDialog::loadSettings()
399 {
400 // load view mode
401 const int index = static_cast<int>(m_viewProps->viewMode());
402 m_viewMode->setCurrentIndex(index);
403
404 // load sort order and sorting
405 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
406 m_sortOrder->setCurrentIndex(sortOrderIndex);
407 m_sorting->setCurrentIndex(m_viewProps->sorting());
408
409 const bool enabled = (index == DolphinView::DetailsView) ||
410 (index == DolphinView::IconsView);
411 m_additionalInfo->setEnabled(enabled);
412
413 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
414 // load show preview, show in groups and show hidden files settings
415 m_showPreview->setChecked(m_viewProps->showPreview());
416
417 m_showInGroups->setChecked(m_viewProps->categorizedSorting());
418 m_showInGroups->setEnabled(index == DolphinView::IconsView); // only the icons view supports categorized sorting
419
420 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
421 markAsDirty(false);
422 }
423
424 #include "viewpropertiesdialog.moc"