Skip to content

Commit 6e51618

Browse files
committed
Install event filters to redirect text to the search line
Signed-off-by: Adel KARA SLIMANE <adel.ks@zegrapher.com>
1 parent ec5f9cb commit 6e51618

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

plugin-mainmenu/lxqtmainmenu.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "lxqtmainmenuconfiguration.h"
3131
#include "../panel/lxqtpanel.h"
3232
#include "actionview.h"
33+
#include "qnamespace.h"
3334
#include <QAction>
3435
#include <QTimer>
3536
#include <QMessageBox>
@@ -102,13 +103,15 @@ LXQtMainMenu::LXQtMainMenu(const ILXQtPanelPluginStartupInfo &startupInfo):
102103
connect(&mButton, &QToolButton::clicked, this, &LXQtMainMenu::showHideMenu);
103104

104105
mSearchView = new ActionView;
106+
mSearchView->installEventFilter(this);
105107
mSearchView->setVisible(false);
106108
mSearchView->setContextMenuPolicy(Qt::CustomContextMenu);
107109
connect(mSearchView, &QAbstractItemView::activated, this, &LXQtMainMenu::showHideMenu);
108110
connect(mSearchView, &ActionView::requestShowHideMenu, this, &LXQtMainMenu::showHideMenu);
109111
connect(mSearchView, &QWidget::customContextMenuRequested, this, &LXQtMainMenu::onRequestingCustomMenu);
110112
mSearchViewAction->setDefaultWidget(mSearchView);
111113
mSearchEdit = new QLineEdit;
114+
mSearchEdit->installEventFilter(this);
112115
mSearchEdit->setClearButtonEnabled(true);
113116
mSearchEdit->setPlaceholderText(LXQtMainMenu::tr("Search..."));
114117
connect(mSearchEdit, &QLineEdit::textChanged, this, [this] (QString const &) {
@@ -399,6 +402,7 @@ static void menuInstallEventFilter(QMenu * menu, QObject * watcher)
399402
{
400403
if (action->menu())
401404
menuInstallEventFilter(action->menu(), watcher); // recursion
405+
else action->installEventFilter(watcher);
402406
}
403407
menu->installEventFilter(watcher);
404408
}
@@ -633,6 +637,10 @@ struct MatchAction
633637

634638
bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
635639
{
640+
QKeyEvent* keyEvent = nullptr;
641+
if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
642+
keyEvent = static_cast<QKeyEvent*>(event);
643+
636644
if(obj == mButton.parentWidget())
637645
{
638646
// the application is given a new QStyle
@@ -642,13 +650,33 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
642650
setButtonIcon();
643651
}
644652
}
645-
else if(QMenu* menu = qobject_cast<QMenu*>(obj))
653+
else if(obj == mSearchEdit)
654+
{
655+
if(event->type() == QEvent::KeyPress)
656+
{
657+
if(keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down)
658+
{
659+
qApp->sendEvent(mSearchView, keyEvent);
660+
return true;
661+
}
662+
return false;
663+
}
664+
}
665+
else if(mWriteToSearch && event->type() == QEvent::KeyPress &&
666+
(keyEvent->key() == Qt::Key_Backspace ||
667+
keyEvent->key() == Qt::Key_Space ||
668+
(keyEvent->text().size() == 1 && keyEvent->text()[0].isLetterOrNumber())))
669+
{
670+
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
671+
qApp->sendEvent(mSearchEdit, keyEvent);
672+
return true;
673+
}
674+
else
646675
{
647676
if(event->type() == QEvent::KeyRelease)
648677
{
649678
static const auto key_meta = QMetaEnum::fromType<Qt::Key>();
650679
// if our shortcut key is pressed while the menu is open, close the menu
651-
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
652680
QFlags<Qt::KeyboardModifier> mod = keyEvent->modifiers();
653681
switch (keyEvent->key()) {
654682
case Qt::Key_Alt:
@@ -672,30 +700,6 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
672700
mMenu->hide(); // close the app menu
673701
return true;
674702
}
675-
676-
QChar key = keyEvent->key();
677-
678-
if(mWriteToSearch && (key.isLetterOrNumber() || key == Qt::Key_Backspace))
679-
{
680-
qApp->sendEvent(mSearchEdit, keyEvent);
681-
return true;
682-
}
683-
else // go to the menu item which starts with the pressed key if there is an active action.
684-
{
685-
if(! key.isLetterOrNumber())
686-
return false;
687-
688-
QAction* action = menu->activeAction();
689-
if(action !=nullptr) {
690-
QList<QAction*> actions = menu->actions();
691-
QList<QAction*>::iterator it = std::find(actions.begin(), actions.end(), action);
692-
it = std::find_if(it + 1, actions.end(), MatchAction(key));
693-
if(it == actions.end())
694-
it = std::find_if(actions.begin(), it, MatchAction(key));
695-
if(it != actions.end())
696-
menu->setActiveAction(*it);
697-
}
698-
}
699703
}
700704

701705
if (obj == mMenu)
@@ -709,8 +713,7 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
709713
}
710714
} else if (event->type() == QEvent::KeyPress)
711715
{
712-
QKeyEvent * e = dynamic_cast<QKeyEvent*>(event);
713-
if (Qt::Key_Escape == e->key())
716+
if (keyEvent->key() == Qt::Key_Escape)
714717
{
715718
if (!mSearchEdit->text().isEmpty())
716719
{

0 commit comments

Comments
 (0)