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