From 14c43668392257060f1d55a459f0c74d42ecfa92 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Mon, 17 Apr 2023 23:19:29 +0800 Subject: [PATCH] fix: when input word in translateline and press Enter can not type word directly again. fix:#185 --- src/ui/articleview.cc | 6 ++-- src/ui/mainwindow.cc | 66 +++++++++++++------------------------------ src/ui/scanpopup.cc | 34 ++++++++++++++++------ 3 files changed, 49 insertions(+), 57 deletions(-) diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index c736ea1c..dde1b760 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -818,10 +818,10 @@ bool ArticleView::eventFilter( QObject * obj, QEvent * ev ) if( handled ) { if( result == Gestures::ZOOM_IN ) - zoomIn(); + emit zoomIn(); else if( result == Gestures::ZOOM_OUT ) - zoomOut(); + emit zoomOut(); else if( result == Gestures::SWIPE_LEFT ) back(); @@ -881,7 +881,7 @@ bool ArticleView::eventFilter( QObject * obj, QEvent * ev ) } } else - if ( ev->type() == QEvent::KeyPress || ev->type ()==QEvent::ShortcutOverride) + if ( ev->type() == QEvent::KeyPress ) { QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev ); diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index 016f457f..1800bab5 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -2476,6 +2476,7 @@ bool MainWindow::eventFilter( QObject * obj, QEvent * ev ) || ev->type() == QEvent::KeyPress ) { QKeyEvent * ke = static_cast( ev ); + qDebug()<type()<text(); // Handle F3/Shift+F3 shortcuts if ( ke->key() == Qt::Key_F3 ) @@ -2582,8 +2583,7 @@ bool MainWindow::eventFilter( QObject * obj, QEvent * ev ) else if ( obj == wordList ) { - if (ev->type() == QEvent::KeyPress || ev->type() == QEvent::ShortcutOverride) - { + if ( ev->type() == QEvent::KeyPress ) { QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev ); if ( keyEvent->matches( QKeySequence::MoveToPreviousLine ) && @@ -2607,54 +2607,29 @@ bool MainWindow::eventFilter( QObject * obj, QEvent * ev ) return cfg.preferences.searchInDock; } + } + } - // Handle typing events used to initiate new lookups - // TODO: refactor to eliminate duplication (see below) + if ( ev->type() == QEvent::KeyPress && obj != translateLine ) { - if ( keyEvent->modifiers() & - ( Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier ) ) - return false; // A non-typing modifier is pressed + if ( const auto key_event = dynamic_cast< QKeyEvent * >(ev); key_event->modifiers() == Qt::NoModifier ) { + const QString text = key_event->text(); - if ( Utils::ignoreKeyEvent(keyEvent)) + if ( Utils::ignoreKeyEvent( key_event ) || + key_event->key() == Qt::Key_Return || + key_event->key() == Qt::Key_Enter ) return false; // Those key have other uses than to start typing - // or don't make sense - - QString text = keyEvent->text(); - - if ( text.size() ) - { + // or don't make sense + if ( !text.isEmpty() ) { typingEvent( text ); return true; } } + } - else - if (obj == ui.dictsList) { - if ( ev->type() == QEvent::KeyPress || ev->type() == QEvent::ShortcutOverride) - { - QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev ); - // Handle typing events used to initiate new lookups - // TODO: refactor to eliminate duplication (see above) - if ( keyEvent->modifiers() & - ( Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier ) ) - return false; // A non-typing modifier is pressed - - if ( Utils::ignoreKeyEvent(keyEvent)) - return false; // Those key have other uses than to start typing - // or don't make sense - - QString text = keyEvent->text(); - - if ( text.size() ) - { - typingEvent( text ); - return true; - } - } - } - return QMainWindow::eventFilter( obj, ev ); + return QMainWindow::eventFilter( obj, ev ); return false; } @@ -2770,13 +2745,12 @@ void MainWindow::typingEvent( QString const & t ) if ( ( cfg.preferences.searchInDock && ui.searchPane->isFloating() ) || ui.dictsPane->isFloating() ) ui.searchPane->activateWindow(); - if( translateLine->isEnabled() ) - { - translateLine->clear(); - translateLine->setFocus(); - // Escaping the typed-in characters is the user's responsibility. - // setTranslateBoxTextAndClearSuffix( t, WildcardsAreAlreadyEscaped, EnablePopup ); - // translateLine->setCursorPosition( t.size() ); + if ( translateLine->isEnabled() ) { + translateLine->clear(); + translateLine->setFocus(); + // Escaping the typed-in characters is the user's responsibility. + setTranslateBoxTextAndClearSuffix( t, WildcardsAreAlreadyEscaped, EnablePopup ); + translateLine->setCursorPosition( t.size() ); } } } diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index 98a8febe..81861976 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -123,6 +123,8 @@ ScanPopup::ScanPopup( QWidget * parent, ui.translateBox->wordList()->attachFinder( &wordFinder ); ui.translateBox->wordList()->setFocusPolicy(Qt::ClickFocus); ui.translateBox->translateLine()->installEventFilter( this ); + definition->installEventFilter(this); + this->installEventFilter(this); connect( ui.translateBox->translateLine(), &QLineEdit::textChanged, this, &ScanPopup::translateInputChanged ); @@ -736,16 +738,14 @@ vector< sptr< Dictionary::Class > > const & ScanPopup::getActiveDicts() void ScanPopup::typingEvent( QString const & t ) { - if ( t == "\n" || t == "\r" ) - { + if ( t == "\n" || t == "\r" ) { focusTranslateLine(); } - else - { - ui.translateBox->translateLine()->clear(); - ui.translateBox->translateLine()->setFocus(); - // ui.translateBox->setText( t, true ); - // ui.translateBox->translateLine()->setCursorPosition( t.size() ); + else { + ui.translateBox->translateLine()->clear(); + ui.translateBox->translateLine()->setFocus(); + ui.translateBox->setText( t, true ); + ui.translateBox->translateLine()->setCursorPosition( t.size() ); } } @@ -773,6 +773,24 @@ bool ScanPopup::eventFilter( QObject * watched, QEvent * event ) } } + if ( event->type() == QEvent::KeyPress && watched != ui.translateBox->translateLine() ) { + + if ( const auto key_event = dynamic_cast< QKeyEvent * >(event); key_event->modifiers() == Qt::NoModifier ) { + const QString text = key_event->text(); + + if ( Utils::ignoreKeyEvent( key_event ) || + key_event->key() == Qt::Key_Return || + key_event->key() == Qt::Key_Enter ) + return false; // Those key have other uses than to start typing + // or don't make sense + if ( !text.isEmpty() ) { + typingEvent( text ); + return true; + } + } + + } + return QMainWindow::eventFilter( watched, event ); }