]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Enable switching rows or columns using the keyboard
authorFrank Reininghaus <frank78ac@googlemail.com>
Sun, 14 Aug 2011 15:18:14 +0000 (17:18 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Sun, 14 Aug 2011 15:19:47 +0000 (17:19 +0200)
src/kitemviews/kitemlistcontroller.cpp
src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistview.h
src/kitemviews/kitemlistviewlayouter.cpp
src/kitemviews/kitemlistviewlayouter_p.h

index af93715cf2d50c6b8c5dd869874c54bda3066c52..ef449f496193617c78e13e8c9ca9170700107370 100644 (file)
@@ -124,57 +124,64 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
     const bool shiftOrControlPressed = shiftPressed || controlPressed;
 
     int index = m_selectionManager->currentItem();
     const bool shiftOrControlPressed = shiftPressed || controlPressed;
 
     int index = m_selectionManager->currentItem();
+    const int itemCount = m_model->count();
+    const int itemsPerRow = m_view->itemsPerOffset();
+
+    // For horizontal scroll orientation, transform
+    // the arrow keys to simplify the event handling.
+    int key = event->key();
+    if (m_view->scrollOrientation() == Qt::Horizontal) {
+        switch (key) {
+        case Qt::Key_Up:    key = Qt::Key_Left; break;
+        case Qt::Key_Down:  key = Qt::Key_Right; break;
+        case Qt::Key_Left:  key = Qt::Key_Up; break;
+        case Qt::Key_Right: key = Qt::Key_Down; break;
+        default:            break;
+        }
+    }
 
 
-    switch (event->key()) {
+    switch (key) {
     case Qt::Key_Home:
         index = 0;
         break;
 
     case Qt::Key_End:
     case Qt::Key_Home:
         index = 0;
         break;
 
     case Qt::Key_End:
-        index = m_model->count() - 1;
+        index = itemCount - 1;
         break;
 
         break;
 
-    case Qt::Key_Up:
-        if (m_view->scrollOrientation() == Qt::Horizontal) {
-            if (index > 0) {
-                index--;
-            }
-        }
-        else {
-            // TODO: Move to the previous row
+    case Qt::Key_Left:
+        if (index > 0) {
+            index--;
         }
         break;
 
         }
         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
+    case Qt::Key_Right:
+        if (index < itemCount - 1) {
+            index++;
         }
         break;
 
         }
         break;
 
-    case Qt::Key_Left:
-        if (m_view->scrollOrientation() == Qt::Vertical) {
-            if (index > 0) {
-                index--;
-            }
-        }
-        else {
-            // TODO: Move to the previous column
+    case Qt::Key_Up:
+        if (index >= itemsPerRow) {
+            index -= itemsPerRow;
         }
         break;
 
         }
         break;
 
-    case Qt::Key_Right:
-        if (m_view->scrollOrientation() == Qt::Vertical) {
-            if (index < m_model->count() - 1) {
-                index++;
-            }
+    case Qt::Key_Down:
+        if (index + itemsPerRow < itemCount) {
+            // We are not in the last row yet.
+            index += itemsPerRow;
         }
         else {
         }
         else {
-            // TODO: Move to the next column
+            // We are either in the last row already, or we are in the second-last row,
+            // and there is no item below the current item.
+            // In the latter case, we jump to the very last item.
+            const int currentColumn = index % itemsPerRow;
+            const int lastItemColumn = (itemCount - 1) % itemsPerRow;
+            const bool inLastRow = currentColumn < lastItemColumn;
+            if (!inLastRow) {
+                index = itemCount - 1;
+            }
         }
         break;
 
         }
         break;
 
@@ -184,7 +191,8 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
             m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
             m_selectionManager->beginAnchoredSelection(index);
         }
             m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
             m_selectionManager->beginAnchoredSelection(index);
         }
-     default:
+
+    default:
         break;
     }
 
         break;
     }
 
index 9c054e11912152b223cca7cecff724e9ef7e20c7..3c343955269adeaa5b7d2e01d164f1c368c1ed64 100644 (file)
@@ -316,6 +316,11 @@ QRectF KItemListView::itemBoundingRect(int index) const
     return m_layouter->itemBoundingRect(index);
 }
 
     return m_layouter->itemBoundingRect(index);
 }
 
+int KItemListView::itemsPerOffset() const
+{
+    return m_layouter->itemsPerOffset();
+}
+
 void KItemListView::beginTransaction()
 {
     ++m_activeTransactions;
 void KItemListView::beginTransaction()
 {
     ++m_activeTransactions;
index 23181db6e81254fbb4a34befe386ae443ec26ce7..47426b5fa5f31523d69527d3019f70259173c250 100644 (file)
@@ -136,6 +136,7 @@ public:
     virtual QHash<QByteArray, QSizeF> visibleRoleSizes() const;
 
     QRectF itemBoundingRect(int index) const;
     virtual QHash<QByteArray, QSizeF> visibleRoleSizes() const;
 
     QRectF itemBoundingRect(int index) const;
+    int itemsPerOffset() const;
 
     void beginTransaction();
     void endTransaction();
 
     void beginTransaction();
     void endTransaction();
index 7d420e09320b5708785425beb1ca519a0673cdc8..4adb612e9d36990b1a520aedb268f53ceb2dcfb3 100644 (file)
@@ -194,6 +194,11 @@ int KItemListViewLayouter::maximumVisibleItems() const
     return rows * m_columnCount;
 }
 
     return rows * m_columnCount;
 }
 
+int KItemListViewLayouter::itemsPerOffset() const
+{
+    return m_columnCount;
+}
+
 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
 {
     return m_groupIndexes.contains(itemIndex);
 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
 {
     return m_groupIndexes.contains(itemIndex);
index 775e9ff855e1827488838f709771f014884bcbd5..f774814ebe7bc99398b21d84c0e33881fa0aa7ce 100644 (file)
@@ -69,6 +69,8 @@ public:
 
     int maximumVisibleItems() const;
 
 
     int maximumVisibleItems() const;
 
+    int itemsPerOffset() const;
+
     /**
      * @return True if the item with the index \p itemIndex
      *         is the first item within a group.
     /**
      * @return True if the item with the index \p itemIndex
      *         is the first item within a group.