]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistcontroller.cpp
New selection effects
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
index 5abf1830be349f2bd397e302920ffbfb3beef259..fdde48abcdd83056a007aeed7f5cde5606b6135d 100644 (file)
@@ -64,7 +64,7 @@ KItemListController::KItemListController(KItemModelBase *model, KItemListView *v
 
     m_autoActivationTimer = new QTimer(this);
     m_autoActivationTimer->setSingleShot(true);
-    m_autoActivationTimer->setInterval(-1);
+    m_autoActivationTimer->setInterval(750);
     connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout);
 
     setModel(model);
@@ -200,14 +200,14 @@ int KItemListController::indexCloseToMousePressedPosition() const
     return -1;
 }
 
-void KItemListController::setAutoActivationDelay(int delay)
+void KItemListController::setAutoActivationEnabled(bool enabled)
 {
-    m_autoActivationTimer->setInterval(delay);
+    m_autoActivationEnabled = enabled;
 }
 
-int KItemListController::autoActivationDelay() const
+bool KItemListController::isAutoActivationEnabled() const
 {
-    return m_autoActivationTimer->interval();
+    return m_autoActivationEnabled;
 }
 
 void KItemListController::setSingleClickActivationEnforced(bool singleClick)
@@ -241,6 +241,22 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
     int key = event->key();
     const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
 
+    const bool horizontalScrolling = m_view->scrollOrientation() == Qt::Horizontal;
+
+    if (m_view->layoutDirection() == Qt::RightToLeft) {
+        // swap left and right arrow keys
+        switch (key) {
+        case Qt::Key_Left:
+            key = Qt::Key_Right;
+            break;
+        case Qt::Key_Right:
+            key = Qt::Key_Left;
+            break;
+        default:
+            break;
+        }
+    }
+
     // Handle the expanding/collapsing of items
     // expand / collapse all selected directories
     if (m_view->supportsItemExpanding() && m_model->isExpandable(index) && (key == Qt::Key_Right || key == Qt::Key_Left)) {
@@ -268,7 +284,9 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
     }
 
     const bool controlPressed = event->modifiers() & Qt::ControlModifier;
-    const bool shiftOrControlPressed = shiftPressed || controlPressed;
+    if (m_selectionMode && !controlPressed && !shiftPressed && (key == Qt::Key_Enter || key == Qt::Key_Return)) {
+        key = Qt::Key_Space; // In selection mode one moves around with arrow keys and toggles selection with Enter.
+    }
     const bool navigationPressed = key == Qt::Key_Home || key == Qt::Key_End || key == Qt::Key_PageUp || key == Qt::Key_PageDown || key == Qt::Key_Up
         || key == Qt::Key_Down || key == Qt::Key_Left || key == Qt::Key_Right;
 
@@ -276,7 +294,7 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
 
     // For horizontal scroll orientation, transform
     // the arrow keys to simplify the event handling.
-    if (m_view->scrollOrientation() == Qt::Horizontal) {
+    if (horizontalScrolling) {
         switch (key) {
         case Qt::Key_Up:
             key = Qt::Key_Left;
@@ -357,7 +375,7 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
         break;
 
     case Qt::Key_PageUp:
-        if (m_view->scrollOrientation() == Qt::Horizontal) {
+        if (horizontalScrolling) {
             // The new current index should correspond to the first item in the current column.
             int newIndex = qMax(index - 1, 0);
             while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() < m_view->itemRect(index).topLeft().y()) {
@@ -385,7 +403,7 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
         break;
 
     case Qt::Key_PageDown:
-        if (m_view->scrollOrientation() == Qt::Horizontal) {
+        if (horizontalScrolling) {
             // The new current index should correspond to the last item in the current column.
             int newIndex = qMin(index + 1, m_model->count() - 1);
             while (newIndex != index && m_view->itemRect(newIndex).topLeft().y() > m_view->itemRect(index).topLeft().y()) {
@@ -425,28 +443,6 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
         break;
     }
 
-    case Qt::Key_Menu: {
-        // Emit the signal itemContextMenuRequested() in case if at least one
-        // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
-        const KItemSet selectedItems = m_selectionManager->selectedItems();
-        int index = -1;
-        if (selectedItems.count() >= 2) {
-            const int currentItemIndex = m_selectionManager->currentItem();
-            index = selectedItems.contains(currentItemIndex) ? currentItemIndex : selectedItems.first();
-        } else if (selectedItems.count() == 1) {
-            index = selectedItems.first();
-        }
-
-        if (index >= 0) {
-            const QRectF contextRect = m_view->itemContextRect(index);
-            const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint()));
-            Q_EMIT itemContextMenuRequested(index, pos);
-        } else {
-            Q_EMIT viewContextMenuRequested(QCursor::pos());
-        }
-        break;
-    }
-
     case Qt::Key_Escape:
         if (m_selectionMode) {
             Q_EMIT selectionModeChangeRequested(false);
@@ -459,7 +455,7 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
 
     case Qt::Key_Space:
         if (m_selectionBehavior == MultiSelection) {
-            if (controlPressed) {
+            if (controlPressed || m_selectionMode) {
                 // Toggle the selection state of the current item.
                 m_selectionManager->endAnchoredSelection();
                 m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
@@ -495,13 +491,13 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
             break;
 
         case MultiSelection:
-            if (controlPressed) {
+            if (controlPressed || (m_selectionMode && !shiftPressed)) {
                 m_selectionManager->endAnchoredSelection();
             }
 
             m_selectionManager->setCurrentItem(index);
 
-            if (!shiftOrControlPressed) {
+            if (!shiftPressed && !controlPressed && !m_selectionMode) {
                 m_selectionManager->clearSelection();
                 m_selectionManager->setSelected(index, 1);
             }
@@ -525,17 +521,23 @@ void KItemListController::slotChangeCurrentItem(const QString &text, bool search
         return;
     }
     int index;
-    if (searchFromNextItem) {
-        const int currentIndex = m_selectionManager->currentItem();
-        index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
+    // In selection mode, always use the current (underlined) item, or the next item, for search start position.
+    if (m_selectionBehavior == NoSelection || m_selectionMode || m_selectionManager->hasSelection()) {
+        index = m_model->indexForKeyboardSearch(text, searchFromNextItem ? m_selectionManager->currentItem() + 1 : m_selectionManager->currentItem());
     } else {
         index = m_model->indexForKeyboardSearch(text, 0);
     }
     if (index >= 0) {
+        if (m_selectionMode) {
+            m_selectionManager->endAnchoredSelection();
+        }
+
         m_selectionManager->setCurrentItem(index);
 
         if (m_selectionBehavior != NoSelection) {
-            m_selectionManager->replaceSelection(index);
+            if (!m_selectionMode) { // Don't clear the selection in selection mode.
+                m_selectionManager->replaceSelection(index);
+            }
             m_selectionManager->beginAnchoredSelection(index);
         }
 
@@ -597,7 +599,7 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent *event, const
 
     const Qt::MouseButtons buttons = event->buttons();
 
-    if (!onPress(event->screenPos(), event->pos(), event->modifiers(), buttons)) {
+    if (!onPress(event->pos(), event->modifiers(), buttons)) {
         startRubberBand();
         return false;
     }
@@ -673,6 +675,10 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent *event, con
         return false;
     }
 
+    for (KItemListWidget *widget : m_view->visibleItemListWidgets()) {
+        widget->setPressed(false);
+    }
+
     if (m_view->m_tapAndHoldIndicator->isActive()) {
         m_view->m_tapAndHoldIndicator->setActive(false);
     }
@@ -692,6 +698,20 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event,
     const QPointF pos = transform.map(event->pos());
     const std::optional<int> index = m_view->itemAt(pos);
 
+    if (event->button() & (Qt::ForwardButton | Qt::BackButton)) {
+        // "Forward" and "Back" are reserved for quickly navigating through the
+        // history. Double-clicking those buttons should be interpreted as two
+        // separate button presses. We arrive here for the second click, which
+        // we now react to just as we would for a singular click
+        Q_EMIT mouseButtonPressed(index.value_or(-1), event->button());
+        return false;
+    }
+
+    if (!index.has_value()) {
+        Q_EMIT doubleClickViewBackground(event->button());
+        return false;
+    }
+
     // Expand item if desired - See Bug 295573
     if (m_mouseDoubleClickAction != ActivateItemOnly) {
         if (m_view && m_model && m_view->supportsItemExpanding() && m_model->isExpandable(index.value_or(-1))) {
@@ -700,30 +720,77 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event,
         }
     }
 
-    if (event->button() & Qt::RightButton) {
-        m_selectionManager->clearSelection();
-        if (index.has_value()) {
-            m_selectionManager->setSelected(index.value());
-            Q_EMIT itemContextMenuRequested(index.value(), event->screenPos());
-        } else {
-            const QRectF headerBounds = m_view->headerBoundaries();
-            if (headerBounds.contains(event->pos())) {
-                Q_EMIT headerContextMenuRequested(event->screenPos());
-            } else {
-                Q_EMIT viewContextMenuRequested(event->screenPos());
-            }
-        }
-        return true;
+    if (event->button() & ~Qt::LeftButton) {
+        return false;
+    }
+
+    if (m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) {
+        return false;
     }
 
-    bool emitItemActivated = !(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced)
-        && (event->button() & Qt::LeftButton) && index.has_value() && index.value() < m_model->count();
+    const bool emitItemActivated = index.has_value() && index.value() < m_model->count() && !m_view->isAboveExpansionToggle(index.value(), pos);
     if (emitItemActivated) {
+        if (!QApplication::keyboardModifiers()) {
+            m_selectionManager->clearSelection(); // The user does not want to manage/manipulate the item currently, only activate it.
+        }
         Q_EMIT itemActivated(index.value());
     }
     return false;
 }
 
+bool KItemListController::contextMenuEvent(QContextMenuEvent *event)
+{
+    if (event->reason() == QContextMenuEvent::Keyboard) {
+        // Emit the signal itemContextMenuRequested() if at least one item is selected.
+        // Otherwise the signal viewContextMenuRequested() will be emitted.
+        const KItemSet selectedItems = m_selectionManager->selectedItems();
+        int index = -1;
+        if (selectedItems.count() >= 2) {
+            const int currentItemIndex = m_selectionManager->currentItem();
+            index = selectedItems.contains(currentItemIndex) ? currentItemIndex : selectedItems.first();
+        } else if (selectedItems.count() == 1) {
+            index = selectedItems.first();
+        }
+
+        if (index >= 0) {
+            const QRectF contextRect = m_view->itemContextRect(index);
+            const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint()));
+            Q_EMIT itemContextMenuRequested(index, pos);
+        } else {
+            Q_EMIT viewContextMenuRequested(event->globalPos());
+        }
+        return true;
+    }
+
+    const auto pos = event->pos();
+    const auto globalPos = event->globalPos();
+
+    if (m_view->headerBoundaries().contains(pos)) {
+        Q_EMIT headerContextMenuRequested(globalPos);
+        return true;
+    }
+
+    const auto pressedItem = m_view->itemAt(pos);
+    // We only open a context menu for the pressed item if it is selected.
+    // 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.
+    if (pressedItem && m_selectionManager->selectedItems().contains(pressedItem.value())) {
+        // The selection rectangle for an item was clicked
+        Q_EMIT itemContextMenuRequested(pressedItem.value(), globalPos);
+        return true;
+    }
+
+    // Remove any hover highlights so the context menu doesn't look like it applies to a row.
+    const auto widgets = m_view->visibleItemListWidgets();
+    for (KItemListWidget *widget : widgets) {
+        if (widget->isHovered()) {
+            widget->setHovered(false);
+            Q_EMIT itemUnhovered(widget->index());
+        }
+    }
+    Q_EMIT viewContextMenuRequested(globalPos);
+    return true;
+}
+
 bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent *event, const QTransform &transform)
 {
     Q_UNUSED(event)
@@ -794,10 +861,12 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent *event, cons
                 Q_EMIT itemHovered(index);
             }
 
-            if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0) {
+            if (m_autoActivationEnabled && !m_autoActivationTimer->isActive() && m_model->canEnterOnHover(index)) {
                 m_autoActivationTimer->setProperty("index", index);
                 m_autoActivationTimer->start();
+                newHoveredWidget->startActivateSoonAnimation(m_autoActivationTimer->remainingTime());
             }
+
         } else {
             m_autoActivationTimer->stop();
             if (newHoveredWidget && newHoveredWidget->isHovered()) {
@@ -925,7 +994,7 @@ bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent *event, const
             // 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)
             unhoverOldExpansionWidget();
 
-            const bool isOverIconAndText = newHoveredWidget->iconRect().contains(mappedPos) || newHoveredWidget->textRect().contains(mappedPos);
+            const bool isOverIconAndText = newHoveredWidget->selectionRectCore().contains(mappedPos);
             const bool hasMultipleSelection = m_selectionManager->selectedItems().count() > 1;
 
             if (hasMultipleSelection && !isOverIconAndText) {
@@ -964,6 +1033,7 @@ bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent *event, const
 
     const auto widgets = m_view->visibleItemListWidgets();
     for (KItemListWidget *widget : widgets) {
+        widget->setPressed(false);
         if (widget->isHovered()) {
             widget->setHovered(false);
             Q_EMIT itemUnhovered(widget->index());
@@ -1068,7 +1138,7 @@ void KItemListController::tapTriggered(QTapGesture *tap, const QTransform &trans
         if (m_dragActionOrRightClick) {
             m_dragActionOrRightClick = false;
         } else {
-            onPress(m_pressedMouseGlobalPos.toPoint(), tap->position().toPoint(), Qt::NoModifier, Qt::LeftButton);
+            onPress(tap->position().toPoint(), Qt::NoModifier, Qt::LeftButton);
             onRelease(transform.map(tap->position()), Qt::NoModifier, Qt::LeftButton, true);
         }
         m_isTouchEvent = false;
@@ -1183,7 +1253,7 @@ void KItemListController::twoFingerTapTriggered(QGestureEvent *event, const QTra
         const QPointF pressedMousePos = transform.map(twoTap->pos());
         m_pressedIndex = m_view->itemAt(pressedMousePos);
         if (m_pressedIndex.has_value()) {
-            onPress(twoTap->screenPos().toPoint(), twoTap->pos().toPoint(), Qt::ControlModifier, Qt::LeftButton);
+            onPress(twoTap->pos().toPoint(), Qt::ControlModifier, Qt::LeftButton);
             onRelease(transform.map(twoTap->pos()), Qt::ControlModifier, Qt::LeftButton, false);
         }
     }
@@ -1208,6 +1278,8 @@ bool KItemListController::processEvent(QEvent *event, const QTransform &transfor
         return mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event), QTransform());
     case QEvent::GraphicsSceneMouseDoubleClick:
         return mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent *>(event), QTransform());
+    case QEvent::ContextMenu:
+        return contextMenuEvent(static_cast<QContextMenuEvent *>(event));
     case QEvent::GraphicsSceneWheel:
         return wheelEvent(static_cast<QGraphicsSceneWheelEvent *>(event), QTransform());
     case QEvent::GraphicsSceneDragEnter:
@@ -1298,9 +1370,8 @@ void KItemListController::slotRubberBandChanged()
         const QRectF widgetRect = m_view->itemRect(index);
         if (widgetRect.intersects(rubberBandRect)) {
             // Select the full row intersecting with the rubberband rectangle
-            const QRectF selectionRect = widget->selectionRect().translated(widgetRect.topLeft());
-            const QRectF iconRect = widget->iconRect().translated(widgetRect.topLeft());
-            if (selectionRect.intersects(rubberBandRect) || iconRect.intersects(rubberBandRect)) {
+            const QRectF selectionRect = widget->selectionRectFull().translated(widgetRect.topLeft());
+            if (selectionRect.intersects(rubberBandRect)) {
                 selectedItems.insert(index);
             }
         }
@@ -1393,10 +1464,14 @@ KItemListWidget *KItemListController::widgetForPos(const QPointF &pos) const
 {
     Q_ASSERT(m_view);
 
+    if (m_view->headerBoundaries().contains(pos)) {
+        return nullptr;
+    }
+
     const auto widgets = m_view->visibleItemListWidgets();
     for (KItemListWidget *widget : widgets) {
         const QPointF mappedPos = widget->mapFromItem(m_view, pos);
-        if (widget->contains(mappedPos) || widget->selectionRect().contains(mappedPos)) {
+        if (widget->contains(mappedPos)) {
             return widget;
         }
     }
@@ -1408,10 +1483,14 @@ KItemListWidget *KItemListController::widgetForDropPos(const QPointF &pos) const
 {
     Q_ASSERT(m_view);
 
+    if (m_view->headerBoundaries().contains(pos)) {
+        return nullptr;
+    }
+
     const auto widgets = m_view->visibleItemListWidgets();
     for (KItemListWidget *widget : widgets) {
         const QPointF mappedPos = widget->mapFromItem(m_view, pos);
-        if (widget->contains(mappedPos)) {
+        if (widget->selectionRectCore().contains(mappedPos)) {
             return widget;
         }
     }
@@ -1441,9 +1520,12 @@ int KItemListController::nextRowIndex(int index) const
         return index;
     }
 
+    const bool reversed = m_view->layoutDirection() == Qt::RightToLeft && m_view->scrollOrientation() == Qt::Vertical;
+
     // Calculate the index of the last column inside the row of the current index
     int lastColumnIndex = index;
-    while (keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex)) {
+    while ((!reversed && keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex))
+           || (reversed && keyboardAnchorPos(lastColumnIndex + 1) < keyboardAnchorPos(lastColumnIndex))) {
         ++lastColumnIndex;
         if (lastColumnIndex >= maxIndex) {
             return index;
@@ -1455,7 +1537,9 @@ int KItemListController::nextRowIndex(int index) const
     int nextRowIndex = lastColumnIndex + 1;
     int searchIndex = nextRowIndex;
     qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(nextRowIndex));
-    while (searchIndex < maxIndex && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex)) {
+    while (searchIndex < maxIndex
+           && ((!reversed && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex))
+               || (reversed && keyboardAnchorPos(searchIndex + 1) < keyboardAnchorPos(searchIndex)))) {
         ++searchIndex;
         const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
         if (searchDiff < minDiff) {
@@ -1473,9 +1557,12 @@ int KItemListController::previousRowIndex(int index) const
         return index;
     }
 
+    const bool reversed = m_view->layoutDirection() == Qt::RightToLeft && m_view->scrollOrientation() == Qt::Vertical;
+
     // Calculate the index of the first column inside the row of the current index
     int firstColumnIndex = index;
-    while (keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex)) {
+    while ((!reversed && keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex))
+           || (reversed && keyboardAnchorPos(firstColumnIndex - 1) > keyboardAnchorPos(firstColumnIndex))) {
         --firstColumnIndex;
         if (firstColumnIndex <= 0) {
             return index;
@@ -1487,7 +1574,9 @@ int KItemListController::previousRowIndex(int index) const
     int previousRowIndex = firstColumnIndex - 1;
     int searchIndex = previousRowIndex;
     qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(previousRowIndex));
-    while (searchIndex > 0 && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex)) {
+    while (searchIndex > 0
+           && ((!reversed && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex))
+               || (reversed && keyboardAnchorPos(searchIndex - 1) > keyboardAnchorPos(searchIndex)))) {
         --searchIndex;
         const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
         if (searchDiff < minDiff) {
@@ -1521,7 +1610,7 @@ void KItemListController::updateExtendedSelectionRegion()
     }
 }
 
-bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, const Qt::KeyboardModifiers modifiers, const Qt::MouseButtons buttons)
+bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifiers modifiers, const Qt::MouseButtons buttons)
 {
     Q_EMIT mouseButtonPressed(m_pressedIndex.value_or(-1), buttons);
 
@@ -1556,6 +1645,7 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
                                                                                       // simplify the overall logic and possibilities both for users and devs.
     const bool leftClick = buttons & Qt::LeftButton;
     const bool rightClick = buttons & Qt::RightButton;
+    const bool singleClickActivation = m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick);
 
     // The previous selection is cleared if either
     // 1. The selection mode is SingleSelection, or
@@ -1576,16 +1666,23 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
         if (selectedItemsCount > 1 && m_pressedIndex.has_value()) {
             const auto row = m_view->m_visibleItems.value(m_pressedIndex.value());
             const auto mappedPos = row->mapFromItem(m_view, pos);
-            if (pressedItemAlreadySelected || row->iconRect().contains(mappedPos) || row->textRect().contains(mappedPos)) {
+            if (row->selectionRectCore().contains(mappedPos)) {
                 // we are indeed inside the text/icon rect, keep m_pressedIndex what it is
                 // and short-circuit for single-click activation (it will then propagate to onRelease and activate the item)
                 // or we just keep going for double-click activation
-                if (m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) {
+                if (singleClickActivation || m_singleClickActivationEnforced) {
                     if (!pressedItemAlreadySelected) {
-                        // An unselected item was clicked directly while deselecting multiple other items so we select it.
-                        m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
+                        // An unselected item was clicked directly while deselecting multiple other items so we mark it "current".
                         m_selectionManager->setCurrentItem(m_pressedIndex.value());
                         m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());
+                        if (!leftClick) {
+                            // We select the item here because this press is not meant to directly activate the item.
+                            // We do not want to select items unless the user wants to edit them.
+                            m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
+                        }
+                    }
+                    if (leftClick) {
+                        row->setPressed(true);
                     }
                     return true; // event handled, don't create rubber band
                 }
@@ -1617,12 +1714,6 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
     }
 
     if (rightClick) {
-        // Do header hit check and short circuit before commencing any state changing effects
-        if (m_view->headerBoundaries().contains(pos)) {
-            Q_EMIT headerContextMenuRequested(screenPos);
-            return true;
-        }
-
         // Stop rubber band from persisting after right-clicks
         KItemListRubberBand *rubberBand = m_view->rubberBand();
         if (rubberBand->isActive()) {
@@ -1630,36 +1721,42 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
             rubberBand->setActive(false);
             m_view->setAutoScroll(false);
         }
+
+        if (!m_pressedIndex.has_value()) {
+            // We have a right-click in an empty region, don't create rubber band.
+            return true;
+        }
     }
 
     if (m_pressedIndex.has_value()) {
         // The hover highlight area of an item is being pressed.
-        m_selectionManager->setCurrentItem(m_pressedIndex.value());
         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
-        const bool hitTargetIsRowEmptyRegion = !row->contains(row->mapFromItem(m_view, pos));
+
+        const bool hitTargetIsRowEmptyRegion = !row->selectionRectCore().contains(row->mapFromItem(m_view, pos));
         // again, when this method returns false, a rubberBand selection is created as the event is not consumed;
         // createRubberBand here tells us whether to return true or false.
         bool createRubberBand = (hitTargetIsRowEmptyRegion && m_selectionManager->selectedItems().isEmpty());
 
+        if (leftClick) {
+            row->setPressed(true);
+        }
+
         if (rightClick && hitTargetIsRowEmptyRegion) {
-            // We have a right click outside the icon and text rect but within the hover highlight area
-            // but it is unclear if this means that a selection rectangle for an item was clicked or the background of the view.
-            if (m_selectionManager->selectedItems().contains(m_pressedIndex.value())) {
-                // The selection rectangle for an item was clicked
-                Q_EMIT itemContextMenuRequested(m_pressedIndex.value(), screenPos);
-            } else {
-                row->setHovered(false); // Removes the hover highlight so the context menu doesn't look like it applies to the row.
-                Q_EMIT viewContextMenuRequested(screenPos);
-            }
+            // We have a right click outside the icon and text rect but within the hover highlight area.
+            // We don't want items to get selected through this, so we return now.
             return true;
         }
 
+        m_selectionManager->setCurrentItem(m_pressedIndex.value());
+
         switch (m_selectionBehavior) {
         case NoSelection:
             break;
 
         case SingleSelection:
-            m_selectionManager->setSelected(m_pressedIndex.value());
+            if (!leftClick || shiftOrControlPressed || (!singleClickActivation && !m_singleClickActivationEnforced)) {
+                m_selectionManager->setSelected(m_pressedIndex.value());
+            }
             break;
 
         case MultiSelection:
@@ -1670,7 +1767,7 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
                 // We rule out the latter, if the item is not clicked directly and was unselected previously.
                 const auto row = m_view->m_visibleItems.value(m_pressedIndex.value());
                 const auto mappedPos = row->mapFromItem(m_view, pos);
-                if (!row->iconRect().contains(mappedPos) && !row->textRect().contains(mappedPos) && !pressedItemAlreadySelected) {
+                if (!row->selectionRectCore().contains(mappedPos)) {
                     createRubberBand = true;
                 } else {
                     m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
@@ -1680,7 +1777,9 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
                 }
             } else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
                 // Select the pressed item and start a new anchored selection
-                m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Select);
+                if (!leftClick || shiftOrControlPressed || (!singleClickActivation && !m_singleClickActivationEnforced)) {
+                    m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Select);
+                }
                 m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());
             }
             break;
@@ -1690,19 +1789,9 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
             break;
         }
 
-        if (rightClick) {
-            Q_EMIT itemContextMenuRequested(m_pressedIndex.value(), screenPos);
-        }
         return !createRubberBand;
     }
 
-    if (rightClick) {
-        // header right click handling would have been done before this so just normal context
-        // menu here is fine
-        Q_EMIT viewContextMenuRequested(screenPos);
-        return true;
-    }
-
     return false;
 }