From 19e848ec5007d1780ad6638745d5620161fbb1d2 Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Thu, 21 Sep 2023 07:44:58 +0800 Subject: [PATCH] fix: enter event in translate textline (#1158) * fix: enter event in translate textline * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/ui/mainwindow.cc | 2 ++ src/ui/scanpopup.cc | 4 +++- src/ui/translatebox.cc | 35 ++++------------------------------- src/ui/translatebox.hh | 1 - 4 files changed, 9 insertions(+), 33 deletions(-) diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index 05453bea..14b3f8cd 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -2442,6 +2442,8 @@ void MainWindow::setInputLineText( QString text, WildcardPolicy wildcardPolicy, else { translateBox->setText( text, popupAction == EnablePopup ); } + + updateSuggestionList(); GlobalBroadcaster::instance()->translateLineText = text; } diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index 8ac833b1..8833fda6 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -124,7 +124,7 @@ ScanPopup::ScanPopup( QWidget * parent, definition->installEventFilter( this ); this->installEventFilter( this ); - connect( ui.translateBox->translateLine(), &QLineEdit::textChanged, this, &ScanPopup::translateInputChanged ); + connect( ui.translateBox->translateLine(), &QLineEdit::textEdited, this, &ScanPopup::translateInputChanged ); connect( ui.translateBox, &TranslateBox::returnPressed, this, &ScanPopup::translateInputFinished ); @@ -694,6 +694,8 @@ void ScanPopup::typingEvent( QString const & t ) ui.translateBox->setText( t, true ); ui.translateBox->translateLine()->setCursorPosition( t.size() ); } + + updateSuggestionList(); } bool ScanPopup::eventFilter( QObject * watched, QEvent * event ) diff --git a/src/ui/translatebox.cc b/src/ui/translatebox.cc index 3bfdaabc..843fc745 100644 --- a/src/ui/translatebox.cc +++ b/src/ui/translatebox.cc @@ -50,29 +50,9 @@ TranslateBox::TranslateBox( QWidget * parent ): completer->setCompletionMode( QCompleter::UnfilteredPopupCompletion ); completer->setMaxVisibleItems( 16 ); - connect( completer, - QOverload< const QString & >::of( &QCompleter::activated ), - translate_line, - [ & ]( const QString & text ) { - translate_line->setText( text ); - emit returnPressed(); - } ); - - - connect( completer, - QOverload< const QString & >::of( &QCompleter::highlighted ), - translate_line, - [ & ]( const QString & text ) { - selectedItem = true; - } ); - - connect( translate_line, - &QLineEdit::returnPressed, - [ this ]() { - if ( selectedItem ) - return; - emit returnPressed(); - } ); + connect( translate_line, &QLineEdit::returnPressed, [ this ]() { + emit returnPressed(); + } ); } void TranslateBox::setText( const QString & text, bool showPopup ) @@ -96,7 +76,7 @@ void TranslateBox::setSizePolicy( QSizePolicy policy ) void TranslateBox::setModel( QStringList & _words ) { - disconnect( completer, 0, translate_line, 0 ); + disconnect( completer, QOverload< const QString & >::of( &QCompleter::activated ), translate_line, nullptr ); const auto model = static_cast< QStringListModel * >( completer->model() ); model->setStringList( _words ); @@ -109,13 +89,6 @@ void TranslateBox::setModel( QStringList & _words ) [ & ]( const QString & text ) { translate_line->setText( text ); emit returnPressed(); - selectedItem = false; - } ); - connect( completer, - QOverload< const QString & >::of( &QCompleter::highlighted ), - translate_line, - [ & ]( const QString & text ) { - selectedItem = true; } ); } diff --git a/src/ui/translatebox.hh b/src/ui/translatebox.hh index efa35d06..e6a3b2b8 100644 --- a/src/ui/translatebox.hh +++ b/src/ui/translatebox.hh @@ -36,7 +36,6 @@ private slots: private: QLineEdit * translate_line; - bool selectedItem = false; bool m_popupEnabled; QCompleter * completer; QStringList words;