30
30
#include " lxqtmainmenuconfiguration.h"
31
31
#include " ../panel/lxqtpanel.h"
32
32
#include " actionview.h"
33
+ #include " qnamespace.h"
33
34
#include < QAction>
34
35
#include < QTimer>
35
36
#include < QMessageBox>
@@ -102,13 +103,15 @@ LXQtMainMenu::LXQtMainMenu(const ILXQtPanelPluginStartupInfo &startupInfo):
102
103
connect (&mButton , &QToolButton::clicked, this , &LXQtMainMenu::showHideMenu);
103
104
104
105
mSearchView = new ActionView;
106
+ mSearchView ->installEventFilter (this );
105
107
mSearchView ->setVisible (false );
106
108
mSearchView ->setContextMenuPolicy (Qt::CustomContextMenu);
107
109
connect (mSearchView , &QAbstractItemView::activated, this , &LXQtMainMenu::showHideMenu);
108
110
connect (mSearchView , &ActionView::requestShowHideMenu, this , &LXQtMainMenu::showHideMenu);
109
111
connect (mSearchView , &QWidget::customContextMenuRequested, this , &LXQtMainMenu::onRequestingCustomMenu);
110
112
mSearchViewAction ->setDefaultWidget (mSearchView );
111
113
mSearchEdit = new QLineEdit;
114
+ mSearchEdit ->installEventFilter (this );
112
115
mSearchEdit ->setClearButtonEnabled (true );
113
116
mSearchEdit ->setPlaceholderText (LXQtMainMenu::tr (" Search..." ));
114
117
connect (mSearchEdit , &QLineEdit::textChanged, this , [this ] (QString const &) {
@@ -399,6 +402,7 @@ static void menuInstallEventFilter(QMenu * menu, QObject * watcher)
399
402
{
400
403
if (action->menu ())
401
404
menuInstallEventFilter (action->menu (), watcher); // recursion
405
+ else action->installEventFilter (watcher);
402
406
}
403
407
menu->installEventFilter (watcher);
404
408
}
@@ -633,6 +637,10 @@ struct MatchAction
633
637
634
638
bool LXQtMainMenu::eventFilter (QObject *obj, QEvent *event)
635
639
{
640
+ QKeyEvent* keyEvent = nullptr ;
641
+ if (event->type () == QEvent::KeyPress || event->type () == QEvent::KeyRelease)
642
+ keyEvent = static_cast <QKeyEvent*>(event);
643
+
636
644
if (obj == mButton .parentWidget ())
637
645
{
638
646
// the application is given a new QStyle
@@ -642,13 +650,33 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
642
650
setButtonIcon ();
643
651
}
644
652
}
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
646
675
{
647
676
if (event->type () == QEvent::KeyRelease)
648
677
{
649
678
static const auto key_meta = QMetaEnum::fromType<Qt::Key>();
650
679
// if our shortcut key is pressed while the menu is open, close the menu
651
- QKeyEvent* keyEvent = static_cast <QKeyEvent*>(event);
652
680
QFlags<Qt::KeyboardModifier> mod = keyEvent->modifiers ();
653
681
switch (keyEvent->key ()) {
654
682
case Qt::Key_Alt:
@@ -672,30 +700,6 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
672
700
mMenu ->hide (); // close the app menu
673
701
return true ;
674
702
}
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
- }
699
703
}
700
704
701
705
if (obj == mMenu )
@@ -709,8 +713,7 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
709
713
}
710
714
} else if (event->type () == QEvent::KeyPress)
711
715
{
712
- QKeyEvent * e = dynamic_cast <QKeyEvent*>(event);
713
- if (Qt::Key_Escape == e->key ())
716
+ if (keyEvent->key () == Qt::Key_Escape)
714
717
{
715
718
if (!mSearchEdit ->text ().isEmpty ())
716
719
{
0 commit comments