Skip to content

Commit e94d40d

Browse files
committed
lxqtmainmenu: eventFilter: factorise code
Signed-off-by: Adel KARA SLIMANE <adel.ks@zegrapher.com>
1 parent 6e51618 commit e94d40d

File tree

1 file changed

+58
-68
lines changed

1 file changed

+58
-68
lines changed

plugin-mainmenu/lxqtmainmenu.cpp

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -641,28 +641,55 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
641641
if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
642642
keyEvent = static_cast<QKeyEvent*>(event);
643643

644-
if(obj == mButton.parentWidget())
644+
// Check if the close/open shortcut has been triggered
645+
// close the menu if so
646+
if(event->type() == QEvent::KeyRelease)
645647
{
646-
// the application is given a new QStyle
647-
if(event->type() == QEvent::StyleChange)
648+
static const auto key_meta = QMetaEnum::fromType<Qt::Key>();
649+
// if our shortcut key is pressed while the menu is open, close the menu
650+
QFlags<Qt::KeyboardModifier> mod = keyEvent->modifiers();
651+
switch (keyEvent->key()) {
652+
case Qt::Key_Alt:
653+
mod &= ~Qt::AltModifier;
654+
break;
655+
case Qt::Key_Control:
656+
mod &= ~Qt::ControlModifier;
657+
break;
658+
case Qt::Key_Shift:
659+
mod &= ~Qt::ShiftModifier;
660+
break;
661+
case Qt::Key_Super_L:
662+
case Qt::Key_Super_R:
663+
mod &= ~Qt::MetaModifier;
664+
break;
665+
}
666+
const QString press = QKeySequence{static_cast<int>(mod)}.toString() % QString::fromLatin1(key_meta.valueToKey(keyEvent->key())).remove(0, 4);
667+
if (press == mShortcutSeq)
648668
{
649-
setMenuFontSize();
650-
setButtonIcon();
669+
mHideTimer.start();
670+
mMenu->hide(); // close the app menu
671+
return true;
651672
}
652673
}
653-
else if(obj == mSearchEdit)
674+
675+
if(obj == mButton.parentWidget() && event->type() == QEvent::StyleChange)
654676
{
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-
}
677+
setMenuFontSize();
678+
setButtonIcon();
664679
}
665-
else if(mWriteToSearch && event->type() == QEvent::KeyPress &&
680+
681+
// Redirect arrow up and arrow down keys to the view that shows search results
682+
// so one can browse among them. Only do this when mWriteToSearch = true
683+
else if(mWriteToSearch && obj == mSearchEdit && event->type() == QEvent::KeyPress &&
684+
(keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down))
685+
{
686+
qApp->sendEvent(mSearchView, keyEvent);
687+
return true;
688+
}
689+
690+
// Redirect text edition input to the search edit line, without consideration to focus
691+
// Only do this when mWriteToSearch = true
692+
else if(mWriteToSearch && obj != mSearchEdit && event->type() == QEvent::KeyPress &&
666693
(keyEvent->key() == Qt::Key_Backspace ||
667694
keyEvent->key() == Qt::Key_Space ||
668695
(keyEvent->text().size() == 1 && keyEvent->text()[0].isLetterOrNumber())))
@@ -671,65 +698,28 @@ bool LXQtMainMenu::eventFilter(QObject *obj, QEvent *event)
671698
qApp->sendEvent(mSearchEdit, keyEvent);
672699
return true;
673700
}
674-
else
701+
else if (obj == mMenu)
675702
{
676-
if(event->type() == QEvent::KeyRelease)
703+
if (event->type() == QEvent::Resize)
677704
{
678-
static const auto key_meta = QMetaEnum::fromType<Qt::Key>();
679-
// if our shortcut key is pressed while the menu is open, close the menu
680-
QFlags<Qt::KeyboardModifier> mod = keyEvent->modifiers();
681-
switch (keyEvent->key()) {
682-
case Qt::Key_Alt:
683-
mod &= ~Qt::AltModifier;
684-
break;
685-
case Qt::Key_Control:
686-
mod &= ~Qt::ControlModifier;
687-
break;
688-
case Qt::Key_Shift:
689-
mod &= ~Qt::ShiftModifier;
690-
break;
691-
case Qt::Key_Super_L:
692-
case Qt::Key_Super_R:
693-
mod &= ~Qt::MetaModifier;
694-
break;
695-
}
696-
const QString press = QKeySequence{static_cast<int>(mod)}.toString() % QString::fromLatin1(key_meta.valueToKey(keyEvent->key())).remove(0, 4);
697-
if (press == mShortcutSeq)
705+
QResizeEvent * e = dynamic_cast<QResizeEvent *>(event);
706+
if (e->oldSize().isValid() && e->oldSize() != e->size())
698707
{
699-
mHideTimer.start();
700-
mMenu->hide(); // close the app menu
701-
return true;
708+
mMenu->move(calculatePopupWindowPos(e->size()).topLeft());
702709
}
703710
}
704-
705-
if (obj == mMenu)
711+
else if (event->type() == QEvent::KeyPress &&
712+
keyEvent->key() == Qt::Key_Escape &&
713+
!mSearchEdit->text().isEmpty())
706714
{
707-
if (event->type() == QEvent::Resize)
708-
{
709-
QResizeEvent * e = dynamic_cast<QResizeEvent *>(event);
710-
if (e->oldSize().isValid() && e->oldSize() != e->size())
711-
{
712-
mMenu->move(calculatePopupWindowPos(e->size()).topLeft());
713-
}
714-
} else if (event->type() == QEvent::KeyPress)
715-
{
716-
if (keyEvent->key() == Qt::Key_Escape)
717-
{
718-
if (!mSearchEdit->text().isEmpty())
719-
{
720-
mSearchEdit->setText(QString{});
721-
//filter out this to not close the menu
722-
return true;
723-
}
724-
}
725-
} else if (QEvent::ActionChanged == event->type()
726-
|| QEvent::ActionAdded == event->type())
727-
{
728-
//filter this if we are performing heavy changes to reduce flicker
729-
if (mHeavyMenuChanges)
730-
return true;
731-
}
715+
mSearchEdit->clear();
716+
//filter out this to not close the menu
717+
return true;
732718
}
719+
else if (mHeavyMenuChanges &&
720+
(QEvent::ActionChanged == event->type() ||
721+
QEvent::ActionAdded == event->type()))
722+
return true;
733723
}
734724
return false;
735725
}

0 commit comments

Comments
 (0)