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 "dolphinfileitemdelegate.h"
26 #include "dolphinsettings.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30 #include "viewproperties.h"
31 #include "zoomlevelinfo.h"
33 #include "dolphin_detailsmodesettings.h"
34 #include "dolphin_generalsettings.h"
36 #include <kdirmodel.h>
40 #include <QAbstractProxyModel>
42 #include <QApplication>
43 #include <QHeaderView>
44 #include <QRubberBand>
48 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
51 m_expandingTogglePressed(false),
53 m_useDefaultIndexAt(true),
54 m_controller(controller
),
55 m_selectionManager(0),
58 m_showElasticBand(false),
59 m_elasticBandOrigin(),
60 m_elasticBandDestination()
62 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
63 Q_ASSERT(settings
!= 0);
64 Q_ASSERT(controller
!= 0);
66 setLayoutDirection(Qt::LeftToRight
);
68 setSortingEnabled(true);
69 setUniformRowHeights(true);
70 setSelectionBehavior(SelectItems
);
71 setDragDropMode(QAbstractItemView::DragDrop
);
72 setDropIndicatorShown(false);
73 setAlternatingRowColors(true);
74 setRootIsDecorated(settings
->expandableFolders());
75 setItemsExpandable(settings
->expandableFolders());
76 setEditTriggers(QAbstractItemView::NoEditTriggers
);
78 setMouseTracking(true);
80 const ViewProperties
props(controller
->url());
81 setSortIndicatorSection(props
.sorting());
82 setSortIndicatorOrder(props
.sortOrder());
84 QHeaderView
* headerView
= header();
85 connect(headerView
, SIGNAL(sectionClicked(int)),
86 this, SLOT(synchronizeSortingState(int)));
87 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
88 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
89 this, SLOT(configureColumns(const QPoint
&)));
90 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
91 this, SLOT(slotHeaderSectionResized(int, int, int)));
92 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
93 this, SLOT(disableAutoResizing()));
95 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
96 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
97 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
98 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
100 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
101 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
102 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
103 // RETURN-key in keyPressEvent().
104 if (KGlobalSettings::singleClick()) {
105 connect(this, SIGNAL(clicked(const QModelIndex
&)),
106 controller
, SLOT(triggerItem(const QModelIndex
&)));
108 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
109 controller
, SLOT(triggerItem(const QModelIndex
&)));
112 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
113 m_selectionManager
= new SelectionManager(this);
114 connect(m_selectionManager
, SIGNAL(selectionChanged()),
115 this, SLOT(requestActivation()));
116 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
117 m_selectionManager
, SLOT(reset()));
120 connect(this, SIGNAL(entered(const QModelIndex
&)),
121 this, SLOT(slotEntered(const QModelIndex
&)));
122 connect(this, SIGNAL(viewportEntered()),
123 controller
, SLOT(emitViewportEntered()));
124 connect(controller
, SIGNAL(zoomLevelChanged(int)),
125 this, SLOT(setZoomLevel(int)));
126 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
127 this, SLOT(updateColumnVisibility()));
128 connect(controller
, SIGNAL(activationChanged(bool)),
129 this, SLOT(slotActivationChanged(bool)));
131 if (settings
->useSystemFont()) {
132 m_font
= KGlobalSettings::generalFont();
134 m_font
= QFont(settings
->fontFamily(),
135 settings
->fontSize(),
136 settings
->fontWeight(),
137 settings
->italicFont());
140 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
141 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
143 const DolphinView
* view
= controller
->dolphinView();
144 connect(view
, SIGNAL(showPreviewChanged()),
145 this, SLOT(slotShowPreviewChanged()));
147 updateDecorationSize(view
->showPreview());
150 viewport()->installEventFilter(this);
152 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
153 this, SLOT(updateFont()));
155 m_useDefaultIndexAt
= false;
158 DolphinDetailsView::~DolphinDetailsView()
162 bool DolphinDetailsView::event(QEvent
* event
)
164 if (event
->type() == QEvent::Polish
) {
165 QHeaderView
* headerView
= header();
166 headerView
->setResizeMode(QHeaderView::Interactive
);
167 headerView
->setMovable(false);
169 updateColumnVisibility();
171 hideColumn(DolphinModel::Rating
);
172 hideColumn(DolphinModel::Tags
);
173 } else if (event
->type() == QEvent::UpdateRequest
) {
174 // a wheel movement will scroll 4 items
175 if (model()->rowCount() > 0) {
176 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
180 return QTreeView::event(event
);
183 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
185 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
186 viewOptions
.font
= m_font
;
187 viewOptions
.showDecorationSelected
= true;
188 viewOptions
.decorationSize
= m_decorationSize
;
192 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
194 QTreeView::contextMenuEvent(event
);
195 m_controller
->triggerContextMenuRequest(event
->pos());
198 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
200 m_controller
->requestActivation();
202 const QModelIndex current
= currentIndex();
203 QTreeView::mousePressEvent(event
);
205 m_expandingTogglePressed
= false;
206 const QModelIndex index
= indexAt(event
->pos());
207 const bool updateState
= index
.isValid() &&
208 (index
.column() == DolphinModel::Name
) &&
209 (event
->button() == Qt::LeftButton
);
211 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
212 // the state if no expanding/collapsing area has been hit:
213 const QRect rect
= visualRect(index
);
214 if (event
->pos().x() >= rect
.x() + indentation()) {
215 setState(QAbstractItemView::DraggingState
);
217 m_expandingTogglePressed
= true;
221 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
222 // the mouse press is done somewhere outside the filename column
223 if (QApplication::mouseButtons() & Qt::MidButton
) {
224 m_controller
->replaceUrlByClipboard();
227 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
228 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
232 // restore the current index, other columns are handled as viewport area
233 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
236 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
237 m_showElasticBand
= true;
238 const QPoint pos
= contentsPos();
239 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
240 m_elasticBandOrigin
= event
->pos() + pos
+ scrollPos
;
241 m_elasticBandDestination
= m_elasticBandOrigin
;
245 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
247 if (m_showElasticBand
) {
248 const QPoint mousePos
= event
->pos();
249 const QModelIndex index
= indexAt(mousePos
);
250 if (!index
.isValid()) {
251 // the destination of the selection rectangle is above the viewport. In this
252 // case QTreeView does no selection at all, which is not the wanted behavior
253 // in Dolphin -> select all items within the elastic band rectangle
255 setState(DragSelectingState
);
257 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
258 QRect selRect
= elasticBandRect();
259 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
260 selRect
= nameColumnsRect
.intersected(selRect
);
262 setSelection(selRect
, QItemSelectionModel::Select
);
266 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
267 // as the Qt-issue #199631 has been fixed.
268 // QTreeView::mouseMoveEvent(event);
269 QAbstractItemView::mouseMoveEvent(event
);
272 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
273 // as the Qt-issue #199631 has been fixed.
274 // QTreeView::mouseMoveEvent(event);
275 QAbstractItemView::mouseMoveEvent(event
);
278 if (m_expandingTogglePressed
) {
279 // Per default QTreeView starts either a selection or a drag operation when dragging
280 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
281 // Turn off this behavior in Dolphin to stay predictable:
283 setState(QAbstractItemView::NoState
);
287 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
289 const QModelIndex index
= indexAt(event
->pos());
290 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
291 QTreeView::mouseReleaseEvent(event
);
293 // don't change the current index if the cursor is released
294 // above any other column than the name column, as the other
295 // columns act as viewport
296 const QModelIndex current
= currentIndex();
297 QTreeView::mouseReleaseEvent(event
);
298 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
301 m_expandingTogglePressed
= false;
302 if (m_showElasticBand
) {
305 m_showElasticBand
= false;
309 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
311 DragAndDropHelper::startDrag(this, supportedActions
);
312 m_showElasticBand
= false;
315 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
317 if (event
->mimeData()->hasUrls()) {
318 event
->acceptProposedAction();
321 if (m_showElasticBand
) {
323 m_showElasticBand
= false;
327 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
329 QTreeView::dragLeaveEvent(event
);
330 setDirtyRegion(m_dropRect
);
333 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
335 QTreeView::dragMoveEvent(event
);
337 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
338 setDirtyRegion(m_dropRect
);
339 const QModelIndex index
= indexAt(event
->pos());
340 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
341 const KFileItem item
= m_controller
->itemForIndex(index
);
342 if (!item
.isNull() && item
.isDir()) {
343 m_dropRect
= visualRect(index
);
345 m_dropRect
.setSize(QSize()); // set as invalid
347 setDirtyRegion(m_dropRect
);
350 if (event
->mimeData()->hasUrls()) {
351 // accept url drops, independently from the destination item
352 event
->acceptProposedAction();
356 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
358 const QModelIndex index
= indexAt(event
->pos());
360 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
361 item
= m_controller
->itemForIndex(index
);
363 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
364 QTreeView::dropEvent(event
);
367 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
369 QTreeView::paintEvent(event
);
370 if (m_showElasticBand
) {
371 // The following code has been taken from QListView
372 // and adapted to DolphinDetailsView.
373 // (C) 1992-2007 Trolltech ASA
374 QStyleOptionRubberBand opt
;
376 opt
.shape
= QRubberBand::Rectangle
;
378 opt
.rect
= elasticBandRect();
380 QPainter
painter(viewport());
382 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
387 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
389 // If the Control modifier is pressed, a multiple selection
390 // is done and DolphinDetailsView::currentChanged() may not
391 // not change the selection in a custom way.
392 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
394 QTreeView::keyPressEvent(event
);
395 m_controller
->handleKeyPressEvent(event
);
398 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
400 QTreeView::keyReleaseEvent(event
);
401 m_keyPressed
= false;
404 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
409 QTreeView::resizeEvent(event
);
412 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
414 if (m_selectionManager
!= 0) {
415 m_selectionManager
->reset();
418 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
419 if (event
->modifiers() & Qt::ControlModifier
) {
424 QTreeView::wheelEvent(event
);
427 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
429 QTreeView::currentChanged(current
, previous
);
431 // Stay consistent with QListView: When changing the current index by key presses,
432 // also change the selection.
434 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
438 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
440 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
441 // if the mouse is above an item and moved very fast outside the widget,
442 // no viewportEntered() signal might be emitted although the mouse has been moved
443 // above the viewport
444 m_controller
->emitViewportEntered();
447 return QTreeView::eventFilter(watched
, event
);
450 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
452 // the blank portion of the name column counts as empty space
453 const QModelIndex index
= QTreeView::indexAt(point
);
454 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
455 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
456 return isAboveEmptySpace
? QModelIndex() : index
;
459 void DolphinDetailsView::setSelection(const QRect
&rect
, QItemSelectionModel::SelectionFlags command
)
461 // We must override setSelection() as Qt calls it internally and when this happens
462 // we must ensure that the default indexAt() is used.
463 if (!m_showElasticBand
) {
464 m_useDefaultIndexAt
= true;
465 QTreeView::setSelection(rect
, command
);
466 m_useDefaultIndexAt
= false;
468 // Select the items contained within the elastic band.
470 // TODO - would this still work if the columns could be re-ordered
471 // (which I want to implement eventually :))?
473 // Very naive implementation - clears all selected, then walks a tree,
474 // selecting all items whose nameColumnRect(...) intersects rect.
476 // Choose a sensible startIndex - a parentless index in the
477 // name column, as close to the top of the rect as we
479 QRect normalisedRect
= rect
.normalized();
480 QModelIndex startIndex
= QTreeView::indexAt(normalisedRect
.topLeft());
481 if (startIndex
.isValid()) {
482 while (startIndex
.parent().isValid()) {
483 startIndex
= startIndex
.parent();
486 // just pick the topmost row for safety
487 model()->index(0, KDirModel::Name
);
489 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
491 setSelectionRecursive(startIndex
, normalisedRect
, command
);
495 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
497 QHeaderView
* headerView
= header();
498 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
501 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
503 QHeaderView
* headerView
= header();
504 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
507 void DolphinDetailsView::synchronizeSortingState(int column
)
509 // The sorting has already been changed in QTreeView if this slot is
510 // invoked, but Dolphin is not informed about this.
511 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
512 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
513 m_controller
->indicateSortingChange(sorting
);
514 m_controller
->indicateSortOrderChange(sortOrder
);
517 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
519 if (index
.column() == DolphinModel::Name
) {
520 m_controller
->emitItemEntered(index
);
522 m_controller
->emitViewportEntered();
526 void DolphinDetailsView::updateElasticBand()
528 if (m_showElasticBand
) {
529 QRect
dirtyRegion(elasticBandRect());
530 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
531 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
532 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
533 setDirtyRegion(dirtyRegion
);
537 QRect
DolphinDetailsView::elasticBandRect() const
539 const QPoint
pos(contentsPos());
540 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
542 const QPoint topLeft
= m_elasticBandOrigin
- pos
- scrollPos
;
543 const QPoint bottomRight
= m_elasticBandDestination
- pos
- scrollPos
;
544 return QRect(topLeft
, bottomRight
).normalized();
547 void DolphinDetailsView::setZoomLevel(int level
)
549 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
550 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
552 const bool showPreview
= m_controller
->dolphinView()->showPreview();
554 settings
->setPreviewSize(size
);
556 settings
->setIconSize(size
);
559 updateDecorationSize(showPreview
);
563 void DolphinDetailsView::slotShowPreviewChanged()
565 const DolphinView
* view
= m_controller
->dolphinView();
566 updateDecorationSize(view
->showPreview());
569 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
572 popup
.addTitle(i18nc("@title:menu", "Columns"));
574 QHeaderView
* headerView
= header();
575 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
576 const int logicalIndex
= headerView
->logicalIndex(i
);
577 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
578 QAction
* action
= popup
.addAction(text
);
579 action
->setCheckable(true);
580 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
584 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
585 if (activatedAction
!= 0) {
586 const bool show
= activatedAction
->isChecked();
587 const int columnIndex
= activatedAction
->data().toInt();
589 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
590 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
592 Q_ASSERT(!list
.contains(info
));
595 Q_ASSERT(list
.contains(info
));
596 const int index
= list
.indexOf(info
);
597 list
.removeAt(index
);
600 m_controller
->indicateAdditionalInfoChange(list
);
601 setColumnHidden(columnIndex
, !show
);
605 void DolphinDetailsView::updateColumnVisibility()
607 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
608 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
609 const KFileItemDelegate::Information info
= infoForColumn(i
);
610 const bool hide
= !list
.contains(info
);
611 if (isColumnHidden(i
) != hide
) {
612 setColumnHidden(i
, hide
);
619 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
621 Q_UNUSED(logicalIndex
);
624 if (QApplication::mouseButtons() & Qt::LeftButton
) {
625 disableAutoResizing();
629 void DolphinDetailsView::slotActivationChanged(bool active
)
631 setAlternatingRowColors(active
);
634 void DolphinDetailsView::disableAutoResizing()
636 m_autoResize
= false;
639 void DolphinDetailsView::requestActivation()
641 m_controller
->requestActivation();
644 void DolphinDetailsView::updateFont()
646 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
647 Q_ASSERT(settings
!= 0);
649 if (settings
->useSystemFont()) {
650 m_font
= KGlobalSettings::generalFont();
654 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
656 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
657 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
658 setIconSize(QSize(iconSize
, iconSize
));
659 m_decorationSize
= QSize(iconSize
, iconSize
);
661 if (m_selectionManager
!= 0) {
662 m_selectionManager
->reset();
668 QPoint
DolphinDetailsView::contentsPos() const
670 // implementation note: the horizonal position is ignored currently, as no
671 // horizontal scrolling is done anyway during a selection
672 const QScrollBar
* scrollbar
= verticalScrollBar();
673 Q_ASSERT(scrollbar
!= 0);
675 const int maxHeight
= maximumViewportSize().height();
676 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
677 const int visibleHeight
= model()->rowCount() + 1 - height
;
678 if (visibleHeight
<= 0) {
682 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
686 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
688 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
690 switch (columnIndex
) {
691 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
692 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
693 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
694 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
695 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
696 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
703 void DolphinDetailsView::resizeColumns()
705 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
706 // around 3 seconds for each (!) resize operation when having > 10000 items).
707 // This gets a problem especially when opening large directories, where several
708 // resize operations are received for showing the currently available items during
709 // loading (the application hangs around 20 seconds when loading > 10000 items).
711 QHeaderView
* headerView
= header();
712 QFontMetrics
fontMetrics(viewport()->font());
714 int columnWidth
[KDirModel::ColumnCount
];
715 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
716 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
717 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
718 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
719 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
720 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
722 int requiredWidth
= 0;
723 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
724 if (!isColumnHidden(i
)) {
725 columnWidth
[i
] += 20; // provide a default gap
726 requiredWidth
+= columnWidth
[i
];
727 headerView
->resizeSection(i
, columnWidth
[i
]);
731 // resize the name column in a way that the whole available width is used
732 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
733 if (columnWidth
[KDirModel::Name
] < 120) {
734 columnWidth
[KDirModel::Name
] = 120;
736 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
739 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
741 QRect rect
= visualRect(index
);
742 const KFileItem item
= m_controller
->itemForIndex(index
);
743 if (!item
.isNull()) {
744 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.name(), viewOptions());
745 rect
.setWidth(width
);
751 void DolphinDetailsView::setSelectionRecursive(const QModelIndex
& startIndex
,
753 QItemSelectionModel::SelectionFlags command
)
755 if (!startIndex
.isValid()) {
759 // rect is assumed to be in viewport coordinates and normalized.
760 // Move down through the siblings of startIndex, exploring the children
761 // of any expanded nodes.
762 Q_ASSERT(rect
.width() >= 0 && rect
.height() >= 0);
763 QModelIndex currIndex
= startIndex
;
765 const QModelIndex belowIndex
= indexBelow(currIndex
);
766 if (isExpanded(currIndex
)) {
767 // If belowIndex exists and is above the top of rect, then we need not explore
768 // the children of currIndex as they will always be above "below". Otherwise,
769 // explore the children.
770 if (!belowIndex
.isValid() || visualRect(belowIndex
).bottom() >= rect
.top()) {
771 setSelectionRecursive(currIndex
.child(0, currIndex
.column()), rect
, command
);
775 QRect itemContentRect
= nameColumnRect(currIndex
);
776 if (itemContentRect
.top() > rect
.bottom()) {
777 // All remaining items will be below itemContentRect, so we may cull.
781 if (itemContentRect
.intersects(rect
)) {
782 selectionModel()->select(currIndex
, QItemSelectionModel::Select
);
785 currIndex
= belowIndex
;
786 } while (currIndex
.isValid());
789 #include "dolphindetailsview.moc"