Skip to content

Commit 0706518

Browse files
committed
Track Menu: close all menus with Left key, fix for menus with QWidgetActions
1 parent f53b0b7 commit 0706518

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/widget/wsearchrelatedtracksmenu.cpp

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

src/widget/wtrackmenu.cpp

Lines changed: 39 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>
@@ -161,6 +162,32 @@ void WTrackMenu::closeEvent(QCloseEvent* event) {
161162
emit trackMenuVisible(false);
162163
}
163164

165+
bool WTrackMenu::eventFilter(QObject* pObj, QEvent* e) {
166+
// If a checkbox in a QWidgetAction is focused, Left/Right keys are translated
167+
// to Up/Down which prevents closing the submenus with Left key like in other
168+
// submenus.
169+
// We simply call hide() of the submenu if Left is pressed.
170+
// We ignore Right.
171+
// Don't continue (close track menu) if the checkbox is at the top level.
172+
if (pObj->parent() && pObj->parent() != this && e->type() == QEvent::KeyPress) {
173+
QCheckBox* pCB = qobject_cast<QCheckBox*>(pObj);
174+
QKeyEvent* pKE = static_cast<QKeyEvent*>(e);
175+
if (!pCB || !pKE) {
176+
return QObject::eventFilter(pObj, e);
177+
}
178+
if (pKE->key() == Qt::Key_Left) {
179+
VERIFY_OR_DEBUG_ASSERT(pCB->parentWidget()) {
180+
return QObject::eventFilter(pObj, e);
181+
}
182+
pCB->parentWidget()->hide();
183+
return true;
184+
} else if (pKE->key() == Qt::Key_Right) {
185+
return true;
186+
}
187+
}
188+
return QObject::eventFilter(pObj, e);
189+
}
190+
164191
void WTrackMenu::popup(const QPoint& pos, QAction* at) {
165192
if (isEmpty()) {
166193
return;
@@ -258,6 +285,16 @@ void WTrackMenu::createMenus() {
258285
m_pSearchRelatedMenu->setEnabled(
259286
!m_pSearchRelatedMenu->isEmpty());
260287
m_bSearchRelatedMenuLoaded = true;
288+
if (!m_pSearchRelatedMenu->isEmpty()) {
289+
// We're interested in keypress Qt::Key_Left, so use our
290+
// event filter like we do for the crate checkboxes.
291+
for (auto* pObj : m_pSearchRelatedMenu->children()) {
292+
QCheckBox* pCB = qobject_cast<QCheckBox*>(pObj);
293+
if (pCB) {
294+
pCB->installEventFilter(this);
295+
}
296+
}
297+
}
261298
});
262299
connect(m_pSearchRelatedMenu,
263300
&WSearchRelatedTracksMenu::triggerSearch,
@@ -1651,6 +1688,8 @@ void WTrackMenu::slotPopulateCrateMenu() {
16511688
m_pCrateMenu);
16521689
pCheckBox->setProperty("crateId", QVariant::fromValue(crate.getId()));
16531690
pCheckBox->setEnabled(!crate.isLocked());
1691+
// We're interested in keypress Qt::Key_Left
1692+
pCheckBox->installEventFilter(this);
16541693
// Strangely, the normal styling of QActions does not automatically
16551694
// apply to QWidgetActions. The :selected pseudo-state unfortunately
16561695
// 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
@@ -115,6 +115,8 @@ class WTrackMenu : public QMenu {
115115
void slotRemoveFromDisk();
116116
const QString getDeckGroup() const;
117117

118+
bool eventFilter(QObject* pObj, QEvent* e) override;
119+
118120
signals:
119121
#ifdef __STEM__
120122
void loadTrackToPlayer(TrackPointer pTrack,

0 commit comments

Comments
 (0)