1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * Copyright (C) 2008 by Simon St. James (kdedevel@etotheipiplusone.com) *
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 "dolphinviewautoscroller.h"
29 #include "draganddrophelper.h"
30 #include "selectionmanager.h"
31 #include "viewproperties.h"
32 #include "zoomlevelinfo.h"
34 #include "dolphin_detailsmodesettings.h"
35 #include "dolphin_generalsettings.h"
37 #include <kdirmodel.h>
41 #include <QAbstractProxyModel>
43 #include <QApplication>
44 #include <QHeaderView>
45 #include <QRubberBand>
49 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
52 m_expandingTogglePressed(false),
54 m_useDefaultIndexAt(true),
55 m_ignoreScrollTo(false),
56 m_controller(controller
),
57 m_selectionManager(0),
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);
79 new DolphinViewAutoScroller(this);
81 const ViewProperties
props(controller
->url());
82 setSortIndicatorSection(props
.sorting());
83 setSortIndicatorOrder(props
.sortOrder());
85 QHeaderView
* headerView
= header();
86 connect(headerView
, SIGNAL(sectionClicked(int)),
87 this, SLOT(synchronizeSortingState(int)));
88 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
89 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
90 this, SLOT(configureColumns(const QPoint
&)));
91 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
92 this, SLOT(slotHeaderSectionResized(int, int, int)));
93 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
94 this, SLOT(disableAutoResizing()));
96 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
97 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
98 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
99 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
101 connect(this, SIGNAL(clicked(const QModelIndex
&)),
102 controller
, SLOT(requestTab(const QModelIndex
&)));
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
);
174 return QTreeView::event(event
);
177 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
179 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
180 viewOptions
.font
= m_font
;
181 viewOptions
.showDecorationSelected
= true;
182 viewOptions
.decorationSize
= m_decorationSize
;
186 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
188 QTreeView::contextMenuEvent(event
);
189 m_controller
->triggerContextMenuRequest(event
->pos());
192 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
194 m_controller
->requestActivation();
196 const QModelIndex current
= currentIndex();
197 QTreeView::mousePressEvent(event
);
199 m_expandingTogglePressed
= false;
200 const QModelIndex index
= indexAt(event
->pos());
201 const bool updateState
= index
.isValid() &&
202 (index
.column() == DolphinModel::Name
) &&
203 (event
->button() == Qt::LeftButton
);
205 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
206 // the state if no expanding/collapsing area has been hit:
207 const QRect rect
= visualRect(index
);
208 if (event
->pos().x() >= rect
.x() + indentation()) {
209 setState(QAbstractItemView::DraggingState
);
211 m_expandingTogglePressed
= true;
212 kDebug() << "m_expandingTogglePressed " << m_expandingTogglePressed
;
216 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
217 // the mouse press is done somewhere outside the filename column
218 if (QApplication::mouseButtons() & Qt::MidButton
) {
219 m_controller
->replaceUrlByClipboard();
222 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
223 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
227 // restore the current index, other columns are handled as viewport area.
228 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
229 m_ignoreScrollTo
= true;
230 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
231 m_ignoreScrollTo
= false;
233 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
234 // Inform Qt about what we are doing - otherwise it starts dragging items around!
235 setState(DragSelectingState
);
237 // Incremental update data will not be useful - start from scratch.
238 m_band
.ignoreOldInfo
= true;
239 const QPoint pos
= contentsPos();
240 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
241 m_band
.origin
= event
->pos() + pos
+ scrollPos
;
242 m_band
.destination
= m_band
.origin
;
243 m_band
.originalSelection
= selectionModel()->selection();
248 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
251 const QPoint mousePos
= event
->pos();
252 const QModelIndex index
= indexAt(mousePos
);
253 if (!index
.isValid()) {
254 // the destination of the selection rectangle is above the viewport. In this
255 // case QTreeView does no selection at all, which is not the wanted behavior
256 // in Dolphin -> select all items within the elastic band rectangle
257 updateElasticBandSelection();
260 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
261 // as the Qt-issue #199631 has been fixed.
262 // QTreeView::mouseMoveEvent(event);
263 QAbstractItemView::mouseMoveEvent(event
);
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 if (m_expandingTogglePressed
) {
273 // Per default QTreeView starts either a selection or a drag operation when dragging
274 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
275 // Turn off this behavior in Dolphin to stay predictable:
277 setState(QAbstractItemView::NoState
);
281 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
283 const QModelIndex index
= indexAt(event
->pos());
284 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
285 QTreeView::mouseReleaseEvent(event
);
287 // don't change the current index if the cursor is released
288 // above any other column than the name column, as the other
289 // columns act as viewport
290 const QModelIndex current
= currentIndex();
291 QTreeView::mouseReleaseEvent(event
);
292 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
295 m_expandingTogglePressed
= false;
303 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
305 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
309 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
311 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
312 event
->acceptProposedAction();
321 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
323 QTreeView::dragLeaveEvent(event
);
324 setDirtyRegion(m_dropRect
);
327 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
329 QTreeView::dragMoveEvent(event
);
331 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
332 setDirtyRegion(m_dropRect
);
333 const QModelIndex index
= indexAt(event
->pos());
334 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
335 const KFileItem item
= m_controller
->itemForIndex(index
);
336 if (!item
.isNull() && item
.isDir()) {
337 m_dropRect
= visualRect(index
);
339 m_dropRect
.setSize(QSize()); // set as invalid
341 setDirtyRegion(m_dropRect
);
344 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
345 // accept url drops, independently from the destination item
346 event
->acceptProposedAction();
350 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
352 const QModelIndex index
= indexAt(event
->pos());
354 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
355 item
= m_controller
->itemForIndex(index
);
357 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
358 QTreeView::dropEvent(event
);
361 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
363 QTreeView::paintEvent(event
);
365 // The following code has been taken from QListView
366 // and adapted to DolphinDetailsView.
367 // (C) 1992-2007 Trolltech ASA
368 QStyleOptionRubberBand opt
;
370 opt
.shape
= QRubberBand::Rectangle
;
372 opt
.rect
= elasticBandRect();
374 QPainter
painter(viewport());
376 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
381 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
383 // If the Control modifier is pressed, a multiple selection
384 // is done and DolphinDetailsView::currentChanged() may not
385 // not change the selection in a custom way.
386 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
388 QTreeView::keyPressEvent(event
);
389 m_controller
->handleKeyPressEvent(event
);
392 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
394 QTreeView::keyReleaseEvent(event
);
395 m_keyPressed
= false;
398 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
400 QTreeView::resizeEvent(event
);
406 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
408 if (m_selectionManager
!= 0) {
409 m_selectionManager
->reset();
412 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
413 if (event
->modifiers() & Qt::ControlModifier
) {
418 const int height
= m_decorationSize
.height();
419 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
420 verticalScrollBar()->setSingleStep(step
);
421 QTreeView::wheelEvent(event
);
424 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
426 QTreeView::currentChanged(current
, previous
);
427 if (current
.isValid()) {
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.
464 m_useDefaultIndexAt
= true;
465 QTreeView::setSelection(rect
, command
);
466 m_useDefaultIndexAt
= false;
469 // Use our own elastic band selection algorithm
470 updateElasticBandSelection();
474 void DolphinDetailsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
476 if (m_ignoreScrollTo
)
478 QTreeView::scrollTo(index
, hint
);
481 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
483 QHeaderView
* headerView
= header();
484 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
487 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
489 QHeaderView
* headerView
= header();
490 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
493 void DolphinDetailsView::synchronizeSortingState(int column
)
495 // The sorting has already been changed in QTreeView if this slot is
496 // invoked, but Dolphin is not informed about this.
497 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
498 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
499 m_controller
->indicateSortingChange(sorting
);
500 m_controller
->indicateSortOrderChange(sortOrder
);
503 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
505 if (index
.column() == DolphinModel::Name
) {
506 m_controller
->emitItemEntered(index
);
508 m_controller
->emitViewportEntered();
512 void DolphinDetailsView::updateElasticBand()
515 QRect
dirtyRegion(elasticBandRect());
516 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
517 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
518 // Going above the (logical) top-left of the view causes complications during selection;
519 // we may as well prevent it.
520 if (m_band
.destination
.y() < 0)
521 m_band
.destination
.setY(0);
522 if (m_band
.destination
.x() < 0)
523 m_band
.destination
.setX(0);
525 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
526 setDirtyRegion(dirtyRegion
);
530 QRect
DolphinDetailsView::elasticBandRect() const
532 const QPoint
pos(contentsPos());
533 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
535 const QPoint topLeft
= m_band
.origin
- pos
- scrollPos
;
536 const QPoint bottomRight
= m_band
.destination
- pos
- scrollPos
;
537 return QRect(topLeft
, bottomRight
).normalized();
540 void DolphinDetailsView::setZoomLevel(int level
)
542 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
543 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
545 const bool showPreview
= m_controller
->dolphinView()->showPreview();
547 settings
->setPreviewSize(size
);
549 settings
->setIconSize(size
);
552 updateDecorationSize(showPreview
);
556 void DolphinDetailsView::slotShowPreviewChanged()
558 const DolphinView
* view
= m_controller
->dolphinView();
559 updateDecorationSize(view
->showPreview());
562 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
565 popup
.addTitle(i18nc("@title:menu", "Columns"));
567 QHeaderView
* headerView
= header();
568 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
569 const int logicalIndex
= headerView
->logicalIndex(i
);
570 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
571 QAction
* action
= popup
.addAction(text
);
572 action
->setCheckable(true);
573 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
577 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
578 if (activatedAction
!= 0) {
579 const bool show
= activatedAction
->isChecked();
580 const int columnIndex
= activatedAction
->data().toInt();
582 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
583 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
585 Q_ASSERT(!list
.contains(info
));
588 Q_ASSERT(list
.contains(info
));
589 const int index
= list
.indexOf(info
);
590 list
.removeAt(index
);
593 m_controller
->indicateAdditionalInfoChange(list
);
594 setColumnHidden(columnIndex
, !show
);
599 void DolphinDetailsView::updateColumnVisibility()
601 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
602 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
603 const KFileItemDelegate::Information info
= infoForColumn(i
);
604 const bool hide
= !list
.contains(info
);
605 if (isColumnHidden(i
) != hide
) {
606 setColumnHidden(i
, hide
);
613 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
615 Q_UNUSED(logicalIndex
);
618 // If the user changes the size of the headers, the autoresize feature should be
619 // turned off. As there is no dedicated interface to find out whether the header
620 // section has been resized by the user or by a resize event, the following approach is used:
621 if ((QApplication::mouseButtons() & Qt::LeftButton
) && isVisible()) {
622 disableAutoResizing();
626 void DolphinDetailsView::slotActivationChanged(bool active
)
628 setAlternatingRowColors(active
);
631 void DolphinDetailsView::disableAutoResizing()
633 m_autoResize
= false;
636 void DolphinDetailsView::requestActivation()
638 m_controller
->requestActivation();
641 void DolphinDetailsView::updateFont()
643 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
644 Q_ASSERT(settings
!= 0);
646 if (settings
->useSystemFont()) {
647 m_font
= KGlobalSettings::generalFont();
651 void DolphinDetailsView::updateElasticBandSelection()
657 // Ensure the elastic band itself is up-to-date, in
658 // case we are being called due to e.g. a drag event.
661 // Clip horizontally to the name column, as some filenames will be
662 // longer than the column. We don't clip vertically as origin
663 // may be above or below the current viewport area.
664 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
665 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
666 QRect selRect
= elasticBandRect().normalized();
667 QRect
nameColumnArea(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
668 selRect
= nameColumnArea
.intersect(selRect
).normalized();
669 // Get the last elastic band rectangle, expressed in viewpoint coordinates.
670 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
671 QRect oldSelRect
= QRect(m_band
.lastSelectionOrigin
- scrollPos
, m_band
.lastSelectionDestination
- scrollPos
).normalized();
673 if (selRect
.isNull()) {
674 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
675 m_band
.ignoreOldInfo
= true;
679 if (!m_band
.ignoreOldInfo
) {
680 // Do some quick checks to see if we can rule out the need to
681 // update the selection.
682 Q_ASSERT(uniformRowHeights());
683 QModelIndex dummyIndex
= model()->index(0, 0);
684 if (!dummyIndex
.isValid()) {
685 // No items in the model presumably.
689 // If the elastic band does not cover the same rows as before, we'll
690 // need to re-check, and also invalidate the old item distances.
691 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
692 const bool coveringSameRows
=
693 (selRect
.top() / rowHeight
== oldSelRect
.top() / rowHeight
) &&
694 (selRect
.bottom() / rowHeight
== oldSelRect
.bottom() / rowHeight
);
695 if (coveringSameRows
) {
696 // Covering the same rows, but have we moved far enough horizontally
697 // that we might have (de)selected some other items?
698 const bool itemSelectionChanged
=
699 ((selRect
.left() > oldSelRect
.left()) &&
700 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
701 ((selRect
.left() < oldSelRect
.left()) &&
702 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
703 ((selRect
.right() < oldSelRect
.right()) &&
704 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
705 ((selRect
.right() > oldSelRect
.right()) &&
706 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
708 if (!itemSelectionChanged
) {
714 // This is the only piece of optimization data that needs to be explicitly
716 m_band
.lastSelectionOrigin
= QPoint();
717 m_band
.lastSelectionDestination
= QPoint();
718 oldSelRect
= selRect
;
721 // Do the selection from scratch. Force a update of the horizontal distances info.
722 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
723 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
724 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
725 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
727 // Include the old selection rect as well, so we can deselect
728 // items that were inside it but not in the new selRect.
729 const QRect boundingRect
= selRect
.united(oldSelRect
).normalized();
730 if (boundingRect
.isNull()) {
734 // Get the index of the item in this row in the name column.
735 // TODO - would this still work if the columns could be re-ordered?
736 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
737 if (startIndex
.parent().isValid()) {
738 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
740 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
742 if (!startIndex
.isValid()) {
743 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
744 m_band
.ignoreOldInfo
= true;
748 // Go through all indexes between the top and bottom of boundingRect, and
749 // update the selection.
750 const int verticalCutoff
= boundingRect
.bottom();
751 QModelIndex currIndex
= startIndex
;
752 QModelIndex lastIndex
;
753 bool allItemsInBoundDone
= false;
755 // Calling selectionModel()->select(...) for each item that needs to be
756 // toggled is slow as each call emits selectionChanged(...) so store them
757 // and do the selection toggle in one batch.
758 QItemSelection itemsToToggle
;
759 // QItemSelection's deal with continuous ranges of indexes better than
760 // single indexes, so try to portion items that need to be toggled into ranges.
761 bool formingToggleIndexRange
= false;
762 QModelIndex toggleIndexRangeBegin
= QModelIndex();
765 QRect currIndexRect
= nameColumnRect(currIndex
);
767 // Update some optimization info as we go.
768 const int cr
= currIndexRect
.right();
769 const int cl
= currIndexRect
.left();
770 const int sl
= selRect
.left();
771 const int sr
= selRect
.right();
772 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
773 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
774 m_band
.outsideNearestLeftEdge
= cr
;
776 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
777 m_band
.outsideNearestRightEdge
= cl
;
779 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
780 m_band
.insideNearestRightEdge
= cl
;
782 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
783 m_band
.insideNearestLeftEdge
= cr
;
786 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
787 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
788 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
789 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
790 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
791 if (needToToggleItem
&& !formingToggleIndexRange
) {
792 toggleIndexRangeBegin
= currIndex
;
793 formingToggleIndexRange
= true;
796 // NOTE: indexBelow actually walks up and down expanded trees for us.
797 QModelIndex nextIndex
= indexBelow(currIndex
);
798 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
800 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
801 (!needToToggleItem
||
802 allItemsInBoundDone
||
803 currIndex
.parent() != toggleIndexRangeBegin
.parent());
804 if (commitToggleIndexRange
) {
805 formingToggleIndexRange
= false;
806 // If this is the last item in the bounds and it is also the beginning of a range,
807 // don't toggle lastIndex - it will already have been dealt with.
808 if (!allItemsInBoundDone
|| toggleIndexRangeBegin
!= currIndex
) {
809 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
811 // Need to start a new range immediately with currIndex?
812 if (needToToggleItem
) {
813 toggleIndexRangeBegin
= currIndex
;
814 formingToggleIndexRange
= true;
816 if (allItemsInBoundDone
&& needToToggleItem
) {
817 // Toggle the very last item in the bounds.
818 itemsToToggle
.select(currIndex
, currIndex
);
823 lastIndex
= currIndex
;
824 currIndex
= nextIndex
;
825 } while (!allItemsInBoundDone
);
827 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
829 m_band
.lastSelectionOrigin
= m_band
.origin
;
830 m_band
.lastSelectionDestination
= m_band
.destination
;
831 m_band
.ignoreOldInfo
= false;
834 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
836 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
837 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
838 setIconSize(QSize(iconSize
, iconSize
));
839 m_decorationSize
= QSize(iconSize
, iconSize
);
841 if (m_selectionManager
!= 0) {
842 m_selectionManager
->reset();
848 QPoint
DolphinDetailsView::contentsPos() const
850 // implementation note: the horizonal position is ignored currently, as no
851 // horizontal scrolling is done anyway during a selection
852 const QScrollBar
* scrollbar
= verticalScrollBar();
853 Q_ASSERT(scrollbar
!= 0);
855 const int maxHeight
= maximumViewportSize().height();
856 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
857 const int visibleHeight
= model()->rowCount() + 1 - height
;
858 if (visibleHeight
<= 0) {
862 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
866 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
868 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
870 switch (columnIndex
) {
871 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
872 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
873 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
874 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
875 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
876 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
883 void DolphinDetailsView::resizeColumns()
885 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
886 // around 3 seconds for each (!) resize operation when having > 10000 items).
887 // This gets a problem especially when opening large directories, where several
888 // resize operations are received for showing the currently available items during
889 // loading (the application hangs around 20 seconds when loading > 10000 items).
891 QHeaderView
* headerView
= header();
892 QFontMetrics
fontMetrics(viewport()->font());
894 int columnWidth
[KDirModel::ColumnCount
];
895 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
896 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
897 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
898 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
899 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
900 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
902 int requiredWidth
= 0;
903 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
904 if (!isColumnHidden(i
)) {
905 columnWidth
[i
] += 20; // provide a default gap
906 requiredWidth
+= columnWidth
[i
];
907 headerView
->resizeSection(i
, columnWidth
[i
]);
911 // resize the name column in a way that the whole available width is used
912 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
914 const int minNameWidth
= 300;
915 if (columnWidth
[KDirModel::Name
] < minNameWidth
) {
916 columnWidth
[KDirModel::Name
] = minNameWidth
;
918 // It might be possible that the name column width can be
919 // decreased without clipping any text. For performance
920 // reasons the exact necessary width for full visible names is
921 // only checked for up to 200 items:
922 const int rowCount
= model()->rowCount();
923 if (rowCount
< 200) {
924 const int nameWidth
= sizeHintForColumn(DolphinModel::Name
);
925 if (nameWidth
+ requiredWidth
<= viewport()->width()) {
926 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
927 } else if (nameWidth
< minNameWidth
) {
928 columnWidth
[KDirModel::Name
] = nameWidth
;
933 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
936 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
938 QRect rect
= visualRect(index
);
939 const KFileItem item
= m_controller
->itemForIndex(index
);
940 if (!item
.isNull()) {
941 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
942 rect
.setWidth(width
);
948 DolphinDetailsView::ElasticBand::ElasticBand() :
952 lastSelectionOrigin(),
953 lastSelectionDestination(),
955 outsideNearestLeftEdge(0),
956 outsideNearestRightEdge(0),
957 insideNearestLeftEdge(0),
958 insideNearestRightEdge(0)
962 #include "dolphindetailsview.moc"