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