1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphindetailsview.h"
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 #include "zoomlevelinfo.h"
32 #include "dolphin_detailsmodesettings.h"
33 #include "dolphin_generalsettings.h"
35 #include <kdirmodel.h>
39 #include <QAbstractProxyModel>
41 #include <QApplication>
42 #include <QHeaderView>
43 #include <QRubberBand>
47 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
50 m_expandingTogglePressed(false),
52 m_useDefaultIndexAt(true),
53 m_controller(controller
),
54 m_selectionManager(0),
57 m_showElasticBand(false),
58 m_elasticBandOrigin(),
59 m_elasticBandDestination()
61 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
62 Q_ASSERT(settings
!= 0);
63 Q_ASSERT(controller
!= 0);
65 setLayoutDirection(Qt::LeftToRight
);
67 setSortingEnabled(true);
68 setUniformRowHeights(true);
69 setSelectionBehavior(SelectItems
);
70 setDragDropMode(QAbstractItemView::DragDrop
);
71 setDropIndicatorShown(false);
72 setAlternatingRowColors(true);
73 setRootIsDecorated(settings
->expandableFolders());
74 setItemsExpandable(settings
->expandableFolders());
75 setEditTriggers(QAbstractItemView::NoEditTriggers
);
77 setMouseTracking(true);
79 const ViewProperties
props(controller
->url());
80 setSortIndicatorSection(props
.sorting());
81 setSortIndicatorOrder(props
.sortOrder());
83 QHeaderView
* headerView
= header();
84 connect(headerView
, SIGNAL(sectionClicked(int)),
85 this, SLOT(synchronizeSortingState(int)));
86 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
87 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
88 this, SLOT(configureColumns(const QPoint
&)));
89 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
90 this, SLOT(slotHeaderSectionResized(int, int, int)));
91 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
92 this, SLOT(disableAutoResizing()));
94 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
95 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
96 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
97 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
99 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
100 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
101 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
102 // RETURN-key in keyPressEvent().
103 if (KGlobalSettings::singleClick()) {
104 connect(this, SIGNAL(clicked(const QModelIndex
&)),
105 controller
, SLOT(triggerItem(const QModelIndex
&)));
107 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
108 controller
, SLOT(triggerItem(const QModelIndex
&)));
111 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
112 m_selectionManager
= new SelectionManager(this);
113 connect(m_selectionManager
, SIGNAL(selectionChanged()),
114 this, SLOT(requestActivation()));
115 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
116 m_selectionManager
, SLOT(reset()));
119 connect(this, SIGNAL(entered(const QModelIndex
&)),
120 this, SLOT(slotEntered(const QModelIndex
&)));
121 connect(this, SIGNAL(viewportEntered()),
122 controller
, SLOT(emitViewportEntered()));
123 connect(controller
, SIGNAL(zoomLevelChanged(int)),
124 this, SLOT(setZoomLevel(int)));
125 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
126 this, SLOT(updateColumnVisibility()));
127 connect(controller
, SIGNAL(activationChanged(bool)),
128 this, SLOT(slotActivationChanged(bool)));
130 if (settings
->useSystemFont()) {
131 m_font
= KGlobalSettings::generalFont();
133 m_font
= QFont(settings
->fontFamily(),
134 settings
->fontSize(),
135 settings
->fontWeight(),
136 settings
->italicFont());
139 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
140 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
142 const DolphinView
* view
= controller
->dolphinView();
143 connect(view
, SIGNAL(showPreviewChanged()),
144 this, SLOT(slotShowPreviewChanged()));
146 updateDecorationSize(view
->showPreview());
149 viewport()->installEventFilter(this);
151 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
152 this, SLOT(updateFont()));
154 m_useDefaultIndexAt
= false;
157 DolphinDetailsView::~DolphinDetailsView()
161 bool DolphinDetailsView::event(QEvent
* event
)
163 if (event
->type() == QEvent::Polish
) {
164 QHeaderView
* headerView
= header();
165 headerView
->setResizeMode(QHeaderView::Interactive
);
166 headerView
->setMovable(false);
168 updateColumnVisibility();
170 hideColumn(DolphinModel::Rating
);
171 hideColumn(DolphinModel::Tags
);
172 } else if (event
->type() == QEvent::UpdateRequest
) {
173 // a wheel movement will scroll 4 items
174 if (model()->rowCount() > 0) {
175 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
179 return QTreeView::event(event
);
182 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
184 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
185 viewOptions
.font
= m_font
;
186 viewOptions
.showDecorationSelected
= true;
187 viewOptions
.decorationSize
= m_decorationSize
;
191 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
193 QTreeView::contextMenuEvent(event
);
194 m_controller
->triggerContextMenuRequest(event
->pos());
197 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
199 m_controller
->requestActivation();
201 const QModelIndex current
= currentIndex();
202 QTreeView::mousePressEvent(event
);
204 m_expandingTogglePressed
= false;
205 const QModelIndex index
= indexAt(event
->pos());
206 const bool updateState
= index
.isValid() &&
207 (index
.column() == DolphinModel::Name
) &&
208 (event
->button() == Qt::LeftButton
);
210 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
211 // the state if no expanding/collapsing area has been hit:
212 const QRect rect
= visualRect(index
);
213 if (event
->pos().x() >= rect
.x() + indentation()) {
214 setState(QAbstractItemView::DraggingState
);
216 m_expandingTogglePressed
= true;
220 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
221 // the mouse press is done somewhere outside the filename column
222 if (QApplication::mouseButtons() & Qt::MidButton
) {
223 m_controller
->replaceUrlByClipboard();
226 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
227 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
231 // restore the current index, other columns are handled as viewport area
232 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
235 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
236 m_showElasticBand
= true;
237 const QPoint pos
= contentsPos();
238 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
239 m_elasticBandOrigin
= event
->pos() + pos
+ scrollPos
;
240 m_elasticBandDestination
= m_elasticBandOrigin
;
244 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
246 if (m_showElasticBand
) {
247 const QPoint mousePos
= event
->pos();
248 const QModelIndex index
= indexAt(mousePos
);
249 if (!index
.isValid()) {
250 // the destination of the selection rectangle is above the viewport. In this
251 // case QTreeView does no selection at all, which is not the wanted behavior
252 // in Dolphin -> select all items within the elastic band rectangle
254 setState(DragSelectingState
);
256 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
257 QRect selRect
= elasticBandRect();
258 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
259 selRect
= nameColumnsRect
.intersected(selRect
);
261 setSelection(selRect
, QItemSelectionModel::Select
);
265 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
266 // as the Qt-issue #199631 has been fixed.
267 // QTreeView::mouseMoveEvent(event);
268 QAbstractItemView::mouseMoveEvent(event
);
271 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
272 // as the Qt-issue #199631 has been fixed.
273 // QTreeView::mouseMoveEvent(event);
274 QAbstractItemView::mouseMoveEvent(event
);
277 if (m_expandingTogglePressed
) {
278 // Per default QTreeView starts either a selection or a drag operation when dragging
279 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
280 // Turn off this behavior in Dolphin to stay predictable:
282 setState(QAbstractItemView::NoState
);
286 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
288 const QModelIndex index
= indexAt(event
->pos());
289 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
290 QTreeView::mouseReleaseEvent(event
);
292 // don't change the current index if the cursor is released
293 // above any other column than the name column, as the other
294 // columns act as viewport
295 const QModelIndex current
= currentIndex();
296 QTreeView::mouseReleaseEvent(event
);
297 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
300 m_expandingTogglePressed
= false;
301 if (m_showElasticBand
) {
304 m_showElasticBand
= false;
308 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
310 DragAndDropHelper::startDrag(this, supportedActions
);
311 m_showElasticBand
= false;
314 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
316 if (event
->mimeData()->hasUrls()) {
317 event
->acceptProposedAction();
320 if (m_showElasticBand
) {
322 m_showElasticBand
= false;
326 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
328 QTreeView::dragLeaveEvent(event
);
329 setDirtyRegion(m_dropRect
);
332 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
334 QTreeView::dragMoveEvent(event
);
336 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
337 setDirtyRegion(m_dropRect
);
338 const QModelIndex index
= indexAt(event
->pos());
339 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
340 const KFileItem item
= m_controller
->itemForIndex(index
);
341 if (!item
.isNull() && item
.isDir()) {
342 m_dropRect
= visualRect(index
);
344 m_dropRect
.setSize(QSize()); // set as invalid
346 setDirtyRegion(m_dropRect
);
349 if (event
->mimeData()->hasUrls()) {
350 // accept url drops, independently from the destination item
351 event
->acceptProposedAction();
355 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
357 const QModelIndex index
= indexAt(event
->pos());
359 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
360 item
= m_controller
->itemForIndex(index
);
362 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
363 QTreeView::dropEvent(event
);
366 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
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
;
375 opt
.shape
= QRubberBand::Rectangle
;
377 opt
.rect
= elasticBandRect();
379 QPainter
painter(viewport());
381 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
386 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
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
);
393 QTreeView::keyPressEvent(event
);
394 m_controller
->handleKeyPressEvent(event
);
397 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
399 QTreeView::keyReleaseEvent(event
);
400 m_keyPressed
= false;
403 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
408 QTreeView::resizeEvent(event
);
411 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
413 if (m_selectionManager
!= 0) {
414 m_selectionManager
->reset();
417 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
418 if (event
->modifiers() & Qt::ControlModifier
) {
423 QTreeView::wheelEvent(event
);
426 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
428 QTreeView::currentChanged(current
, previous
);
430 // Stay consistent with QListView: When changing the current index by key presses,
431 // also change the selection.
433 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
437 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
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();
446 return QTreeView::eventFilter(watched
, event
);
449 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
451 // the blank portion of the name column counts as empty space
452 const QModelIndex index
= QTreeView::indexAt(point
);
453 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
454 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
455 return isAboveEmptySpace
? QModelIndex() : index
;
458 void DolphinDetailsView::setSelection(const QRect
&rect
, QItemSelectionModel::SelectionFlags command
)
460 // We must override setSelection() as Qt calls it internally and when this happens
461 // we must ensure that the default indexAt() is used.
462 if (!m_showElasticBand
) {
463 m_useDefaultIndexAt
= true;
464 QTreeView::setSelection(rect
, command
);
465 m_useDefaultIndexAt
= false;
467 // Select the items contained within the elastic band.
469 // TODO - would this still work if the columns could be re-ordered
470 // (which I want to implement eventually :))?
472 // Very naive implementation - clears all selected, then walks a tree,
473 // selecting all items whose nameColumnRect(...) intersects rect.
475 // Choose a sensible startIndex - a parentless index in the
476 // name column, as close to the top of the rect as we
478 QRect normalisedRect
= rect
.normalized();
479 QModelIndex startIndex
= QTreeView::indexAt(normalisedRect
.topLeft());
480 if (startIndex
.isValid()) {
481 while (startIndex
.parent().isValid()) {
482 startIndex
= startIndex
.parent();
485 // just pick the topmost row for safety
486 model()->index(0, KDirModel::Name
);
488 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
490 setSelectionRecursive(startIndex
, normalisedRect
, command
);
494 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
496 QHeaderView
* headerView
= header();
497 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
500 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
502 QHeaderView
* headerView
= header();
503 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
506 void DolphinDetailsView::synchronizeSortingState(int column
)
508 // The sorting has already been changed in QTreeView if this slot is
509 // invoked, but Dolphin is not informed about this.
510 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
511 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
512 m_controller
->indicateSortingChange(sorting
);
513 m_controller
->indicateSortOrderChange(sortOrder
);
516 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
518 if (index
.column() == DolphinModel::Name
) {
519 m_controller
->emitItemEntered(index
);
521 m_controller
->emitViewportEntered();
525 void DolphinDetailsView::updateElasticBand()
527 if (m_showElasticBand
) {
528 QRect
dirtyRegion(elasticBandRect());
529 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
530 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
531 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
532 setDirtyRegion(dirtyRegion
);
536 QRect
DolphinDetailsView::elasticBandRect() const
538 const QPoint
pos(contentsPos());
539 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
541 const QPoint topLeft
= m_elasticBandOrigin
- pos
- scrollPos
;
542 const QPoint bottomRight
= m_elasticBandDestination
- pos
- scrollPos
;
543 return QRect(topLeft
, bottomRight
).normalized();
546 void DolphinDetailsView::setZoomLevel(int level
)
548 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
549 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
551 const bool showPreview
= m_controller
->dolphinView()->showPreview();
553 settings
->setPreviewSize(size
);
555 settings
->setIconSize(size
);
558 updateDecorationSize(showPreview
);
562 void DolphinDetailsView::slotShowPreviewChanged()
564 const DolphinView
* view
= m_controller
->dolphinView();
565 updateDecorationSize(view
->showPreview());
568 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
571 popup
.addTitle(i18nc("@title:menu", "Columns"));
573 QHeaderView
* headerView
= header();
574 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
575 const int logicalIndex
= headerView
->logicalIndex(i
);
576 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
577 QAction
* action
= popup
.addAction(text
);
578 action
->setCheckable(true);
579 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
583 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
584 if (activatedAction
!= 0) {
585 const bool show
= activatedAction
->isChecked();
586 const int columnIndex
= activatedAction
->data().toInt();
588 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
589 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
591 Q_ASSERT(!list
.contains(info
));
594 Q_ASSERT(list
.contains(info
));
595 const int index
= list
.indexOf(info
);
596 list
.removeAt(index
);
599 m_controller
->indicateAdditionalInfoChange(list
);
600 setColumnHidden(columnIndex
, !show
);
604 void DolphinDetailsView::updateColumnVisibility()
606 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
607 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
608 const KFileItemDelegate::Information info
= infoForColumn(i
);
609 const bool hide
= !list
.contains(info
);
610 if (isColumnHidden(i
) != hide
) {
611 setColumnHidden(i
, hide
);
618 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
620 Q_UNUSED(logicalIndex
);
623 if (QApplication::mouseButtons() & Qt::LeftButton
) {
624 disableAutoResizing();
628 void DolphinDetailsView::slotActivationChanged(bool active
)
630 setAlternatingRowColors(active
);
633 void DolphinDetailsView::disableAutoResizing()
635 m_autoResize
= false;
638 void DolphinDetailsView::requestActivation()
640 m_controller
->requestActivation();
643 void DolphinDetailsView::updateFont()
645 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
646 Q_ASSERT(settings
!= 0);
648 if (settings
->useSystemFont()) {
649 m_font
= KGlobalSettings::generalFont();
653 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
655 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
656 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
657 setIconSize(QSize(iconSize
, iconSize
));
658 m_decorationSize
= QSize(iconSize
, iconSize
);
660 if (m_selectionManager
!= 0) {
661 m_selectionManager
->reset();
667 QPoint
DolphinDetailsView::contentsPos() const
669 // implementation note: the horizonal position is ignored currently, as no
670 // horizontal scrolling is done anyway during a selection
671 const QScrollBar
* scrollbar
= verticalScrollBar();
672 Q_ASSERT(scrollbar
!= 0);
674 const int maxHeight
= maximumViewportSize().height();
675 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
676 const int visibleHeight
= model()->rowCount() + 1 - height
;
677 if (visibleHeight
<= 0) {
681 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
685 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
687 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
689 switch (columnIndex
) {
690 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
691 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
692 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
693 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
694 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
695 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
702 void DolphinDetailsView::resizeColumns()
704 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
705 // around 3 seconds for each (!) resize operation when having > 10000 items).
706 // This gets a problem especially when opening large directories, where several
707 // resize operations are received for showing the currently available items during
708 // loading (the application hangs around 20 seconds when loading > 10000 items).
710 QHeaderView
* headerView
= header();
711 QFontMetrics
fontMetrics(viewport()->font());
713 int columnWidth
[KDirModel::ColumnCount
];
714 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
715 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
716 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
717 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
718 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
719 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
721 int requiredWidth
= 0;
722 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
723 if (!isColumnHidden(i
)) {
724 columnWidth
[i
] += 20; // provide a default gap
725 requiredWidth
+= columnWidth
[i
];
726 headerView
->resizeSection(i
, columnWidth
[i
]);
730 // resize the name column in a way that the whole available width is used
731 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
732 if (columnWidth
[KDirModel::Name
] < 120) {
733 columnWidth
[KDirModel::Name
] = 120;
735 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
738 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
740 // TODO: The code guesses the width of the name, but it is defined
741 // by KFileItemDelegate. Find a way to tell the item delegate to
742 // use a specified width.
743 QRect guessedItemContentRect
= visualRect(index
);
744 const KFileItem fileItem
= m_controller
->itemForIndex(index
);
745 if (!fileItem
.isNull()) {
746 QStyleOptionViewItem itemStyle
= viewOptions();
747 QFontMetrics
fontMetrics(itemStyle
.font
);
748 const int itemContentWidth
= itemStyle
.decorationSize
.width() + fontMetrics
.width(fileItem
.name());
749 guessedItemContentRect
.setWidth(itemContentWidth
);
752 return guessedItemContentRect
;
755 void DolphinDetailsView::setSelectionRecursive(const QModelIndex
& startIndex
,
757 QItemSelectionModel::SelectionFlags command
)
759 if (!startIndex
.isValid()) {
763 // rect is assumed to be in viewport coordinates and normalized.
764 // Move down through the siblings of startIndex, exploring the children
765 // of any expanded nodes.
766 Q_ASSERT(rect
.width() >= 0 && rect
.height() >= 0);
767 QModelIndex currIndex
= startIndex
;
769 const QModelIndex belowIndex
= indexBelow(currIndex
);
770 if (isExpanded(currIndex
)) {
771 // If belowIndex exists and is above the top of rect, then we need not explore
772 // the children of currIndex as they will always be above "below". Otherwise,
773 // explore the children.
774 if (!belowIndex
.isValid() || visualRect(belowIndex
).bottom() >= rect
.top()) {
775 setSelectionRecursive(currIndex
.child(0, currIndex
.column()), rect
, command
);
779 QRect itemContentRect
= nameColumnRect(currIndex
);
780 if (itemContentRect
.top() > rect
.bottom()) {
781 // All remaining items will be below itemContentRect, so we may cull.
785 if (itemContentRect
.intersects(rect
)) {
786 selectionModel()->select(currIndex
, QItemSelectionModel::Select
);
789 currIndex
= belowIndex
;
790 } while (currIndex
.isValid());
793 #include "dolphindetailsview.moc"