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