Skip to content

Commit 7ca7aea

Browse files
committed
(fix) Track menu: make Left key close the Search / Crates submenus again
1 parent 34aa89c commit 7ca7aea

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/widget/wsearchrelatedtracksmenu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ void WSearchRelatedTracksMenu::addTriggerSearchAction(
8989
auto pAction = make_parented<QWidgetAction>(this);
9090
pAction->setDefaultWidget(pCheckBox.get());
9191
// While the checkbox is selected (via keyboard, not hovered by pointer)
92-
// pressing Space will toggle it whereas pressing Return triggers the action.
92+
// pressing Space will toggle it whereas pressing Return triggers the action
93+
// and closes the menu (see WTrackMenu).
9394
connect(pAction.get(),
9495
&QAction::triggered,
9596
this,

src/widget/wtrackmenu.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <QCheckBox>
44
#include <QDialogButtonBox>
55
#include <QInputDialog>
6+
#include <QKeyEvent>
67
#include <QList>
78
#include <QListWidget>
89
#include <QModelIndex>
@@ -152,6 +153,29 @@ void WTrackMenu::closeEvent(QCloseEvent* event) {
152153
emit trackMenuVisible(false);
153154
}
154155

156+
bool WTrackMenu::eventFilter(QObject* pObj, QEvent* e) {
157+
// If a checkbox in a QWidgetAction is focused, Left/Right keys are translated
158+
// to Up/Down which prevents closing the submenus with Left key like in other
159+
// submenus.
160+
// We simply call hide() of the submenu if Left is pressed.
161+
// We ignore Right.
162+
// Don't continue (close track menu) if the checkbox is at the top level.
163+
if (pObj->parent() != this && e->type() == QEvent::KeyPress) {
164+
QCheckBox* pCB = qobject_cast<QCheckBox*>(pObj);
165+
QKeyEvent* pKE = static_cast<QKeyEvent*>(e);
166+
if (!pCB || !pKE) {
167+
return QObject::eventFilter(pObj, e);
168+
}
169+
if (pKE->key() == Qt::Key_Left) {
170+
pCB->parentWidget()->hide();
171+
return true;
172+
} else if (pKE->key() == Qt::Key_Right) {
173+
return true;
174+
}
175+
}
176+
return QObject::eventFilter(pObj, e);
177+
}
178+
155179
void WTrackMenu::popup(const QPoint& pos, QAction* at) {
156180
if (isEmpty()) {
157181
return;
@@ -236,6 +260,16 @@ void WTrackMenu::createMenus() {
236260
}
237261
m_pSearchRelatedMenu->setEnabled(
238262
!m_pSearchRelatedMenu->isEmpty());
263+
if (!m_pSearchRelatedMenu->isEmpty()) {
264+
// We're interested in keypress Qt::Key_Left, so use our
265+
// event filter like we do for the crate checkboxes.
266+
for (const auto pObj : m_pSearchRelatedMenu->children()) {
267+
QCheckBox* pCB = qobject_cast<QCheckBox*>(pObj);
268+
if (pCB) {
269+
pCB->installEventFilter(this);
270+
}
271+
}
272+
}
239273
});
240274
connect(m_pSearchRelatedMenu,
241275
&WSearchRelatedTracksMenu::triggerSearch,
@@ -1499,6 +1533,8 @@ void WTrackMenu::slotPopulateCrateMenu() {
14991533
m_pCrateMenu);
15001534
pCheckBox->setProperty("crateId", QVariant::fromValue(crate.getId()));
15011535
pCheckBox->setEnabled(!crate.isLocked());
1536+
// We're interested in keypress Qt::Key_Left
1537+
pCheckBox->installEventFilter(this);
15021538
// Strangely, the normal styling of QActions does not automatically
15031539
// apply to QWidgetActions. The :selected pseudo-state unfortunately
15041540
// does not work with QWidgetAction. :hover works for selecting items

src/widget/wtrackmenu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class WTrackMenu : public QMenu {
109109
void slotRemoveFromDisk();
110110
const QString getDeckGroup() const;
111111

112+
bool eventFilter(QObject* pObj, QEvent* e) override;
113+
112114
signals:
113115
void loadTrackToPlayer(TrackPointer pTrack, const QString& group, bool play = false);
114116
void trackMenuVisible(bool visible);

0 commit comments

Comments
 (0)