diff options
| author | Tomasz Kot <[email protected]> | 2025-11-28 16:11:00 +0100 |
|---|---|---|
| committer | Méven Car <[email protected]> | 2026-01-21 09:01:28 +0000 |
| commit | 843d10ae2ab6a7f2c6f69b63fa278fdd6162254c (patch) | |
| tree | ab8a57d633493ead07320e8113e268126b96c5b4 | |
| parent | d04dc824270891b7e3d617b5bb7b5ed668388fd2 (diff) | |
Add keyboard anchor assignments to mouse events
The mouse events need to modify the keyboard anchor assignments as well,
because selecting an item with a mouse and then navigating with keyboard
wouldn't follow the same selection.
BUG: 508609
| -rw-r--r-- | src/kitemviews/kitemlistcontroller.cpp | 12 | ||||
| -rw-r--r-- | src/tests/kitemlistcontrollertest.cpp | 72 |
2 files changed, 84 insertions, 0 deletions
diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index d889deb4e..8723ba4d9 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -534,6 +534,8 @@ void KItemListController::slotChangeCurrentItem(const QString &text, bool search } m_selectionManager->setCurrentItem(index); + m_keyboardAnchorIndex = index; + m_keyboardAnchorPos = keyboardAnchorPos(index); if (m_selectionBehavior != NoSelection) { if (!m_selectionMode) { // Don't clear the selection in selection mode. @@ -654,6 +656,8 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent *event, const m_selectionManager->endAnchoredSelection(); m_selectionManager->setCurrentItem(newCurrent.value()); m_selectionManager->beginAnchoredSelection(newCurrent.value()); + m_keyboardAnchorIndex = newCurrent.value(); + m_keyboardAnchorPos = keyboardAnchorPos(newCurrent.value()); } if (m_view->scrollOrientation() == Qt::Vertical) { @@ -1625,6 +1629,8 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier m_selectionManager->endAnchoredSelection(); m_selectionManager->setCurrentItem(m_pressedIndex.value()); m_selectionManager->beginAnchoredSelection(m_pressedIndex.value()); + m_keyboardAnchorIndex = m_pressedIndex.value(); + m_keyboardAnchorPos = keyboardAnchorPos(m_pressedIndex.value()); return true; } @@ -1636,6 +1642,8 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier // the current item and start a new anchored selection now. m_selectionManager->setCurrentItem(m_pressedIndex.value()); m_selectionManager->beginAnchoredSelection(m_pressedIndex.value()); + m_keyboardAnchorIndex = m_pressedIndex.value(); + m_keyboardAnchorPos = keyboardAnchorPos(m_pressedIndex.value()); return true; } @@ -1675,6 +1683,8 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle); m_selectionManager->setCurrentItem(m_pressedIndex.value()); m_selectionManager->beginAnchoredSelection(m_pressedIndex.value()); + m_keyboardAnchorIndex = m_pressedIndex.value(); + m_keyboardAnchorPos = keyboardAnchorPos(m_pressedIndex.value()); } if (leftClick) { row->setPressed(true); @@ -1743,6 +1753,8 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier } m_selectionManager->setCurrentItem(m_pressedIndex.value()); + m_keyboardAnchorIndex = m_pressedIndex.value(); + m_keyboardAnchorPos = keyboardAnchorPos(m_pressedIndex.value()); switch (m_selectionBehavior) { case NoSelection: diff --git a/src/tests/kitemlistcontrollertest.cpp b/src/tests/kitemlistcontrollertest.cpp index 8fd0f6158..a38e7803d 100644 --- a/src/tests/kitemlistcontrollertest.cpp +++ b/src/tests/kitemlistcontrollertest.cpp @@ -78,6 +78,7 @@ private Q_SLOTS: void testKeyboardNavigationSingleSelectionNoSelection_data(); void testKeyboardNavigationSingleSelectionNoSelection(); void testMouseClickActivation(); + void testKeyboardNavigationAfterMouseSelection(); private: /** @@ -85,6 +86,7 @@ private: * by changing the geometry of the container. */ void adjustGeometryForColumnCount(int count); + void simulateMouseClickOnItem(int index); private: KFileItemListView *m_view; @@ -1151,6 +1153,76 @@ void KItemListControllerTest::testMouseClickActivation() m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick); } +/** + * This function simulates a mouse click on an item under a given index. + */ +void KItemListControllerTest::simulateMouseClickOnItem(int index) +{ + const QPointF pos = m_view->itemContextRect(index).center(); + QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress); + mousePressEvent.setPos(pos); + mousePressEvent.setButton(Qt::LeftButton); + mousePressEvent.setButtons(Qt::LeftButton); + + QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease); + mouseReleaseEvent.setPos(pos); + mouseReleaseEvent.setButton(Qt::LeftButton); + mouseReleaseEvent.setButtons(Qt::NoButton); + + m_view->event(&mousePressEvent); + m_view->event(&mouseReleaseEvent); +} + +void KItemListControllerTest::testKeyboardNavigationAfterMouseSelection() +{ + QApplication::setLayoutDirection(Qt::LeftToRight); + m_view->setLayoutDirection(Qt::LeftToRight); + m_view->setItemLayout(KFileItemListView::IconsLayout); + m_model->setGroupedSorting(false); + + adjustGeometryForColumnCount(3); + QCOMPARE(m_view->m_layouter->m_columnCount, 3); + + m_view->setScrollOffset(0); + QCOMPARE(m_view->firstVisibleIndex(), 0); + + simulateMouseClickOnItem(0); + QCOMPARE(m_selectionManager->currentItem(), 0); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 3); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 6); + + simulateMouseClickOnItem(1); + QCOMPARE(m_selectionManager->currentItem(), 1); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 4); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 7); + + simulateMouseClickOnItem(2); + QCOMPARE(m_selectionManager->currentItem(), 2); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 5); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 8); + + simulateMouseClickOnItem(10); + QCOMPARE(m_selectionManager->currentItem(), 10); + + QTest::keyClick(m_container, Qt::Key_Up); + QCOMPARE(m_selectionManager->currentItem(), 7); + + QTest::keyClick(m_container, Qt::Key_Up); + QCOMPARE(m_selectionManager->currentItem(), 4); +} + void KItemListControllerTest::adjustGeometryForColumnCount(int count) { const QSize size = m_view->itemSize().toSize(); |
