Skip to content

Commit

Permalink
(fix) Track menu: make Left key close the Search / Crates submenus again
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Sep 9, 2024
1 parent 34aa89c commit 693c17e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/widget/wsearchrelatedtracksmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void WSearchRelatedTracksMenu::addTriggerSearchAction(
auto pAction = make_parented<QWidgetAction>(this);
pAction->setDefaultWidget(pCheckBox.get());
// While the checkbox is selected (via keyboard, not hovered by pointer)
// pressing Space will toggle it whereas pressing Return triggers the action.
// pressing Space will toggle it whereas pressing Return triggers the action
// and closes the menu (see WTrackMenu).
connect(pAction.get(),
&QAction::triggered,
this,
Expand Down
32 changes: 32 additions & 0 deletions src/widget/wtrackmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QInputDialog>
#include <QKeyEvent>
#include <QList>
#include <QListWidget>
#include <QModelIndex>
Expand Down Expand Up @@ -152,6 +153,25 @@ void WTrackMenu::closeEvent(QCloseEvent* event) {
emit trackMenuVisible(false);
}

bool WTrackMenu::eventFilter(QObject* pObj, QEvent* e) {
// If a checkbox in a QWidgetAction is focused, Left/Right keys are translated
// to Up/Down which prevents closing the submenus with Left key like in other
// submenus. We simply call hide() of the submenu if Left is pressed.
if (e->type() == QEvent::KeyPress) {
QCheckBox* cbx = qobject_cast<QCheckBox*>(pObj);
if (cbx) {
// Note: if we ever add checkboxes to the top level we need to
// assert that cbx->parentWidget() != this.
QKeyEvent* pKe = static_cast<QKeyEvent*>(e);
if (pKe && pKe->key() == Qt::Key_Left) {
cbx->parentWidget()->hide();
return true;
}
}
}
return QObject::eventFilter(pObj, e);
}

void WTrackMenu::popup(const QPoint& pos, QAction* at) {
if (isEmpty()) {
return;
Expand Down Expand Up @@ -236,6 +256,16 @@ void WTrackMenu::createMenus() {
}
m_pSearchRelatedMenu->setEnabled(
!m_pSearchRelatedMenu->isEmpty());
if (!m_pSearchRelatedMenu->isEmpty()) {
// We're interested in keypress Qt::Key_Left, so use our
// event filter like we do for the crate checkboxes.
for (const auto ch : m_pSearchRelatedMenu->children()) {

Check warning on line 262 in src/widget/wtrackmenu.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy

'const auto ch' can be declared as 'auto *const ch' [readability-qualified-auto]
QCheckBox* cb = qobject_cast<QCheckBox*>(ch);
if (cb) {
cb->installEventFilter(this);
}
}
}
});
connect(m_pSearchRelatedMenu,
&WSearchRelatedTracksMenu::triggerSearch,
Expand Down Expand Up @@ -1499,6 +1529,8 @@ void WTrackMenu::slotPopulateCrateMenu() {
m_pCrateMenu);
pCheckBox->setProperty("crateId", QVariant::fromValue(crate.getId()));
pCheckBox->setEnabled(!crate.isLocked());
// We're interested in keypress Qt::Key_Left
pCheckBox->installEventFilter(this);
// Strangely, the normal styling of QActions does not automatically
// apply to QWidgetActions. The :selected pseudo-state unfortunately
// does not work with QWidgetAction. :hover works for selecting items
Expand Down
2 changes: 2 additions & 0 deletions src/widget/wtrackmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class WTrackMenu : public QMenu {
void slotRemoveFromDisk();
const QString getDeckGroup() const;

bool eventFilter(QObject* pObj, QEvent* e) override;

signals:
void loadTrackToPlayer(TrackPointer pTrack, const QString& group, bool play = false);
void trackMenuVisible(bool visible);
Expand Down

0 comments on commit 693c17e

Please sign in to comment.