]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphindetailsview.cpp
Disallow renaming to names containing '/', names being equal to "." or "..".
[dolphin.git] / src / views / dolphindetailsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * Copyright (C) 2008 by Simon St. James (kdedevel@etotheipiplusone.com) *
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 "dolphindetailsview.h"
22
23 #include "additionalinfoaccessor.h"
24 #include "dolphinmodel.h"
25 #include "dolphinviewcontroller.h"
26 #include "dolphinfileitemdelegate.h"
27 #include "settings/dolphinsettings.h"
28 #include "dolphinsortfilterproxymodel.h"
29 #include "dolphinviewautoscroller.h"
30 #include "draganddrophelper.h"
31 #include "viewextensionsfactory.h"
32 #include "viewmodecontroller.h"
33 #include "viewproperties.h"
34 #include "zoomlevelinfo.h"
35
36 #include "dolphin_detailsmodesettings.h"
37 #include "dolphin_generalsettings.h"
38
39 #include <KDirModel>
40 #include <KDirLister>
41 #include <KLocale>
42 #include <KMenu>
43
44 #include <QApplication>
45 #include <QHeaderView>
46 #include <QScrollBar>
47
48 DolphinDetailsView::DolphinDetailsView(QWidget* parent,
49 DolphinViewController* dolphinViewController,
50 const ViewModeController* viewModeController,
51 DolphinSortFilterProxyModel* proxyModel) :
52 DolphinTreeView(parent),
53 m_autoResize(true),
54 m_dolphinViewController(dolphinViewController),
55 m_extensionsFactory(0),
56 m_expandableFoldersAction(0),
57 m_expandedUrls(),
58 m_font(),
59 m_decorationSize()
60 {
61 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
62 Q_ASSERT(settings);
63 Q_ASSERT(dolphinViewController);
64 Q_ASSERT(viewModeController);
65
66 setLayoutDirection(Qt::LeftToRight);
67 setAcceptDrops(true);
68 setSortingEnabled(true);
69 setSelectionBehavior(SelectItems);
70 setDragDropMode(QAbstractItemView::DragDrop);
71 setDropIndicatorShown(false);
72 setAlternatingRowColors(true);
73 setRootIsDecorated(settings->expandableFolders());
74 setItemsExpandable(settings->expandableFolders());
75 setEditTriggers(QAbstractItemView::NoEditTriggers);
76 setModel(proxyModel);
77
78 setMouseTracking(true);
79
80 const ViewProperties props(viewModeController->url());
81 setSortIndicatorSection(props.sorting());
82 setSortIndicatorOrder(props.sortOrder());
83
84 QHeaderView* headerView = header();
85 connect(headerView, SIGNAL(sectionClicked(int)),
86 this, SLOT(synchronizeSortingState(int)));
87 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
88 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
89 this, SLOT(configureSettings(const QPoint&)));
90 connect(headerView, SIGNAL(sectionResized(int, int, int)),
91 this, SLOT(slotHeaderSectionResized(int, int, int)));
92 connect(headerView, SIGNAL(sectionHandleDoubleClicked(int)),
93 this, SLOT(disableAutoResizing()));
94
95 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
96 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
97 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
98 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
99
100 connect(this, SIGNAL(clicked(const QModelIndex&)),
101 dolphinViewController, SLOT(requestTab(const QModelIndex&)));
102 if (KGlobalSettings::singleClick()) {
103 connect(this, SIGNAL(clicked(const QModelIndex&)),
104 dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
105 } else {
106 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
107 dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
108 }
109
110 connect(this, SIGNAL(entered(const QModelIndex&)),
111 this, SLOT(slotEntered(const QModelIndex&)));
112 connect(this, SIGNAL(viewportEntered()),
113 dolphinViewController, SLOT(emitViewportEntered()));
114 connect(viewModeController, SIGNAL(zoomLevelChanged(int)),
115 this, SLOT(setZoomLevel(int)));
116 connect(dolphinViewController->view(), SIGNAL(additionalInfoChanged()),
117 this, SLOT(updateColumnVisibility()));
118 connect(viewModeController, SIGNAL(activationChanged(bool)),
119 this, SLOT(slotActivationChanged(bool)));
120
121 if (settings->useSystemFont()) {
122 m_font = KGlobalSettings::generalFont();
123 } else {
124 m_font = QFont(settings->fontFamily(),
125 qRound(settings->fontSize()),
126 settings->fontWeight(),
127 settings->italicFont());
128 m_font.setPointSizeF(settings->fontSize());
129 }
130
131 setVerticalScrollMode(QTreeView::ScrollPerPixel);
132 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
133
134 const DolphinView* view = dolphinViewController->view();
135 connect(view, SIGNAL(showPreviewChanged()),
136 this, SLOT(slotShowPreviewChanged()));
137
138 viewport()->installEventFilter(this);
139
140 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
141 this, SLOT(slotGlobalSettingsChanged(int)));
142
143 m_expandableFoldersAction = new QAction(i18nc("@option:check", "Expandable Folders"), this);
144 m_expandableFoldersAction->setCheckable(true);
145 connect(m_expandableFoldersAction, SIGNAL(toggled(bool)),
146 this, SLOT(setFoldersExpandable(bool)));
147
148 connect(this, SIGNAL(expanded(const QModelIndex&)), this, SLOT(slotExpanded(const QModelIndex&)));
149 connect(this, SIGNAL(collapsed(const QModelIndex&)), this, SLOT(slotCollapsed(const QModelIndex&)));
150
151 updateDecorationSize(view->showPreview());
152
153 m_extensionsFactory = new ViewExtensionsFactory(this, dolphinViewController, viewModeController);
154 m_extensionsFactory->fileItemDelegate()->setMinimizedNameColumn(true);
155
156 KDirLister *dirLister = qobject_cast<KDirModel*>(proxyModel->sourceModel())->dirLister();
157 connect(dirLister, SIGNAL(newItems(KFileItemList)), this, SLOT(resizeColumns()));
158 }
159
160 DolphinDetailsView::~DolphinDetailsView()
161 {
162 }
163
164 QSet<KUrl> DolphinDetailsView::expandedUrls() const
165 {
166 return m_expandedUrls;
167 }
168
169 bool DolphinDetailsView::event(QEvent* event)
170 {
171 if (event->type() == QEvent::Polish) {
172 header()->setResizeMode(QHeaderView::Interactive);
173 updateColumnVisibility();
174 }
175
176 return DolphinTreeView::event(event);
177 }
178
179 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
180 {
181 QStyleOptionViewItem viewOptions = DolphinTreeView::viewOptions();
182 viewOptions.font = m_font;
183 viewOptions.fontMetrics = QFontMetrics(m_font);
184 viewOptions.showDecorationSelected = true;
185 viewOptions.decorationSize = m_decorationSize;
186 return viewOptions;
187 }
188
189 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
190 {
191 DolphinTreeView::contextMenuEvent(event);
192
193 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
194 m_expandableFoldersAction->setChecked(settings->expandableFolders());
195 m_dolphinViewController->triggerContextMenuRequest(event->pos(),
196 QList<QAction*>() << m_expandableFoldersAction);
197 }
198
199 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
200 {
201 m_dolphinViewController->requestActivation();
202
203 DolphinTreeView::mousePressEvent(event);
204
205 const QModelIndex index = indexAt(event->pos());
206 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
207 // The mouse press is done somewhere outside the filename column
208 if (QApplication::mouseButtons() & Qt::MidButton) {
209 m_dolphinViewController->replaceUrlByClipboard();
210 }
211 }
212 }
213
214 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
215 {
216 DragAndDropHelper::instance().startDrag(this, supportedActions, m_dolphinViewController);
217 DolphinTreeView::startDrag(supportedActions);
218 }
219
220 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
221 {
222 event->acceptProposedAction();
223 DolphinTreeView::dragEnterEvent(event);
224 }
225
226 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
227 {
228 DolphinTreeView::dragMoveEvent(event);
229 event->acceptProposedAction();
230 }
231
232 void DolphinDetailsView::dropEvent(QDropEvent* event)
233 {
234 const QModelIndex index = indexAt(event->pos());
235 KFileItem item;
236 if (index.isValid() && (index.column() == DolphinModel::Name)) {
237 item = m_dolphinViewController->itemForIndex(index);
238 }
239 m_dolphinViewController->indicateDroppedUrls(item, event);
240 DolphinTreeView::dropEvent(event);
241 }
242
243 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
244 {
245 DolphinTreeView::keyPressEvent(event);
246 m_dolphinViewController->handleKeyPressEvent(event);
247 }
248
249 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
250 {
251 DolphinTreeView::resizeEvent(event);
252 if (m_autoResize) {
253 resizeColumns();
254 }
255 }
256
257 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
258 {
259 const int step = m_decorationSize.height();
260 verticalScrollBar()->setSingleStep(step);
261 DolphinTreeView::wheelEvent(event);
262 }
263
264 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
265 {
266 m_extensionsFactory->handleCurrentIndexChange(current, previous);
267 DolphinTreeView::currentChanged(current, previous);
268
269 // If folders are expanded, the width which is available for editing may have changed
270 // because it depends on the level of the current item in the folder hierarchy.
271 adjustMaximumSizeForEditing(current);
272 }
273
274 bool DolphinDetailsView::eventFilter(QObject* watched, QEvent* event)
275 {
276 if ((watched == viewport()) && (event->type() == QEvent::Leave)) {
277 // If the mouse is above an item and moved very fast outside the widget,
278 // no viewportEntered() signal might be emitted although the mouse has been moved
279 // above the viewport.
280 m_dolphinViewController->emitViewportEntered();
281 }
282
283 return DolphinTreeView::eventFilter(watched, event);
284 }
285
286 QRect DolphinDetailsView::visualRect(const QModelIndex& index) const
287 {
288 QRect rect = DolphinTreeView::visualRect(index);
289 const KFileItem item = m_dolphinViewController->itemForIndex(index);
290 if (!item.isNull()) {
291 const int width = DolphinFileItemDelegate::nameColumnWidth(item.text(), viewOptions());
292
293 if (width < rect.width()) {
294 rect.setWidth(width);
295 }
296 }
297
298 return rect;
299 }
300
301 bool DolphinDetailsView::acceptsDrop(const QModelIndex& index) const
302 {
303 if (index.isValid() && (index.column() == DolphinModel::Name)) {
304 // Accept drops above directories
305 const KFileItem item = m_dolphinViewController->itemForIndex(index);
306 return !item.isNull() && item.isDir();
307 }
308
309 return false;
310 }
311
312 void DolphinDetailsView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
313 {
314 removeExpandedIndexes(parent, start, end);
315 DolphinTreeView::rowsAboutToBeRemoved(parent, start, end);
316 }
317
318 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
319 {
320 header()->setSortIndicator(sorting, header()->sortIndicatorOrder());
321 }
322
323 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
324 {
325 header()->setSortIndicator(header()->sortIndicatorSection(), sortOrder);
326 }
327
328 void DolphinDetailsView::synchronizeSortingState(int column)
329 {
330 // The sorting has already been changed in QTreeView if this slot is
331 // invoked, but Dolphin is not informed about this.
332 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
333 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
334 m_dolphinViewController->indicateSortingChange(sorting);
335 m_dolphinViewController->indicateSortOrderChange(sortOrder);
336 }
337
338 void DolphinDetailsView::slotEntered(const QModelIndex& index)
339 {
340 if (index.column() == DolphinModel::Name) {
341 m_dolphinViewController->emitItemEntered(index);
342 } else {
343 m_dolphinViewController->emitViewportEntered();
344 }
345 }
346
347 void DolphinDetailsView::setZoomLevel(int level)
348 {
349 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
350 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
351
352 const bool showPreview = m_dolphinViewController->view()->showPreview();
353 if (showPreview) {
354 settings->setPreviewSize(size);
355 } else {
356 settings->setIconSize(size);
357 }
358
359 updateDecorationSize(showPreview);
360 }
361
362 void DolphinDetailsView::slotShowPreviewChanged()
363 {
364 const DolphinView* view = m_dolphinViewController->view();
365 updateDecorationSize(view->showPreview());
366 }
367
368 void DolphinDetailsView::configureSettings(const QPoint& pos)
369 {
370 KMenu popup(this);
371 popup.addTitle(i18nc("@title:menu", "Columns"));
372
373 // Add checkbox items for each column
374 QHeaderView* headerView = header();
375 const int columns = model()->columnCount();
376 for (int i = 0; i < columns; ++i) {
377 const int logicalIndex = headerView->logicalIndex(i);
378 const QString text = model()->headerData(logicalIndex, Qt::Horizontal).toString();
379 if (!text.isEmpty()) {
380 QAction* action = popup.addAction(text);
381 action->setCheckable(true);
382 action->setChecked(!headerView->isSectionHidden(logicalIndex));
383 action->setData(logicalIndex);
384 action->setEnabled(logicalIndex != DolphinModel::Name);
385 }
386 }
387 popup.addSeparator();
388
389 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
390 if (activatedAction) {
391 const bool show = activatedAction->isChecked();
392 const int columnIndex = activatedAction->data().toInt();
393
394 KFileItemDelegate::InformationList list = m_dolphinViewController->view()->additionalInfo();
395 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
396 if (show) {
397 Q_ASSERT(!list.contains(info));
398 list.append(info);
399 } else {
400 Q_ASSERT(list.contains(info));
401 const int index = list.indexOf(info);
402 list.removeAt(index);
403 }
404
405 m_dolphinViewController->indicateAdditionalInfoChange(list);
406 setColumnHidden(columnIndex, !show);
407 resizeColumns();
408 }
409 }
410
411 void DolphinDetailsView::updateColumnVisibility()
412 {
413 QHeaderView* headerView = header();
414 disconnect(headerView, SIGNAL(sectionMoved(int, int, int)),
415 this, SLOT(saveColumnPositions()));
416
417 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
418 const QList<int> columnPositions = settings->columnPositions();
419
420 const KFileItemDelegate::InformationList list = m_dolphinViewController->view()->additionalInfo();
421 for (int i = DolphinModel::Name; i < DolphinModel::ExtraColumnCount; ++i) {
422 const KFileItemDelegate::Information info = infoForColumn(i);
423 const bool hide = !list.contains(info) && (i != DolphinModel::Name);
424 if (isColumnHidden(i) != hide) {
425 setColumnHidden(i, hide);
426 }
427
428 // If the list columnPositions has been written by an older Dolphin version,
429 // its length might be smaller than DolphinModel::ExtraColumnCount. Therefore,
430 // we have to check if item number i exists before accessing it.
431 if (i < columnPositions.length()) {
432 const int position = columnPositions[i];
433
434 // The position might be outside the correct range if the list columnPositions
435 // has been written by a newer Dolphin version with more columns.
436 if (position < DolphinModel::ExtraColumnCount) {
437 const int from = headerView->visualIndex(i);
438 headerView->moveSection(from, position);
439 }
440 }
441 }
442
443 resizeColumns();
444
445 connect(headerView, SIGNAL(sectionMoved(int, int, int)),
446 this, SLOT(saveColumnPositions()));
447 }
448
449 void DolphinDetailsView::resizeColumns()
450 {
451 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
452 // around 3 seconds for each (!) resize operation when having > 10000 items).
453 // This gets a problem especially when opening large directories, where several
454 // resize operations are received for showing the currently available items during
455 // loading (the application hangs around 20 seconds when loading > 10000 items).
456
457 QHeaderView* headerView = header();
458 const int rowCount = model()->rowCount();
459 QFontMetrics fontMetrics(viewport()->font());
460 const int horizontalGap = fontMetrics.height();
461
462 // Define the maximum number of rows, where an exact (but expensive) calculation
463 // of the widths is done.
464 const int maxRowCount = 200;
465
466 // Calculate the required with for each column and store it in columnWidth[]
467 int columnWidth[DolphinModel::ExtraColumnCount];
468
469 for (int column = 0; column < DolphinModel::ExtraColumnCount; ++column) {
470 columnWidth[column] = 0;
471 if (!isColumnHidden(column)) {
472 // Calculate the required width for the current column and consider only
473 // up to maxRowCount columns for performance reasons
474 if (rowCount > 0) {
475 const int count = qMin(rowCount, maxRowCount);
476 for (int row = 0; row < count; ++row) {
477 const QModelIndex index = model()->index(row, column);
478 QString text;
479 if (column == DolphinModel::Size) {
480 // This is a workaround as KFileItemDelegate::sizeHint() does not
481 // work in a way that is required for calculating the size.
482 const QAbstractProxyModel* proxyModel = qobject_cast<const QAbstractProxyModel*>(model());
483 const KDirModel* dirModel = qobject_cast<const KDirModel*>(proxyModel->sourceModel());
484 const QModelIndex dirIndex = proxyModel->mapToSource(index);
485 text = itemSizeString(dirIndex, dirModel->itemForIndex(dirIndex));
486 } else {
487 text = model()->data(index).toString();
488 }
489 const int width = fontMetrics.width(text) + horizontalGap;
490 if (width > columnWidth[column]) {
491 columnWidth[column] = width;
492 }
493 }
494 }
495
496 // Assure that the required width is sufficient for the header too
497 const int logicalIndex = headerView->logicalIndex(column);
498 const QString headline = model()->headerData(logicalIndex, Qt::Horizontal).toString();
499 const int headlineWidth = fontMetrics.width(headline) + horizontalGap;
500
501 columnWidth[column] = qMax(columnWidth[column], headlineWidth);
502 }
503 }
504
505 // Resize all columns except of the name column
506 int requiredWidth = 0;
507 for (int column = KDirModel::Size; column < DolphinModel::ExtraColumnCount; ++column) {
508 if (!isColumnHidden(column)) {
509 requiredWidth += columnWidth[column];
510 headerView->resizeSection(column, columnWidth[column]);
511 }
512 }
513
514 // Resize the name column in a way that the whole available width is used
515 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
516
517 const int minNameWidth = 300;
518 if (columnWidth[KDirModel::Name] < minNameWidth) {
519 columnWidth[KDirModel::Name] = minNameWidth;
520
521 if ((rowCount > 0) && (rowCount < maxRowCount)) {
522 // Try to decrease the name column width without clipping any text
523 const int nameWidth = sizeHintForColumn(DolphinModel::Name);
524 if (nameWidth + requiredWidth <= viewport()->width()) {
525 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
526 } else if (nameWidth < minNameWidth) {
527 columnWidth[KDirModel::Name] = nameWidth;
528 }
529 }
530 }
531
532 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
533 }
534
535 void DolphinDetailsView::saveColumnPositions()
536 {
537 QList<int> columnPositions;
538 for (int i = DolphinModel::Name; i < DolphinModel::ExtraColumnCount; ++i) {
539 columnPositions.append(header()->visualIndex(i));
540 }
541
542 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
543 settings->setColumnPositions(columnPositions);
544 }
545
546 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
547 {
548 Q_UNUSED(logicalIndex);
549 Q_UNUSED(oldSize);
550 Q_UNUSED(newSize);
551 // If the user changes the size of the headers, the autoresize feature should be
552 // turned off. As there is no dedicated interface to find out whether the header
553 // section has been resized by the user or by a resize event, another approach is used.
554 // Attention: Take care when changing the if-condition to verify that there is no
555 // regression in combination with bug 178630 (see fix in comment #8).
556 if ((QApplication::mouseButtons() & Qt::LeftButton) && header()->underMouse()) {
557 disableAutoResizing();
558 }
559
560 adjustMaximumSizeForEditing(currentIndex());
561 }
562
563 void DolphinDetailsView::slotActivationChanged(bool active)
564 {
565 setAlternatingRowColors(active);
566 }
567
568 void DolphinDetailsView::disableAutoResizing()
569 {
570 m_autoResize = false;
571 }
572
573 void DolphinDetailsView::requestActivation()
574 {
575 m_dolphinViewController->requestActivation();
576 }
577
578 void DolphinDetailsView::slotGlobalSettingsChanged(int category)
579 {
580 Q_UNUSED(category);
581
582 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
583 Q_ASSERT(settings);
584 if (settings->useSystemFont()) {
585 m_font = KGlobalSettings::generalFont();
586 }
587 // Disconnect then reconnect, since the settings have been changed, the connection requirements may have also.
588 disconnect(this, SIGNAL(clicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
589 disconnect(this, SIGNAL(doubleClicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
590 if (KGlobalSettings::singleClick()) {
591 connect(this, SIGNAL(clicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
592 } else {
593 connect(this, SIGNAL(doubleClicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
594 }
595 }
596
597
598 void DolphinDetailsView::setFoldersExpandable(bool expandable)
599 {
600 if (!expandable) {
601 // Collapse all expanded folders, as QTreeView::setItemsExpandable(false)
602 // does not do this task
603 const int rowCount = model()->rowCount();
604 for (int row = 0; row < rowCount; ++row) {
605 setExpanded(model()->index(row, 0), false);
606 }
607 }
608 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
609 settings->setExpandableFolders(expandable);
610 setRootIsDecorated(expandable);
611 setItemsExpandable(expandable);
612
613 // The width of the space which is available for editing has changed
614 // because of the (dis)appearance of the expanding toggles
615 adjustMaximumSizeForEditing(currentIndex());
616 }
617
618 void DolphinDetailsView::slotExpanded(const QModelIndex& index)
619 {
620 KFileItem item = m_dolphinViewController->itemForIndex(index);
621 if (!item.isNull()) {
622 m_expandedUrls.insert(item.url());
623 }
624 }
625
626 void DolphinDetailsView::slotCollapsed(const QModelIndex& index)
627 {
628 KFileItem item = m_dolphinViewController->itemForIndex(index);
629 if (!item.isNull()) {
630 m_expandedUrls.remove(item.url());
631 }
632 }
633
634 void DolphinDetailsView::removeExpandedIndexes(const QModelIndex& parent, int start, int end)
635 {
636 if (m_expandedUrls.isEmpty()) {
637 return;
638 }
639
640 for (int row = start; row <= end; row++) {
641 const QModelIndex index = model()->index(row, 0, parent);
642 if (isExpanded(index)) {
643 slotCollapsed(index);
644 removeExpandedIndexes(index, 0, model()->rowCount(index) - 1);
645 }
646 }
647 }
648
649 void DolphinDetailsView::updateDecorationSize(bool showPreview)
650 {
651 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
652 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
653 setIconSize(QSize(iconSize, iconSize));
654 m_decorationSize = QSize(iconSize, iconSize);
655
656 if (m_extensionsFactory) {
657 // The old maximumSize used by KFileItemDelegate is not valid any more after the icon size change.
658 // It must be discarded before doItemsLayout() is called (see bug 234600).
659 m_extensionsFactory->fileItemDelegate()->setMaximumSize(QSize());
660 }
661
662 doItemsLayout();
663
664 // Calculate the new maximumSize for KFileItemDelegate after the icon size change.
665 QModelIndex current = currentIndex();
666 if (current.isValid()) {
667 adjustMaximumSizeForEditing(current);
668 }
669 }
670
671 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
672 {
673 return AdditionalInfoAccessor::instance().keyForColumn(columnIndex);
674 }
675
676 void DolphinDetailsView::adjustMaximumSizeForEditing(const QModelIndex& index)
677 {
678 // Make sure that the full width of the "Name" column is available for "Rename Inline".
679 // Before we do that, we have to check if m_extensionsFactory has been initialised because
680 // it is possible that we end up here before the constructor is finished (see bug 257035)
681 if (m_extensionsFactory) {
682 m_extensionsFactory->fileItemDelegate()->setMaximumSize(QTreeView::visualRect(index).size());
683 }
684 }
685
686 QString DolphinDetailsView::itemSizeString(const QModelIndex& index, const KFileItem& item) const
687 {
688 // The following code has been copied from KFileItemDelegate::Private::itemSize()
689 // Copyright (c) 2006-2007, 2008 Fredrik Höglund <fredrik@kde.org>
690 // Ideally this should be handled by KFileItemDelegate::sizeHint().
691 if (item.isFile()) {
692 return KGlobal::locale()->formatByteSize(item.size());
693 }
694
695 // Return the number of items in the directory
696 const QVariant value = index.data(KDirModel::ChildCountRole);
697 const int count = value.type() == QVariant::Int ? value.toInt() : KDirModel::ChildCountUnknown;
698
699 if (count == KDirModel::ChildCountUnknown) {
700 return QString();
701 }
702
703 return i18ncp("Items in a folder", "1 item", "%1 items", count);
704 }
705
706 #include "dolphindetailsview.moc"