diff --git a/article-style-search-selection.css b/article-style-search-selection.css new file mode 100644 index 00000000..0411127d --- /dev/null +++ b/article-style-search-selection.css @@ -0,0 +1,10 @@ +/* This stylesheet is used only to highligh current selection when doing a search. + It's programatically set/unset to restore default selection color (e.g. blue under Windows, orange under Ubuntu) */ + +/* highlight/select color in Windows when a widget is inactive is nearly impossible to read (light gray). + * then change it to green to use with the find option. */ +::selection { + background: #23DC23; + color: white; +} + diff --git a/articleview.cc b/articleview.cc index 5c24862f..f3a8a958 100644 --- a/articleview.cc +++ b/articleview.cc @@ -214,6 +214,9 @@ void ArticleView::showDefinition( QString const & word, unsigned group, // Any search opened is probably irrelevant now closeSearch(); + // Clear highlight all button selection + ui.highlightAllButton->setChecked(false); + ui.definition->load( req ); //QApplication::setOverrideCursor( Qt::WaitCursor ); @@ -1291,6 +1294,16 @@ void ArticleView::on_searchCloseButton_clicked() closeSearch(); } +void ArticleView::on_searchCaseSensitive_clicked() +{ + performFindOperation( false, false, true ); +} + +void ArticleView::on_highlightAllButton_clicked() +{ + performFindOperation( false, false, true ); +} + void ArticleView::doubleClicked() { // We might want to initiate translation of the selected word @@ -1317,19 +1330,40 @@ void ArticleView::doubleClicked() } -void ArticleView::performFindOperation( bool restart, bool backwards ) +void ArticleView::performFindOperation( bool restart, bool backwards, bool checkHighlight ) { QString text = ui.searchText->text(); - if ( restart ) + //highlight in a non-default color current selection + if( ui.definition->settings()->userStyleSheetUrl().isEmpty() ) + ui.definition->settings()->setUserStyleSheetUrl(QUrl::fromLocalFile( ":/article-style-search-selection.css" )); + + if ( restart || checkHighlight ) { - // Anyone knows how we reset the search position? - // For now we resort to this hack: - if ( ui.definition->selectedText().size() ) - { - ui.definition->page()->currentFrame()-> - evaluateJavaScript( "window.getSelection().removeAllRanges();" ); + if( restart ) { + // Anyone knows how we reset the search position? + // For now we resort to this hack: + if ( ui.definition->selectedText().size() ) + { + ui.definition->page()->currentFrame()-> + evaluateJavaScript( "window.getSelection().removeAllRanges();" ); + } } + + QWebPage::FindFlags f( 0 ); + + if ( ui.searchCaseSensitive->isChecked() ) + f |= QWebPage::FindCaseSensitively; + + f |= QWebPage::HighlightAllOccurrences; + + ui.definition->findText( "", f ); + + if( ui.highlightAllButton->isChecked() ) + ui.definition->findText( text, f ); + + if( checkHighlight ) + return; } QWebPage::FindFlags f( 0 ); diff --git a/articleview.hh b/articleview.hh index 76f36dd2..909abf5c 100644 --- a/articleview.hh +++ b/articleview.hh @@ -200,6 +200,8 @@ private slots: void on_searchText_textEdited(); void on_searchText_returnPressed(); void on_searchCloseButton_clicked(); + void on_searchCaseSensitive_clicked(); + void on_highlightAllButton_clicked(); /// Handles the double-click from the definition. void doubleClicked(); @@ -244,7 +246,7 @@ private: bool eventFilter( QObject * obj, QEvent * ev ); - void performFindOperation( bool restart, bool backwards ); + void performFindOperation( bool restart, bool backwards, bool checkHighlight = false ); void reloadStyleSheet(); diff --git a/articleview.ui b/articleview.ui index 0cb7704c..2be498c6 100644 --- a/articleview.ui +++ b/articleview.ui @@ -127,6 +127,29 @@ + + + + Highlight &all + + + + :/icons/highlighter.png:/icons/highlighter.png + + + true + + + false + + + Qt::ToolButtonTextBesideIcon + + + true + + + diff --git a/articlewebview.cc b/articlewebview.cc index b8fb72b7..5ed83ede 100644 --- a/articlewebview.cc +++ b/articlewebview.cc @@ -6,6 +6,10 @@ void ArticleWebView::mousePressEvent( QMouseEvent * event ) { + //restore default color for selection + if( !this->settings()->userStyleSheetUrl().isEmpty() ) + this->settings()->setUserStyleSheetUrl(QUrl()); + if ( event->buttons() & Qt::MidButton ) midButtonPressed = true; diff --git a/icons/highlighter.png b/icons/highlighter.png new file mode 100644 index 00000000..3392167b Binary files /dev/null and b/icons/highlighter.png differ diff --git a/resources.qrc b/resources.qrc index 0cdc3cf4..cc018384 100644 --- a/resources.qrc +++ b/resources.qrc @@ -18,6 +18,7 @@ icons/warning.png article-style.css article-style-print.css + article-style-search-selection.css qt-style.css icons/icon32_dsl.png icons/icon32_stardict.png @@ -44,5 +45,6 @@ icons/forvo.png icons/windows-list.png CREDITS.txt + icons/highlighter.png