+ return previousRowIndex;
+}
+
+qreal KItemListController::keyboardAnchorPos(int index) const
+{
+ const QRectF itemRect = m_view->itemRect(index);
+ if (!itemRect.isEmpty()) {
+ return (m_view->scrollOrientation() == Qt::Vertical) ? itemRect.x() : itemRect.y();
+ }
+
+ return 0;
+}
+
+void KItemListController::updateExtendedSelectionRegion()
+{
+ if (m_view) {
+ const bool extend = (m_selectionBehavior != MultiSelection);
+ KItemListStyleOption option = m_view->styleOption();
+ if (option.extendedSelectionRegion != extend) {
+ option.extendedSelectionRegion = extend;
+ m_view->setStyleOption(option);
+ }
+ }
+}
+
+bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, const Qt::KeyboardModifiers modifiers, const Qt::MouseButtons buttons)
+{
+ Q_EMIT mouseButtonPressed(m_pressedIndex.value_or(-1), buttons);
+
+ if (buttons & (Qt::BackButton | Qt::ForwardButton)) {
+ // Do not select items when clicking the back/forward buttons, see
+ // https://bugs.kde.org/show_bug.cgi?id=327412.
+ return true;
+ }
+
+ if (m_view->isAboveExpansionToggle(m_pressedIndex.value_or(-1), m_pressedMousePos)) {
+ m_selectionManager->endAnchoredSelection();
+ m_selectionManager->setCurrentItem(m_pressedIndex.value());
+ m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());
+ return true;
+ }
+
+ m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex.value_or(-1), m_pressedMousePos);
+ if (m_selectionTogglePressed) {
+ m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
+ // The previous anchored selection has been finished already in
+ // KItemListSelectionManager::setSelected(). We can safely change
+ // the current item and start a new anchored selection now.
+ m_selectionManager->setCurrentItem(m_pressedIndex.value());
+ m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());
+ return true;
+ }
+
+ const bool shiftPressed = modifiers & Qt::ShiftModifier;
+ const bool controlPressed = (modifiers & Qt::ControlModifier) || m_selectionMode; // Keeping selectionMode similar to pressing control will hopefully
+ // simplify the overall logic and possibilities both for users and devs.
+ const bool leftClick = buttons & Qt::LeftButton;
+ const bool rightClick = buttons & Qt::RightButton;
+
+ // The previous selection is cleared if either
+ // 1. The selection mode is SingleSelection, or
+ // 2. the selection mode is MultiSelection, and *none* of the following conditions are met:
+ // a) Shift or Control are pressed.
+ // b) The clicked item is selected already. In that case, the user might want to:
+ // - start dragging multiple items, or
+ // - open the context menu and perform an action for all selected items.
+ const bool shiftOrControlPressed = shiftPressed || controlPressed;
+ const bool pressedItemAlreadySelected = m_pressedIndex.has_value() && m_selectionManager->isSelected(m_pressedIndex.value());
+ const bool clearSelection = m_selectionBehavior == SingleSelection || (!shiftOrControlPressed && !pressedItemAlreadySelected);
+
+ // When this method returns false, a rubberBand selection is created using KItemListController::startRubberBand via the caller.
+ if (clearSelection) {
+ const int selectedItemsCount = m_selectionManager->selectedItems().count();
+ m_selectionManager->clearSelection();
+ // clear and bail when we got an existing multi-selection
+ 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)) {
+ // 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 (!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);
+ m_selectionManager->setCurrentItem(m_pressedIndex.value());
+ m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());
+ }
+ return true; // event handled, don't create rubber band
+ }
+ } else {
+ // we're not inside the text/icon rect, as we've already cleared the selection
+ // we can just stop here and make sure handlers down the line (i.e. onRelease) don't activate
+ m_pressedIndex.reset();
+ // we don't stop event propagation and proceed to create a rubber band and let onRelease
+ // decide (based on m_pressedIndex) whether we're in a drag (drag => new rubber band, click => don't select the item)
+ return false;
+ }
+ }
+ } else if (pressedItemAlreadySelected && !shiftOrControlPressed && leftClick) {
+ // The user might want to start dragging multiple items, but if he clicks the item
+ // in order to trigger it instead, the other selected items must be deselected.
+ // However, we do not know yet what the user is going to do.
+ // -> remember that the user pressed an item which had been selected already and
+ // clear the selection in mouseReleaseEvent(), unless the items are dragged.
+ m_clearSelectionIfItemsAreNotDragged = true;
+
+ if (m_selectionManager->selectedItems().count() == 1 && m_view->isAboveText(m_pressedIndex.value_or(-1), m_pressedMousePos)) {
+ Q_EMIT selectedItemTextPressed(m_pressedIndex.value_or(-1));
+ }
+ }
+
+ if (!shiftPressed) {
+ // Finish the anchored selection before the current index is changed
+ m_selectionManager->endAnchoredSelection();
+ }
+
+ 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()) {
+ disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
+ rubberBand->setActive(false);
+ m_view->setAutoScroll(false);
+ }
+ }
+
+ 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));
+ // 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 (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);
+ }
+ return true;
+ }