]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
fixed issue that CTRL + mouseclick did not work anymore in the details view
[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 const QModelIndex current = currentIndex();
191 QTreeView::mousePressEvent(event);
192
193 m_expandingTogglePressed = false;
194 const QModelIndex index = indexAt(event->pos());
195 const bool updateState = index.isValid() &&
196 (index.column() == DolphinModel::Name) &&
197 (event->button() == Qt::LeftButton);
198 if (updateState) {
199 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
200 // the state if no expanding/collapsing area has been hit:
201 const QRect rect = visualRect(index);
202 if (event->pos().x() >= rect.x() + indentation()) {
203 setState(QAbstractItemView::DraggingState);
204 } else {
205 m_expandingTogglePressed = true;
206 }
207 }
208
209 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
210 if (QApplication::mouseButtons() & Qt::MidButton) {
211 m_controller->replaceUrlByClipboard();
212 }
213
214 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
215 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
216 clearSelection();
217 }
218
219 // restore the current index, other columns are handled as viewport area
220 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
221 }
222
223 if ((event->button() == Qt::LeftButton) && !m_expandingTogglePressed) {
224 m_showElasticBand = true;
225
226 const QPoint pos(contentsPos());
227 m_elasticBandOrigin = event->pos();
228 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
229 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
230 m_elasticBandDestination = event->pos();
231 }
232 }
233
234 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
235 {
236 if (m_showElasticBand) {
237 const QPoint mousePos = event->pos();
238 const QModelIndex index = indexAt(mousePos);
239 if (!index.isValid()) {
240 // the destination of the selection rectangle is above the viewport. In this
241 // case QTreeView does no selection at all, which is not the wanted behavior
242 // in Dolphin -> select all items within the elastic band rectangle
243 clearSelection();
244
245 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
246 QRect selRect = QRect(m_elasticBandOrigin, m_elasticBandDestination).normalized();
247 const QRect nameColumnsRect(0, 0, nameColumnWidth, viewport()->height());
248 selRect = nameColumnsRect.intersected(selRect);
249
250 setSelection(selRect, QItemSelectionModel::Select);
251 }
252
253 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
254 // as the Qt-issue #199631 has been fixed.
255 // QTreeView::mouseMoveEvent(event);
256 QAbstractItemView::mouseMoveEvent(event);
257 updateElasticBand();
258 } else {
259 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
260 // as the Qt-issue #199631 has been fixed.
261 // QTreeView::mouseMoveEvent(event);
262 QAbstractItemView::mouseMoveEvent(event);
263 }
264
265 if (m_expandingTogglePressed) {
266 // Per default QTreeView starts either a selection or a drag operation when dragging
267 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
268 // Turn off this behavior in Dolphin to stay predictable:
269 clearSelection();
270 setState(QAbstractItemView::NoState);
271 }
272 }
273
274 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
275 {
276 const QModelIndex index = indexAt(event->pos());
277 if (index.isValid() && (index.column() == DolphinModel::Name)) {
278 QTreeView::mouseReleaseEvent(event);
279 } else {
280 // don't change the current index if the cursor is released
281 // above any other column than the name column, as the other
282 // columns act as viewport
283 const QModelIndex current = currentIndex();
284 QTreeView::mouseReleaseEvent(event);
285 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
286 }
287
288 m_expandingTogglePressed = false;
289 if (m_showElasticBand) {
290 updateElasticBand();
291 m_showElasticBand = false;
292 }
293 }
294
295 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
296 {
297 DragAndDropHelper::startDrag(this, supportedActions);
298 m_showElasticBand = false;
299 }
300
301 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
302 {
303 if (event->mimeData()->hasUrls()) {
304 event->acceptProposedAction();
305 }
306
307 if (m_showElasticBand) {
308 updateElasticBand();
309 m_showElasticBand = false;
310 }
311 }
312
313 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
314 {
315 QTreeView::dragLeaveEvent(event);
316 setDirtyRegion(m_dropRect);
317 }
318
319 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
320 {
321 QTreeView::dragMoveEvent(event);
322
323 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
324 setDirtyRegion(m_dropRect);
325 const QModelIndex index = indexAt(event->pos());
326 if (index.isValid() && (index.column() == DolphinModel::Name)) {
327 const KFileItem item = m_controller->itemForIndex(index);
328 if (!item.isNull() && item.isDir()) {
329 m_dropRect = visualRect(index);
330 } else {
331 m_dropRect.setSize(QSize()); // set as invalid
332 }
333 setDirtyRegion(m_dropRect);
334 }
335
336 if (event->mimeData()->hasUrls()) {
337 // accept url drops, independently from the destination item
338 event->acceptProposedAction();
339 }
340 }
341
342 void DolphinDetailsView::dropEvent(QDropEvent* event)
343 {
344 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
345 if (!urls.isEmpty()) {
346 event->acceptProposedAction();
347 const QModelIndex index = indexAt(event->pos());
348 KFileItem item;
349 if (index.isValid() && (index.column() == DolphinModel::Name)) {
350 item = m_controller->itemForIndex(index);
351 }
352 m_controller->indicateDroppedUrls(urls,
353 m_controller->url(),
354 item);
355 }
356 QTreeView::dropEvent(event);
357 }
358
359 void DolphinDetailsView::paintEvent(QPaintEvent* event)
360 {
361 QTreeView::paintEvent(event);
362 if (m_showElasticBand) {
363 // The following code has been taken from QListView
364 // and adapted to DolphinDetailsView.
365 // (C) 1992-2007 Trolltech ASA
366 QStyleOptionRubberBand opt;
367 opt.initFrom(this);
368 opt.shape = QRubberBand::Rectangle;
369 opt.opaque = false;
370 opt.rect = elasticBandRect();
371
372 QPainter painter(viewport());
373 painter.save();
374 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
375 painter.restore();
376 }
377 }
378
379 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
380 {
381 // If the Control modifier is pressed, a multiple selection
382 // is done and DolphinDetailsView::currentChanged() may not
383 // not change the selection in a custom way.
384 m_keyPressed = !(event->modifiers() & Qt::ControlModifier);
385
386 QTreeView::keyPressEvent(event);
387 m_controller->handleKeyPressEvent(event);
388 }
389
390 void DolphinDetailsView::keyReleaseEvent(QKeyEvent* event)
391 {
392 QTreeView::keyReleaseEvent(event);
393 m_keyPressed = false;
394 }
395
396 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
397 {
398 if (m_autoResize) {
399 resizeColumns();
400 }
401 QTreeView::resizeEvent(event);
402 }
403
404 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
405 {
406 if (m_selectionManager != 0) {
407 m_selectionManager->reset();
408 }
409
410 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
411 if (event->modifiers() & Qt::ControlModifier) {
412 event->ignore();
413 return;
414 }
415
416 QTreeView::wheelEvent(event);
417 }
418
419 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
420 {
421 QTreeView::currentChanged(current, previous);
422
423 // Stay consistent with QListView: When changing the current index by key presses,
424 // also change the selection.
425 if (m_keyPressed) {
426 selectionModel()->select(current, QItemSelectionModel::ClearAndSelect);
427 }
428 }
429
430 bool DolphinDetailsView::eventFilter(QObject* watched, QEvent* event)
431 {
432 if ((watched == viewport()) && (event->type() == QEvent::Leave)) {
433 // if the mouse is above an item and moved very fast outside the widget,
434 // no viewportEntered() signal might be emitted although the mouse has been moved
435 // above the viewport
436 m_controller->emitViewportEntered();
437 }
438
439 return QTreeView::eventFilter(watched, event);
440 }
441
442 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
443 {
444 QHeaderView* headerView = header();
445 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
446 }
447
448 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
449 {
450 QHeaderView* headerView = header();
451 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
452 }
453
454 void DolphinDetailsView::synchronizeSortingState(int column)
455 {
456 // The sorting has already been changed in QTreeView if this slot is
457 // invoked, but Dolphin is not informed about this.
458 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
459 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
460 m_controller->indicateSortingChange(sorting);
461 m_controller->indicateSortOrderChange(sortOrder);
462 }
463
464 void DolphinDetailsView::slotEntered(const QModelIndex& index)
465 {
466 if (index.column() == DolphinModel::Name) {
467 m_controller->emitItemEntered(index);
468 } else {
469 m_controller->emitViewportEntered();
470 }
471 }
472
473 void DolphinDetailsView::updateElasticBand()
474 {
475 if (m_showElasticBand) {
476 QRect dirtyRegion(elasticBandRect());
477 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
478 dirtyRegion = dirtyRegion.united(elasticBandRect());
479 setDirtyRegion(dirtyRegion);
480 }
481 }
482
483 QRect DolphinDetailsView::elasticBandRect() const
484 {
485 const QPoint pos(contentsPos());
486 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
487 return QRect(topLeft, m_elasticBandDestination).normalized();
488 }
489
490 void DolphinDetailsView::zoomIn()
491 {
492 if (isZoomInPossible()) {
493 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
494 switch (settings->iconSize()) {
495 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
496 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
497 default: Q_ASSERT(false); break;
498 }
499 updateDecorationSize();
500 }
501 }
502
503 void DolphinDetailsView::zoomOut()
504 {
505 if (isZoomOutPossible()) {
506 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
507 switch (settings->iconSize()) {
508 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
509 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
510 default: Q_ASSERT(false); break;
511 }
512 updateDecorationSize();
513 }
514 }
515
516 void DolphinDetailsView::configureColumns(const QPoint& pos)
517 {
518 KMenu popup(this);
519 popup.addTitle(i18nc("@title:menu", "Columns"));
520
521 QHeaderView* headerView = header();
522 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
523 const int logicalIndex = headerView->logicalIndex(i);
524 const QString text = model()->headerData(i, Qt::Horizontal).toString();
525 QAction* action = popup.addAction(text);
526 action->setCheckable(true);
527 action->setChecked(!headerView->isSectionHidden(logicalIndex));
528 action->setData(i);
529 }
530
531 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
532 if (activatedAction != 0) {
533 const bool show = activatedAction->isChecked();
534 const int columnIndex = activatedAction->data().toInt();
535
536 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
537 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
538 if (show) {
539 Q_ASSERT(!list.contains(info));
540 list.append(info);
541 } else {
542 Q_ASSERT(list.contains(info));
543 const int index = list.indexOf(info);
544 list.removeAt(index);
545 }
546
547 m_controller->indicateAdditionalInfoChange(list);
548 setColumnHidden(columnIndex, !show);
549 }
550 }
551
552 void DolphinDetailsView::updateColumnVisibility()
553 {
554 const KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
555 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
556 const KFileItemDelegate::Information info = infoForColumn(i);
557 const bool hide = !list.contains(info);
558 if (isColumnHidden(i) != hide) {
559 setColumnHidden(i, hide);
560 }
561 }
562
563 resizeColumns();
564 }
565
566 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
567 {
568 Q_UNUSED(logicalIndex);
569 Q_UNUSED(oldSize);
570 Q_UNUSED(newSize);
571 if (QApplication::mouseButtons() & Qt::LeftButton) {
572 disableAutoResizing();
573 }
574 }
575
576 void DolphinDetailsView::disableAutoResizing()
577 {
578 m_autoResize = false;
579 }
580
581 void DolphinDetailsView::requestActivation()
582 {
583 m_controller->requestActivation();
584 }
585
586 void DolphinDetailsView::updateFont()
587 {
588 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
589 Q_ASSERT(settings != 0);
590
591 if (settings->useSystemFont()) {
592 m_font = KGlobalSettings::generalFont();
593 }
594 }
595
596 bool DolphinDetailsView::isZoomInPossible() const
597 {
598 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
599 return settings->iconSize() < KIconLoader::SizeLarge;
600 }
601
602 bool DolphinDetailsView::isZoomOutPossible() const
603 {
604 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
605 return settings->iconSize() > KIconLoader::SizeSmall;
606 }
607
608 void DolphinDetailsView::updateDecorationSize()
609 {
610 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
611 const int iconSize = settings->iconSize();
612 setIconSize(QSize(iconSize, iconSize));
613 m_decorationSize = QSize(iconSize, iconSize);
614
615 m_controller->setZoomInPossible(isZoomInPossible());
616 m_controller->setZoomOutPossible(isZoomOutPossible());
617
618 if (m_selectionManager != 0) {
619 m_selectionManager->reset();
620 }
621
622 doItemsLayout();
623 }
624
625 QPoint DolphinDetailsView::contentsPos() const
626 {
627 // implementation note: the horizonal position is ignored currently, as no
628 // horizontal scrolling is done anyway during a selection
629 const QScrollBar* scrollbar = verticalScrollBar();
630 Q_ASSERT(scrollbar != 0);
631
632 const int maxHeight = maximumViewportSize().height();
633 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
634 const int visibleHeight = model()->rowCount() + 1 - height;
635 if (visibleHeight <= 0) {
636 return QPoint(0, 0);
637 }
638
639 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
640 return QPoint(0, y);
641 }
642
643 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
644 {
645 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
646
647 switch (columnIndex) {
648 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
649 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
650 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
651 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
652 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
653 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
654 default: break;
655 }
656
657 return info;
658 }
659
660 void DolphinDetailsView::resizeColumns()
661 {
662 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
663 // around 3 seconds for each (!) resize operation when having > 10000 items).
664 // This gets a problem especially when opening large directories, where several
665 // resize operations are received for showing the currently available items during
666 // loading (the application hangs around 20 seconds when loading > 10000 items).
667
668 QHeaderView* headerView = header();
669 QFontMetrics fontMetrics(viewport()->font());
670
671 int columnWidth[KDirModel::ColumnCount];
672 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
673 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
674 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
675 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
676 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
677 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
678
679 int requiredWidth = 0;
680 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
681 if (!isColumnHidden(i)) {
682 columnWidth[i] += 20; // provide a default gap
683 requiredWidth += columnWidth[i];
684 headerView->resizeSection(i, columnWidth[i]);
685 }
686 }
687
688 // resize the name column in a way that the whole available width is used
689 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
690 if (columnWidth[KDirModel::Name] < 120) {
691 columnWidth[KDirModel::Name] = 120;
692 }
693 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
694 }
695
696 #include "dolphindetailsview.moc"