From 9e4642d435085f27134ed6c8431734dcacf65834 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Thu, 15 Oct 2020 23:11:17 +0200 Subject: [PATCH] Fix delete action switching via 'Shift' when sub-context-menu is open Switching the 'Move to Trash' action to 'Delete' using the shift key does not work when the mouse hovers a submenu. This fix resolves the issue by using an event filter instead of the key event functions. BUG: 425997 --- src/dolphincontextmenu.cpp | 27 +++++++++++++++++++-------- src/dolphincontextmenu.h | 4 ++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 126baf6e8..0c192de38 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -61,6 +61,8 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, // or the items itself. To increase the performance both lists are cached. const DolphinView* view = m_mainWindow->activeViewContainer()->view(); m_selectedItems = view->selectedItems(); + + installEventFilter(this); } DolphinContextMenu::~DolphinContextMenu() @@ -110,20 +112,28 @@ DolphinContextMenu::Command DolphinContextMenu::open() return m_command; } -void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) +void DolphinContextMenu::childEvent(QChildEvent* event) { - if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); + if(event->added()) { + event->child()->installEventFilter(this); } - QMenu::keyPressEvent(ev); + QMenu::childEvent(event); } -void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev) +bool DolphinContextMenu::eventFilter(QObject* dest, QEvent* event) { - if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(DolphinRemoveAction::ShiftState::Released); + if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { + QKeyEvent* keyEvent = static_cast(event); + if(m_removeAction && keyEvent->key() == Qt::Key_Shift) { + if(event->type() == QEvent::KeyPress) { + m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); + } else { + m_removeAction->update(DolphinRemoveAction::ShiftState::Released); + } + return true; + } } - QMenu::keyReleaseEvent(ev); + return QMenu::eventFilter(dest, event); } void DolphinContextMenu::openTrashContextMenu() @@ -198,6 +208,7 @@ void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemA QMenu* menu = newFileMenu->menu(); menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); + menu->setParent(this, Qt::Popup); addMenu(menu); addSeparator(); diff --git a/src/dolphincontextmenu.h b/src/dolphincontextmenu.h index 523c0555a..7f0b6988a 100644 --- a/src/dolphincontextmenu.h +++ b/src/dolphincontextmenu.h @@ -74,8 +74,8 @@ public: Command open(); protected: - void keyPressEvent(QKeyEvent *ev) override; - void keyReleaseEvent(QKeyEvent *ev) override; + void childEvent(QChildEvent* event) override; + bool eventFilter(QObject* dest, QEvent* event) override; private: void openTrashContextMenu(); -- 2.47.3