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
)
53 , m_pressedMouseGlobalPos()
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 const QPointF pressedMousePos
= m_view
->transform().map(m_view
->scene()->views().first()->mapFromGlobal(m_pressedMouseGlobalPos
.toPoint()));
191 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_view
->m_visibleGroups
);
192 while (it
.hasNext()) {
194 KItemListGroupHeader
*groupHeader
= it
.value();
195 const QPointF mappedToGroup
= groupHeader
->mapFromItem(nullptr, pressedMousePos
);
196 if (groupHeader
->contains(mappedToGroup
)) {
197 return it
.key()->index();
203 void KItemListController::setAutoActivationDelay(int delay
)
205 m_autoActivationTimer
->setInterval(delay
);
208 int KItemListController::autoActivationDelay() const
210 return m_autoActivationTimer
->interval();
213 void KItemListController::setSingleClickActivationEnforced(bool singleClick
)
215 m_singleClickActivationEnforced
= singleClick
;
218 bool KItemListController::singleClickActivationEnforced() const
220 return m_singleClickActivationEnforced
;
223 void KItemListController::setSelectionModeEnabled(bool enabled
)
225 m_selectionMode
= enabled
;
228 bool KItemListController::selectionMode() const
230 return m_selectionMode
;
233 bool KItemListController::isSearchAsYouTypeActive() const
235 return m_keyboardManager
->isSearchAsYouTypeActive();
238 bool KItemListController::keyPressEvent(QKeyEvent
*event
)
240 int index
= m_selectionManager
->currentItem();
241 int key
= event
->key();
242 const bool shiftPressed
= event
->modifiers() & Qt::ShiftModifier
;
244 const bool horizontalScrolling
= m_view
->scrollOrientation() == Qt::Horizontal
;
246 // Handle the expanding/collapsing of items
247 // expand / collapse all selected directories
248 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
) && (key
== Qt::Key_Right
|| key
== Qt::Key_Left
)) {
249 const bool expandOrCollapse
= key
== Qt::Key_Right
? true : false;
250 bool shouldReturn
= m_model
->setExpanded(index
, expandOrCollapse
);
252 // edit in reverse to preserve index of the first handled items
253 const auto selectedItems
= m_selectionManager
->selectedItems();
254 for (auto it
= selectedItems
.rbegin(); it
!= selectedItems
.rend(); ++it
) {
255 shouldReturn
|= m_model
->setExpanded(*it
, expandOrCollapse
);
257 m_selectionManager
->setSelected(*it
);
261 // update keyboard anchors
263 m_keyboardAnchorIndex
= selectedItems
.count() > 0 ? qMin(index
, selectedItems
.last()) : index
;
264 m_keyboardAnchorPos
= keyboardAnchorPos(m_keyboardAnchorIndex
);
272 const bool controlPressed
= event
->modifiers() & Qt::ControlModifier
;
273 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
274 const bool navigationPressed
= key
== Qt::Key_Home
|| key
== Qt::Key_End
|| key
== Qt::Key_PageUp
|| key
== Qt::Key_PageDown
|| key
== Qt::Key_Up
275 || key
== Qt::Key_Down
|| key
== Qt::Key_Left
|| key
== Qt::Key_Right
;
277 const int itemCount
= m_model
->count();
279 // For horizontal scroll orientation, transform
280 // the arrow keys to simplify the event handling.
281 if (horizontalScrolling
) {
300 if (m_view
->layoutDirection() == Qt::RightToLeft
) {
301 if (horizontalScrolling
) {
302 // swap up and down arrow keys
313 } else if (!m_view
->supportsItemExpanding()) {
314 // swap left and right arrow keys
328 const bool selectSingleItem
= m_selectionBehavior
!= NoSelection
&& itemCount
== 1 && navigationPressed
;
330 if (selectSingleItem
) {
331 const int current
= m_selectionManager
->currentItem();
332 m_selectionManager
->setSelected(current
);
339 m_keyboardAnchorIndex
= index
;
340 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
344 index
= itemCount
- 1;
345 m_keyboardAnchorIndex
= index
;
346 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
351 const int expandedParentsCount
= m_model
->expandedParentsCount(index
);
352 if (expandedParentsCount
== 0) {
355 // Go to the parent of the current item.
358 } while (index
> 0 && m_model
->expandedParentsCount(index
) == expandedParentsCount
);
360 m_keyboardAnchorIndex
= index
;
361 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
366 if (index
< itemCount
- 1) {
368 m_keyboardAnchorIndex
= index
;
369 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
374 updateKeyboardAnchor();
375 if (shiftPressed
&& !m_selectionManager
->isAnchoredSelectionActive() && m_selectionManager
->isSelected(index
)) {
376 m_selectionManager
->beginAnchoredSelection(index
);
378 index
= previousRowIndex(index
);
382 updateKeyboardAnchor();
383 if (shiftPressed
&& !m_selectionManager
->isAnchoredSelectionActive() && m_selectionManager
->isSelected(index
)) {
384 m_selectionManager
->beginAnchoredSelection(index
);
386 index
= nextRowIndex(index
);
390 if (horizontalScrolling
) {
391 // The new current index should correspond to the first item in the current column.
392 int newIndex
= qMax(index
- 1, 0);
393 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() < m_view
->itemRect(index
).topLeft().y()) {
395 newIndex
= qMax(index
- 1, 0);
397 m_keyboardAnchorIndex
= index
;
398 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
400 const qreal currentItemBottom
= m_view
->itemRect(index
).bottomLeft().y();
401 const qreal height
= m_view
->geometry().height();
403 // The new current item should be the first item in the current
404 // column whose itemRect's top coordinate is larger than targetY.
405 const qreal targetY
= currentItemBottom
- height
;
407 updateKeyboardAnchor();
408 int newIndex
= previousRowIndex(index
);
411 updateKeyboardAnchor();
412 newIndex
= previousRowIndex(index
);
413 } while (m_view
->itemRect(newIndex
).topLeft().y() > targetY
&& newIndex
!= index
);
417 case Qt::Key_PageDown
:
418 if (horizontalScrolling
) {
419 // The new current index should correspond to the last item in the current column.
420 int newIndex
= qMin(index
+ 1, m_model
->count() - 1);
421 while (newIndex
!= index
&& m_view
->itemRect(newIndex
).topLeft().y() > m_view
->itemRect(index
).topLeft().y()) {
423 newIndex
= qMin(index
+ 1, m_model
->count() - 1);
425 m_keyboardAnchorIndex
= index
;
426 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
428 const qreal currentItemTop
= m_view
->itemRect(index
).topLeft().y();
429 const qreal height
= m_view
->geometry().height();
431 // The new current item should be the last item in the current
432 // column whose itemRect's bottom coordinate is smaller than targetY.
433 const qreal targetY
= currentItemTop
+ height
;
435 updateKeyboardAnchor();
436 int newIndex
= nextRowIndex(index
);
439 updateKeyboardAnchor();
440 newIndex
= nextRowIndex(index
);
441 } while (m_view
->itemRect(newIndex
).bottomLeft().y() < targetY
&& newIndex
!= index
);
446 case Qt::Key_Return
: {
447 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
448 if (selectedItems
.count() >= 2) {
449 Q_EMIT
itemsActivated(selectedItems
);
450 } else if (selectedItems
.count() == 1) {
451 Q_EMIT
itemActivated(selectedItems
.first());
453 Q_EMIT
itemActivated(index
);
459 if (m_selectionMode
) {
460 Q_EMIT
selectionModeChangeRequested(false);
461 } else if (m_selectionBehavior
!= SingleSelection
) {
462 m_selectionManager
->clearSelection();
464 m_keyboardManager
->cancelSearch();
465 Q_EMIT
escapePressed();
469 if (m_selectionBehavior
== MultiSelection
) {
470 #ifndef QT_NO_ACCESSIBILITY
471 // Move accessible focus to the item that is acted upon, so only the state change of this item is announced and not the whole view.
472 QAccessibleEvent
accessibilityEvent(view(), QAccessible::Focus
);
473 accessibilityEvent
.setChild(index
);
474 QAccessible::updateAccessibility(&accessibilityEvent
);
476 if (controlPressed
) {
477 // Toggle the selection state of the current item.
478 m_selectionManager
->endAnchoredSelection();
479 m_selectionManager
->setSelected(index
, 1, KItemListSelectionManager::Toggle
);
480 m_selectionManager
->beginAnchoredSelection(index
);
483 // Select the current item if it is not selected yet.
484 const int current
= m_selectionManager
->currentItem();
485 if (!m_selectionManager
->isSelected(current
)) {
486 m_selectionManager
->setSelected(current
);
491 Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
493 m_keyboardManager
->addKeys(event
->text());
494 // Make sure unconsumed events get propagated up the chain. #302329
499 if (m_selectionManager
->currentItem() != index
) {
500 switch (m_selectionBehavior
) {
502 m_selectionManager
->setCurrentItem(index
);
505 case SingleSelection
:
506 m_selectionManager
->setCurrentItem(index
);
507 m_selectionManager
->clearSelection();
508 m_selectionManager
->setSelected(index
, 1);
512 if (controlPressed
) {
513 m_selectionManager
->endAnchoredSelection();
516 m_selectionManager
->setCurrentItem(index
);
518 if (!shiftOrControlPressed
) {
519 m_selectionManager
->clearSelection();
520 m_selectionManager
->setSelected(index
, 1);
524 m_selectionManager
->beginAnchoredSelection(index
);
530 if (navigationPressed
) {
531 m_view
->scrollToItem(index
);
536 void KItemListController::slotChangeCurrentItem(const QString
&text
, bool searchFromNextItem
)
538 if (!m_model
|| m_model
->count() == 0) {
542 if (searchFromNextItem
) {
543 const int currentIndex
= m_selectionManager
->currentItem();
544 index
= m_model
->indexForKeyboardSearch(text
, (currentIndex
+ 1) % m_model
->count());
546 index
= m_model
->indexForKeyboardSearch(text
, 0);
549 m_selectionManager
->setCurrentItem(index
);
551 if (m_selectionBehavior
!= NoSelection
) {
552 m_selectionManager
->replaceSelection(index
);
553 m_selectionManager
->beginAnchoredSelection(index
);
556 m_view
->scrollToItem(index
, KItemListView::ViewItemPosition::Beginning
);
560 void KItemListController::slotAutoActivationTimeout()
562 if (!m_model
|| !m_view
) {
566 const int index
= m_autoActivationTimer
->property("index").toInt();
567 if (index
< 0 || index
>= m_model
->count()) {
571 /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
574 * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
575 * then move away before the auto-activation timeout triggers, than the
576 * item still becomes activated/expanded.
578 * See Bug 293200 and 305783
580 if (m_view
->isUnderMouse()) {
581 if (m_view
->supportsItemExpanding() && m_model
->isExpandable(index
)) {
582 const bool expanded
= m_model
->isExpanded(index
);
583 m_model
->setExpanded(index
, !expanded
);
584 } else if (m_autoActivationBehavior
!= ExpansionOnly
) {
585 Q_EMIT
itemActivated(index
);
590 bool KItemListController::inputMethodEvent(QInputMethodEvent
*event
)
596 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
599 m_pressedMouseGlobalPos
= event
->screenPos();
601 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& m_isTouchEvent
) {
609 const QPointF pressedMousePos
= transform
.map(event
->pos());
610 m_pressedIndex
= m_view
->itemAt(pressedMousePos
);
612 const Qt::MouseButtons buttons
= event
->buttons();
614 if (!onPress(event
->pos(), event
->modifiers(), buttons
)) {
622 bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
628 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
629 m_view
->m_tapAndHoldIndicator
->setActive(false);
632 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& !m_dragActionOrRightClick
&& m_isTouchEvent
) {
636 if (m_pressedIndex
.has_value() && !m_view
->rubberBand()->isActive()) {
637 // Check whether a dragging should be started
638 if (event
->buttons() & Qt::LeftButton
) {
639 const auto distance
= (event
->screenPos() - m_pressedMouseGlobalPos
).manhattanLength();
640 if (distance
>= QApplication::startDragDistance()) {
641 if (!m_selectionManager
->isSelected(m_pressedIndex
.value())) {
642 // Always assure that the dragged item gets selected. Usually this is already
643 // done on the mouse-press event, but when using the selection-toggle on a
644 // selected item the dragged item is not selected yet.
645 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
647 // A selected item has been clicked to drag all selected items
648 // -> the selection should not be cleared when the mouse button is released.
649 m_clearSelectionIfItemsAreNotDragged
= false;
652 m_mousePress
= false;
656 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
657 if (rubberBand
->isActive()) {
658 QPointF endPos
= transform
.map(event
->pos());
660 // Update the current item.
661 const std::optional
<int> newCurrent
= m_view
->itemAt(endPos
);
662 if (newCurrent
.has_value()) {
663 // It's expected that the new current index is also the new anchor (bug 163451).
664 m_selectionManager
->endAnchoredSelection();
665 m_selectionManager
->setCurrentItem(newCurrent
.value());
666 m_selectionManager
->beginAnchoredSelection(newCurrent
.value());
669 if (m_view
->scrollOrientation() == Qt::Vertical
) {
670 endPos
.ry() += m_view
->scrollOffset();
672 endPos
.rx() += m_view
->scrollOffset();
674 rubberBand
->setEndPosition(endPos
);
681 bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
683 m_mousePress
= false;
684 m_isTouchEvent
= false;
690 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
691 m_view
->m_tapAndHoldIndicator
->setActive(false);
694 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
695 if (event
->source() == Qt::MouseEventSynthesizedByQt
&& !rubberBand
->isActive() && m_isTouchEvent
) {
699 Q_EMIT
mouseButtonReleased(m_pressedIndex
.value_or(-1), event
->buttons());
701 return onRelease(transform
.map(event
->pos()), event
->modifiers(), event
->button(), false);
704 bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
)
706 const QPointF pos
= transform
.map(event
->pos());
707 const std::optional
<int> index
= m_view
->itemAt(pos
);
709 if (event
->button() & (Qt::ForwardButton
| Qt::BackButton
)) {
710 // "Forward" and "Back" are reserved for quickly navigating through the
711 // history. Double-clicking those buttons should be interpreted as two
712 // separate button presses. We arrive here for the second click, which
713 // we now react to just as we would for a singular click
714 Q_EMIT
mouseButtonPressed(index
.value_or(-1), event
->button());
718 if (!index
.has_value()) {
719 Q_EMIT
doubleClickViewBackground(event
->button());
723 // Expand item if desired - See Bug 295573
724 if (m_mouseDoubleClickAction
!= ActivateItemOnly
) {
725 if (m_view
&& m_model
&& m_view
->supportsItemExpanding() && m_model
->isExpandable(index
.value_or(-1))) {
726 const bool expanded
= m_model
->isExpanded(index
.value());
727 m_model
->setExpanded(index
.value(), !expanded
);
731 if (event
->button() & ~Qt::LeftButton
) {
735 if (m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) {
739 const bool emitItemActivated
= index
.has_value() && index
.value() < m_model
->count() && !m_view
->isAboveExpansionToggle(index
.value(), pos
);
740 if (emitItemActivated
) {
741 if (!QApplication::keyboardModifiers()) {
742 m_selectionManager
->clearSelection(); // The user does not want to manage/manipulate the item currently, only activate it.
744 Q_EMIT
itemActivated(index
.value());
749 bool KItemListController::contextMenuEvent(QContextMenuEvent
*event
)
751 if (event
->reason() == QContextMenuEvent::Keyboard
) {
752 // Emit the signal itemContextMenuRequested() if at least one item is selected.
753 // Otherwise the signal viewContextMenuRequested() will be emitted.
754 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
756 if (selectedItems
.count() >= 2) {
757 const int currentItemIndex
= m_selectionManager
->currentItem();
758 index
= selectedItems
.contains(currentItemIndex
) ? currentItemIndex
: selectedItems
.first();
759 } else if (selectedItems
.count() == 1) {
760 index
= selectedItems
.first();
764 const QRectF contextRect
= m_view
->itemContextRect(index
);
765 const QPointF
pos(m_view
->scene()->views().first()->mapToGlobal(contextRect
.bottomRight().toPoint()));
766 Q_EMIT
itemContextMenuRequested(index
, pos
);
768 Q_EMIT
viewContextMenuRequested(event
->globalPos());
773 const auto pos
= event
->pos();
774 const auto globalPos
= event
->globalPos();
776 if (m_view
->headerBoundaries().contains(pos
)) {
777 Q_EMIT
headerContextMenuRequested(globalPos
);
781 const auto pressedItem
= m_view
->itemAt(pos
);
782 // We only open a context menu for the pressed item if it is selected.
783 // That's because the same click might have de-selected the item or because the press was only in the row of the item but not on it.
784 if (pressedItem
&& m_selectionManager
->selectedItems().contains(pressedItem
.value())) {
785 // The selection rectangle for an item was clicked
786 Q_EMIT
itemContextMenuRequested(pressedItem
.value(), globalPos
);
790 // Remove any hover highlights so the context menu doesn't look like it applies to a row.
791 const auto widgets
= m_view
->visibleItemListWidgets();
792 for (KItemListWidget
*widget
: widgets
) {
793 if (widget
->isHovered()) {
794 widget
->setHovered(false);
795 Q_EMIT
itemUnhovered(widget
->index());
798 Q_EMIT
viewContextMenuRequested(globalPos
);
802 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
807 DragAndDropHelper::clearUrlListMatchesUrlCache();
812 bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
817 m_autoActivationTimer
->stop();
818 m_view
->setAutoScroll(false);
819 m_view
->hideDropIndicator();
821 KItemListWidget
*widget
= hoveredWidget();
823 widget
->setHovered(false);
824 Q_EMIT
itemUnhovered(widget
->index());
829 bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
831 if (!m_model
|| !m_view
) {
835 QUrl hoveredDir
= m_model
->directory();
836 KItemListWidget
*oldHoveredWidget
= hoveredWidget();
838 const QPointF pos
= transform
.map(event
->pos());
839 KItemListWidget
*newHoveredWidget
= widgetForDropPos(pos
);
842 if (oldHoveredWidget
!= newHoveredWidget
) {
843 m_autoActivationTimer
->stop();
845 if (oldHoveredWidget
) {
846 oldHoveredWidget
->setHovered(false);
847 Q_EMIT
itemUnhovered(oldHoveredWidget
->index());
851 if (newHoveredWidget
) {
852 bool droppingBetweenItems
= false;
853 if (m_model
->sortRole().isEmpty()) {
854 // The model supports inserting items between other items.
855 droppingBetweenItems
= (m_view
->showDropIndicator(pos
) >= 0);
858 index
= newHoveredWidget
->index();
860 if (m_model
->isDir(index
)) {
861 hoveredDir
= m_model
->url(index
);
864 if (!droppingBetweenItems
) {
865 // Something has been dragged on an item.
866 m_view
->hideDropIndicator();
867 if (!newHoveredWidget
->isHovered()) {
868 newHoveredWidget
->setHovered(true);
869 Q_EMIT
itemHovered(index
);
872 if (!m_autoActivationTimer
->isActive() && m_autoActivationTimer
->interval() >= 0 && m_model
->canEnterOnHover(index
)) {
873 m_autoActivationTimer
->setProperty("index", index
);
874 m_autoActivationTimer
->start();
875 newHoveredWidget
->startActivateSoonAnimation(m_autoActivationTimer
->remainingTime());
879 m_autoActivationTimer
->stop();
880 if (newHoveredWidget
&& newHoveredWidget
->isHovered()) {
881 newHoveredWidget
->setHovered(false);
882 Q_EMIT
itemUnhovered(index
);
886 m_view
->hideDropIndicator();
889 if (DragAndDropHelper::urlListMatchesUrl(event
->mimeData()->urls(), hoveredDir
)) {
890 event
->setDropAction(Qt::IgnoreAction
);
893 if (m_model
->supportsDropping(index
)) {
894 event
->setDropAction(event
->proposedAction());
897 event
->setDropAction(Qt::IgnoreAction
);
904 bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
)
910 m_autoActivationTimer
->stop();
911 m_view
->setAutoScroll(false);
913 const QPointF pos
= transform
.map(event
->pos());
915 int dropAboveIndex
= -1;
916 if (m_model
->sortRole().isEmpty()) {
917 // The model supports inserting of items between other items.
918 dropAboveIndex
= m_view
->showDropIndicator(pos
);
921 if (dropAboveIndex
>= 0) {
922 // Something has been dropped between two items.
923 m_view
->hideDropIndicator();
924 Q_EMIT
aboveItemDropEvent(dropAboveIndex
, event
);
925 } else if (!event
->mimeData()->hasFormat(m_model
->blacklistItemDropEventMimeType())) {
926 // Something has been dropped on an item or on an empty part of the view.
927 const KItemListWidget
*receivingWidget
= widgetForDropPos(pos
);
928 if (receivingWidget
) {
929 Q_EMIT
itemDropEvent(receivingWidget
->index(), event
);
931 Q_EMIT
itemDropEvent(-1, event
);
935 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropEnd
);
936 QAccessible::updateAccessibility(&accessibilityEvent
);
941 bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
)
948 bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
)
951 if (!m_model
|| !m_view
) {
955 // We identify the widget whose expansionArea had been hovered before this hoverMoveEvent() triggered.
956 // we can't use hoveredWidget() here (it handles the icon+text rect, not the expansion rect)
957 // like hoveredWidget(), we find the hovered widget for the expansion rect
958 const auto visibleItemListWidgets
= m_view
->visibleItemListWidgets();
959 const auto oldHoveredExpansionWidgetIterator
= std::find_if(visibleItemListWidgets
.begin(), visibleItemListWidgets
.end(), [](auto &widget
) {
960 return widget
->expansionAreaHovered();
962 const auto oldHoveredExpansionWidget
=
963 oldHoveredExpansionWidgetIterator
== visibleItemListWidgets
.end() ? std::nullopt
: std::make_optional(*oldHoveredExpansionWidgetIterator
);
965 const auto unhoverOldHoveredWidget
= [&]() {
966 if (auto oldHoveredWidget
= hoveredWidget(); oldHoveredWidget
) {
967 // handle the text+icon one
968 oldHoveredWidget
->setHovered(false);
969 Q_EMIT
itemUnhovered(oldHoveredWidget
->index());
973 const auto unhoverOldExpansionWidget
= [&]() {
974 if (oldHoveredExpansionWidget
) {
975 // then the expansion toggle
976 (*oldHoveredExpansionWidget
)->setExpansionAreaHovered(false);
980 const QPointF pos
= transform
.map(event
->pos());
981 if (KItemListWidget
*newHoveredWidget
= widgetForPos(pos
); newHoveredWidget
) {
982 // something got hovered, work out which part and set hover for the appropriate widget
983 const auto mappedPos
= newHoveredWidget
->mapFromItem(m_view
, pos
);
984 const bool isOnExpansionToggle
= newHoveredWidget
->expansionToggleRect().contains(mappedPos
);
986 if (isOnExpansionToggle
) {
987 // make sure we unhover the old one first if old!=new
988 if (oldHoveredExpansionWidget
&& *oldHoveredExpansionWidget
!= newHoveredWidget
) {
989 (*oldHoveredExpansionWidget
)->setExpansionAreaHovered(false);
991 // 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)
992 unhoverOldHoveredWidget();
994 newHoveredWidget
->setExpansionAreaHovered(true);
996 // make sure we unhover the old one first if old!=new
997 auto oldHoveredWidget
= hoveredWidget();
998 if (oldHoveredWidget
&& oldHoveredWidget
!= newHoveredWidget
) {
999 oldHoveredWidget
->setHovered(false);
1000 Q_EMIT
itemUnhovered(oldHoveredWidget
->index());
1002 // 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)
1003 unhoverOldExpansionWidget();
1005 const bool isOverIconAndText
= newHoveredWidget
->iconRect().contains(mappedPos
) || newHoveredWidget
->textRect().contains(mappedPos
);
1006 const bool hasMultipleSelection
= m_selectionManager
->selectedItems().count() > 1;
1008 if (hasMultipleSelection
&& !isOverIconAndText
) {
1009 // In case we have multiple selections, clicking on any row will deselect the selection.
1010 // So, as a visual cue for signalling that clicking anywhere won't select, but clear current highlights,
1011 // we disable hover of the *row*(i.e. blank space to the right of the icon+text)
1013 // (no-op in this branch for masked hover)
1015 newHoveredWidget
->setHoverPosition(mappedPos
);
1016 if (oldHoveredWidget
!= newHoveredWidget
) {
1017 newHoveredWidget
->setHovered(true);
1018 Q_EMIT
itemHovered(newHoveredWidget
->index());
1023 // unhover any currently hovered expansion and text+icon widgets
1024 unhoverOldHoveredWidget();
1025 unhoverOldExpansionWidget();
1030 bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
)
1035 m_mousePress
= false;
1036 m_isTouchEvent
= false;
1038 if (!m_model
|| !m_view
) {
1042 const auto widgets
= m_view
->visibleItemListWidgets();
1043 for (KItemListWidget
*widget
: widgets
) {
1044 if (widget
->isHovered()) {
1045 widget
->setHovered(false);
1046 Q_EMIT
itemUnhovered(widget
->index());
1052 bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent
*event
, const QTransform
&transform
)
1059 bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent
*event
, const QTransform
&transform
)
1066 bool KItemListController::gestureEvent(QGestureEvent
*event
, const QTransform
&transform
)
1072 //you can touch on different views at the same time, but only one QWidget gets a mousePressEvent
1073 //we use this to get the right QWidget
1074 //the only exception is a tap gesture with state GestureStarted, we need to reset some variable
1075 if (!m_mousePress
) {
1076 if (QGesture
*tap
= event
->gesture(Qt::TapGesture
)) {
1077 QTapGesture
*tapGesture
= static_cast<QTapGesture
*>(tap
);
1078 if (tapGesture
->state() == Qt::GestureStarted
) {
1079 tapTriggered(tapGesture
, transform
);
1085 bool accepted
= false;
1087 if (QGesture
*tap
= event
->gesture(Qt::TapGesture
)) {
1088 tapTriggered(static_cast<QTapGesture
*>(tap
), transform
);
1091 if (event
->gesture(Qt::TapAndHoldGesture
)) {
1092 tapAndHoldTriggered(event
, transform
);
1095 if (event
->gesture(Qt::PinchGesture
)) {
1096 pinchTriggered(event
, transform
);
1099 if (event
->gesture(m_swipeGesture
)) {
1100 swipeTriggered(event
, transform
);
1103 if (event
->gesture(m_twoFingerTapGesture
)) {
1104 twoFingerTapTriggered(event
, transform
);
1110 bool KItemListController::touchBeginEvent(QTouchEvent
*event
, const QTransform
&transform
)
1115 m_isTouchEvent
= true;
1119 void KItemListController::tapTriggered(QTapGesture
*tap
, const QTransform
&transform
)
1121 static bool scrollerWasActive
= false;
1123 if (tap
->state() == Qt::GestureStarted
) {
1124 m_dragActionOrRightClick
= false;
1125 m_isSwipeGesture
= false;
1126 m_pinchGestureInProgress
= false;
1127 scrollerWasActive
= m_scrollerIsScrolling
;
1130 if (tap
->state() == Qt::GestureFinished
) {
1131 m_mousePress
= false;
1133 //if at the moment of the gesture start the QScroller was active, the user made the tap
1134 //to stop the QScroller and not to tap on an item
1135 if (scrollerWasActive
) {
1139 if (m_view
->m_tapAndHoldIndicator
->isActive()) {
1140 m_view
->m_tapAndHoldIndicator
->setActive(false);
1143 const QPointF pressedMousePos
= transform
.map(tap
->position());
1144 m_pressedIndex
= m_view
->itemAt(pressedMousePos
);
1145 if (m_dragActionOrRightClick
) {
1146 m_dragActionOrRightClick
= false;
1148 onPress(tap
->position().toPoint(), Qt::NoModifier
, Qt::LeftButton
);
1149 onRelease(transform
.map(tap
->position()), Qt::NoModifier
, Qt::LeftButton
, true);
1151 m_isTouchEvent
= false;
1155 void KItemListController::tapAndHoldTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1157 //the Qt TabAndHold gesture is triggerable with a mouse click, we don't want this
1158 if (!m_isTouchEvent
) {
1162 const QTapAndHoldGesture
*tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
1163 if (tap
->state() == Qt::GestureFinished
) {
1164 //if a pinch gesture is in progress we don't want a TabAndHold gesture
1165 if (m_pinchGestureInProgress
) {
1168 const QPointF pressedMousePos
= transform
.map(event
->mapToGraphicsScene(tap
->position()));
1169 m_pressedIndex
= m_view
->itemAt(pressedMousePos
);
1170 if (m_pressedIndex
.has_value()) {
1171 if (!m_selectionManager
->isSelected(m_pressedIndex
.value())) {
1172 m_selectionManager
->clearSelection();
1173 m_selectionManager
->setSelected(m_pressedIndex
.value());
1175 if (!m_selectionMode
) {
1176 Q_EMIT
selectionModeChangeRequested(true);
1179 m_selectionManager
->clearSelection();
1183 Q_EMIT
scrollerStop();
1185 m_view
->m_tapAndHoldIndicator
->setStartPosition(pressedMousePos
);
1186 m_view
->m_tapAndHoldIndicator
->setActive(true);
1188 m_dragActionOrRightClick
= true;
1192 void KItemListController::pinchTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1196 const QPinchGesture
*pinch
= static_cast<QPinchGesture
*>(event
->gesture(Qt::PinchGesture
));
1197 const qreal sensitivityModifier
= 0.2;
1198 static qreal counter
= 0;
1200 if (pinch
->state() == Qt::GestureStarted
) {
1201 m_pinchGestureInProgress
= true;
1204 if (pinch
->state() == Qt::GestureUpdated
) {
1205 //if a swipe gesture was recognized or in progress, we don't want a pinch gesture to change the zoom
1206 if (m_isSwipeGesture
) {
1209 counter
= counter
+ (pinch
->scaleFactor() - 1);
1210 if (counter
>= sensitivityModifier
) {
1211 Q_EMIT
increaseZoom();
1213 } else if (counter
<= -sensitivityModifier
) {
1214 Q_EMIT
decreaseZoom();
1220 void KItemListController::swipeTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1224 const KTwoFingerSwipe
*swipe
= static_cast<KTwoFingerSwipe
*>(event
->gesture(m_swipeGesture
));
1229 if (swipe
->state() == Qt::GestureStarted
) {
1230 m_isSwipeGesture
= true;
1233 if (swipe
->state() == Qt::GestureCanceled
) {
1234 m_isSwipeGesture
= false;
1237 if (swipe
->state() == Qt::GestureFinished
) {
1238 Q_EMIT
scrollerStop();
1240 if (swipe
->swipeAngle() <= 20 || swipe
->swipeAngle() >= 340) {
1241 Q_EMIT
mouseButtonPressed(m_pressedIndex
.value_or(-1), Qt::BackButton
);
1242 } else if (swipe
->swipeAngle() <= 200 && swipe
->swipeAngle() >= 160) {
1243 Q_EMIT
mouseButtonPressed(m_pressedIndex
.value_or(-1), Qt::ForwardButton
);
1244 } else if (swipe
->swipeAngle() <= 110 && swipe
->swipeAngle() >= 60) {
1247 m_isSwipeGesture
= true;
1251 void KItemListController::twoFingerTapTriggered(QGestureEvent
*event
, const QTransform
&transform
)
1253 const KTwoFingerTap
*twoTap
= static_cast<KTwoFingerTap
*>(event
->gesture(m_twoFingerTapGesture
));
1259 if (twoTap
->state() == Qt::GestureStarted
) {
1260 const QPointF pressedMousePos
= transform
.map(twoTap
->pos());
1261 m_pressedIndex
= m_view
->itemAt(pressedMousePos
);
1262 if (m_pressedIndex
.has_value()) {
1263 onPress(twoTap
->pos().toPoint(), Qt::ControlModifier
, Qt::LeftButton
);
1264 onRelease(transform
.map(twoTap
->pos()), Qt::ControlModifier
, Qt::LeftButton
, false);
1269 bool KItemListController::processEvent(QEvent
*event
, const QTransform
&transform
)
1275 switch (event
->type()) {
1276 case QEvent::KeyPress
:
1277 return keyPressEvent(static_cast<QKeyEvent
*>(event
));
1278 case QEvent::InputMethod
:
1279 return inputMethodEvent(static_cast<QInputMethodEvent
*>(event
));
1280 case QEvent::GraphicsSceneMousePress
:
1281 return mousePressEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1282 case QEvent::GraphicsSceneMouseMove
:
1283 return mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1284 case QEvent::GraphicsSceneMouseRelease
:
1285 return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1286 case QEvent::GraphicsSceneMouseDoubleClick
:
1287 return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent
*>(event
), QTransform());
1288 case QEvent::ContextMenu
:
1289 return contextMenuEvent(static_cast<QContextMenuEvent
*>(event
));
1290 case QEvent::GraphicsSceneWheel
:
1291 return wheelEvent(static_cast<QGraphicsSceneWheelEvent
*>(event
), QTransform());
1292 case QEvent::GraphicsSceneDragEnter
:
1293 return dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1294 case QEvent::GraphicsSceneDragLeave
:
1295 return dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1296 case QEvent::GraphicsSceneDragMove
:
1297 return dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1298 case QEvent::GraphicsSceneDrop
:
1299 return dropEvent(static_cast<QGraphicsSceneDragDropEvent
*>(event
), QTransform());
1300 case QEvent::GraphicsSceneHoverEnter
:
1301 return hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1302 case QEvent::GraphicsSceneHoverMove
:
1303 return hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1304 case QEvent::GraphicsSceneHoverLeave
:
1305 return hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent
*>(event
), QTransform());
1306 case QEvent::GraphicsSceneResize
:
1307 return resizeEvent(static_cast<QGraphicsSceneResizeEvent
*>(event
), transform
);
1308 case QEvent::Gesture
:
1309 return gestureEvent(static_cast<QGestureEvent
*>(event
), transform
);
1310 case QEvent::TouchBegin
:
1311 return touchBeginEvent(static_cast<QTouchEvent
*>(event
), transform
);
1319 void KItemListController::slotViewScrollOffsetChanged(qreal current
, qreal previous
)
1325 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1326 if (rubberBand
->isActive()) {
1327 const qreal diff
= current
- previous
;
1328 // TODO: Ideally just QCursor::pos() should be used as
1329 // new end-position but it seems there is no easy way
1330 // to have something like QWidget::mapFromGlobal() for QGraphicsWidget
1331 // (... or I just missed an easy way to do the mapping)
1332 QPointF endPos
= rubberBand
->endPosition();
1333 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1334 endPos
.ry() += diff
;
1336 endPos
.rx() += diff
;
1339 rubberBand
->setEndPosition(endPos
);
1343 void KItemListController::slotRubberBandChanged()
1345 if (!m_view
|| !m_model
|| m_model
->count() <= 0) {
1349 const KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1350 const QPointF startPos
= rubberBand
->startPosition();
1351 const QPointF endPos
= rubberBand
->endPosition();
1352 QRectF rubberBandRect
= QRectF(startPos
, endPos
).normalized();
1354 const bool scrollVertical
= (m_view
->scrollOrientation() == Qt::Vertical
);
1355 if (scrollVertical
) {
1356 rubberBandRect
.translate(0, -m_view
->scrollOffset());
1358 rubberBandRect
.translate(-m_view
->scrollOffset(), 0);
1361 if (!m_oldSelection
.isEmpty()) {
1362 // Clear the old selection that was available before the rubberband has
1363 // been activated in case if no Shift- or Control-key are pressed
1364 const bool shiftOrControlPressed
= QApplication::keyboardModifiers() & Qt::ShiftModifier
|| QApplication::keyboardModifiers() & Qt::ControlModifier
;
1365 if (!shiftOrControlPressed
&& !m_selectionMode
) {
1366 m_oldSelection
.clear();
1370 KItemSet selectedItems
;
1372 // Select all visible items that intersect with the rubberband
1373 const auto widgets
= m_view
->visibleItemListWidgets();
1374 for (const KItemListWidget
*widget
: widgets
) {
1375 const int index
= widget
->index();
1377 const QRectF widgetRect
= m_view
->itemRect(index
);
1378 if (widgetRect
.intersects(rubberBandRect
)) {
1379 // Select the full row intersecting with the rubberband rectangle
1380 const QRectF selectionRect
= widget
->selectionRect().translated(widgetRect
.topLeft());
1381 const QRectF iconRect
= widget
->iconRect().translated(widgetRect
.topLeft());
1382 if (selectionRect
.intersects(rubberBandRect
) || iconRect
.intersects(rubberBandRect
)) {
1383 selectedItems
.insert(index
);
1388 // Select all invisible items that intersect with the rubberband. Instead of
1389 // iterating all items only the area which might be touched by the rubberband
1391 const bool increaseIndex
= scrollVertical
? startPos
.y() > endPos
.y() : startPos
.x() > endPos
.x();
1393 int index
= increaseIndex
? m_view
->lastVisibleIndex() + 1 : m_view
->firstVisibleIndex() - 1;
1394 bool selectionFinished
= false;
1396 const QRectF widgetRect
= m_view
->itemRect(index
);
1397 if (widgetRect
.intersects(rubberBandRect
)) {
1398 selectedItems
.insert(index
);
1401 if (increaseIndex
) {
1403 selectionFinished
= (index
>= m_model
->count()) || (scrollVertical
&& widgetRect
.top() > rubberBandRect
.bottom())
1404 || (!scrollVertical
&& widgetRect
.left() > rubberBandRect
.right());
1407 selectionFinished
= (index
< 0) || (scrollVertical
&& widgetRect
.bottom() < rubberBandRect
.top())
1408 || (!scrollVertical
&& widgetRect
.right() < rubberBandRect
.left());
1410 } while (!selectionFinished
);
1412 if ((QApplication::keyboardModifiers() & Qt::ControlModifier
) || m_selectionMode
) {
1413 // If Control is pressed, the selection state of all items in the rubberband is toggled.
1414 // Therefore, the new selection contains:
1415 // 1. All previously selected items which are not inside the rubberband, and
1416 // 2. all items inside the rubberband which have not been selected previously.
1417 m_selectionManager
->setSelectedItems(m_oldSelection
^ selectedItems
);
1419 m_selectionManager
->setSelectedItems(selectedItems
+ m_oldSelection
);
1423 void KItemListController::startDragging()
1425 if (!m_view
|| !m_model
) {
1429 const KItemSet selectedItems
= m_selectionManager
->selectedItems();
1430 if (selectedItems
.isEmpty()) {
1434 QMimeData
*data
= m_model
->createMimeData(selectedItems
);
1438 KUrlMimeData::exportUrlsToPortal(data
);
1440 // The created drag object will be owned and deleted
1441 // by QApplication::activeWindow().
1442 QDrag
*drag
= new QDrag(QApplication::activeWindow());
1443 drag
->setMimeData(data
);
1445 const QPixmap pixmap
= m_view
->createDragPixmap(selectedItems
);
1446 drag
->setPixmap(pixmap
);
1448 const QPoint
hotSpot((pixmap
.width() / pixmap
.devicePixelRatio()) / 2, 0);
1449 drag
->setHotSpot(hotSpot
);
1451 drag
->exec(Qt::MoveAction
| Qt::CopyAction
| Qt::LinkAction
, Qt::CopyAction
);
1453 QAccessibleEvent
accessibilityEvent(view(), QAccessible::DragDropStart
);
1454 QAccessible::updateAccessibility(&accessibilityEvent
);
1457 KItemListWidget
*KItemListController::hoveredWidget() const
1461 const auto widgets
= m_view
->visibleItemListWidgets();
1462 for (KItemListWidget
*widget
: widgets
) {
1463 if (widget
->isHovered()) {
1471 KItemListWidget
*KItemListController::widgetForPos(const QPointF
&pos
) const
1475 if (m_view
->headerBoundaries().contains(pos
)) {
1479 const auto widgets
= m_view
->visibleItemListWidgets();
1480 for (KItemListWidget
*widget
: widgets
) {
1481 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1482 if (widget
->contains(mappedPos
) || widget
->selectionRect().contains(mappedPos
)) {
1490 KItemListWidget
*KItemListController::widgetForDropPos(const QPointF
&pos
) const
1494 if (m_view
->headerBoundaries().contains(pos
)) {
1498 const auto widgets
= m_view
->visibleItemListWidgets();
1499 for (KItemListWidget
*widget
: widgets
) {
1500 const QPointF mappedPos
= widget
->mapFromItem(m_view
, pos
);
1501 if (widget
->contains(mappedPos
)) {
1509 void KItemListController::updateKeyboardAnchor()
1511 const bool validAnchor
=
1512 m_keyboardAnchorIndex
>= 0 && m_keyboardAnchorIndex
< m_model
->count() && keyboardAnchorPos(m_keyboardAnchorIndex
) == m_keyboardAnchorPos
;
1514 const int index
= m_selectionManager
->currentItem();
1515 m_keyboardAnchorIndex
= index
;
1516 m_keyboardAnchorPos
= keyboardAnchorPos(index
);
1520 int KItemListController::nextRowIndex(int index
) const
1522 if (m_keyboardAnchorIndex
< 0) {
1526 const int maxIndex
= m_model
->count() - 1;
1527 if (index
== maxIndex
) {
1531 const bool reversed
= m_view
->layoutDirection() == Qt::RightToLeft
&& m_view
->scrollOrientation() == Qt::Vertical
;
1533 // Calculate the index of the last column inside the row of the current index
1534 int lastColumnIndex
= index
;
1535 while ((!reversed
&& keyboardAnchorPos(lastColumnIndex
+ 1) > keyboardAnchorPos(lastColumnIndex
))
1536 || (reversed
&& keyboardAnchorPos(lastColumnIndex
+ 1) < keyboardAnchorPos(lastColumnIndex
))) {
1538 if (lastColumnIndex
>= maxIndex
) {
1543 // Based on the last column index go to the next row and calculate the nearest index
1544 // that is below the current index
1545 int nextRowIndex
= lastColumnIndex
+ 1;
1546 int searchIndex
= nextRowIndex
;
1547 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(nextRowIndex
));
1548 while (searchIndex
< maxIndex
1549 && ((!reversed
&& keyboardAnchorPos(searchIndex
+ 1) > keyboardAnchorPos(searchIndex
))
1550 || (reversed
&& keyboardAnchorPos(searchIndex
+ 1) < keyboardAnchorPos(searchIndex
)))) {
1552 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1553 if (searchDiff
< minDiff
) {
1554 minDiff
= searchDiff
;
1555 nextRowIndex
= searchIndex
;
1559 return nextRowIndex
;
1562 int KItemListController::previousRowIndex(int index
) const
1564 if (m_keyboardAnchorIndex
< 0 || index
== 0) {
1568 const bool reversed
= m_view
->layoutDirection() == Qt::RightToLeft
&& m_view
->scrollOrientation() == Qt::Vertical
;
1570 // Calculate the index of the first column inside the row of the current index
1571 int firstColumnIndex
= index
;
1572 while ((!reversed
&& keyboardAnchorPos(firstColumnIndex
- 1) < keyboardAnchorPos(firstColumnIndex
))
1573 || (reversed
&& keyboardAnchorPos(firstColumnIndex
- 1) > keyboardAnchorPos(firstColumnIndex
))) {
1575 if (firstColumnIndex
<= 0) {
1580 // Based on the first column index go to the previous row and calculate the nearest index
1581 // that is above the current index
1582 int previousRowIndex
= firstColumnIndex
- 1;
1583 int searchIndex
= previousRowIndex
;
1584 qreal minDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(previousRowIndex
));
1585 while (searchIndex
> 0
1586 && ((!reversed
&& keyboardAnchorPos(searchIndex
- 1) < keyboardAnchorPos(searchIndex
))
1587 || (reversed
&& keyboardAnchorPos(searchIndex
- 1) > keyboardAnchorPos(searchIndex
)))) {
1589 const qreal searchDiff
= qAbs(m_keyboardAnchorPos
- keyboardAnchorPos(searchIndex
));
1590 if (searchDiff
< minDiff
) {
1591 minDiff
= searchDiff
;
1592 previousRowIndex
= searchIndex
;
1596 return previousRowIndex
;
1599 qreal
KItemListController::keyboardAnchorPos(int index
) const
1601 const QRectF itemRect
= m_view
->itemRect(index
);
1602 if (!itemRect
.isEmpty()) {
1603 return (m_view
->scrollOrientation() == Qt::Vertical
) ? itemRect
.x() : itemRect
.y();
1609 void KItemListController::updateExtendedSelectionRegion()
1612 const bool extend
= (m_selectionBehavior
!= MultiSelection
);
1613 KItemListStyleOption option
= m_view
->styleOption();
1614 if (option
.extendedSelectionRegion
!= extend
) {
1615 option
.extendedSelectionRegion
= extend
;
1616 m_view
->setStyleOption(option
);
1621 bool KItemListController::onPress(const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
)
1623 Q_EMIT
mouseButtonPressed(m_pressedIndex
.value_or(-1), buttons
);
1625 if (buttons
& (Qt::BackButton
| Qt::ForwardButton
)) {
1626 // Do not select items when clicking the back/forward buttons, see
1627 // https://bugs.kde.org/show_bug.cgi?id=327412.
1631 const QPointF pressedMousePos
= m_view
->transform().map(pos
);
1633 if (m_view
->isAboveExpansionToggle(m_pressedIndex
.value_or(-1), pressedMousePos
)) {
1634 m_selectionManager
->endAnchoredSelection();
1635 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1636 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1640 m_selectionTogglePressed
= m_view
->isAboveSelectionToggle(m_pressedIndex
.value_or(-1), pressedMousePos
);
1641 if (m_selectionTogglePressed
) {
1642 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1643 // The previous anchored selection has been finished already in
1644 // KItemListSelectionManager::setSelected(). We can safely change
1645 // the current item and start a new anchored selection now.
1646 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1647 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1651 const bool shiftPressed
= modifiers
& Qt::ShiftModifier
;
1652 const bool controlPressed
= (modifiers
& Qt::ControlModifier
) || m_selectionMode
; // Keeping selectionMode similar to pressing control will hopefully
1653 // simplify the overall logic and possibilities both for users and devs.
1654 const bool leftClick
= buttons
& Qt::LeftButton
;
1655 const bool rightClick
= buttons
& Qt::RightButton
;
1657 // The previous selection is cleared if either
1658 // 1. The selection mode is SingleSelection, or
1659 // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
1660 // a) Shift or Control are pressed.
1661 // b) The clicked item is selected already. In that case, the user might want to:
1662 // - start dragging multiple items, or
1663 // - open the context menu and perform an action for all selected items.
1664 const bool shiftOrControlPressed
= shiftPressed
|| controlPressed
;
1665 const bool pressedItemAlreadySelected
= m_pressedIndex
.has_value() && m_selectionManager
->isSelected(m_pressedIndex
.value());
1666 const bool clearSelection
= m_selectionBehavior
== SingleSelection
|| (!shiftOrControlPressed
&& !pressedItemAlreadySelected
);
1668 // When this method returns false, a rubberBand selection is created using KItemListController::startRubberBand via the caller.
1669 if (clearSelection
) {
1670 const int selectedItemsCount
= m_selectionManager
->selectedItems().count();
1671 m_selectionManager
->clearSelection();
1672 // clear and bail when we got an existing multi-selection
1673 if (selectedItemsCount
> 1 && m_pressedIndex
.has_value()) {
1674 const auto row
= m_view
->m_visibleItems
.value(m_pressedIndex
.value());
1675 const auto mappedPos
= row
->mapFromItem(m_view
, pos
);
1676 if (pressedItemAlreadySelected
|| row
->iconRect().contains(mappedPos
) || row
->textRect().contains(mappedPos
)) {
1677 // we are indeed inside the text/icon rect, keep m_pressedIndex what it is
1678 // and short-circuit for single-click activation (it will then propagate to onRelease and activate the item)
1679 // or we just keep going for double-click activation
1680 if (m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
) {
1681 if (!pressedItemAlreadySelected
) {
1682 // An unselected item was clicked directly while deselecting multiple other items so we mark it "current".
1683 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1684 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1686 // We select the item here because this press is not meant to directly activate the item.
1687 // We do not want to select items unless the user wants to edit them.
1688 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1691 return true; // event handled, don't create rubber band
1694 // we're not inside the text/icon rect, as we've already cleared the selection
1695 // we can just stop here and make sure handlers down the line (i.e. onRelease) don't activate
1696 m_pressedIndex
.reset();
1697 // we don't stop event propagation and proceed to create a rubber band and let onRelease
1698 // decide (based on m_pressedIndex) whether we're in a drag (drag => new rubber band, click => don't select the item)
1702 } else if (pressedItemAlreadySelected
&& !shiftOrControlPressed
&& leftClick
) {
1703 // The user might want to start dragging multiple items, but if he clicks the item
1704 // in order to trigger it instead, the other selected items must be deselected.
1705 // However, we do not know yet what the user is going to do.
1706 // -> remember that the user pressed an item which had been selected already and
1707 // clear the selection in mouseReleaseEvent(), unless the items are dragged.
1708 m_clearSelectionIfItemsAreNotDragged
= true;
1710 if (m_selectionManager
->selectedItems().count() == 1 && m_view
->isAboveText(m_pressedIndex
.value_or(-1), pressedMousePos
)) {
1711 Q_EMIT
selectedItemTextPressed(m_pressedIndex
.value_or(-1));
1715 if (!shiftPressed
) {
1716 // Finish the anchored selection before the current index is changed
1717 m_selectionManager
->endAnchoredSelection();
1721 // Stop rubber band from persisting after right-clicks
1722 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1723 if (rubberBand
->isActive()) {
1724 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1725 rubberBand
->setActive(false);
1726 m_view
->setAutoScroll(false);
1729 if (!m_pressedIndex
.has_value()) {
1730 // We have a right-click in an empty region, don't create rubber band.
1735 if (m_pressedIndex
.has_value()) {
1736 // The hover highlight area of an item is being pressed.
1737 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
1738 const bool hitTargetIsRowEmptyRegion
= !row
->contains(row
->mapFromItem(m_view
, pos
));
1739 // again, when this method returns false, a rubberBand selection is created as the event is not consumed;
1740 // createRubberBand here tells us whether to return true or false.
1741 bool createRubberBand
= (hitTargetIsRowEmptyRegion
&& m_selectionManager
->selectedItems().isEmpty());
1743 if (rightClick
&& hitTargetIsRowEmptyRegion
) {
1744 // We have a right click outside the icon and text rect but within the hover highlight area.
1745 // We don't want items to get selected through this, so we return now.
1749 m_selectionManager
->setCurrentItem(m_pressedIndex
.value());
1751 switch (m_selectionBehavior
) {
1755 case SingleSelection
:
1756 if (!leftClick
|| shiftOrControlPressed
1757 || (!m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) && !m_singleClickActivationEnforced
)) {
1758 m_selectionManager
->setSelected(m_pressedIndex
.value());
1762 case MultiSelection
:
1763 if (controlPressed
&& !shiftPressed
&& leftClick
) {
1764 // A left mouse button press is happening on an item while control is pressed. This either means a user wants to:
1765 // - toggle the selection of item(s) or
1766 // - they want to begin a drag on the item(s) to copy them.
1767 // We rule out the latter, if the item is not clicked directly and was unselected previously.
1768 const auto row
= m_view
->m_visibleItems
.value(m_pressedIndex
.value());
1769 const auto mappedPos
= row
->mapFromItem(m_view
, pos
);
1770 if (!row
->iconRect().contains(mappedPos
) && !row
->textRect().contains(mappedPos
) && !pressedItemAlreadySelected
) {
1771 createRubberBand
= true;
1773 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1774 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1775 createRubberBand
= false; // multi selection, don't propagate any further
1776 // This will be the start of an item drag-to-copy operation if the user now moves the mouse before releasing the mouse button.
1778 } else if (!shiftPressed
|| !m_selectionManager
->isAnchoredSelectionActive()) {
1779 // Select the pressed item and start a new anchored selection
1780 if (!leftClick
|| shiftOrControlPressed
1781 || (!m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) && !m_singleClickActivationEnforced
)) {
1782 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Select
);
1784 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1793 return !createRubberBand
;
1799 bool KItemListController::onRelease(const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
)
1801 const QPointF pressedMousePos
= pos
;
1802 const bool isAboveSelectionToggle
= m_view
->isAboveSelectionToggle(m_pressedIndex
.value_or(-1), pressedMousePos
);
1803 if (isAboveSelectionToggle
) {
1804 m_selectionTogglePressed
= false;
1808 if (!isAboveSelectionToggle
&& m_selectionTogglePressed
) {
1809 m_selectionManager
->setSelected(m_pressedIndex
.value_or(-1), 1, KItemListSelectionManager::Toggle
);
1810 m_selectionTogglePressed
= false;
1814 const bool controlPressed
= modifiers
& Qt::ControlModifier
;
1815 const bool shiftOrControlPressed
= modifiers
& Qt::ShiftModifier
|| controlPressed
;
1817 const std::optional
<int> index
= m_view
->itemAt(pos
);
1819 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1820 bool rubberBandRelease
= false;
1821 if (rubberBand
->isActive()) {
1822 disconnect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1823 rubberBand
->setActive(false);
1824 m_oldSelection
.clear();
1825 m_view
->setAutoScroll(false);
1826 rubberBandRelease
= true;
1827 // We check for actual rubber band drag here: if delta between start and end is less than drag threshold,
1828 // then we have a single click on one of the rows
1829 if ((rubberBand
->endPosition() - rubberBand
->startPosition()).manhattanLength() < QApplication::startDragDistance()) {
1830 rubberBandRelease
= false; // since we're only selecting, unmark rubber band release flag
1831 // m_pressedIndex will have no value if we came from a multi-selection clearing onPress
1832 // in that case, we don't select anything
1833 if (index
.has_value() && m_pressedIndex
.has_value()) {
1834 if (controlPressed
&& m_selectionBehavior
== MultiSelection
) {
1835 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Toggle
);
1837 m_selectionManager
->setSelected(index
.value());
1839 if (!m_selectionManager
->isAnchoredSelectionActive()) {
1840 m_selectionManager
->beginAnchoredSelection(index
.value());
1846 if (index
.has_value() && index
== m_pressedIndex
) {
1847 // The release event is done above the same item as the press event
1849 if (m_clearSelectionIfItemsAreNotDragged
) {
1850 // A selected item has been clicked, but no drag operation has been started
1851 // -> clear the rest of the selection.
1852 m_selectionManager
->clearSelection();
1853 m_selectionManager
->setSelected(m_pressedIndex
.value(), 1, KItemListSelectionManager::Select
);
1854 m_selectionManager
->beginAnchoredSelection(m_pressedIndex
.value());
1857 if (buttons
& Qt::LeftButton
) {
1858 bool emitItemActivated
= true;
1859 if (m_view
->isAboveExpansionToggle(index
.value(), pos
)) {
1860 const bool expanded
= m_model
->isExpanded(index
.value());
1861 m_model
->setExpanded(index
.value(), !expanded
);
1863 Q_EMIT
itemExpansionToggleClicked(index
.value());
1864 emitItemActivated
= false;
1865 } else if (shiftOrControlPressed
&& m_selectionBehavior
!= SingleSelection
) {
1866 // The mouse click should only update the selection, not trigger the item, except when
1867 // we are in single selection mode
1868 emitItemActivated
= false;
1870 const bool singleClickActivation
= m_view
->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick
) || m_singleClickActivationEnforced
;
1871 if (!singleClickActivation
) {
1872 emitItemActivated
= touch
&& !m_selectionMode
;
1874 // activate on single click only if we didn't come from a rubber band release
1875 emitItemActivated
= !rubberBandRelease
;
1878 if (emitItemActivated
) {
1879 Q_EMIT
itemActivated(index
.value());
1881 } else if (buttons
& Qt::MiddleButton
) {
1882 Q_EMIT
itemMiddleClicked(index
.value());
1886 m_pressedMouseGlobalPos
= QPointF();
1887 m_pressedIndex
= std::nullopt
;
1888 m_clearSelectionIfItemsAreNotDragged
= false;
1892 void KItemListController::startRubberBand()
1894 if (m_selectionBehavior
== MultiSelection
) {
1895 QPoint startPos
= m_view
->transform().map(m_view
->scene()->views().first()->mapFromGlobal(m_pressedMouseGlobalPos
.toPoint()));
1896 if (m_view
->scrollOrientation() == Qt::Vertical
) {
1897 startPos
.ry() += m_view
->scrollOffset();
1899 startPos
.rx() += m_view
->scrollOffset();
1902 m_oldSelection
= m_selectionManager
->selectedItems();
1903 KItemListRubberBand
*rubberBand
= m_view
->rubberBand();
1904 rubberBand
->setStartPosition(startPos
);
1905 rubberBand
->setEndPosition(startPos
);
1906 rubberBand
->setActive(true);
1907 connect(rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListController::slotRubberBandChanged
);
1908 m_view
->setAutoScroll(true);
1912 void KItemListController::slotStateChanged(QScroller::State newState
)
1914 if (newState
== QScroller::Scrolling
) {
1915 m_scrollerIsScrolling
= true;
1916 } else if (newState
== QScroller::Inactive
) {
1917 m_scrollerIsScrolling
= false;
1921 #include "moc_kitemlistcontroller.cpp"