]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
make the Viewproperties dialog less cluttered and smaller
[dolphin.git] / src / 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 #include "viewpropsprogressinfo.h"
23 #include "dolphinview.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphin_generalsettings.h"
27 #include "viewproperties.h"
28
29 #include <kcomponentdata.h>
30 #include <klocale.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
33 #include <kmessagebox.h>
34 #include <kstandarddirs.h>
35 #include <kurl.h>
36
37 #include <QAction>
38 #include <QButtonGroup>
39 #include <QCheckBox>
40 #include <QComboBox>
41 #include <QGridLayout>
42 #include <QGroupBox>
43 #include <QLabel>
44 #include <QMenu>
45 #include <QPushButton>
46 #include <QRadioButton>
47 #include <QBoxLayout>
48
49 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
50 KDialog(dolphinView),
51 m_isDirty(false),
52 m_dolphinView(dolphinView),
53 m_viewProps(0),
54 m_viewMode(0),
55 m_sorting(0),
56 m_descendingAction(0),
57 m_showInGroupsAction(0),
58 m_additionalInfo(0),
59 m_showPreview(0),
60 m_showHiddenFiles(0),
61 m_applyToCurrentFolder(0),
62 m_applyToSubFolders(0),
63 m_useAsDefault(0)
64 {
65 Q_ASSERT(dolphinView != 0);
66 const bool useGlobalViewProps = DolphinSettings::instance().generalSettings()->globalViewProps();
67
68 setCaption(i18n("View Properties"));
69 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
70
71 const KUrl& url = dolphinView->url();
72 m_viewProps = new ViewProperties(url);
73 m_viewProps->setAutoSaveEnabled(false);
74
75 QWidget* main = new QWidget();
76 QVBoxLayout* topLayout = new QVBoxLayout();
77
78 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
79 QWidget* propsBox = main;
80 if (!useGlobalViewProps) {
81 propsBox = new QGroupBox(i18n("Properties"), main);
82 }
83
84 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
85 m_viewMode = new QComboBox(propsBox);
86 m_viewMode->addItem(KIcon("fileview-icon"), i18n("Icons"));
87 m_viewMode->addItem(KIcon("fileview-detailed"), i18n("Details"));
88 m_viewMode->addItem(KIcon("fileview-column"), i18n("Column"));
89 const int index = static_cast<int>(m_viewProps->viewMode());
90 m_viewMode->setCurrentIndex(index);
91 const bool iconsViewEnabled = (index == DolphinView::IconsView);
92
93 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
94 QWidget* sortingBox = new QWidget(propsBox);
95
96 QMenu* sortingFlagsMenu = new QMenu(sortingBox);
97 m_descendingAction = sortingFlagsMenu->addAction(i18n("Descending"));
98 m_descendingAction->setCheckable(true);
99 m_descendingAction->setChecked(m_viewProps->sortOrder() == Qt::Descending);
100 m_showInGroupsAction = sortingFlagsMenu->addAction(i18n("Show in Groups"));
101 m_showInGroupsAction->setCheckable(true);
102 m_showInGroupsAction->setChecked(m_viewProps->categorizedSorting());
103
104 QPushButton* sortFlagsButton = new QPushButton(KIcon("configure"), QString(), sortingBox);
105 sortFlagsButton->setMenu(sortingFlagsMenu);
106
107 m_sorting = new QComboBox(sortingBox);
108 m_sorting->addItem("By Name");
109 m_sorting->addItem("By Size");
110 m_sorting->addItem("By Date");
111 m_sorting->addItem("By Permissions");
112 m_sorting->addItem("By Owner");
113 m_sorting->addItem("By Group");
114 m_sorting->addItem("By Type");
115 m_sorting->setCurrentIndex(m_viewProps->sorting());
116
117 QHBoxLayout* sortingLayout = new QHBoxLayout();
118 sortingLayout->setMargin(0);
119 sortingLayout->addWidget(m_sorting);
120 sortingLayout->addWidget(sortFlagsButton);
121 sortingBox->setLayout(sortingLayout);
122
123 QLabel* additionalInfoLabel = new QLabel(i18n("Additional information:"), propsBox);
124 m_additionalInfo = new QComboBox(propsBox);
125 m_additionalInfo->addItem(i18n("No Information"), KFileItemDelegate::NoInformation);
126 m_additionalInfo->addItem(i18n("Type"), KFileItemDelegate::FriendlyMimeType);
127 m_additionalInfo->addItem(i18n("Size"), KFileItemDelegate::Size);
128 m_additionalInfo->addItem(i18n("Date"), KFileItemDelegate::ModificationTime);
129 const int addInfoIndex = m_additionalInfo->findData(m_viewProps->additionalInfo());
130 m_additionalInfo->setCurrentIndex(addInfoIndex);
131 m_additionalInfo->setEnabled(iconsViewEnabled);
132
133 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
134 m_showPreview->setChecked(m_viewProps->showPreview());
135
136 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
137 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
138
139 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
140 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
141 propsBoxLayout->addWidget(m_viewMode, 0, 1);
142 propsBoxLayout->addWidget(sortingLabel, 1, 0);
143 propsBoxLayout->addWidget(sortingBox, 1, 1);
144 propsBoxLayout->addWidget(additionalInfoLabel, 2, 0);
145 propsBoxLayout->addWidget(m_additionalInfo, 2, 1);
146 propsBoxLayout->addWidget(m_showPreview, 3, 0);
147 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
148
149 topLayout->addWidget(propsBox);
150
151 connect(m_viewMode, SIGNAL(activated(int)),
152 this, SLOT(slotViewModeChanged(int)));
153 connect(m_sorting, SIGNAL(activated(int)),
154 this, SLOT(slotSortingChanged(int)));
155 connect(m_descendingAction, SIGNAL(changed()),
156 this, SLOT(slotSortOrderChanged()));
157 connect(m_showInGroupsAction, SIGNAL(changed()),
158 this, SLOT(slotCategorizedSortingChanged()));
159 connect(m_additionalInfo, SIGNAL(activated(int)),
160 this, SLOT(slotAdditionalInfoChanged(int)));
161 connect(m_showPreview, SIGNAL(clicked()),
162 this, SLOT(slotShowPreviewChanged()));
163 connect(m_showHiddenFiles, SIGNAL(clicked()),
164 this, SLOT(slotShowHiddenFilesChanged()));
165
166 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
167 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
168
169 // Only show the following settings if the view properties are remembered
170 // for each directory:
171 if (!useGlobalViewProps) {
172 // create 'Apply View Properties To' group
173 QGroupBox* applyBox = new QGroupBox(i18n("Apply View Properties To"), main);
174
175 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), applyBox);
176 m_applyToCurrentFolder->setChecked(true);
177 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), applyBox);
178 m_applyToAllFolders = new QRadioButton(i18n("All folders"), applyBox);
179
180 QButtonGroup* applyGroup = new QButtonGroup(this);
181 applyGroup->addButton(m_applyToCurrentFolder);
182 applyGroup->addButton(m_applyToSubFolders);
183 applyGroup->addButton(m_applyToAllFolders);
184
185 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
186 applyBoxLayout->addWidget(m_applyToCurrentFolder);
187 applyBoxLayout->addWidget(m_applyToSubFolders);
188 applyBoxLayout->addWidget(m_applyToAllFolders);
189
190 m_useAsDefault = new QCheckBox(i18n("Use as default for new folders"), main);
191
192 topLayout->addWidget(applyBox);
193 topLayout->addWidget(m_useAsDefault);
194
195 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
196 this, SLOT(markAsDirty()));
197 connect(m_applyToSubFolders, SIGNAL(clicked()),
198 this, SLOT(markAsDirty()));
199 connect(m_applyToAllFolders, SIGNAL(clicked()),
200 this, SLOT(markAsDirty()));
201 connect(m_useAsDefault, SIGNAL(clicked()),
202 this, SLOT(markAsDirty()));
203 }
204
205 main->setLayout(topLayout);
206 setMainWidget(main);
207
208 const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
209 "ViewPropertiesDialog");
210 restoreDialogSize(dialogConfig);
211 }
212
213 ViewPropertiesDialog::~ViewPropertiesDialog()
214 {
215 m_isDirty = false;
216 delete m_viewProps;
217 m_viewProps = 0;
218
219 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
220 "ViewPropertiesDialog");
221 saveDialogSize(dialogConfig, KConfigFlags::Persistent);
222 }
223
224 void ViewPropertiesDialog::slotOk()
225 {
226 applyViewProperties();
227 accept();
228 }
229
230 void ViewPropertiesDialog::slotApply()
231 {
232 applyViewProperties();
233 }
234
235 void ViewPropertiesDialog::slotViewModeChanged(int index)
236 {
237 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
238 m_isDirty = true;
239
240 const bool iconsViewEnabled = (m_viewProps->viewMode() == DolphinView::IconsView);
241 m_showInGroupsAction->setEnabled(iconsViewEnabled);
242 m_additionalInfo->setEnabled(iconsViewEnabled);
243 }
244
245 void ViewPropertiesDialog::slotSortingChanged(int index)
246 {
247 const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
248 m_viewProps->setSorting(sorting);
249 m_isDirty = true;
250 }
251
252 void ViewPropertiesDialog::slotSortOrderChanged()
253 {
254 Qt::SortOrder sortOrder = m_descendingAction->isChecked() ?
255 Qt::DescendingOrder :
256 Qt::AscendingOrder;
257 m_viewProps->setSortOrder(sortOrder);
258 m_isDirty = true;
259 }
260
261 void ViewPropertiesDialog::slotCategorizedSortingChanged()
262 {
263 m_viewProps->setCategorizedSorting(m_showInGroupsAction->isChecked());
264 m_isDirty = true;
265 }
266
267 void ViewPropertiesDialog::slotAdditionalInfoChanged(int index)
268 {
269 KFileItemDelegate::AdditionalInformation info = KFileItemDelegate::NoInformation;
270 switch (index) {
271 case 1: info = KFileItemDelegate::FriendlyMimeType; break;
272 case 2: info = KFileItemDelegate::Size; break;
273 case 3: info = KFileItemDelegate::ModificationTime; break;
274 default: break;
275 }
276 m_viewProps->setAdditionalInfo(info);
277 m_isDirty = true;
278 }
279
280 void ViewPropertiesDialog::slotShowPreviewChanged()
281 {
282 const bool show = m_showPreview->isChecked();
283 m_viewProps->setShowPreview(show);
284 m_isDirty = true;
285 }
286
287 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
288 {
289 const bool show = m_showHiddenFiles->isChecked();
290 m_viewProps->setShowHiddenFiles(show);
291 m_isDirty = true;
292 }
293
294 void ViewPropertiesDialog::markAsDirty()
295 {
296 m_isDirty = true;
297 }
298
299 void ViewPropertiesDialog::applyViewProperties()
300 {
301 const bool applyToSubFolders = m_isDirty &&
302 (m_applyToSubFolders != 0) &&
303 m_applyToSubFolders->isChecked();
304 if (applyToSubFolders) {
305 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
306 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
307 return;
308 }
309
310 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
311 m_dolphinView->url(),
312 *m_viewProps);
313 info->setWindowModality(Qt::NonModal);
314 info->show();
315 }
316
317 const bool applyToAllFolders = m_isDirty &&
318 (m_applyToAllFolders != 0) &&
319 m_applyToAllFolders->isChecked();
320 if (applyToAllFolders) {
321 const QString text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
322 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
323 return;
324 }
325
326 // Updating the global view properties time stamp in the general settings makes
327 // all existing viewproperties invalid, as they have a smaller time stamp.
328 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
329 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
330
331 // This is also a good chance to make a cleanup of all mirrored view properties:
332 const KUrl mirroredDir = ViewProperties::mirroredDirectory();
333 KIO::NetAccess::del(mirroredDir, this);
334 }
335
336 m_viewProps->save();
337
338 m_dolphinView->setMode(m_viewProps->viewMode());
339 m_dolphinView->setSorting(m_viewProps->sorting());
340 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
341 m_dolphinView->setCategorizedSorting(m_viewProps->categorizedSorting());
342 m_dolphinView->setAdditionalInfo(m_viewProps->additionalInfo());
343 m_dolphinView->setShowPreview(m_viewProps->showPreview());
344 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
345
346 m_isDirty = false;
347
348 if (m_useAsDefault->isChecked()) {
349 // For directories where no .directory file is available, the .directory
350 // file stored for the global view properties is used as fallback. To update
351 // this file we temporary turn on the global view properties mode.
352 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
353 Q_ASSERT(!settings->globalViewProps());
354
355 settings->setGlobalViewProps(true);
356 ViewProperties defaultProps(m_dolphinView->url());
357 defaultProps.setDirProperties(*m_viewProps);
358 defaultProps.save();
359 settings->setGlobalViewProps(false);
360 }
361 }
362
363 #include "viewpropertiesdialog.moc"