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