From 22fb1d2f5f3aa97175d912dc2da56ac871665fce Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Sat, 16 May 2009 11:14:43 +0000 Subject: [PATCH] + 'Text Find' implemented. --- src/articleview.cc | 106 ++++++++++++++++++++++++++++++++++++++++++++- src/articleview.hh | 18 +++++++- src/articleview.ui | 55 ++++++++++++++++++++++- src/mainwindow.cc | 3 ++ src/qt-style.css | 5 +++ src/scanpopup.cc | 15 +++++++ src/scanpopup.hh | 3 ++ 7 files changed, 202 insertions(+), 3 deletions(-) diff --git a/src/articleview.cc b/src/articleview.cc index 2f33ee26..15a7f98a 100644 --- a/src/articleview.cc +++ b/src/articleview.cc @@ -36,6 +36,8 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, pasteAction( this ), articleUpAction( this ), articleDownAction( this ), + openSearchAction( this ), + searchIsOpened( false ), groupComboBox( groupComboBox_ ) { ui.setupUi( this ); @@ -82,6 +84,10 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, ui.definition->addAction( &articleDownAction ); connect( &articleDownAction, SIGNAL( triggered() ), this, SLOT( moveOneArticleDown() ) ); + openSearchAction.setShortcut( QKeySequence( "Ctrl+F" ) ); + ui.definition->addAction( &openSearchAction ); + connect( &openSearchAction, SIGNAL( triggered() ), this, SLOT( openSearch() ) ); + ui.definition->installEventFilter( this ); // Load the default blank page instantly, so there would be no flicker. @@ -782,9 +788,107 @@ void ArticleView::moveOneArticleDown() } } +void ArticleView::openSearch() +{ + if ( !searchIsOpened ) + { + ui.searchFrame->show(); + ui.searchText->setText( getTitle() ); + searchIsOpened = true; + } + + ui.searchText->setFocus(); + ui.searchText->selectAll(); + + // Clear any current selection + if ( ui.definition->selectedText().size() ) + { + ui.definition->triggerPageAction( QWebPage::SelectAll ); + ui.definition->triggerPageAction( QWebPage::SelectStartOfDocument ); + } + + if ( ui.searchText->property( "noResults" ).toBool() ) + { + ui.searchText->setProperty( "noResults", false ); + qApp->setStyleSheet( qApp->styleSheet() ); + } +} + +void ArticleView::on_searchPrevious_clicked() +{ + performFindOperation( false, true ); +} + +void ArticleView::on_searchNext_clicked() +{ + performFindOperation( false, false ); +} + +void ArticleView::on_searchText_textEdited() +{ + performFindOperation( true, false ); +} + +void ArticleView::on_searchText_returnPressed() +{ + on_searchNext_clicked(); +} + +void ArticleView::on_searchCloseButton_clicked() +{ + closeSearch(); +} + +void ArticleView::performFindOperation( bool restart, bool backwards ) +{ + QString text = ui.searchText->text(); + + if ( restart ) + { + // Anyone knows how we reset the search position? + // For now we resort to this hack: + if ( ui.definition->selectedText().size() ) + { + ui.definition->triggerPageAction( QWebPage::SelectAll ); + ui.definition->triggerPageAction( QWebPage::SelectStartOfDocument ); + } + } + + QWebPage::FindFlags f( 0 ); + + if ( ui.searchCaseSensitive->isChecked() ) + f |= QWebPage::FindCaseSensitively; + + if ( backwards ) + f |= QWebPage::FindBackward; + + bool setMark = text.size() && !ui.definition->findText( text, f ); + + if ( ui.searchText->property( "noResults" ).toBool() != setMark ) + { + ui.searchText->setProperty( "noResults", setMark ); + qApp->setStyleSheet( qApp->styleSheet() ); + } +} + +bool ArticleView::closeSearch() +{ + if ( searchIsOpened ) + { + ui.searchFrame->hide(); + ui.definition->setFocus(); + searchIsOpened = false; + + return true; + } + else + return false; +} + void ArticleView::showEvent( QShowEvent * ev ) { QFrame::showEvent( ev ); - ui.searchFrame->hide(); + if ( !searchIsOpened ) + ui.searchFrame->hide(); } diff --git a/src/articleview.hh b/src/articleview.hh index bd10d6dc..85cef3ed 100644 --- a/src/articleview.hh +++ b/src/articleview.hh @@ -26,7 +26,8 @@ class ArticleView: public QFrame Ui::ArticleView ui; - QAction pasteAction, articleUpAction, articleDownAction; + QAction pasteAction, articleUpAction, articleDownAction, openSearchAction; + bool searchIsOpened; #ifdef Q_OS_WIN32 // Used in Windows only @@ -113,6 +114,10 @@ public: /// Prints current article void print( QPrinter * ) const; + + /// Closes search if it's open and returns true. Returns false if it + /// wasn't open. + bool closeSearch(); signals: @@ -152,6 +157,15 @@ private slots: /// Nagivates to the next article relative to the active one. void moveOneArticleDown(); + /// Opens the search area + void openSearch(); + + void on_searchPrevious_clicked(); + void on_searchNext_clicked(); + void on_searchText_textEdited(); + void on_searchText_returnPressed(); + void on_searchCloseButton_clicked(); + private: /// Deduces group from the url. If there doesn't seem to be any group, @@ -174,6 +188,8 @@ private: bool eventFilter( QObject * obj, QEvent * ev ); + void performFindOperation( bool restart, bool backwards ); + protected: // We need this to hide the search bar when we're showed diff --git a/src/articleview.ui b/src/articleview.ui index 2d9c29c5..2d54ecfc 100644 --- a/src/articleview.ui +++ b/src/articleview.ui @@ -58,6 +58,13 @@ x + + + :/icons/closetab.png:/icons/closetab.png + + + true + @@ -70,6 +77,50 @@ + + + + &Previous + + + + :/icons/previous.png:/icons/previous.png + + + Qt::ToolButtonTextBesideIcon + + + true + + + + + + + &Next + + + + :/icons/next.png:/icons/next.png + + + Ctrl+G + + + Qt::ToolButtonTextBesideIcon + + + true + + + + + + + &Case Sensitive + + + @@ -95,6 +146,8 @@
QtWebKit/QWebView
- + + + diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 87bfc65a..072e70c8 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -750,6 +750,9 @@ void MainWindow::translateInputFinished() void MainWindow::focusTranslateLine() { + if ( dynamic_cast< ArticleView & >( *( ui.tabWidget->currentWidget() ) ).closeSearch() ) + return; + if ( ui.searchPane->isFloating() ) ui.searchPane->activateWindow(); diff --git a/src/qt-style.css b/src/qt-style.css index 39e06590..a67261f7 100644 --- a/src/qt-style.css +++ b/src/qt-style.css @@ -8,6 +8,11 @@ MainWindow #centralWidget #translateLine[noResults="true"] background: #febb7d; } +ArticleView #searchText[noResults="true"] +{ + background: #febb7d; +} + .ScanPopup #outerFrame { border: 1px solid grey; diff --git a/src/scanpopup.cc b/src/scanpopup.cc index 48a68840..91f96300 100644 --- a/src/scanpopup.cc +++ b/src/scanpopup.cc @@ -25,6 +25,7 @@ ScanPopup::ScanPopup( QWidget * parent, isScanningEnabled( false ), allDictionaries( allDictionaries_ ), groups( groups_ ), + escapeAction( this ), wordFinder( this ), mouseEnteredOnce( false ), hideTimer( this ) @@ -68,6 +69,11 @@ ScanPopup::ScanPopup( QWidget * parent, setAttribute( Qt::WA_NoSystemBackground ); #endif + escapeAction.setShortcut( QKeySequence( "Esc" ) ); + addAction( &escapeAction ); + connect( &escapeAction, SIGNAL( triggered() ), + this, SLOT( escapePressed() ) ); + connect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ), this, SLOT( currentGroupChanged( QString const & ) ) ); @@ -499,3 +505,12 @@ void ScanPopup::pageLoaded( ArticleView * ) if ( cfg.preferences.pronounceOnLoadPopup ) definition->playSound(); } + +void ScanPopup::escapePressed() +{ + if ( !definition->closeSearch() ) + { + unsetCursor(); + hide(); + } +} diff --git a/src/scanpopup.hh b/src/scanpopup.hh index b80bba44..d2bf1e18 100644 --- a/src/scanpopup.hh +++ b/src/scanpopup.hh @@ -52,6 +52,7 @@ private: Instances::Groups const & groups; Ui::ScanPopup ui; ArticleView * definition; + QAction escapeAction; QString pendingInputWord, inputWord; WordFinder wordFinder; @@ -96,6 +97,8 @@ private slots: void altModePoll(); void pageLoaded( ArticleView * ); + + void escapePressed(); }; #endif