]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistcontroller.cpp
selection: fix rubberband vertices
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
index 2e7d2f057d5fa61b0efe8b9ae32921d2dc78824c..be7a63e0993b43b16e0b5e8564fdb568999034d7 100644 (file)
@@ -50,7 +50,7 @@ KItemListController::KItemListController(KItemModelBase *model, KItemListView *v
     , m_selectionManager(new KItemListSelectionManager(this))
     , m_keyboardManager(new KItemListKeyboardSearchManager(this))
     , m_pressedIndex(std::nullopt)
-    , m_pressedMousePos()
+    , m_pressedMouseGlobalPos()
     , m_autoActivationTimer(nullptr)
     , m_swipeGesture(Qt::CustomGesture)
     , m_twoFingerTapGesture(Qt::CustomGesture)
@@ -186,11 +186,13 @@ KItemListController::MouseDoubleClickAction KItemListController::mouseDoubleClic
 
 int KItemListController::indexCloseToMousePressedPosition() const
 {
+    const QPointF pressedMousePos = m_view->transform().map(m_view->scene()->views().first()->mapFromGlobal(m_pressedMouseGlobalPos.toPoint()));
+
     QHashIterator<KItemListWidget *, KItemListGroupHeader *> it(m_view->m_visibleGroups);
     while (it.hasNext()) {
         it.next();
         KItemListGroupHeader *groupHeader = it.value();
-        const QPointF mappedToGroup = groupHeader->mapFromItem(nullptr, m_pressedMousePos);
+        const QPointF mappedToGroup = groupHeader->mapFromItem(nullptr, pressedMousePos);
         if (groupHeader->contains(mappedToGroup)) {
             return it.key()->index();
         }
@@ -237,21 +239,34 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
 {
     int index = m_selectionManager->currentItem();
     int key = event->key();
+    const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
 
     // Handle the expanding/collapsing of items
-    if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
-        if (key == Qt::Key_Right) {
-            if (m_model->setExpanded(index, true)) {
-                return true;
+    // expand / collapse all selected directories
+    if (m_view->supportsItemExpanding() && m_model->isExpandable(index) && (key == Qt::Key_Right || key == Qt::Key_Left)) {
+        const bool expandOrCollapse = key == Qt::Key_Right ? true : false;
+        bool shouldReturn = m_model->setExpanded(index, expandOrCollapse);
+
+        // edit in reverse to preserve index of the first handled items
+        const auto selectedItems = m_selectionManager->selectedItems();
+        for (auto it = selectedItems.rbegin(); it != selectedItems.rend(); ++it) {
+            shouldReturn |= m_model->setExpanded(*it, expandOrCollapse);
+            if (!shiftPressed) {
+                m_selectionManager->setSelected(*it);
             }
-        } else if (key == Qt::Key_Left) {
-            if (m_model->setExpanded(index, false)) {
-                return true;
+        }
+        if (shouldReturn) {
+            // update keyboard anchors
+            if (shiftPressed) {
+                m_keyboardAnchorIndex = selectedItems.count() > 0 ? qMin(index, selectedItems.last()) : index;
+                m_keyboardAnchorPos = keyboardAnchorPos(m_keyboardAnchorIndex);
             }
+
+            event->ignore();
+            return true;
         }
     }
 
-    const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
     const bool controlPressed = event->modifiers() & Qt::ControlModifier;
     const bool shiftOrControlPressed = shiftPressed || controlPressed;
     const bool navigationPressed = key == Qt::Key_Home || key == Qt::Key_End || key == Qt::Key_PageUp || key == Qt::Key_PageDown || key == Qt::Key_Up
@@ -327,11 +342,17 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
 
     case Qt::Key_Up:
         updateKeyboardAnchor();
+        if (shiftPressed && !m_selectionManager->isAnchoredSelectionActive() && m_selectionManager->isSelected(index)) {
+            m_selectionManager->beginAnchoredSelection(index);
+        }
         index = previousRowIndex(index);
         break;
 
     case Qt::Key_Down:
         updateKeyboardAnchor();
+        if (shiftPressed && !m_selectionManager->isAnchoredSelectionActive() && m_selectionManager->isSelected(index)) {
+            m_selectionManager->beginAnchoredSelection(index);
+        }
         index = nextRowIndex(index);
         break;
 
@@ -561,6 +582,7 @@ bool KItemListController::inputMethodEvent(QInputMethodEvent *event)
 bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent *event, const QTransform &transform)
 {
     m_mousePress = true;
+    m_pressedMouseGlobalPos = event->screenPos();
 
     if (event->source() == Qt::MouseEventSynthesizedByQt && m_isTouchEvent) {
         return false;
@@ -570,8 +592,8 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent *event, const
         return false;
     }
 
-    m_pressedMousePos = transform.map(event->pos());
-    m_pressedIndex = m_view->itemAt(m_pressedMousePos);
+    const QPointF pressedMousePos = transform.map(event->pos());
+    m_pressedIndex = m_view->itemAt(pressedMousePos);
 
     const Qt::MouseButtons buttons = event->buttons();
 
@@ -597,12 +619,11 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent *event, const
         return false;
     }
 
-    const QPointF pos = transform.map(event->pos());
-
     if (m_pressedIndex.has_value() && !m_view->rubberBand()->isActive()) {
         // Check whether a dragging should be started
         if (event->buttons() & Qt::LeftButton) {
-            if ((pos - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) {
+            const auto distance = (event->screenPos() - m_pressedMouseGlobalPos).manhattanLength();
+            if (distance >= QApplication::startDragDistance()) {
                 if (!m_selectionManager->isSelected(m_pressedIndex.value())) {
                     // Always assure that the dragged item gets selected. Usually this is already
                     // done on the mouse-press event, but when using the selection-toggle on a
@@ -633,11 +654,6 @@ bool KItemListController::mouseMoveEvent(QGraphicsSceneMouseEvent *event, const
 
             if (m_view->scrollOrientation() == Qt::Vertical) {
                 endPos.ry() += m_view->scrollOffset();
-                if (m_view->itemSize().width() < 0) {
-                    // Use a special rubberband for views that have only one column and
-                    // expand the rubberband to use the whole width of the view.
-                    endPos.setX(m_view->size().width());
-                }
             } else {
                 endPos.rx() += m_view->scrollOffset();
             }
@@ -1047,13 +1063,12 @@ void KItemListController::tapTriggered(QTapGesture *tap, const QTransform &trans
             m_view->m_tapAndHoldIndicator->setActive(false);
         }
 
-        m_pressedMousePos = transform.map(tap->position());
-        m_pressedIndex = m_view->itemAt(m_pressedMousePos);
-
+        const QPointF pressedMousePos = transform.map(tap->position());
+        m_pressedIndex = m_view->itemAt(pressedMousePos);
         if (m_dragActionOrRightClick) {
             m_dragActionOrRightClick = false;
         } else {
-            onPress(tap->hotSpot().toPoint(), tap->position().toPoint(), Qt::NoModifier, Qt::LeftButton);
+            onPress(m_pressedMouseGlobalPos.toPoint(), tap->position().toPoint(), Qt::NoModifier, Qt::LeftButton);
             onRelease(transform.map(tap->position()), Qt::NoModifier, Qt::LeftButton, true);
         }
         m_isTouchEvent = false;
@@ -1073,23 +1088,24 @@ void KItemListController::tapAndHoldTriggered(QGestureEvent *event, const QTrans
         if (m_pinchGestureInProgress) {
             return;
         }
-        m_pressedMousePos = transform.map(event->mapToGraphicsScene(tap->position()));
-        m_pressedIndex = m_view->itemAt(m_pressedMousePos);
-
-        if (m_pressedIndex.has_value() && !m_selectionManager->isSelected(m_pressedIndex.value())) {
-            m_selectionManager->clearSelection();
-            m_selectionManager->setSelected(m_pressedIndex.value());
+        const QPointF pressedMousePos = transform.map(event->mapToGraphicsScene(tap->position()));
+        m_pressedIndex = m_view->itemAt(pressedMousePos);
+        if (m_pressedIndex.has_value()) {
+            if (!m_selectionManager->isSelected(m_pressedIndex.value())) {
+                m_selectionManager->clearSelection();
+                m_selectionManager->setSelected(m_pressedIndex.value());
+            }
             if (!m_selectionMode) {
                 Q_EMIT selectionModeChangeRequested(true);
             }
-        } else if (!m_pressedIndex.has_value()) {
+        } else {
             m_selectionManager->clearSelection();
             startRubberBand();
         }
 
         Q_EMIT scrollerStop();
 
-        m_view->m_tapAndHoldIndicator->setStartPosition(m_pressedMousePos);
+        m_view->m_tapAndHoldIndicator->setStartPosition(pressedMousePos);
         m_view->m_tapAndHoldIndicator->setActive(true);
 
         m_dragActionOrRightClick = true;
@@ -1164,8 +1180,8 @@ void KItemListController::twoFingerTapTriggered(QGestureEvent *event, const QTra
     }
 
     if (twoTap->state() == Qt::GestureStarted) {
-        m_pressedMousePos = transform.map(twoTap->pos());
-        m_pressedIndex = m_view->itemAt(m_pressedMousePos);
+        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);
             onRelease(transform.map(twoTap->pos()), Qt::ControlModifier, Qt::LeftButton, false);
@@ -1281,9 +1297,9 @@ void KItemListController::slotRubberBandChanged()
 
         const QRectF widgetRect = m_view->itemRect(index);
         if (widgetRect.intersects(rubberBandRect)) {
-            const QRectF iconRect = widget->iconRect().translated(widgetRect.topLeft());
-            const QRectF textRect = widget->textRect().translated(widgetRect.topLeft());
-            if (iconRect.intersects(rubberBandRect) || textRect.intersects(rubberBandRect)) {
+            // Select the full row intersecting with the rubberband rectangle
+            const QRectF selectionRect = widget->selectionRect().translated(widgetRect.topLeft());
+            if (selectionRect.intersects(rubberBandRect)) {
                 selectedItems.insert(index);
             }
         }
@@ -1514,14 +1530,16 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
         return true;
     }
 
-    if (m_view->isAboveExpansionToggle(m_pressedIndex.value_or(-1), m_pressedMousePos)) {
+    const QPointF pressedMousePos = m_view->transform().map(pos);
+
+    if (m_view->isAboveExpansionToggle(m_pressedIndex.value_or(-1), 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);
+    m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex.value_or(-1), pressedMousePos);
     if (m_selectionTogglePressed) {
         m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Toggle);
         // The previous anchored selection has been finished already in
@@ -1587,7 +1605,7 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
         //    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)) {
+        if (m_selectionManager->selectedItems().count() == 1 && m_view->isAboveText(m_pressedIndex.value_or(-1), pressedMousePos)) {
             Q_EMIT selectedItemTextPressed(m_pressedIndex.value_or(-1));
         }
     }
@@ -1689,7 +1707,8 @@ bool KItemListController::onPress(const QPoint &screenPos, const QPointF &pos, c
 
 bool KItemListController::onRelease(const QPointF &pos, const Qt::KeyboardModifiers modifiers, const Qt::MouseButtons buttons, bool touch)
 {
-    const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex.value_or(-1), m_pressedMousePos);
+    const QPointF pressedMousePos = pos;
+    const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex.value_or(-1), pressedMousePos);
     if (isAboveSelectionToggle) {
         m_selectionTogglePressed = false;
         return true;
@@ -1773,7 +1792,7 @@ bool KItemListController::onRelease(const QPointF &pos, const Qt::KeyboardModifi
         }
     }
 
-    m_pressedMousePos = QPointF();
+    m_pressedMouseGlobalPos = QPointF();
     m_pressedIndex = std::nullopt;
     m_clearSelectionIfItemsAreNotDragged = false;
     return false;
@@ -1782,14 +1801,9 @@ bool KItemListController::onRelease(const QPointF &pos, const Qt::KeyboardModifi
 void KItemListController::startRubberBand()
 {
     if (m_selectionBehavior == MultiSelection) {
-        QPointF startPos = m_pressedMousePos;
+        QPoint startPos = m_view->transform().map(m_view->scene()->views().first()->mapFromGlobal(m_pressedMouseGlobalPos.toPoint()));
         if (m_view->scrollOrientation() == Qt::Vertical) {
             startPos.ry() += m_view->scrollOffset();
-            if (m_view->itemSize().width() < 0) {
-                // Use a special rubberband for views that have only one column and
-                // expand the rubberband to use the whole width of the view.
-                startPos.setX(0);
-            }
         } else {
             startPos.rx() += m_view->scrollOffset();
         }
@@ -1812,3 +1826,5 @@ void KItemListController::slotStateChanged(QScroller::State newState)
         m_scrollerIsScrolling = false;
     }
 }
+
+#include "moc_kitemlistcontroller.cpp"