FilterBar: improve keyboard behavior and tab ordering

With this MR, when the filterbar has the keyboard focus:
- pressing the Tab key moves the keyboard focus to the view
- pressing the Down, PageDown, Up and PageUp keys moves the focus to the view and emulate pressing the same key in it (you can navigate directly from the filterbar)
This commit is contained in:
Eugene Popov 2023-04-21 15:41:13 +03:00 committed by Méven Car
parent c9217337c7
commit 6fd77a96b4
2 changed files with 22 additions and 6 deletions

View file

@ -10,6 +10,7 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <QApplication>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QKeyEvent> #include <QKeyEvent>
#include <QLineEdit> #include <QLineEdit>
@ -47,6 +48,9 @@ FilterBar::FilterBar(QWidget *parent)
hLayout->addWidget(m_lockButton); hLayout->addWidget(m_lockButton);
hLayout->addWidget(m_filterInput); hLayout->addWidget(m_filterInput);
hLayout->addWidget(closeButton); hLayout->addWidget(closeButton);
setTabOrder(m_lockButton, closeButton);
setTabOrder(closeButton, m_filterInput);
} }
FilterBar::~FilterBar() FilterBar::~FilterBar()
@ -96,10 +100,8 @@ void FilterBar::showEvent(QShowEvent *event)
} }
} }
void FilterBar::keyReleaseEvent(QKeyEvent *event) void FilterBar::keyPressEvent(QKeyEvent *event)
{ {
QWidget::keyReleaseEvent(event);
switch (event->key()) { switch (event->key()) {
case Qt::Key_Escape: case Qt::Key_Escape:
if (m_filterInput->text().isEmpty()) { if (m_filterInput->text().isEmpty()) {
@ -107,14 +109,28 @@ void FilterBar::keyReleaseEvent(QKeyEvent *event)
} else { } else {
m_filterInput->clear(); m_filterInput->clear();
} }
break; return;
case Qt::Key_Enter: case Qt::Key_Enter:
case Qt::Key_Return: case Qt::Key_Return:
Q_EMIT focusViewRequest(); Q_EMIT focusViewRequest();
break; return;
case Qt::Key_Down:
case Qt::Key_PageDown:
case Qt::Key_Up:
case Qt::Key_PageUp: {
Q_EMIT focusViewRequest();
QWidget *focusWidget = QApplication::focusWidget();
if (focusWidget && focusWidget != this) {
QApplication::sendEvent(focusWidget, event);
}
return;
}
default: default:
break; break;
} }
QWidget::keyPressEvent(event);
} }

View file

@ -62,7 +62,7 @@ Q_SIGNALS:
protected: protected:
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override; void keyPressEvent(QKeyEvent *event) override;
private: private:
QLineEdit *m_filterInput; QLineEdit *m_filterInput;