From a7060e6fec46c859000fe963ddf1f8381246741d Mon Sep 17 00:00:00 2001 From: Frank Reininghaus Date: Sun, 14 Aug 2011 17:18:14 +0200 Subject: [PATCH] Enable switching rows or columns using the keyboard --- src/kitemviews/kitemlistcontroller.cpp | 74 +++++++++++++----------- src/kitemviews/kitemlistview.cpp | 5 ++ src/kitemviews/kitemlistview.h | 1 + src/kitemviews/kitemlistviewlayouter.cpp | 5 ++ src/kitemviews/kitemlistviewlayouter_p.h | 2 + 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index af93715cf..ef449f496 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -124,57 +124,64 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) 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: - index = m_model->count() - 1; + index = itemCount - 1; 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; - 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; - 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; - 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 { - // 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; @@ -184,7 +191,8 @@ bool KItemListController::keyPressEvent(QKeyEvent* event) m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle); m_selectionManager->beginAnchoredSelection(index); } - default: + + default: break; } diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp index 9c054e119..3c3439552 100644 --- a/src/kitemviews/kitemlistview.cpp +++ b/src/kitemviews/kitemlistview.cpp @@ -316,6 +316,11 @@ QRectF KItemListView::itemBoundingRect(int index) const return m_layouter->itemBoundingRect(index); } +int KItemListView::itemsPerOffset() const +{ + return m_layouter->itemsPerOffset(); +} + void KItemListView::beginTransaction() { ++m_activeTransactions; diff --git a/src/kitemviews/kitemlistview.h b/src/kitemviews/kitemlistview.h index 23181db6e..47426b5fa 100644 --- a/src/kitemviews/kitemlistview.h +++ b/src/kitemviews/kitemlistview.h @@ -136,6 +136,7 @@ public: virtual QHash visibleRoleSizes() const; QRectF itemBoundingRect(int index) const; + int itemsPerOffset() const; void beginTransaction(); void endTransaction(); diff --git a/src/kitemviews/kitemlistviewlayouter.cpp b/src/kitemviews/kitemlistviewlayouter.cpp index 7d420e093..4adb612e9 100644 --- a/src/kitemviews/kitemlistviewlayouter.cpp +++ b/src/kitemviews/kitemlistviewlayouter.cpp @@ -194,6 +194,11 @@ int KItemListViewLayouter::maximumVisibleItems() const return rows * m_columnCount; } +int KItemListViewLayouter::itemsPerOffset() const +{ + return m_columnCount; +} + bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const { return m_groupIndexes.contains(itemIndex); diff --git a/src/kitemviews/kitemlistviewlayouter_p.h b/src/kitemviews/kitemlistviewlayouter_p.h index 775e9ff85..f774814eb 100644 --- a/src/kitemviews/kitemlistviewlayouter_p.h +++ b/src/kitemviews/kitemlistviewlayouter_p.h @@ -69,6 +69,8 @@ public: int maximumVisibleItems() const; + int itemsPerOffset() const; + /** * @return True if the item with the index \p itemIndex * is the first item within a group. -- 2.47.3