]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistcontroller.cpp
Implement basic keyboard navigation in Icons and Compact View
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
index dcd62ad52caf89c1e5a67a0318109621beff64e4..af93715cf2d50c6b8c5dd869874c54bda3066c52 100644 (file)
@@ -119,8 +119,92 @@ bool KItemListController::hideEvent(QHideEvent* event)
 
 bool KItemListController::keyPressEvent(QKeyEvent* event)
 {
-    Q_UNUSED(event);
-    return false;
+    const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
+    const bool controlPressed = event->modifiers() & Qt::ControlModifier;
+    const bool shiftOrControlPressed = shiftPressed || controlPressed;
+
+    int index = m_selectionManager->currentItem();
+
+    switch (event->key()) {
+    case Qt::Key_Home:
+        index = 0;
+        break;
+
+    case Qt::Key_End:
+        index = m_model->count() - 1;
+        break;
+
+    case Qt::Key_Up:
+        if (m_view->scrollOrientation() == Qt::Horizontal) {
+            if (index > 0) {
+                index--;
+            }
+        }
+        else {
+            // TODO: Move to the previous row
+        }
+        break;
+
+    case Qt::Key_Down:
+        if (m_view->scrollOrientation() == Qt::Horizontal) {
+            if (index < m_model->count() - 1) {
+                index++;
+            }
+        }
+        else {
+            // TODO: Move to the next row
+        }
+        break;
+
+    case Qt::Key_Left:
+        if (m_view->scrollOrientation() == Qt::Vertical) {
+            if (index > 0) {
+                index--;
+            }
+        }
+        else {
+            // TODO: Move to the previous column
+        }
+        break;
+
+    case Qt::Key_Right:
+        if (m_view->scrollOrientation() == Qt::Vertical) {
+            if (index < m_model->count() - 1) {
+                index++;
+            }
+        }
+        else {
+            // TODO: Move to the next column
+        }
+        break;
+
+    case Qt::Key_Space:
+        if (controlPressed) {
+            m_selectionManager->endAnchoredSelection();
+            m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
+            m_selectionManager->beginAnchoredSelection(index);
+        }
+     default:
+        break;
+    }
+
+    if (m_selectionManager->currentItem() != index) {
+        if (controlPressed) {
+            m_selectionManager->endAnchoredSelection();
+        }
+
+        m_selectionManager->setCurrentItem(index);
+
+        if (!shiftOrControlPressed || m_selectionBehavior == SingleSelection) {
+            m_selectionManager->clearSelection();
+            m_selectionManager->setSelected(index, 1);
+        }
+
+        if (!shiftPressed) {
+            m_selectionManager->beginAnchoredSelection(index);
+        }
+    }
+    return true;
 }
 
 bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
@@ -134,18 +218,51 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const
     const QPointF pos = transform.map(event->pos());
     m_pressedIndex = m_view->itemAt(pos);
 
-    const bool shiftOrControlPressed = event->modifiers() & Qt::ShiftModifier ||
-                                       event->modifiers() & Qt::ControlModifier;
-    if (!shiftOrControlPressed) {
+    if (m_view->isAboveExpansionToggle(m_pressedIndex, pos)) {
+        return true;
+    }
+
+    const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
+    const bool controlPressed = event->modifiers() & Qt::ControlModifier;
+    const bool shiftOrControlPressed = shiftPressed || controlPressed;
+
+    if (!shiftOrControlPressed || m_selectionBehavior == SingleSelection) {
         m_selectionManager->clearSelection();
-        m_selectionManager->setAnchorItem(m_pressedIndex);
+    }
+
+    if (!shiftPressed) {
+        // Finish the anchored selection before the current index is changed
+        m_selectionManager->endAnchoredSelection();
     }
 
     if (m_pressedIndex >= 0) {
         m_selectionManager->setCurrentItem(m_pressedIndex);
-        if (!m_view->isAboveExpansionToggle(m_pressedIndex, pos)) {
+
+        switch (m_selectionBehavior) {
+        case NoSelection:
+            return true;
+        case SingleSelection:
             m_selectionManager->setSelected(m_pressedIndex);
+            return true;
+        case MultiSelection:
+            if (controlPressed) {
+                m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
+                m_selectionManager->beginAnchoredSelection(m_pressedIndex);
+            }
+            else {
+                if (shiftPressed && m_selectionManager->isAnchoredSelectionActive()) {
+                    // The anchored selection is continued automatically by calling
+                    // m_selectionManager->setCurrentItem(m_pressedIndex), see above -> nothing more to do here
+                    return true;
+                }
+
+                // Select the pressed item and start a new anchored selection
+                m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Select);
+                m_selectionManager->beginAnchoredSelection(m_pressedIndex);
+            }
         }
+
+        return true;
     }
 
     return false;