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