]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Make PageUp/PageDown work in Dolphin's new view engine
authorFrank Reininghaus <frank78ac@googlemail.com>
Wed, 18 Jan 2012 09:18:47 +0000 (10:18 +0100)
committerFrank Reininghaus <frank78ac@googlemail.com>
Wed, 18 Jan 2012 09:46:47 +0000 (10:46 +0100)
BUG: 288748
FIXED-IN: 4.8.0
REVIEW: 103721
(cherry picked from commit e56a363aa6106477fa1577cb0b1bc2a6389910c8)

src/kitemviews/kitemlistcontroller.cpp
src/kitemviews/kitemlistcontroller.h

index fedbe61a90ddff63d4b8241229693c66d471bac2..0f22d70bbc4653e2cb9832df866be6195c493d59 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com>             *
+ *   Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com>    *
  *                                                                         *
  *   Based on the Itemviews NG project from Trolltech Labs:                *
  *   http://qt.gitorious.org/qt-labs/itemviews-ng                          *
@@ -239,12 +240,68 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
 
     case Qt::Key_Up:
         updateKeyboardAnchor();
-        index = previousRowIndex();
+        index = previousRowIndex(index);
         break;
 
     case Qt::Key_Down:
         updateKeyboardAnchor();
-        index = nextRowIndex();
+        index = nextRowIndex(index);
+        break;
+
+    case Qt::Key_PageUp:
+        if (m_view->scrollOrientation() == Qt::Horizontal) {
+            // 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()) {
+                index = newIndex;
+                newIndex = qMax(index - 1, 0);
+            }
+            m_keyboardAnchorIndex = index;
+            m_keyboardAnchorPos = keyboardAnchorPos(index);
+        } else {
+            const qreal currentItemBottom = m_view->itemRect(index).bottomLeft().y();
+            const qreal height = m_view->geometry().height();
+
+            // The new current item should be the first item in the current
+            // column whose itemRect's top coordinate is larger than targetY.
+            const qreal targetY = currentItemBottom - height;
+
+            updateKeyboardAnchor();
+            int newIndex = previousRowIndex(index);
+            do {
+                index = newIndex;
+                updateKeyboardAnchor();
+                newIndex = previousRowIndex(index);
+            } while (m_view->itemRect(newIndex).topLeft().y() > targetY && newIndex != index);
+        }
+        break;
+
+    case Qt::Key_PageDown:
+        if (m_view->scrollOrientation() == Qt::Horizontal) {
+            // 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()) {
+                index = newIndex;
+                newIndex = qMin(index + 1, m_model->count() - 1);
+            }
+            m_keyboardAnchorIndex = index;
+            m_keyboardAnchorPos = keyboardAnchorPos(index);
+        } else {
+            const qreal currentItemTop = m_view->itemRect(index).topLeft().y();
+            const qreal height = m_view->geometry().height();
+
+            // The new current item should be the last item in the current
+            // column whose itemRect's bottom coordinate is smaller than targetY.
+            const qreal targetY = currentItemTop + height;
+
+            updateKeyboardAnchor();
+            int newIndex = nextRowIndex(index);
+            do {
+                index = newIndex;
+                updateKeyboardAnchor();
+                newIndex = nextRowIndex(index);
+            } while (m_view->itemRect(newIndex).bottomLeft().y() < targetY && newIndex != index);
+        }
         break;
 
     case Qt::Key_Enter:
@@ -994,24 +1051,23 @@ void KItemListController::updateKeyboardAnchor()
     }
 }
 
-int KItemListController::nextRowIndex() const
+int KItemListController::nextRowIndex(int index) const
 {
-    const int currentIndex = m_selectionManager->currentItem();
     if (m_keyboardAnchorIndex < 0) {
-        return currentIndex;
+        return index;
     }
 
     const int maxIndex = m_model->count() - 1;
-    if (currentIndex == maxIndex) {
-        return currentIndex;
+    if (index == maxIndex) {
+        return index;
     }
 
     // Calculate the index of the last column inside the row of the current index
-    int lastColumnIndex = currentIndex;
+    int lastColumnIndex = index;
     while (keyboardAnchorPos(lastColumnIndex + 1) > keyboardAnchorPos(lastColumnIndex)) {
         ++lastColumnIndex;
         if (lastColumnIndex >= maxIndex) {
-            return currentIndex;
+            return index;
         }
     }
 
@@ -1032,19 +1088,18 @@ int KItemListController::nextRowIndex() const
     return nextRowIndex;
 }
 
-int KItemListController::previousRowIndex() const
+int KItemListController::previousRowIndex(int index) const
 {
-    const int currentIndex = m_selectionManager->currentItem();
-    if (m_keyboardAnchorIndex < 0 || currentIndex == 0) {
-        return currentIndex;
+    if (m_keyboardAnchorIndex < 0 || index == 0) {
+        return index;
     }
 
     // Calculate the index of the first column inside the row of the current index
-    int firstColumnIndex = currentIndex;
+    int firstColumnIndex = index;
     while (keyboardAnchorPos(firstColumnIndex - 1) < keyboardAnchorPos(firstColumnIndex)) {
         --firstColumnIndex;
         if (firstColumnIndex <= 0) {
-            return currentIndex;
+            return index;
         }
     }
 
index d0e5f72ec7882b11905462467159cd32320c1f99..9ac4c76e664b17751e65de09a61893664f4cd376 100644 (file)
@@ -238,16 +238,16 @@ private:
     void updateKeyboardAnchor();
 
     /**
-     * @return Index for the next row based on the current index.
-     *         If there is no next row the current index will be returned.
+     * @return Index for the next row based on \a index.
+     *         If there is no next row \a index will be returned.
      */
-    int nextRowIndex() const;
+    int nextRowIndex(int index) const;
 
     /**
-     * @return Index for the previous row based on the current index.
-     *         If there is no previous row the current index will be returned.
+     * @return Index for the previous row based on  \a index.
+     *         If there is no previous row \a index will be returned.
      */
-    int previousRowIndex() const;
+    int previousRowIndex(int index) const;
 
     /**
      * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().