2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
5 * Based on the Itemviews NG project from Trolltech Labs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #include "kitemlistcontroller.h"
12 #include "kitemlistselectionmanager.h"
13 #include "kitemlistview.h"
14 #include "private/kitemlistkeyboardsearchmanager.h"
15 #include "private/kitemlistrubberband.h"
16 #include "views/draganddrophelper.h"
18 #include <KTwoFingerSwipe>
19 #include <KTwoFingerTap>
20 #include <KUrlMimeData>
22 #include <QAccessible>
23 #include <QApplication>
26 #include <QGraphicsScene>
27 #include <QGraphicsSceneEvent>
28 #include <QGraphicsView>
31 #include <QTouchEvent>
33 KItemListController::KItemListController(KItemModelBase
*model
, KItemListView
*view
, QObject
*parent
)
35 , m_singleClickActivationEnforced(false)
36 , m_selectionMode(false)
37 , m_selectionTogglePressed(false)
38 , m_clearSelectionIfItemsAreNotDragged(false)
39 , m_isSwipeGesture(false)
40 , m_dragActionOrRightClick(false)
41 , m_scrollerIsScrolling(false)
42 , m_pinchGestureInProgress(false)
44 , m_isTouchEvent(false)
45 , m_selectionBehavior(NoSelection
)
46 , m_autoActivationBehavior(ActivationAndExpansion
)
47 , m_mouseDoubleClickAction(ActivateItemOnly
)
50 , m_selectionManager(new KItemListSelectionManager(this))
51 , m_keyboardManager(new KItemListKeyboardSearchManager(this))
52 , m_pressedIndex(std::nullopt
)
54 , m_autoActivationTimer(nullptr)
55 , m_swipeGesture(Qt::CustomGesture
)
56 , m_twoFingerTapGesture(Qt::CustomGesture
)
58 , m_keyboardAnchorIndex(-1)
59 , m_keyboardAnchorPos(0)
61 connect(m_keyboardManager
, &KItemListKeyboardSearchManager::changeCurrentItem
, this, &KItemListController::slotChangeCurrentItem
);
62 connect(m_selectionManager
, &KItemListSelectionManager::currentChanged
, m_keyboardManager
, &KItemListKeyboardSearchManager::slotCurrentChanged
);
63 connect(m_selectionManager
, &KItemListSelectionManager::selectionChanged
, m_keyboardManager
, &KItemListKeyboardSearchManager::slotSelectionChanged
);
65 m_autoActivationTimer
= new QTimer(this);
66 m_autoActivationTimer
->setSingleShot(true);
67 m_autoActivationTimer
->setInterval(-1);
68 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &KItemListController::slotAutoActivationTimeout
);
73 m_swipeGesture
= QGestureRecognizer::registerRecognizer(new KTwoFingerSwipeRecognizer());
74 m_twoFingerTapGesture
= QGestureRecognizer::registerRecognizer(new KTwoFingerTapRecognizer());
75 view
->grabGesture(m_swipeGesture
);
76 view
->grabGesture(m_twoFingerTapGesture
);
77 view
->grabGesture(Qt::TapGesture
);
78 view
->grabGesture(Qt::TapAndHoldGesture
);
79 view
->grabGesture(Qt::PinchGesture
);
82 KItemListController::~KItemListController()
91 void KItemListController::setModel(KItemModelBase
*model
)
93 if (m_model
== model
) {
97 KItemModelBase
*oldModel
= m_model
;
99 oldModel
->deleteLater();
104 m_model
->setParent(this);
108 m_view
->setModel(m_model
);
111 m_selectionManager
->setModel(m_model
);
113 Q_EMIT
modelChanged(m_model
, oldModel
);
116 KItemModelBase
*KItemListController::model() const
121 KItemListSelectionManager
*KItemListController::selectionManager() const
123 return m_selectionManager
;
126 void KItemListController::setView(KItemListView
*view
)
128 if (m_view
== view
) {
132 KItemListView
*oldView
= m_view
;
134 disconnect(oldView
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
135 oldView
->deleteLater();
141 m_view
->setParent(this);
142 m_view
->setController(this);
143 m_view
->setModel(m_model
);
144 connect(m_view
, &KItemListView::scrollOffsetChanged
, this, &KItemListController::slotViewScrollOffsetChanged
);
145 updateExtendedSelectionRegion();
148 Q_EMIT
viewChanged(m_view
, oldView
);
151 KItemListView
*KItemListController::view() const
156 void KItemListController::setSelectionBehavior(SelectionBehavior behavior
)
158 m_selectionBehavior
= behavior
;
159 updateExtendedSelectionRegion();
162 KItemListController::SelectionBehavior
KItemListController::selectionBehavior() const
164 return m_selectionBehavior
;
167 void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior
)
169 m_autoActivationBehavior
= behavior
;
172 KItemListController::AutoActivationBehavior
KItemListController::autoActivationBehavior() const
174 return m_autoActivationBehavior
;
177 void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action
)
179 m_mouseDoubleClickAction
= action
;
182 KItemListController::MouseDoubleClickAction
KItemListController::mouseDoubleClickAction() const
184 return m_mouseDoubleClickAction
;
187 int KItemListController::indexCloseToMousePressedPosition() const
189 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
190 while (it
.hasNext()) {
192 KItemListGroupHeader
*groupHeader
= it
.value();
193 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, m_pressedMousePos
);
194 if (groupHeader
->contains(mappedToGroup
)) {
195 return it
.key()->index();
201 void KItemListController::setAutoActivationDelay(int delay
)
203 m_autoActivationTimer
->setInterval(delay
);
206 int KItemListController::autoActivationDelay() const
208 return m_autoActivationTimer
->interval();
211 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
213 m_singleClickActivationEnforced
= singleClick
;
216 bool KItemListController::singleClickActivationEnforced() const
218 return m_singleClickActivationEnforced
;
221 void KItemListController::setSelectionModeEnabled(bool enabled
)
223 m_selectionMode
= enabled
;
226 bool KItemListController::selectionMode() const
228 return m_selectionMode
;
231 bool KItemListController::isSearchAsYouTypeActive() const
233 return m_keyboardManager
->isSearchAsYouTypeActive();
236 bool KItemListController::keyPressEvent(QKeyEvent
*event
)
238 int index
= m_selectionManager
->currentItem();
239 int key
= event
->key();
241 // Handle the expanding/collapsing of items
242 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
243 if (key
== Qt::Key_Right
) {
244 if (m_model
->setExpanded(index
, true)) {
247 } else if (key
== Qt::Key_Left
) {
248 if (m_model
->setExpanded(index
, false)) {
254 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
255 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
256 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
257 const bool navigationPressed
= key
== Qt::Key_Home
|| key
== Qt::Key_End
|| key
== Qt::Key_PageUp
|| key
== Qt::Key_PageDown
|| key
== Qt::Key_Up
258 || key
== Qt::Key_Down
|| key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
260 const int itemCount
= m_model
->count();
262 // For horizontal scroll orientation, transform
263 // the arrow keys to simplify the event handling.
264 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
283 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
285 if (selectSingleItem
) {
286 const int current
= m_selectionManager
->currentItem();
287 m_selectionManager
->setSelected(current
);
294 m_keyboardAnchorIndex
= index
;
295 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
299 index
= itemCount
- 1;
300 m_keyboardAnchorIndex
= index
;
301 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
306 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
307 if (expandedParentsCount
== 0) {
310 // Go to the parent of the current item.
313 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
315 m_keyboardAnchorIndex
= index
;
316 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
321 if (index
< itemCount
- 1) {
323 m_keyboardAnchorIndex
= index
;
324 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
329 updateKeyboardAnchor();
330 index
= previousRowIndex(index
);
334 updateKeyboardAnchor();
335 index
= nextRowIndex(index
);
339 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
340 // The new current index should correspond to the first item in the current column.
341 int newIndex
= qMax(index
- 1, 0);
342 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
344 newIndex
= qMax(index
- 1, 0);
346 m_keyboardAnchorIndex
= index
;
347 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
349 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
350 const qreal height
= m_view
->geometry().height();
352 // The new current item should be the first item in the current
353 // column whose itemRect's top coordinate is larger than targetY.
354 const qreal targetY
= currentItemBottom
- height
;
356 updateKeyboardAnchor();
357 int newIndex
= previousRowIndex(index
);
360 updateKeyboardAnchor();
361 newIndex
= previousRowIndex(index
);
362 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
366 case Qt::Key_PageDown
:
367 if (m_view
->scrollOrientation() == Qt::Horizontal
) {
368 // The new current index should correspond to the last item in the current column.
369 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
370 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
372 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
374 m_keyboardAnchorIndex
= index
;
375 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
377 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
378 const qreal height
= m_view
->geometry().height();
380 // The new current item should be the last item in the current
381 // column whose itemRect's bottom coordinate is smaller than targetY.
382 const qreal targetY
= currentItemTop
+ height
;
384 updateKeyboardAnchor();
385 int newIndex
= nextRowIndex(index
);
388 updateKeyboardAnchor();
389 newIndex
= nextRowIndex(index
);
390 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
395 case Qt::Key_Return
: {
396 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
397 if (selectedItems
.count() >= 2) {
398 Q_EMIT
itemsActivated(selectedItems
);
399 } else if (selectedItems
.count() == 1) {
400 Q_EMIT
itemActivated(selectedItems
.first());
402 Q_EMIT
itemActivated(index
);
408 // Emit the signal itemContextMenuRequested() in case if at least one
409 // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
410 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
412 if (selectedItems
.count() >= 2) {
413 const int currentItemIndex
= m_selectionManager
->currentItem();
414 index
= selectedItems
.contains(currentItemIndex
) ? currentItemIndex
: selectedItems
.first();
415 } else if (selectedItems
.count() == 1) {
416 index
= selectedItems
.first();
420 const QRectF contextRect
= m_view
->itemContextRect(index
);
421 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
422 Q_EMIT
itemContextMenuRequested(index
, pos
);
424 Q_EMIT
viewContextMenuRequested(QCursor::pos());
430 if (m_selectionMode
) {
431 Q_EMIT
selectionModeChangeRequested(false);
432 } else if (m_selectionBehavior
!= SingleSelection
) {
433 m_selectionManager
->clearSelection();
435 m_keyboardManager
->cancelSearch();
436 Q_EMIT
escapePressed();
440 if (m_selectionBehavior
== MultiSelection
) {
441 if (controlPressed
) {
442 // Toggle the selection state of the current item.
443 m_selectionManager
->endAnchoredSelection();
444 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
445 m_selectionManager
->beginAnchoredSelection(index
);
448 // Select the current item if it is not selected yet.
449 const int current
= m_selectionManager
->currentItem();
450 if (!m_selectionManager
->isSelected(current
)) {
451 m_selectionManager
->setSelected(current
);
456 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
458 m_keyboardManager
->addKeys(event
->text());
459 // Make sure unconsumed events get propagated up the chain. #302329
464 if (m_selectionManager
->currentItem() != index
) {
465 switch (m_selectionBehavior
) {
467 m_selectionManager
->setCurrentItem(index
);
470 case SingleSelection
:
471 m_selectionManager
->setCurrentItem(index
);
472 m_selectionManager
->clearSelection();
473 m_selectionManager
->setSelected(index
, 1);
477 if (controlPressed
) {
478 m_selectionManager
->endAnchoredSelection();
481 m_selectionManager
->setCurrentItem(index
);
483 if (!shiftOrControlPressed
) {
484 m_selectionManager
->clearSelection();
485 m_selectionManager
->setSelected(index
, 1);
489 m_selectionManager
->beginAnchoredSelection(index
);
495 if (navigationPressed
) {
496 m_view
->scrollToItem(index
);
501 void KItemListController::slotChangeCurrentItem(const QString
&text
, bool searchFromNextItem
)
503 if (!m_model
|| m_model
->count() == 0) {
507 if (searchFromNextItem
) {
508 const int currentIndex
= m_selectionManager
->currentItem();
509 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
511 index
= m_model
->indexForKeyboardSearch(text
, 0);
514 m_selectionManager
->setCurrentItem(index
);
516 if (m_selectionBehavior
!= NoSelection
) {
517 m_selectionManager
->replaceSelection(index
);
518 m_selectionManager
->beginAnchoredSelection(index
);
521 m_view
->scrollToItem(index
);
525 void KItemListController::slotAutoActivationTimeout()
527 if (!m_model
|| !m_view
) {
531 const int index
= m_autoActivationTimer
->property("index").toInt();
532 if (index
< 0 || index
>= m_model
->count()) {
536 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
539 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
540 * then move away before the auto-activation timeout triggers, than the
541 * item still becomes activated/expanded.
543 * See Bug 293200 and 305783
545 if (m_view
->isUnderMouse()) {
546 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
547 const bool expanded
= m_model
->isExpanded(index
);
548 m_model
->setExpanded(index
, !expanded
);
549 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
550 Q_EMIT
itemActivated(index
);
555 bool KItemListController::inputMethodEvent(QInputMethodEvent
*event
)
561 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
565 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& m_isTouchEvent
) {
573 m_pressedMousePos
= transform
.map(event
->pos());
574 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
576 const Qt::MouseButtons buttons
= event
->buttons();
578 if (!onPress(event
->screenPos(), event
->pos(), event
->modifiers(), buttons
)) {
586 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
592 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
593 m_view
->m_tapAndHoldIndicator
->setActive(false);
596 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& !m_dragActionOrRightClick
&& m_isTouchEvent
) {
600 const QPointF pos
= transform
.map(event
->pos());
602 if (m_pressedIndex
.has_value() && !m_view
->rubberBand()->isActive()) {
603 // Check whether a dragging should be started
604 if (event
->buttons() & Qt::LeftButton
) {
605 if ((pos
- m_pressedMousePos
).manhattanLength() >= QApplication::startDragDistance()) {
606 if (!m_selectionManager
->isSelected(m_pressedIndex
.value())) {
607 // Always assure that the dragged item gets selected. Usually this is already
608 // done on the mouse-press event, but when using the selection-toggle on a
609 // selected item the dragged item is not selected yet.
610 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
612 // A selected item has been clicked to drag all selected items
613 // -> the selection should not be cleared when the mouse button is released.
614 m_clearSelectionIfItemsAreNotDragged
= false;
617 m_mousePress
= false;
621 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
622 if (rubberBand
->isActive()) {
623 QPointF endPos
= transform
.map(event
->pos());
625 // Update the current item.
626 const std::optional
<int> newCurrent
= m_view
->itemAt(endPos
);
627 if (newCurrent
.has_value()) {
628 // It's expected that the new current index is also the new anchor (bug 163451).
629 m_selectionManager
->endAnchoredSelection();
630 m_selectionManager
->setCurrentItem(newCurrent
.value());
631 m_selectionManager
->beginAnchoredSelection(newCurrent
.value());
634 if (m_view
->scrollOrientation() == Qt::Vertical
) {
635 endPos
.ry() += m_view
->scrollOffset();
636 if (m_view
->itemSize().width() < 0) {
637 // Use a special rubberband for views that have only one column and
638 // expand the rubberband to use the whole width of the view.
639 endPos
.setX(m_view
->size().width());
642 endPos
.rx() += m_view
->scrollOffset();
644 rubberBand
->setEndPosition(endPos
);
651 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
653 m_mousePress
= false;
654 m_isTouchEvent
= false;
660 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
661 m_view
->m_tapAndHoldIndicator
->setActive(false);
664 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
665 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& !rubberBand
->isActive() && m_isTouchEvent
) {
669 Q_EMIT
mouseButtonReleased(m_pressedIndex
.value_or(-1), event
->buttons());
671 return onRelease(transform
.map(event
->pos()), event
->modifiers(), event
->button(), false);
674 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
676 const QPointF pos
= transform
.map(event
->pos());
677 const std::optional
<int> index
= m_view
->itemAt(pos
);
679 // Expand item if desired - See Bug 295573
680 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
681 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
.value_or(-1))) {
682 const bool expanded
= m_model
->isExpanded(index
.value());
683 m_model
->setExpanded(index
.value(), !expanded
);
687 if (event
->button() & Qt::RightButton
) {
688 m_selectionManager
->clearSelection();
689 if (index
.has_value()) {
690 m_selectionManager
->setSelected(index
.value());
691 Q_EMIT
itemContextMenuRequested(index
.value(), event
->screenPos());
693 const QRectF headerBounds
= m_view
->headerBoundaries();
694 if (headerBounds
.contains(event
->pos())) {
695 Q_EMIT
headerContextMenuRequested(event
->screenPos());
697 Q_EMIT
viewContextMenuRequested(event
->screenPos());
703 bool emitItemActivated
= !(m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
)
704 && (event
->button() & Qt::LeftButton
) && index
.has_value() && index
.value() < m_model
->count();
705 if (emitItemActivated
) {
706 Q_EMIT
itemActivated(index
.value());
711 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
716 DragAndDropHelper::clearUrlListMatchesUrlCache();
721 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
726 m_autoActivationTimer
->stop();
727 m_view
->setAutoScroll(false);
728 m_view
->hideDropIndicator();
730 KItemListWidget
*widget
= hoveredWidget();
732 widget
->setHovered(false);
733 Q_EMIT
itemUnhovered(widget
->index());
738 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
740 if (!m_model
|| !m_view
) {
744 QUrl hoveredDir
= m_model
->directory();
745 KItemListWidget
*oldHoveredWidget
= hoveredWidget();
747 const QPointF pos
= transform
.map(event
->pos());
748 KItemListWidget
*newHoveredWidget
= widgetForDropPos(pos
);
751 if (oldHoveredWidget
!= newHoveredWidget
) {
752 m_autoActivationTimer
->stop();
754 if (oldHoveredWidget
) {
755 oldHoveredWidget
->setHovered(false);
756 Q_EMIT
itemUnhovered(oldHoveredWidget
->index());
760 if (newHoveredWidget
) {
761 bool droppingBetweenItems
= false;
762 if (m_model
->sortRole().isEmpty()) {
763 // The model supports inserting items between other items.
764 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
767 index
= newHoveredWidget
->index();
769 if (m_model
->isDir(index
)) {
770 hoveredDir
= m_model
->url(index
);
773 if (!droppingBetweenItems
) {
774 // Something has been dragged on an item.
775 m_view
->hideDropIndicator();
776 if (!newHoveredWidget
->isHovered()) {
777 newHoveredWidget
->setHovered(true);
778 Q_EMIT
itemHovered(index
);
781 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0) {
782 m_autoActivationTimer
->setProperty("index", index
);
783 m_autoActivationTimer
->start();
786 m_autoActivationTimer
->stop();
787 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
788 newHoveredWidget
->setHovered(false);
789 Q_EMIT
itemUnhovered(index
);
793 m_view
->hideDropIndicator();
796 if (DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
)) {
797 event
->setDropAction(Qt::IgnoreAction
);
800 if (m_model
->supportsDropping(index
)) {
801 event
->setDropAction(event
->proposedAction());
804 event
->setDropAction(Qt::IgnoreAction
);
811 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
817 m_autoActivationTimer
->stop();
818 m_view
->setAutoScroll(false);
820 const QPointF pos
= transform
.map(event
->pos());
822 int dropAboveIndex
= -1;
823 if (m_model
->sortRole().isEmpty()) {
824 // The model supports inserting of items between other items.
825 dropAboveIndex
= m_view
->showDropIndicator(pos
);
828 if (dropAboveIndex
>= 0) {
829 // Something has been dropped between two items.
830 m_view
->hideDropIndicator();
831 Q_EMIT
aboveItemDropEvent(dropAboveIndex
, event
);
832 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
833 // Something has been dropped on an item or on an empty part of the view.
834 const KItemListWidget
*receivingWidget
= widgetForDropPos(pos
);
835 if (receivingWidget
) {
836 Q_EMIT
itemDropEvent(receivingWidget
->index(), event
);
838 Q_EMIT
itemDropEvent(-1, event
);
842 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
843 QAccessible::updateAccessibility(&accessibilityEvent
);
848 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
)
855 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
)
858 if (!m_model
|| !m_view
) {
862 // We identify the widget whose expansionArea had been hovered before this hoverMoveEvent() triggered.
863 // we can't use hoveredWidget() here (it handles the icon+text rect, not the expansion rect)
864 // like hoveredWidget(), we find the hovered widget for the expansion rect
865 const auto visibleItemListWidgets
= m_view
->visibleItemListWidgets();
866 const auto oldHoveredExpansionWidgetIterator
= std::find_if(visibleItemListWidgets
.begin(), visibleItemListWidgets
.end(), [](auto &widget
) {
867 return widget
->expansionAreaHovered();
869 const auto oldHoveredExpansionWidget
=
870 oldHoveredExpansionWidgetIterator
== visibleItemListWidgets
.end() ? std::nullopt
: std::make_optional(*oldHoveredExpansionWidgetIterator
);
872 const auto unhoverOldHoveredWidget
= [&]() {
873 if (auto oldHoveredWidget
= hoveredWidget(); oldHoveredWidget
) {
874 // handle the text+icon one
875 oldHoveredWidget
->setHovered(false);
876 Q_EMIT
itemUnhovered(oldHoveredWidget
->index());
880 const auto unhoverOldExpansionWidget
= [&]() {
881 if (oldHoveredExpansionWidget
) {
882 // then the expansion toggle
883 (*oldHoveredExpansionWidget
)->setExpansionAreaHovered(false);
887 const QPointF pos
= transform
.map(event
->pos());
888 if (KItemListWidget
*newHoveredWidget
= widgetForPos(pos
); newHoveredWidget
) {
889 // something got hovered, work out which part and set hover for the appropriate widget
890 const auto mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
891 const bool isOnExpansionToggle
= newHoveredWidget
->expansionToggleRect().contains(mappedPos
);
893 if (isOnExpansionToggle
) {
894 // make sure we unhover the old one first if old!=new
895 if (oldHoveredExpansionWidget
&& *oldHoveredExpansionWidget
!= newHoveredWidget
) {
896 (*oldHoveredExpansionWidget
)->setExpansionAreaHovered(false);
898 // we also unhover any old icon+text hovers, in case the mouse movement from icon+text to expansion toggle is too fast (i.e. newHoveredWidget is never null between the transition)
899 unhoverOldHoveredWidget();
901 newHoveredWidget
->setExpansionAreaHovered(true);
903 // make sure we unhover the old one first if old!=new
904 auto oldHoveredWidget
= hoveredWidget();
905 if (oldHoveredWidget
&& oldHoveredWidget
!= newHoveredWidget
) {
906 oldHoveredWidget
->setHovered(false);
907 Q_EMIT
itemUnhovered(oldHoveredWidget
->index());
909 // we also unhover any old expansion toggle hovers, in case the mouse movement from expansion toggle to icon+text is too fast (i.e. newHoveredWidget is never null between the transition)
910 unhoverOldExpansionWidget();
912 const bool isOverIconAndText
= newHoveredWidget
->iconRect().contains(mappedPos
) || newHoveredWidget
->textRect().contains(mappedPos
);
913 const bool hasMultipleSelection
= m_selectionManager
->selectedItems().count() > 1;
915 if (hasMultipleSelection
&& !isOverIconAndText
) {
916 // In case we have multiple selections, clicking on any row will deselect the selection.
917 // So, as a visual cue for signalling that clicking anywhere won't select, but clear current highlights,
918 // we disable hover of the *row*(i.e. blank space to the right of the icon+text)
920 // (no-op in this branch for masked hover)
922 newHoveredWidget
->setHoverPosition(mappedPos
);
923 if (oldHoveredWidget
!= newHoveredWidget
) {
924 newHoveredWidget
->setHovered(true);
925 Q_EMIT
itemHovered(newHoveredWidget
->index());
930 // unhover any currently hovered expansion and text+icon widgets
931 unhoverOldHoveredWidget();
932 unhoverOldExpansionWidget();
937 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
)
942 m_mousePress
= false;
943 m_isTouchEvent
= false;
945 if (!m_model
|| !m_view
) {
949 const auto widgets
= m_view
->visibleItemListWidgets();
950 for (KItemListWidget
*widget
: widgets
) {
951 if (widget
->isHovered()) {
952 widget
->setHovered(false);
953 Q_EMIT
itemUnhovered(widget
->index());
959 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
*event
, const QTransform
&transform
)
966 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
*event
, const QTransform
&transform
)
973 bool KItemListController::gestureEvent(QGestureEvent
*event
, const QTransform
&transform
)
979 //you can touch on different views at the same time, but only one QWidget gets a mousePressEvent
980 //we use this to get the right QWidget
981 //the only exception is a tap gesture with state GestureStarted, we need to reset some variable
983 if (QGesture
*tap
= event
->gesture(Qt::TapGesture
)) {
984 QTapGesture
*tapGesture
= static_cast<QTapGesture
*>(tap
);
985 if (tapGesture
->state() == Qt::GestureStarted
) {
986 tapTriggered(tapGesture
, transform
);
992 bool accepted
= false;
994 if (QGesture
*tap
= event
->gesture(Qt::TapGesture
)) {
995 tapTriggered(static_cast<QTapGesture
*>(tap
), transform
);
998 if (event
->gesture(Qt::TapAndHoldGesture
)) {
999 tapAndHoldTriggered(event
, transform
);
1002 if (event
->gesture(Qt::PinchGesture
)) {
1003 pinchTriggered(event
, transform
);
1006 if (event
->gesture(m_swipeGesture
)) {
1007 swipeTriggered(event
, transform
);
1010 if (event
->gesture(m_twoFingerTapGesture
)) {
1011 twoFingerTapTriggered(event
, transform
);
1017 bool KItemListController::touchBeginEvent(QTouchEvent
*event
, const QTransform
&transform
)
1022 m_isTouchEvent
= true;
1026 void KItemListController::tapTriggered(QTapGesture
*tap
, const QTransform
&transform
)
1028 static bool scrollerWasActive
= false;
1030 if (tap
->state() == Qt::GestureStarted
) {
1031 m_dragActionOrRightClick
= false;
1032 m_isSwipeGesture
= false;
1033 m_pinchGestureInProgress
= false;
1034 scrollerWasActive
= m_scrollerIsScrolling
;
1037 if (tap
->state() == Qt::GestureFinished
) {
1038 m_mousePress
= false;
1040 //if at the moment of the gesture start the QScroller was active, the user made the tap
1041 //to stop the QScroller and not to tap on an item
1042 if (scrollerWasActive
) {
1046 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
1047 m_view
->m_tapAndHoldIndicator
->setActive(false);
1050 m_pressedMousePos
= transform
.map(tap
->position());
1051 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
1053 if (m_dragActionOrRightClick
) {
1054 m_dragActionOrRightClick
= false;
1056 onPress(tap
->hotSpot().toPoint(), tap
->position().toPoint(), Qt::NoModifier
, Qt::LeftButton
);
1057 onRelease(transform
.map(tap
->position()), Qt::NoModifier
, Qt::LeftButton
, true);
1059 m_isTouchEvent
= false;
1063 void KItemListController::tapAndHoldTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1065 //the Qt TabAndHold gesture is triggerable with a mouse click, we don't want this
1066 if (!m_isTouchEvent
) {
1070 const QTapAndHoldGesture
*tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
1071 if (tap
->state() == Qt::GestureFinished
) {
1072 //if a pinch gesture is in progress we don't want a TabAndHold gesture
1073 if (m_pinchGestureInProgress
) {
1076 m_pressedMousePos
= transform
.map(event
->mapToGraphicsScene(tap
->position()));
1077 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
1079 if (m_pressedIndex
.has_value() && !m_selectionManager
->isSelected(m_pressedIndex
.value())) {
1080 m_selectionManager
->clearSelection();
1081 m_selectionManager
->setSelected(m_pressedIndex
.value());
1082 if (!m_selectionMode
) {
1083 Q_EMIT
selectionModeChangeRequested(true);
1085 } else if (!m_pressedIndex
.has_value()) {
1086 m_selectionManager
->clearSelection();
1090 Q_EMIT
scrollerStop();
1092 m_view
->m_tapAndHoldIndicator
->setStartPosition(m_pressedMousePos
);
1093 m_view
->m_tapAndHoldIndicator
->setActive(true);
1095 m_dragActionOrRightClick
= true;
1099 void KItemListController::pinchTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1103 const QPinchGesture
*pinch
= static_cast<QPinchGesture
*>(event
->gesture(Qt::PinchGesture
));
1104 const qreal sensitivityModifier
= 0.2;
1105 static qreal counter
= 0;
1107 if (pinch
->state() == Qt::GestureStarted
) {
1108 m_pinchGestureInProgress
= true;
1111 if (pinch
->state() == Qt::GestureUpdated
) {
1112 //if a swipe gesture was recognized or in progress, we don't want a pinch gesture to change the zoom
1113 if (m_isSwipeGesture
) {
1116 counter
= counter
+ (pinch
->scaleFactor() - 1);
1117 if (counter
>= sensitivityModifier
) {
1118 Q_EMIT
increaseZoom();
1120 } else if (counter
<= -sensitivityModifier
) {
1121 Q_EMIT
decreaseZoom();
1127 void KItemListController::swipeTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1131 const KTwoFingerSwipe
*swipe
= static_cast<KTwoFingerSwipe
*>(event
->gesture(m_swipeGesture
));
1136 if (swipe
->state() == Qt::GestureStarted
) {
1137 m_isSwipeGesture
= true;
1140 if (swipe
->state() == Qt::GestureCanceled
) {
1141 m_isSwipeGesture
= false;
1144 if (swipe
->state() == Qt::GestureFinished
) {
1145 Q_EMIT
scrollerStop();
1147 if (swipe
->swipeAngle() <= 20 || swipe
->swipeAngle() >= 340) {
1148 Q_EMIT
mouseButtonPressed(m_pressedIndex
.value_or(-1), Qt::BackButton
);
1149 } else if (swipe
->swipeAngle() <= 200 && swipe
->swipeAngle() >= 160) {
1150 Q_EMIT
mouseButtonPressed(m_pressedIndex
.value_or(-1), Qt::ForwardButton
);
1151 } else if (swipe
->swipeAngle() <= 110 && swipe
->swipeAngle() >= 60) {
1154 m_isSwipeGesture
= true;
1158 void KItemListController::twoFingerTapTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1160 const KTwoFingerTap
*twoTap
= static_cast<KTwoFingerTap
*>(event
->gesture(m_twoFingerTapGesture
));
1166 if (twoTap
->state() == Qt::GestureStarted
) {
1167 m_pressedMousePos
= transform
.map(twoTap
->pos());
1168 m_pressedIndex
= m_view
->itemAt(m_pressedMousePos
);
1169 if (m_pressedIndex
.has_value()) {
1170 onPress(twoTap
->screenPos().toPoint(), twoTap
->pos().toPoint(), Qt::ControlModifier
, Qt::LeftButton
);
1171 onRelease(transform
.map(twoTap
->pos()), Qt::ControlModifier
, Qt::LeftButton
, false);
1176 bool KItemListController::processEvent(QEvent
*event
, const QTransform
&transform
)
1182 switch (event
->type()) {
1183 case QEvent::KeyPress
:
1184 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1185 case QEvent::InputMethod
:
1186 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1187 case QEvent::GraphicsSceneMousePress
:
1188 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1189 case QEvent::GraphicsSceneMouseMove
:
1190 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1191 case QEvent::GraphicsSceneMouseRelease
:
1192 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1193 case QEvent::GraphicsSceneMouseDoubleClick
:
1194 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1195 case QEvent::GraphicsSceneWheel
:
1196 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1197 case QEvent::GraphicsSceneDragEnter
:
1198 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1199 case QEvent::GraphicsSceneDragLeave
:
1200 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1201 case QEvent::GraphicsSceneDragMove
:
1202 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1203 case QEvent::GraphicsSceneDrop
:
1204 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1205 case QEvent::GraphicsSceneHoverEnter
:
1206 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1207 case QEvent::GraphicsSceneHoverMove
:
1208 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1209 case QEvent::GraphicsSceneHoverLeave
:
1210 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1211 case QEvent::GraphicsSceneResize
:
1212 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1213 case QEvent::Gesture
:
1214 return gestureEvent(static_cast<QGestureEvent
*>(event
), transform
);
1215 case QEvent::TouchBegin
:
1216 return touchBeginEvent(static_cast<QTouchEvent
*>(event
), transform
);
1224 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1230 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1231 if (rubberBand
->isActive()) {
1232 const qreal diff
= current
- previous
;
1233 // TODO: Ideally just QCursor::pos() should be used as
1234 // new end-position but it seems there is no easy way
1235 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1236 // (... or I just missed an easy way to do the mapping)
1237 QPointF endPos
= rubberBand
->endPosition();
1238 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1239 endPos
.ry() += diff
;
1241 endPos
.rx() += diff
;
1244 rubberBand
->setEndPosition(endPos
);
1248 void KItemListController::slotRubberBandChanged()
1250 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1254 const KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1255 const QPointF startPos
= rubberBand
->startPosition();
1256 const QPointF endPos
= rubberBand
->endPosition();
1257 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1259 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1260 if (scrollVertical
) {
1261 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1263 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1266 if (!m_oldSelection
.isEmpty()) {
1267 // Clear the old selection that was available before the rubberband has
1268 // been activated in case if no Shift- or Control-key are pressed
1269 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
|| QApplication::keyboardModifiers() & Qt::ControlModifier
;
1270 if (!shiftOrControlPressed
&& !m_selectionMode
) {
1271 m_oldSelection
.clear();
1275 KItemSet selectedItems
;
1277 // Select all visible items that intersect with the rubberband
1278 const auto widgets
= m_view
->visibleItemListWidgets();
1279 for (const KItemListWidget
*widget
: widgets
) {
1280 const int index
= widget
->index();
1282 const QRectF widgetRect
= m_view
->itemRect(index
);
1283 if (widgetRect
.intersects(rubberBandRect
)) {
1284 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1285 const QRectF textRect
= widget
->textRect().translated(widgetRect
.topLeft());
1286 if (iconRect
.intersects(rubberBandRect
) || textRect
.intersects(rubberBandRect
)) {
1287 selectedItems
.insert(index
);
1292 // Select all invisible items that intersect with the rubberband. Instead of
1293 // iterating all items only the area which might be touched by the rubberband
1295 const bool increaseIndex
= scrollVertical
? startPos
.y() > endPos
.y() : startPos
.x() > endPos
.x();
1297 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1298 bool selectionFinished
= false;
1300 const QRectF widgetRect
= m_view
->itemRect(index
);
1301 if (widgetRect
.intersects(rubberBandRect
)) {
1302 selectedItems
.insert(index
);
1305 if (increaseIndex
) {
1307 selectionFinished
= (index
>= m_model
->count()) || (scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom())
1308 || (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1311 selectionFinished
= (index
< 0) || (scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top())
1312 || (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1314 } while (!selectionFinished
);
1316 if ((QApplication::keyboardModifiers() & Qt::ControlModifier
) || m_selectionMode
) {
1317 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1318 // Therefore, the new selection contains:
1319 // 1. All previously selected items which are not inside the rubberband, and
1320 // 2. all items inside the rubberband which have not been selected previously.
1321 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1323 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1327 void KItemListController::startDragging()
1329 if (!m_view
|| !m_model
) {
1333 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1334 if (selectedItems
.isEmpty()) {
1338 QMimeData
*data
= m_model
->createMimeData(selectedItems
);
1342 KUrlMimeData::exportUrlsToPortal(data
);
1344 // The created drag object will be owned and deleted
1345 // by QApplication::activeWindow().
1346 QDrag
*drag
= new QDrag(QApplication::activeWindow());
1347 drag
->setMimeData(data
);
1349 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1350 drag
->setPixmap(pixmap
);
1352 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1353 drag
->setHotSpot(hotSpot
);
1355 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1357 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1358 QAccessible::updateAccessibility(&accessibilityEvent
);
1361 KItemListWidget
*KItemListController::hoveredWidget() const
1365 const auto widgets
= m_view
->visibleItemListWidgets();
1366 for (KItemListWidget
*widget
: widgets
) {
1367 if (widget
->isHovered()) {
1375 KItemListWidget
*KItemListController::widgetForPos(const QPointF
&pos
) const
1379 const auto widgets
= m_view
->visibleItemListWidgets();
1380 for (KItemListWidget
*widget
: widgets
) {
1381 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1382 if (widget
->contains(mappedPos
) || widget
->selectionRect().contains(mappedPos
)) {
1390 KItemListWidget
*KItemListController::widgetForDropPos(const QPointF
&pos
) const
1394 const auto widgets
= m_view
->visibleItemListWidgets();
1395 for (KItemListWidget
*widget
: widgets
) {
1396 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1397 if (widget
->contains(mappedPos
)) {
1405 void KItemListController::updateKeyboardAnchor()
1407 const bool validAnchor
=
1408 m_keyboardAnchorIndex
>= 0 && m_keyboardAnchorIndex
< m_model
->count() && keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1410 const int index
= m_selectionManager
->currentItem();
1411 m_keyboardAnchorIndex
= index
;
1412 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1416 int KItemListController::nextRowIndex(int index
) const
1418 if (m_keyboardAnchorIndex
< 0) {
1422 const int maxIndex
= m_model
->count() - 1;
1423 if (index
== maxIndex
) {
1427 // Calculate the index of the last column inside the row of the current index
1428 int lastColumnIndex
= index
;
1429 while (keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
)) {
1431 if (lastColumnIndex
>= maxIndex
) {
1436 // Based on the last column index go to the next row and calculate the nearest index
1437 // that is below the current index
1438 int nextRowIndex
= lastColumnIndex
+ 1;
1439 int searchIndex
= nextRowIndex
;
1440 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1441 while (searchIndex
< maxIndex
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
)) {
1443 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1444 if (searchDiff
< minDiff
) {
1445 minDiff
= searchDiff
;
1446 nextRowIndex
= searchIndex
;
1450 return nextRowIndex
;
1453 int KItemListController::previousRowIndex(int index
) const
1455 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1459 // Calculate the index of the first column inside the row of the current index
1460 int firstColumnIndex
= index
;
1461 while (keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
)) {
1463 if (firstColumnIndex
<= 0) {
1468 // Based on the first column index go to the previous row and calculate the nearest index
1469 // that is above the current index
1470 int previousRowIndex
= firstColumnIndex
- 1;
1471 int searchIndex
= previousRowIndex
;
1472 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1473 while (searchIndex
> 0 && keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
)) {
1475 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1476 if (searchDiff
< minDiff
) {
1477 minDiff
= searchDiff
;
1478 previousRowIndex
= searchIndex
;
1482 return previousRowIndex
;
1485 qreal
KItemListController::keyboardAnchorPos(int index
) const
1487 const QRectF itemRect
= m_view
->itemRect(index
);
1488 if (!itemRect
.isEmpty()) {
1489 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1495 void KItemListController::updateExtendedSelectionRegion()
1498 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1499 KItemListStyleOption option
= m_view
->styleOption();
1500 if (option
.extendedSelectionRegion
!= extend
) {
1501 option
.extendedSelectionRegion
= extend
;
1502 m_view
->setStyleOption(option
);
1507 bool KItemListController::onPress(const QPoint
&screenPos
, const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
)
1509 Q_EMIT
mouseButtonPressed(m_pressedIndex
.value_or(-1), buttons
);
1511 if (buttons
& (Qt::BackButton
| Qt::ForwardButton
)) {
1512 // Do not select items when clicking the back/forward buttons, see
1513 // https://bugs.kde.org/show_bug.cgi?id=327412.
1517 if (m_view
->isAboveExpansionToggle(m_pressedIndex
.value_or(-1), m_pressedMousePos
)) {
1518 m_selectionManager
->endAnchoredSelection();
1519 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1520 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1524 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
.value_or(-1), m_pressedMousePos
);
1525 if (m_selectionTogglePressed
) {
1526 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1527 // The previous anchored selection has been finished already in
1528 // KItemListSelectionManager::setSelected(). We can safely change
1529 // the current item and start a new anchored selection now.
1530 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1531 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1535 const bool shiftPressed
= modifiers
& Qt::ShiftModifier
;
1536 const bool controlPressed
= (modifiers
& Qt::ControlModifier
) || m_selectionMode
; // Keeping selectionMode similar to pressing control will hopefully
1537 // simplify the overall logic and possibilities both for users and devs.
1538 const bool leftClick
= buttons
& Qt::LeftButton
;
1539 const bool rightClick
= buttons
& Qt::RightButton
;
1541 // The previous selection is cleared if either
1542 // 1. The selection mode is SingleSelection, or
1543 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
1544 // a) Shift or Control are pressed.
1545 // b) The clicked item is selected already. In that case, the user might want to:
1546 // - start dragging multiple items, or
1547 // - open the context menu and perform an action for all selected items.
1548 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
1549 const bool pressedItemAlreadySelected
= m_pressedIndex
.has_value() && m_selectionManager
->isSelected(m_pressedIndex
.value());
1550 const bool clearSelection
= m_selectionBehavior
== SingleSelection
|| (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
1552 // When this method returns false, a rubberBand selection is created using KItemListController::startRubberBand via the caller.
1553 if (clearSelection
) {
1554 const int selectedItemsCount
= m_selectionManager
->selectedItems().count();
1555 m_selectionManager
->clearSelection();
1556 // clear and bail when we got an existing multi-selection
1557 if (selectedItemsCount
> 1 && m_pressedIndex
.has_value()) {
1558 const auto row
= m_view
->m_visibleItems
.value(m_pressedIndex
.value());
1559 const auto mappedPos
= row
->mapFromItem(m_view
, pos
);
1560 if (pressedItemAlreadySelected
|| row
->iconRect().contains(mappedPos
) || row
->textRect().contains(mappedPos
)) {
1561 // we are indeed inside the text/icon rect, keep m_pressedIndex what it is
1562 // and short-circuit for single-click activation (it will then propagate to onRelease and activate the item)
1563 // or we just keep going for double-click activation
1564 if (m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) {
1565 if (!pressedItemAlreadySelected
) {
1566 // An unselected item was clicked directly while deselecting multiple other items so we select it.
1567 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1568 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1569 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1571 return true; // event handled, don't create rubber band
1574 // we're not inside the text/icon rect, as we've already cleared the selection
1575 // we can just stop here and make sure handlers down the line (i.e. onRelease) don't activate
1576 m_pressedIndex
.reset();
1577 // we don't stop event propagation and proceed to create a rubber band and let onRelease
1578 // decide (based on m_pressedIndex) whether we're in a drag (drag => new rubber band, click => don't select the item)
1582 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& leftClick
) {
1583 // The user might want to start dragging multiple items, but if he clicks the item
1584 // in order to trigger it instead, the other selected items must be deselected.
1585 // However, we do not know yet what the user is going to do.
1586 // -> remember that the user pressed an item which had been selected already and
1587 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
1588 m_clearSelectionIfItemsAreNotDragged
= true;
1590 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
.value_or(-1), m_pressedMousePos
)) {
1591 Q_EMIT
selectedItemTextPressed(m_pressedIndex
.value_or(-1));
1595 if (!shiftPressed
) {
1596 // Finish the anchored selection before the current index is changed
1597 m_selectionManager
->endAnchoredSelection();
1601 // Do header hit check and short circuit before commencing any state changing effects
1602 if (m_view
->headerBoundaries().contains(pos
)) {
1603 Q_EMIT
headerContextMenuRequested(screenPos
);
1607 // Stop rubber band from persisting after right-clicks
1608 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1609 if (rubberBand
->isActive()) {
1610 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1611 rubberBand
->setActive(false);
1612 m_view
->setAutoScroll(false);
1616 if (m_pressedIndex
.has_value()) {
1617 // The hover highlight area of an item is being pressed.
1618 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1619 const auto row
= m_view
->m_visibleItems
.value(m_pressedIndex
.value()); // anything outside of row.contains() will be the empty region of the row rect
1620 const bool hitTargetIsRowEmptyRegion
= !row
->contains(row
->mapFromItem(m_view
, pos
));
1621 // again, when this method returns false, a rubberBand selection is created as the event is not consumed;
1622 // createRubberBand here tells us whether to return true or false.
1623 bool createRubberBand
= (hitTargetIsRowEmptyRegion
&& m_selectionManager
->selectedItems().isEmpty());
1625 if (rightClick
&& hitTargetIsRowEmptyRegion
) {
1626 // We have a right click outside the icon and text rect but within the hover highlight area
1627 // but it is unclear if this means that a selection rectangle for an item was clicked or the background of the view.
1628 if (m_selectionManager
->selectedItems().contains(m_pressedIndex
.value())) {
1629 // The selection rectangle for an item was clicked
1630 Q_EMIT
itemContextMenuRequested(m_pressedIndex
.value(), screenPos
);
1632 row
->setHovered(false); // Removes the hover highlight so the context menu doesn't look like it applies to the row.
1633 Q_EMIT
viewContextMenuRequested(screenPos
);
1638 switch (m_selectionBehavior
) {
1642 case SingleSelection
:
1643 m_selectionManager
->setSelected(m_pressedIndex
.value());
1646 case MultiSelection
:
1647 if (controlPressed
&& !shiftPressed
&& leftClick
) {
1648 // A left mouse button press is happening on an item while control is pressed. This either means a user wants to:
1649 // - toggle the selection of item(s) or
1650 // - they want to begin a drag on the item(s) to copy them.
1651 // We rule out the latter, if the item is not clicked directly and was unselected previously.
1652 const auto row
= m_view
->m_visibleItems
.value(m_pressedIndex
.value());
1653 const auto mappedPos
= row
->mapFromItem(m_view
, pos
);
1654 if (!row
->iconRect().contains(mappedPos
) && !row
->textRect().contains(mappedPos
) && !pressedItemAlreadySelected
) {
1655 createRubberBand
= true;
1657 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1658 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1659 createRubberBand
= false; // multi selection, don't propagate any further
1660 // This will be the start of an item drag-to-copy operation if the user now moves the mouse before releasing the mouse button.
1662 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
1663 // Select the pressed item and start a new anchored selection
1664 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Select
);
1665 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1675 Q_EMIT
itemContextMenuRequested(m_pressedIndex
.value(), screenPos
);
1677 return !createRubberBand
;
1681 // header right click handling would have been done before this so just normal context
1682 // menu here is fine
1683 Q_EMIT
viewContextMenuRequested(screenPos
);
1690 bool KItemListController::onRelease(const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
)
1692 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
.value_or(-1), m_pressedMousePos
);
1693 if (isAboveSelectionToggle
) {
1694 m_selectionTogglePressed
= false;
1698 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
1699 m_selectionManager
->setSelected(m_pressedIndex
.value_or(-1), 1, KItemListSelectionManager::Toggle
);
1700 m_selectionTogglePressed
= false;
1704 const bool controlPressed
= modifiers
& Qt::ControlModifier
;
1705 const bool shiftOrControlPressed
= modifiers
& Qt::ShiftModifier
|| controlPressed
;
1707 const std::optional
<int> index
= m_view
->itemAt(pos
);
1709 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1710 bool rubberBandRelease
= false;
1711 if (rubberBand
->isActive()) {
1712 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1713 rubberBand
->setActive(false);
1714 m_oldSelection
.clear();
1715 m_view
->setAutoScroll(false);
1716 rubberBandRelease
= true;
1717 // We check for actual rubber band drag here: if delta between start and end is less than drag threshold,
1718 // then we have a single click on one of the rows
1719 if ((rubberBand
->endPosition() - rubberBand
->startPosition()).manhattanLength() < QApplication::startDragDistance()) {
1720 rubberBandRelease
= false; // since we're only selecting, unmark rubber band release flag
1721 // m_pressedIndex will have no value if we came from a multi-selection clearing onPress
1722 // in that case, we don't select anything
1723 if (index
.has_value() && m_pressedIndex
.has_value()) {
1724 if (controlPressed
&& m_selectionBehavior
== MultiSelection
) {
1725 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1727 m_selectionManager
->setSelected(index
.value());
1729 if (!m_selectionManager
->isAnchoredSelectionActive()) {
1730 m_selectionManager
->beginAnchoredSelection(index
.value());
1736 if (index
.has_value() && index
== m_pressedIndex
) {
1737 // The release event is done above the same item as the press event
1739 if (m_clearSelectionIfItemsAreNotDragged
) {
1740 // A selected item has been clicked, but no drag operation has been started
1741 // -> clear the rest of the selection.
1742 m_selectionManager
->clearSelection();
1743 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Select
);
1744 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1747 if (buttons
& Qt::LeftButton
) {
1748 bool emitItemActivated
= true;
1749 if (m_view
->isAboveExpansionToggle(index
.value(), pos
)) {
1750 const bool expanded
= m_model
->isExpanded(index
.value());
1751 m_model
->setExpanded(index
.value(), !expanded
);
1753 Q_EMIT
itemExpansionToggleClicked(index
.value());
1754 emitItemActivated
= false;
1755 } else if (shiftOrControlPressed
&& m_selectionBehavior
!= SingleSelection
) {
1756 // The mouse click should only update the selection, not trigger the item, except when
1757 // we are in single selection mode
1758 emitItemActivated
= false;
1760 const bool singleClickActivation
= m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
;
1761 if (!singleClickActivation
) {
1762 emitItemActivated
= touch
&& !m_selectionMode
;
1764 // activate on single click only if we didn't come from a rubber band release
1765 emitItemActivated
= !rubberBandRelease
;
1768 if (emitItemActivated
) {
1769 Q_EMIT
itemActivated(index
.value());
1771 } else if (buttons
& Qt::MiddleButton
) {
1772 Q_EMIT
itemMiddleClicked(index
.value());
1776 m_pressedMousePos
= QPointF();
1777 m_pressedIndex
= std::nullopt
;
1778 m_clearSelectionIfItemsAreNotDragged
= false;
1782 void KItemListController::startRubberBand()
1784 if (m_selectionBehavior
== MultiSelection
) {
1785 QPointF startPos
= m_pressedMousePos
;
1786 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1787 startPos
.ry() += m_view
->scrollOffset();
1788 if (m_view
->itemSize().width() < 0) {
1789 // Use a special rubberband for views that have only one column and
1790 // expand the rubberband to use the whole width of the view.
1794 startPos
.rx() += m_view
->scrollOffset();
1797 m_oldSelection
= m_selectionManager
->selectedItems();
1798 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1799 rubberBand
->setStartPosition(startPos
);
1800 rubberBand
->setEndPosition(startPos
);
1801 rubberBand
->setActive(true);
1802 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1803 m_view
->setAutoScroll(true);
1807 void KItemListController::slotStateChanged(QScroller::State newState
)
1809 if (newState
== QScroller::Scrolling
) {
1810 m_scrollerIsScrolling
= true;
1811 } else if (newState
== QScroller::Inactive
) {
1812 m_scrollerIsScrolling
= false;