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