diff --git a/src/articleview.hh b/src/articleview.hh index 92e40fc5..2a31f89c 100644 --- a/src/articleview.hh +++ b/src/articleview.hh @@ -72,6 +72,10 @@ public: void forward() { ui.definition->forward(); } + /// Takes the focus to the view + void focus() + { ui.definition->setFocus( Qt::ShortcutFocusReason ); } + signals: void iconChanged( ArticleView *, QIcon const & icon ); diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 01c44297..77a7f89e 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -127,6 +127,9 @@ MainWindow::MainWindow(): connect( &wordFinder, SIGNAL( finished() ), this, SLOT( prefixMatchFinished() ) ); + ui.translateLine->installEventFilter( this ); + ui.wordList->installEventFilter( this ); + makeDictionaries(); addNewTab(); @@ -596,8 +599,10 @@ void MainWindow::translateInputChanged( QString const & newValue ) void MainWindow::translateInputFinished() { - if ( ui.wordList->count() ) - wordListItemActivated( ui.wordList->item( 0 ) ); + QString word = ui.translateLine->text(); + + if ( word.size() ) + showTranslationFor( word ); } void MainWindow::prefixMatchUpdated() @@ -665,6 +670,52 @@ void MainWindow::updateMatchResults( bool finished ) } } +bool MainWindow::eventFilter( QObject * obj, QEvent * ev ) +{ + if ( obj == ui.translateLine ) + { + if ( ev->type() == QEvent::KeyPress ) + { + QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev ); + + if ( keyEvent->matches( QKeySequence::MoveToNextLine ) && ui.wordList->count() ) + { + ui.wordList->setFocus( Qt::ShortcutFocusReason ); + ui.wordList->setCurrentRow( 0, QItemSelectionModel::ClearAndSelect ); + return true; + } + } + } + else + if ( obj == ui.wordList ) + { + if ( ev->type() == QEvent::KeyPress ) + { + QKeyEvent * keyEvent = static_cast< QKeyEvent * >( ev ); + + if ( keyEvent->matches( QKeySequence::MoveToPreviousLine ) && + !ui.wordList->currentRow() ) + { + ui.wordList->setCurrentRow( 0, QItemSelectionModel::Clear ); + ui.translateLine->setFocus( Qt::ShortcutFocusReason ); + return true; + } + + if ( keyEvent->matches( QKeySequence::InsertParagraphSeparator ) && + ui.wordList->selectedItems().size() ) + { + dynamic_cast< ArticleView & >( *( ui.tabWidget->currentWidget() ) ).focus(); + + return true; + } + } + } + else + return QMainWindow::eventFilter( obj, ev ); + + return false; +} + void MainWindow::wordListItemActivated( QListWidgetItem * item ) { showTranslationFor( item->text() ); diff --git a/src/mainwindow.hh b/src/mainwindow.hh index c5f6743a..7bb067cc 100644 --- a/src/mainwindow.hh +++ b/src/mainwindow.hh @@ -105,6 +105,8 @@ private: void updateMatchResults( bool finished ); + virtual bool eventFilter( QObject *, QEvent * ); + /// Returns the reference to dictionaries stored in the currently active /// group, or to all dictionaries if there are no groups. vector< sptr< Dictionary::Class > > const & getActiveDicts();