UnitedEntry: fix key press event

This commit is contained in:
Le Tan 2022-06-19 14:38:12 +08:00
parent cd53eedc7f
commit c08296bc3b
2 changed files with 30 additions and 15 deletions

View File

@ -181,7 +181,7 @@ void UnitedEntry::deactivateUnitedEntry()
hide(); hide();
} }
void UnitedEntry::keyPressEvent(QKeyEvent *p_event) bool UnitedEntry::handleLineEditKeyPress(QKeyEvent *p_event)
{ {
const int key = p_event->key(); const int key = p_event->key();
const int modifiers = p_event->modifiers(); const int modifiers = p_event->modifiers();
@ -194,7 +194,7 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
Q_FALLTHROUGH(); Q_FALLTHROUGH();
case Qt::Key_Escape: case Qt::Key_Escape:
exitUnitedEntry(); exitUnitedEntry();
return; return true;
// Up/Down Ctrl+K/J to navigate to previous/next item. // Up/Down Ctrl+K/J to navigate to previous/next item.
case Qt::Key_Up: case Qt::Key_Up:
@ -206,7 +206,7 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
} else if (m_entryListWidget && m_entryListWidget->isVisible()) { } else if (m_entryListWidget && m_entryListWidget->isVisible()) {
IUnitedEntry::handleActionCommon(act, m_entryListWidget.data()); IUnitedEntry::handleActionCommon(act, m_entryListWidget.data());
} }
return; return true;
case Qt::Key_K: case Qt::Key_K:
act = IUnitedEntry::Action::PreviousItem; act = IUnitedEntry::Action::PreviousItem;
@ -218,8 +218,9 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
} else if (m_entryListWidget && m_entryListWidget->isVisible()) { } else if (m_entryListWidget && m_entryListWidget->isVisible()) {
IUnitedEntry::handleActionCommon(act, m_entryListWidget.data()); IUnitedEntry::handleActionCommon(act, m_entryListWidget.data());
} }
return true;
} }
return; break;
case Qt::Key_Enter: case Qt::Key_Enter:
Q_FALLTHROUGH(); Q_FALLTHROUGH();
@ -227,7 +228,7 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (m_lastEntry) { if (m_lastEntry) {
m_lastEntry->handleAction(IUnitedEntry::Action::Activate); m_lastEntry->handleAction(IUnitedEntry::Action::Activate);
} }
return; return true;
case Qt::Key_E: case Qt::Key_E:
if (WidgetUtils::isViControlModifier(modifiers)) { if (WidgetUtils::isViControlModifier(modifiers)) {
@ -237,8 +238,9 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (!entry.m_name.isEmpty()) { if (!entry.m_name.isEmpty()) {
m_lineEdit->setText(entry.m_name + QLatin1Char(' ')); m_lineEdit->setText(entry.m_name + QLatin1Char(' '));
} }
return true;
} }
return; break;
case Qt::Key_F: case Qt::Key_F:
if (WidgetUtils::isViControlModifier(modifiers)) { if (WidgetUtils::isViControlModifier(modifiers)) {
@ -248,8 +250,9 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (!entry.m_name.isEmpty()) { if (!entry.m_name.isEmpty()) {
m_lineEdit->setSelection(0, entry.m_name.size()); m_lineEdit->setSelection(0, entry.m_name.size());
} }
return true;
} }
return; break;
case Qt::Key_D: case Qt::Key_D:
if (WidgetUtils::isViControlModifier(modifiers)) { if (WidgetUtils::isViControlModifier(modifiers)) {
@ -257,8 +260,9 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (m_lastEntry) { if (m_lastEntry) {
m_lastEntry->stop(); m_lastEntry->stop();
} }
return true;
} }
return; break;
case Qt::Key_L: case Qt::Key_L:
if (WidgetUtils::isViControlModifier(modifiers)) { if (WidgetUtils::isViControlModifier(modifiers)) {
@ -266,8 +270,9 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (m_lastEntry) { if (m_lastEntry) {
m_lastEntry->handleAction(IUnitedEntry::Action::LevelUp); m_lastEntry->handleAction(IUnitedEntry::Action::LevelUp);
} }
return true;
} }
return; break;
case Qt::Key_I: case Qt::Key_I:
if (WidgetUtils::isViControlModifier(modifiers)) { if (WidgetUtils::isViControlModifier(modifiers)) {
@ -275,8 +280,9 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (m_lastEntry) { if (m_lastEntry) {
m_lastEntry->handleAction(IUnitedEntry::Action::ExpandCollapse); m_lastEntry->handleAction(IUnitedEntry::Action::ExpandCollapse);
} }
return true;
} }
return; break;
case Qt::Key_B: case Qt::Key_B:
if (WidgetUtils::isViControlModifier(modifiers)) { if (WidgetUtils::isViControlModifier(modifiers)) {
@ -284,14 +290,15 @@ void UnitedEntry::keyPressEvent(QKeyEvent *p_event)
if (m_lastEntry) { if (m_lastEntry) {
m_lastEntry->handleAction(IUnitedEntry::Action::ExpandCollapseAll); m_lastEntry->handleAction(IUnitedEntry::Action::ExpandCollapseAll);
} }
return true;
} }
return; break;
default: default:
break; break;
} }
QFrame::keyPressEvent(p_event); return false;
} }
void UnitedEntry::clear() void UnitedEntry::clear()
@ -432,7 +439,7 @@ void UnitedEntry::setBusy(bool p_busy)
bool UnitedEntry::eventFilter(QObject *p_watched, QEvent *p_event) bool UnitedEntry::eventFilter(QObject *p_watched, QEvent *p_event)
{ {
if (p_watched == m_popup || p_watched == m_lineEdit) { if (p_watched == m_popup) {
if (p_event->type() == QEvent::KeyPress) { if (p_event->type() == QEvent::KeyPress) {
auto eve = static_cast<QKeyEvent *>(p_event); auto eve = static_cast<QKeyEvent *>(p_event);
switch (eve->key()) { switch (eve->key()) {
@ -449,6 +456,13 @@ bool UnitedEntry::eventFilter(QObject *p_watched, QEvent *p_event)
break; break;
} }
} }
} else if (p_watched == m_lineEdit) {
if (p_event->type() == QEvent::KeyPress) {
auto eve = static_cast<QKeyEvent *>(p_event);
if (handleLineEditKeyPress(eve)) {
return true;
}
}
} }
return QFrame::eventFilter(p_watched, p_event); return QFrame::eventFilter(p_watched, p_event);

View File

@ -29,8 +29,6 @@ namespace vnotex
QAction *getTriggerAction(); QAction *getTriggerAction();
protected: protected:
void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE;
private: private:
@ -69,6 +67,9 @@ namespace vnotex
QSize preferredSize() const; QSize preferredSize() const;
// Return true if want to stop the propogation.
bool handleLineEditKeyPress(QKeyEvent *p_event);
QMainWindow *m_mainWindow = nullptr; QMainWindow *m_mainWindow = nullptr;
LineEditWithSnippet *m_lineEdit = nullptr; LineEditWithSnippet *m_lineEdit = nullptr;