]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistcontroller.cpp
"Group by" exists, group sorting rule is separate from sorting rule. Very WIP and...
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
index 3c1b088bec6796a33a97901b0dbf73d3d03f0693..392dc410e605a9b4a069dd985071732933eed807 100644 (file)
@@ -241,6 +241,8 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
     int key = event->key();
     const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
 
+    const bool horizontalScrolling = m_view->scrollOrientation() == Qt::Horizontal;
+
     // 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)) {
@@ -276,7 +278,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;
@@ -295,17 +297,31 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
         }
     }
 
-    // For right to left languages, exchange right and left arrow keys.
     if (m_view->layoutDirection() == Qt::RightToLeft) {
-        switch (key) {
-        case Qt::Key_Left:
-            key = Qt::Key_Right;
-            break;
-        case Qt::Key_Right:
-            key = Qt::Key_Left;
-            break;
-        default:
-            break;
+        if (horizontalScrolling) {
+            // swap up and down arrow keys
+            switch (key) {
+            case Qt::Key_Up:
+                key = Qt::Key_Down;
+                break;
+            case Qt::Key_Down:
+                key = Qt::Key_Up;
+                break;
+            default:
+                break;
+            }
+        } else if (!m_view->supportsItemExpanding()) {
+            // 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;
+            }
         }
     }
 
@@ -371,7 +387,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()) {
@@ -399,7 +415,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()) {
@@ -684,6 +700,11 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event,
     const QPointF pos = transform.map(event->pos());
     const std::optional<int> index = m_view->itemAt(pos);
 
+    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))) {
@@ -696,9 +717,15 @@ bool KItemListController::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event,
         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();
+    if (m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) {
+        return false;
+    }
+
+    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;
@@ -1486,12 +1513,12 @@ int KItemListController::nextRowIndex(int index) const
         return index;
     }
 
-    const bool leftToRight = m_view->layoutDirection() != Qt::RightToLeft;
+    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 ((leftToRight && keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex))
-           || (!leftToRight && keyboardAnchorPos(lastColumnIndex + 1) < keyboardAnchorPos(lastColumnIndex))) {
+    while ((!reversed && keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex))
+           || (reversed && keyboardAnchorPos(lastColumnIndex + 1) < keyboardAnchorPos(lastColumnIndex))) {
         ++lastColumnIndex;
         if (lastColumnIndex >= maxIndex) {
             return index;
@@ -1504,8 +1531,8 @@ int KItemListController::nextRowIndex(int index) const
     int searchIndex = nextRowIndex;
     qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(nextRowIndex));
     while (searchIndex < maxIndex
-           && ((leftToRight && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex))
-               || (!leftToRight && keyboardAnchorPos(searchIndex + 1) < keyboardAnchorPos(searchIndex)))) {
+           && ((!reversed && keyboardAnchorPos(searchIndex + 1) > keyboardAnchorPos(searchIndex))
+               || (reversed && keyboardAnchorPos(searchIndex + 1) < keyboardAnchorPos(searchIndex)))) {
         ++searchIndex;
         const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
         if (searchDiff < minDiff) {
@@ -1523,12 +1550,12 @@ int KItemListController::previousRowIndex(int index) const
         return index;
     }
 
-    const bool leftToRight = m_view->layoutDirection() != Qt::RightToLeft;
+    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 ((leftToRight && keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex))
-           || (!leftToRight && keyboardAnchorPos(firstColumnIndex - 1) > keyboardAnchorPos(firstColumnIndex))) {
+    while ((!reversed && keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex))
+           || (reversed && keyboardAnchorPos(firstColumnIndex - 1) > keyboardAnchorPos(firstColumnIndex))) {
         --firstColumnIndex;
         if (firstColumnIndex <= 0) {
             return index;
@@ -1541,8 +1568,8 @@ int KItemListController::previousRowIndex(int index) const
     int searchIndex = previousRowIndex;
     qreal minDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(previousRowIndex));
     while (searchIndex > 0
-           && ((leftToRight && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex))
-               || (!leftToRight && keyboardAnchorPos(searchIndex - 1) > keyboardAnchorPos(searchIndex)))) {
+           && ((!reversed && keyboardAnchorPos(searchIndex - 1) < keyboardAnchorPos(searchIndex))
+               || (reversed && keyboardAnchorPos(searchIndex - 1) > keyboardAnchorPos(searchIndex)))) {
         --searchIndex;
         const qreal searchDiff = qAbs(m_keyboardAnchorPos - keyboardAnchorPos(searchIndex));
         if (searchDiff < minDiff) {
@@ -1637,10 +1664,14 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier
                 // 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);
+                        // 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);
+                        }
                     }
                     return true; // event handled, don't create rubber band
                 }
@@ -1679,6 +1710,11 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier
             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()) {
@@ -1702,7 +1738,10 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier
             break;
 
         case SingleSelection:
-            m_selectionManager->setSelected(m_pressedIndex.value());
+            if (!leftClick || shiftOrControlPressed
+                || (!m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) && !m_singleClickActivationEnforced)) {
+                m_selectionManager->setSelected(m_pressedIndex.value());
+            }
             break;
 
         case MultiSelection:
@@ -1723,7 +1762,10 @@ bool KItemListController::onPress(const QPointF &pos, const Qt::KeyboardModifier
                 }
             } 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
+                    || (!m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) && !m_singleClickActivationEnforced)) {
+                    m_selectionManager->setSelected(m_pressedIndex.value(), 1, KItemListSelectionManager::Select);
+                }
                 m_selectionManager->beginAnchoredSelection(m_pressedIndex.value());
             }
             break;