]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
Column view + detail view: use different settings for preview and icon size like...
[dolphin.git] / src / dolphindetailsview.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 "dolphindetailsview.h"
22
23 #include "dolphinmodel.h"
24 #include "dolphincontroller.h"
25 #include "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
29 #include "viewproperties.h"
30
31 #include "dolphin_detailsmodesettings.h"
32 #include "dolphin_generalsettings.h"
33
34 #include <kdirmodel.h>
35 #include <klocale.h>
36 #include <kmenu.h>
37
38 #include <QAbstractProxyModel>
39 #include <QAction>
40 #include <QApplication>
41 #include <QHeaderView>
42 #include <QRubberBand>
43 #include <QPainter>
44 #include <QScrollBar>
45
46 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
47 QTreeView(parent),
48 m_autoResize(true),
49 m_expandingTogglePressed(false),
50 m_keyPressed(false),
51 m_controller(controller),
52 m_selectionManager(0),
53 m_font(),
54 m_decorationSize(),
55 m_showElasticBand(false),
56 m_elasticBandOrigin(),
57 m_elasticBandDestination()
58 {
59 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
60 Q_ASSERT(settings != 0);
61 Q_ASSERT(controller != 0);
62
63 setLayoutDirection(Qt::LeftToRight);
64 setAcceptDrops(true);
65 setSortingEnabled(true);
66 setUniformRowHeights(true);
67 setSelectionBehavior(SelectItems);
68 setDragDropMode(QAbstractItemView::DragDrop);
69 setDropIndicatorShown(false);
70 setAlternatingRowColors(true);
71 setRootIsDecorated(settings->expandableFolders());
72 setItemsExpandable(settings->expandableFolders());
73 setEditTriggers(QAbstractItemView::NoEditTriggers);
74
75 setMouseTracking(true);
76
77 const ViewProperties props(controller->url());
78 setSortIndicatorSection(props.sorting());
79 setSortIndicatorOrder(props.sortOrder());
80
81 QHeaderView* headerView = header();
82 connect(headerView, SIGNAL(sectionClicked(int)),
83 this, SLOT(synchronizeSortingState(int)));
84 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
85 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
86 this, SLOT(configureColumns(const QPoint&)));
87 connect(headerView, SIGNAL(sectionResized(int, int, int)),
88 this, SLOT(slotHeaderSectionResized(int, int, int)));
89 connect(headerView, SIGNAL(sectionHandleDoubleClicked(int)),
90 this, SLOT(disableAutoResizing()));
91
92 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
93 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
94 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
95 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
96
97 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
98 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
99 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
100 // RETURN-key in keyPressEvent().
101 if (KGlobalSettings::singleClick()) {
102 connect(this, SIGNAL(clicked(const QModelIndex&)),
103 controller, SLOT(triggerItem(const QModelIndex&)));
104 } else {
105 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
106 controller, SLOT(triggerItem(const QModelIndex&)));
107 }
108
109 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
110 m_selectionManager = new SelectionManager(this);
111 connect(m_selectionManager, SIGNAL(selectionChanged()),
112 this, SLOT(requestActivation()));
113 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
114 m_selectionManager, SLOT(reset()));
115 }
116
117 connect(this, SIGNAL(entered(const QModelIndex&)),
118 this, SLOT(slotEntered(const QModelIndex&)));
119 connect(this, SIGNAL(viewportEntered()),
120 controller, SLOT(emitViewportEntered()));
121 connect(controller, SIGNAL(zoomLevelChanged(int)),
122 this, SLOT(setZoomLevel(int)));
123 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged()),
124 this, SLOT(updateColumnVisibility()));
125 connect(controller, SIGNAL(activationChanged(bool)),
126 this, SLOT(slotActivationChanged(bool)));
127
128 if (settings->useSystemFont()) {
129 m_font = KGlobalSettings::generalFont();
130 } else {
131 m_font = QFont(settings->fontFamily(),
132 settings->fontSize(),
133 settings->fontWeight(),
134 settings->italicFont());
135 }
136
137 setVerticalScrollMode(QTreeView::ScrollPerPixel);
138 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
139
140 const DolphinView* view = controller->dolphinView();
141 connect(view, SIGNAL(showPreviewChanged()),
142 this, SLOT(slotShowPreviewChanged()));
143
144 updateDecorationSize(view->showPreview());
145
146 setFocus();
147 viewport()->installEventFilter(this);
148
149 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
150 this, SLOT(updateFont()));
151 }
152
153 DolphinDetailsView::~DolphinDetailsView()
154 {
155 }
156
157 bool DolphinDetailsView::event(QEvent* event)
158 {
159 if (event->type() == QEvent::Polish) {
160 QHeaderView* headerView = header();
161 headerView->setResizeMode(QHeaderView::Interactive);
162 headerView->setMovable(false);
163
164 updateColumnVisibility();
165
166 hideColumn(DolphinModel::Rating);
167 hideColumn(DolphinModel::Tags);
168 } else if (event->type() == QEvent::UpdateRequest) {
169 // a wheel movement will scroll 4 items
170 if (model()->rowCount() > 0) {
171 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
172 }
173 }
174
175 return QTreeView::event(event);
176 }
177
178 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
179 {
180 QStyleOptionViewItem viewOptions = QTreeView::viewOptions();
181 viewOptions.font = m_font;
182 viewOptions.showDecorationSelected = true;
183 viewOptions.decorationSize = m_decorationSize;
184 return viewOptions;
185 }
186
187 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
188 {
189 QTreeView::contextMenuEvent(event);
190 m_controller->triggerContextMenuRequest(event->pos());
191 }
192
193 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
194 {
195 m_controller->requestActivation();
196
197 const QModelIndex current = currentIndex();
198 QTreeView::mousePressEvent(event);
199
200 m_expandingTogglePressed = false;
201 const QModelIndex index = indexAt(event->pos());
202 const bool updateState = index.isValid() &&
203 (index.column() == DolphinModel::Name) &&
204 (event->button() == Qt::LeftButton);
205 if (updateState) {
206 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
207 // the state if no expanding/collapsing area has been hit:
208 const QRect rect = visualRect(index);
209 if (event->pos().x() >= rect.x() + indentation()) {
210 setState(QAbstractItemView::DraggingState);
211 } else {
212 m_expandingTogglePressed = true;
213 }
214 }
215
216 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
217 if (QApplication::mouseButtons() & Qt::MidButton) {
218 m_controller->replaceUrlByClipboard();
219 }
220
221 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
222 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
223 clearSelection();
224 }
225
226 // restore the current index, other columns are handled as viewport area
227 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
228 }
229
230 if ((event->button() == Qt::LeftButton) && !m_expandingTogglePressed) {
231 m_showElasticBand = true;
232
233 const QPoint pos(contentsPos());
234 m_elasticBandOrigin = event->pos();
235 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
236 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
237 m_elasticBandDestination = event->pos();
238 }
239 }
240
241 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
242 {
243 if (m_showElasticBand) {
244 const QPoint mousePos = event->pos();
245 const QModelIndex index = indexAt(mousePos);
246 if (!index.isValid()) {
247 // the destination of the selection rectangle is above the viewport. In this
248 // case QTreeView does no selection at all, which is not the wanted behavior
249 // in Dolphin -> select all items within the elastic band rectangle
250 clearSelection();
251
252 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
253 QRect selRect = QRect(m_elasticBandOrigin, m_elasticBandDestination).normalized();
254 const QRect nameColumnsRect(0, 0, nameColumnWidth, viewport()->height());
255 selRect = nameColumnsRect.intersected(selRect);
256
257 setSelection(selRect, QItemSelectionModel::Select);
258 }
259
260 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
261 // as the Qt-issue #199631 has been fixed.
262 // QTreeView::mouseMoveEvent(event);
263 QAbstractItemView::mouseMoveEvent(event);
264 updateElasticBand();
265 } else {
266 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
267 // as the Qt-issue #199631 has been fixed.
268 // QTreeView::mouseMoveEvent(event);
269 QAbstractItemView::mouseMoveEvent(event);
270 }
271
272 if (m_expandingTogglePressed) {
273 // Per default QTreeView starts either a selection or a drag operation when dragging
274 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
275 // Turn off this behavior in Dolphin to stay predictable:
276 clearSelection();
277 setState(QAbstractItemView::NoState);
278 }
279 }
280
281 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
282 {
283 const QModelIndex index = indexAt(event->pos());
284 if (index.isValid() && (index.column() == DolphinModel::Name)) {
285 QTreeView::mouseReleaseEvent(event);
286 } else {
287 // don't change the current index if the cursor is released
288 // above any other column than the name column, as the other
289 // columns act as viewport
290 const QModelIndex current = currentIndex();
291 QTreeView::mouseReleaseEvent(event);
292 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
293 }
294
295 m_expandingTogglePressed = false;
296 if (m_showElasticBand) {
297 updateElasticBand();
298 m_showElasticBand = false;
299 }
300 }
301
302 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
303 {
304 DragAndDropHelper::startDrag(this, supportedActions);
305 m_showElasticBand = false;
306 }
307
308 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
309 {
310 if (event->mimeData()->hasUrls()) {
311 event->acceptProposedAction();
312 }
313
314 if (m_showElasticBand) {
315 updateElasticBand();
316 m_showElasticBand = false;
317 }
318 }
319
320 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
321 {
322 QTreeView::dragLeaveEvent(event);
323 setDirtyRegion(m_dropRect);
324 }
325
326 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
327 {
328 QTreeView::dragMoveEvent(event);
329
330 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
331 setDirtyRegion(m_dropRect);
332 const QModelIndex index = indexAt(event->pos());
333 if (index.isValid() && (index.column() == DolphinModel::Name)) {
334 const KFileItem item = m_controller->itemForIndex(index);
335 if (!item.isNull() && item.isDir()) {
336 m_dropRect = visualRect(index);
337 } else {
338 m_dropRect.setSize(QSize()); // set as invalid
339 }
340 setDirtyRegion(m_dropRect);
341 }
342
343 if (event->mimeData()->hasUrls()) {
344 // accept url drops, independently from the destination item
345 event->acceptProposedAction();
346 }
347 }
348
349 void DolphinDetailsView::dropEvent(QDropEvent* event)
350 {
351 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
352 if (!urls.isEmpty()) {
353 event->acceptProposedAction();
354 const QModelIndex index = indexAt(event->pos());
355 KFileItem item;
356 if (index.isValid() && (index.column() == DolphinModel::Name)) {
357 item = m_controller->itemForIndex(index);
358 }
359 m_controller->indicateDroppedUrls(urls,
360 m_controller->url(),
361 item);
362 }
363 QTreeView::dropEvent(event);
364 }
365
366 void DolphinDetailsView::paintEvent(QPaintEvent* event)
367 {
368 QTreeView::paintEvent(event);
369 if (m_showElasticBand) {
370 // The following code has been taken from QListView
371 // and adapted to DolphinDetailsView.
372 // (C) 1992-2007 Trolltech ASA
373 QStyleOptionRubberBand opt;
374 opt.initFrom(this);
375 opt.shape = QRubberBand::Rectangle;
376 opt.opaque = false;
377 opt.rect = elasticBandRect();
378
379 QPainter painter(viewport());
380 painter.save();
381 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
382 painter.restore();
383 }
384 }
385
386 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
387 {
388 // If the Control modifier is pressed, a multiple selection
389 // is done and DolphinDetailsView::currentChanged() may not
390 // not change the selection in a custom way.
391 m_keyPressed = !(event->modifiers() & Qt::ControlModifier);
392
393 QTreeView::keyPressEvent(event);
394 m_controller->handleKeyPressEvent(event);
395 }
396
397 void DolphinDetailsView::keyReleaseEvent(QKeyEvent* event)
398 {
399 QTreeView::keyReleaseEvent(event);
400 m_keyPressed = false;
401 }
402
403 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
404 {
405 if (m_autoResize) {
406 resizeColumns();
407 }
408 QTreeView::resizeEvent(event);
409 }
410
411 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
412 {
413 if (m_selectionManager != 0) {
414 m_selectionManager->reset();
415 }
416
417 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
418 if (event->modifiers() & Qt::ControlModifier) {
419 event->ignore();
420 return;
421 }
422
423 QTreeView::wheelEvent(event);
424 }
425
426 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
427 {
428 QTreeView::currentChanged(current, previous);
429
430 // Stay consistent with QListView: When changing the current index by key presses,
431 // also change the selection.
432 if (m_keyPressed) {
433 selectionModel()->select(current, QItemSelectionModel::ClearAndSelect);
434 }
435 }
436
437 bool DolphinDetailsView::eventFilter(QObject* watched, QEvent* event)
438 {
439 if ((watched == viewport()) && (event->type() == QEvent::Leave)) {
440 // if the mouse is above an item and moved very fast outside the widget,
441 // no viewportEntered() signal might be emitted although the mouse has been moved
442 // above the viewport
443 m_controller->emitViewportEntered();
444 }
445
446 return QTreeView::eventFilter(watched, event);
447 }
448
449 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
450 {
451 QHeaderView* headerView = header();
452 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
453 }
454
455 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
456 {
457 QHeaderView* headerView = header();
458 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
459 }
460
461 void DolphinDetailsView::synchronizeSortingState(int column)
462 {
463 // The sorting has already been changed in QTreeView if this slot is
464 // invoked, but Dolphin is not informed about this.
465 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
466 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
467 m_controller->indicateSortingChange(sorting);
468 m_controller->indicateSortOrderChange(sortOrder);
469 }
470
471 void DolphinDetailsView::slotEntered(const QModelIndex& index)
472 {
473 if (index.column() == DolphinModel::Name) {
474 m_controller->emitItemEntered(index);
475 } else {
476 m_controller->emitViewportEntered();
477 }
478 }
479
480 void DolphinDetailsView::updateElasticBand()
481 {
482 if (m_showElasticBand) {
483 QRect dirtyRegion(elasticBandRect());
484 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
485 dirtyRegion = dirtyRegion.united(elasticBandRect());
486 setDirtyRegion(dirtyRegion);
487 }
488 }
489
490 QRect DolphinDetailsView::elasticBandRect() const
491 {
492 const QPoint pos(contentsPos());
493 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
494 return QRect(topLeft, m_elasticBandDestination).normalized();
495 }
496
497 void DolphinDetailsView::setZoomLevel(int level)
498 {
499 const int size = DolphinController::iconSizeForZoomLevel(level);
500 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
501
502 const bool showPreview = m_controller->dolphinView()->showPreview();
503 if (showPreview) {
504 settings->setPreviewSize(size);
505 } else {
506 settings->setIconSize(size);
507 }
508
509 updateDecorationSize(showPreview);
510 }
511
512
513 void DolphinDetailsView::slotShowPreviewChanged()
514 {
515 const DolphinView* view = m_controller->dolphinView();
516 updateDecorationSize(view->showPreview());
517 }
518
519 void DolphinDetailsView::configureColumns(const QPoint& pos)
520 {
521 KMenu popup(this);
522 popup.addTitle(i18nc("@title:menu", "Columns"));
523
524 QHeaderView* headerView = header();
525 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
526 const int logicalIndex = headerView->logicalIndex(i);
527 const QString text = model()->headerData(i, Qt::Horizontal).toString();
528 QAction* action = popup.addAction(text);
529 action->setCheckable(true);
530 action->setChecked(!headerView->isSectionHidden(logicalIndex));
531 action->setData(i);
532 }
533
534 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
535 if (activatedAction != 0) {
536 const bool show = activatedAction->isChecked();
537 const int columnIndex = activatedAction->data().toInt();
538
539 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
540 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
541 if (show) {
542 Q_ASSERT(!list.contains(info));
543 list.append(info);
544 } else {
545 Q_ASSERT(list.contains(info));
546 const int index = list.indexOf(info);
547 list.removeAt(index);
548 }
549
550 m_controller->indicateAdditionalInfoChange(list);
551 setColumnHidden(columnIndex, !show);
552 }
553 }
554
555 void DolphinDetailsView::updateColumnVisibility()
556 {
557 const KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
558 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
559 const KFileItemDelegate::Information info = infoForColumn(i);
560 const bool hide = !list.contains(info);
561 if (isColumnHidden(i) != hide) {
562 setColumnHidden(i, hide);
563 }
564 }
565
566 resizeColumns();
567 }
568
569 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
570 {
571 Q_UNUSED(logicalIndex);
572 Q_UNUSED(oldSize);
573 Q_UNUSED(newSize);
574 if (QApplication::mouseButtons() & Qt::LeftButton) {
575 disableAutoResizing();
576 }
577 }
578
579 void DolphinDetailsView::slotActivationChanged(bool active)
580 {
581 setAlternatingRowColors(active);
582 }
583
584 void DolphinDetailsView::disableAutoResizing()
585 {
586 m_autoResize = false;
587 }
588
589 void DolphinDetailsView::requestActivation()
590 {
591 m_controller->requestActivation();
592 }
593
594 void DolphinDetailsView::updateFont()
595 {
596 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
597 Q_ASSERT(settings != 0);
598
599 if (settings->useSystemFont()) {
600 m_font = KGlobalSettings::generalFont();
601 }
602 }
603
604 void DolphinDetailsView::updateDecorationSize(bool showPreview)
605 {
606 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
607 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
608 setIconSize(QSize(iconSize, iconSize));
609 m_decorationSize = QSize(iconSize, iconSize);
610
611 if (m_selectionManager != 0) {
612 m_selectionManager->reset();
613 }
614
615 doItemsLayout();
616 }
617
618 QPoint DolphinDetailsView::contentsPos() const
619 {
620 // implementation note: the horizonal position is ignored currently, as no
621 // horizontal scrolling is done anyway during a selection
622 const QScrollBar* scrollbar = verticalScrollBar();
623 Q_ASSERT(scrollbar != 0);
624
625 const int maxHeight = maximumViewportSize().height();
626 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
627 const int visibleHeight = model()->rowCount() + 1 - height;
628 if (visibleHeight <= 0) {
629 return QPoint(0, 0);
630 }
631
632 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
633 return QPoint(0, y);
634 }
635
636 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
637 {
638 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
639
640 switch (columnIndex) {
641 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
642 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
643 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
644 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
645 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
646 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
647 default: break;
648 }
649
650 return info;
651 }
652
653 void DolphinDetailsView::resizeColumns()
654 {
655 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
656 // around 3 seconds for each (!) resize operation when having > 10000 items).
657 // This gets a problem especially when opening large directories, where several
658 // resize operations are received for showing the currently available items during
659 // loading (the application hangs around 20 seconds when loading > 10000 items).
660
661 QHeaderView* headerView = header();
662 QFontMetrics fontMetrics(viewport()->font());
663
664 int columnWidth[KDirModel::ColumnCount];
665 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
666 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
667 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
668 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
669 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
670 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
671
672 int requiredWidth = 0;
673 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
674 if (!isColumnHidden(i)) {
675 columnWidth[i] += 20; // provide a default gap
676 requiredWidth += columnWidth[i];
677 headerView->resizeSection(i, columnWidth[i]);
678 }
679 }
680
681 // resize the name column in a way that the whole available width is used
682 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
683 if (columnWidth[KDirModel::Name] < 120) {
684 columnWidth[KDirModel::Name] = 120;
685 }
686 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
687 }
688
689 #include "dolphindetailsview.moc"