fix: when input word in translateline and press Enter

can not type word directly again.

fix:#185
This commit is contained in:
Xiao YiFang 2023-04-17 23:19:29 +08:00 committed by xiaoyifang
parent 75b0a92be7
commit 14c4366839
3 changed files with 49 additions and 57 deletions

View file

@ -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 );

View file

@ -2476,6 +2476,7 @@ bool MainWindow::eventFilter( QObject * obj, QEvent * ev )
|| ev->type() == QEvent::KeyPress )
{
QKeyEvent * ke = static_cast<QKeyEvent*>( ev );
qDebug()<<obj<<ke->type()<<ke->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,53 +2607,28 @@ 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() )
{
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 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() )
{
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() );
setTranslateBoxTextAndClearSuffix( t, WildcardsAreAlreadyEscaped, EnablePopup );
translateLine->setCursorPosition( t.size() );
}
}
}

View file

@ -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
{
else {
ui.translateBox->translateLine()->clear();
ui.translateBox->translateLine()->setFocus();
// ui.translateBox->setText( t, true );
// ui.translateBox->translateLine()->setCursorPosition( t.size() );
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 );
}