From 4a1b48852cf57a567936b2bddc6f9dbd4c3252f3 Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Tue, 16 Apr 2024 21:52:51 -0400 Subject: [PATCH] #178: Add ability to scroll through entry view using keyboard (#179) # Description This commit adds ability to scroll through the entry view using the keyboard arrow keys, page up/down, and the home/end keys. Closes #178. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Inspected manually on macOS, Linux, and Windows. # Checklist: - [x] My code follows the style guidelines of this project (`black` for Python code, `.clang-format` in the `src/jyut-dict` directory for C++) - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have translated my user-facing strings to all currently-supported languages - [x] I have made corresponding changes to the documentation --- .../components/entryview/entryscrollarea.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/jyut-dict/components/entryview/entryscrollarea.cpp b/src/jyut-dict/components/entryview/entryscrollarea.cpp index 74fbc090..3940e3dc 100644 --- a/src/jyut-dict/components/entryview/entryscrollarea.cpp +++ b/src/jyut-dict/components/entryview/entryscrollarea.cpp @@ -13,6 +13,7 @@ EntryScrollArea::EntryScrollArea(std::shared_ptr sqlUserUtils, _updateUITimer = new QTimer{this}; setFrameShape(QFrame::NoFrame); + verticalScrollBar()->setFocusPolicy(Qt::StrongFocus); _scrollAreaWidget = new EntryScrollAreaWidget{sqlUserUtils, manager, this}; @@ -84,7 +85,43 @@ void EntryScrollArea::keyPressEvent(QKeyEvent *event) { if (isWindow() && event->key() == Qt::Key_Escape) { close(); + return; + } else { + // Not sure why setting the focus policy of the scroll bar + // doesn't work, but this is a workaround! + switch (event->key()) { + case Qt::Key_Down: { + verticalScrollBar()->triggerAction( + QAbstractSlider::SliderSingleStepAdd); + break; + } + case Qt::Key_Up: { + verticalScrollBar()->triggerAction( + QAbstractSlider::SliderSingleStepSub); + break; + } + case Qt::Key_PageDown: { + verticalScrollBar()->triggerAction( + QAbstractSlider::SliderPageStepAdd); + break; + } + case Qt::Key_PageUp: { + verticalScrollBar()->triggerAction( + QAbstractSlider::SliderPageStepSub); + break; + } + case Qt::Key_Home: { + verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMinimum); + break; + } + case Qt::Key_End: { + verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMaximum); + break; + } + } + return; } + QWidget::keyPressEvent(event); } void EntryScrollArea::setEntry(const Entry &entry)