From 25dce23d50598ffdc26cb4e02e1742f7c4f9cb42 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Tue, 12 May 2009 13:25:18 +0000 Subject: [PATCH] + Pressing Enter in translateLine focuses on definition. + Typing any text key switches back to translateLine and puts it there. --- src/articleview.cc | 26 ++++++++++++++++++++++++++ src/articleview.hh | 6 ++++++ src/mainwindow.cc | 12 ++++++++++++ src/mainwindow.hh | 1 + 4 files changed, 45 insertions(+) diff --git a/src/articleview.cc b/src/articleview.cc index 11f8124d..af1aabf3 100644 --- a/src/articleview.cc +++ b/src/articleview.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include "folding.hh" #include "wstring_qt.hh" @@ -63,6 +64,8 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, ui.definition->addAction( &pasteAction ); connect( &pasteAction, SIGNAL( triggered() ), this, SLOT( pasteTriggered() ) ); + ui.definition->installEventFilter( this ); + // Load the default blank page instantly, so there would be no flicker. QString contentType; @@ -224,6 +227,29 @@ void ArticleView::cleanupTemp() } } +bool ArticleView::eventFilter( QObject * obj, QEvent * ev ) +{ + if ( obj == ui.definition ) + { + if ( ev->type() == QEvent::KeyPress ) + { + QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev ); + + QString text = keyEvent->text(); + + if ( text.size() ) + { + emit typingEvent( text ); + return true; + } + } + } + else + return QFrame::eventFilter( obj, ev ); + + return false; +} + void ArticleView::linkClicked( QUrl const & url ) { diff --git a/src/articleview.hh b/src/articleview.hh index 24f040aa..e6f03075 100644 --- a/src/articleview.hh +++ b/src/articleview.hh @@ -120,6 +120,10 @@ signals: void showDefinitionInNewTab( QString const & word, unsigned group, QString const & fromArticle ); + /// Emitted when user types a text key. This should typically be used to + /// switch focus to word input. + void typingEvent( QString const & text ); + private slots: void loadFinished( bool ok ); @@ -152,6 +156,8 @@ private: /// Attempts removing last temporary file created. void cleanupTemp(); + bool eventFilter( QObject * obj, QEvent * ev ); + protected: // We need this to hide the search bar when we're showed diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 2702beb0..d65ab849 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -499,6 +499,9 @@ ArticleView * MainWindow::createNewTab( bool switchToIt, connect( view, SIGNAL( showDefinitionInNewTab( QString const &, unsigned, QString const & ) ), this, SLOT( showDefinitionInNewTab( QString const &, unsigned, QString const & ) ) ); + connect( view, SIGNAL( typingEvent( QString const & ) ), + this, SLOT( typingEvent( QString const & ) ) ); + int index = cfg.preferences.newTabsOpenAfterCurrentOne ? ui.tabWidget->currentIndex() + 1 : ui.tabWidget->count(); @@ -729,6 +732,8 @@ void MainWindow::translateInputFinished() addNewTab(); showTranslationFor( word ); + + dynamic_cast< ArticleView & >( *( ui.tabWidget->currentWidget() ) ).focus(); } } @@ -900,6 +905,13 @@ void MainWindow::showDefinitionInNewTab( QString const & word, showDefinition( word, group, fromArticle ); } +void MainWindow::typingEvent( QString const & t ) +{ + ui.translateLine->setText( t ); + ui.translateLine->setFocus(); + ui.translateLine->setCursorPosition( t.size() ); +} + void MainWindow::showTranslationFor( QString const & inWord ) { ArticleView & view = diff --git a/src/mainwindow.hh b/src/mainwindow.hh index 6cf17c81..24d98b80 100644 --- a/src/mainwindow.hh +++ b/src/mainwindow.hh @@ -179,6 +179,7 @@ private slots: void openLinkInNewTab( QUrl const &, QUrl const &, QString const & ); void showDefinitionInNewTab( QString const & word, unsigned group, QString const & fromArticle ); + void typingEvent( QString const & ); void showTranslationFor( QString const & );