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