Merge pull request #1578 from xiaoyifang/opt/page-search-remove-highlight

opt: Ctrl+F page search remove highlight checkbox
This commit is contained in:
xiaoyifang 2024-06-15 21:19:48 +08:00 committed by GitHub
commit ed680f54b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 118 deletions

View file

@ -112,17 +112,16 @@ ArticleView::ArticleView( QWidget * parent,
selectCurrentArticleAction( this ),
copyAsTextAction( this ),
inspectAction( this ),
searchIsOpened( false ),
dictionaryBarToggled( dictionaryBarToggled_ ),
currentGroupId( currentGroupId_ ),
translateLine( translateLine_ )
{
// setup GUI
webview = new ArticleWebView( this );
ftsSearchPanel = new FtsSearchPanel( this );
searchPanel = new SearchPanel( this );
searchPanel->hide();
ftsSearchPanel->hide();
// Layout
auto * mainLayout = new QVBoxLayout( this );
mainLayout->addWidget( webview );
@ -141,7 +140,6 @@ ArticleView::ArticleView( QWidget * parent,
connect( searchPanel->next, &QPushButton::clicked, this, &ArticleView::on_searchNext_clicked );
connect( searchPanel->close, &QPushButton::clicked, this, &ArticleView::on_searchCloseButton_clicked );
connect( searchPanel->caseSensitive, &QPushButton::clicked, this, &ArticleView::on_searchCaseSensitive_clicked );
connect( searchPanel->highlightAll, &QPushButton::clicked, this, &ArticleView::on_highlightAllButton_clicked );
connect( searchPanel->lineEdit, &QLineEdit::textEdited, this, &ArticleView::on_searchText_textEdited );
connect( searchPanel->lineEdit, &QLineEdit::returnPressed, this, &ArticleView::on_searchText_returnPressed );
connect( ftsSearchPanel->next, &QPushButton::clicked, this, &ArticleView::on_ftsSearchNext_clicked );
@ -245,15 +243,10 @@ ArticleView::ArticleView( QWidget * parent,
webview->grabGesture( Gestures::GDPinchGestureType );
webview->grabGesture( Gestures::GDSwipeGestureType );
#endif
// Variable name for store current selection range
rangeVarName = QString( "sr_%1" ).arg( QString::number( (quint64)this, 16 ) );
connect( GlobalBroadcaster::instance(), &GlobalBroadcaster::dictionaryChanges, this, &ArticleView::setActiveDictIds );
connect( GlobalBroadcaster::instance(), &GlobalBroadcaster::dictionaryClear, this, &ArticleView::dictionaryClear );
channel = new QWebChannel( webview->page() );
agent = new ArticleViewAgent( this );
attachWebChannelToHtml();
@ -398,8 +391,6 @@ void ArticleView::showDefinition( QString const & word,
// Any search opened is probably irrelevant now
closeSearch();
// Clear highlight all button selection
searchPanel->highlightAll->setChecked( false );
webview->setCursor( Qt::WaitCursor );
load( req );
@ -616,7 +607,7 @@ bool ArticleView::handleF3( QObject * /*obj*/, QEvent * ev )
return true;
}
}
if ( ke->key() == Qt::Key_F3 && ftsSearchIsOpened ) {
if ( ke->key() == Qt::Key_F3 && ftsSearchPanel->isVisible() ) {
if ( !ke->modifiers() ) {
if ( ev->type() == QEvent::KeyPress )
on_ftsSearchNext_clicked();
@ -1939,45 +1930,40 @@ void ArticleView::openSearch()
if ( !isVisible() )
return;
if ( ftsSearchIsOpened )
if ( ftsSearchPanel->isVisible() )
closeSearch();
if ( !searchIsOpened ) {
if ( !searchPanel->isVisible() ) {
searchPanel->show();
searchPanel->lineEdit->setText( getTitle() );
searchIsOpened = true;
}
searchPanel->lineEdit->setFocus();
searchPanel->lineEdit->selectAll();
// Clear any current selection
if ( webview->selectedText().size() ) {
webview->page()->runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
if ( !webview->selectedText().isEmpty() ) {
webview->findText( "" );
}
if ( searchPanel->lineEdit->property( "noResults" ).toBool() ) {
searchPanel->lineEdit->setProperty( "noResults", false );
Utils::Widget::setNoResultColor( searchPanel->lineEdit, false );
}
}
void ArticleView::on_searchPrevious_clicked()
{
if ( searchIsOpened )
performFindOperation( false, true );
if ( searchPanel->isVisible() )
performFindOperation( true );
}
void ArticleView::on_searchNext_clicked()
{
if ( searchIsOpened )
performFindOperation( false, false );
if ( searchPanel->isVisible() )
performFindOperation( false );
}
void ArticleView::on_searchText_textEdited()
{
performFindOperation( true, false );
performFindOperation( false );
}
void ArticleView::on_searchText_returnPressed()
@ -1992,12 +1978,7 @@ void ArticleView::on_searchCloseButton_clicked()
void ArticleView::on_searchCaseSensitive_clicked()
{
performFindOperation( true, false );
}
void ArticleView::on_highlightAllButton_clicked()
{
performFindOperation( false, false, true );
performFindOperation( false );
}
//the id start with "gdform-"
@ -2045,33 +2026,10 @@ void ArticleView::doubleClicked( QPoint pos )
}
void ArticleView::performFindOperation( bool restart, bool backwards, bool checkHighlight )
void ArticleView::performFindOperation( bool backwards )
{
QString text = searchPanel->lineEdit->text();
if ( restart || checkHighlight ) {
if ( restart ) {
// Anyone knows how we reset the search position?
// For now we resort to this hack:
if ( webview->selectedText().size() ) {
webview->page()->runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
}
}
QWebEnginePage::FindFlags f( 0 );
if ( searchPanel->caseSensitive->isChecked() )
f |= QWebEnginePage::FindCaseSensitively;
webview->findText( "", f );
if ( searchPanel->highlightAll->isChecked() )
webview->findText( text, f );
if ( checkHighlight )
return;
}
QWebEnginePage::FindFlags f( 0 );
if ( searchPanel->caseSensitive->isChecked() )
@ -2082,12 +2040,7 @@ void ArticleView::performFindOperation( bool restart, bool backwards, bool check
findText( text, f, [ text, this ]( bool match ) {
bool setMark = !text.isEmpty() && !match;
if ( searchPanel->lineEdit->property( "noResults" ).toBool() != setMark ) {
searchPanel->lineEdit->setProperty( "noResults", setMark );
Utils::Widget::setNoResultColor( searchPanel->lineEdit, setMark );
}
} );
}
@ -2111,17 +2064,16 @@ void ArticleView::findText( QString & text,
bool ArticleView::closeSearch()
{
if ( searchIsOpened ) {
webview->findText( "" );
if ( searchPanel->isVisible() ) {
searchPanel->hide();
webview->setFocus();
searchIsOpened = false;
return true;
}
else if ( ftsSearchIsOpened ) {
if ( ftsSearchPanel->isVisible() ) {
firstAvailableText.clear();
uniqueMatches.clear();
ftsSearchIsOpened = false;
ftsSearchPanel->hide();
webview->setFocus();
@ -2132,24 +2084,12 @@ bool ArticleView::closeSearch()
return true;
}
else
return false;
}
bool ArticleView::isSearchOpened()
{
return searchIsOpened;
}
void ArticleView::showEvent( QShowEvent * ev )
{
QWidget::showEvent( ev );
if ( !searchIsOpened )
searchPanel->hide();
if ( !ftsSearchIsOpened )
ftsSearchPanel->hide();
return searchPanel->isVisible();
}
void ArticleView::copyAsText()
@ -2215,7 +2155,7 @@ void ArticleView::dictionaryClear( const ActiveDictIds & ad )
void ArticleView::performFtsFindOperation( bool backwards )
{
if ( !ftsSearchIsOpened )
if ( !ftsSearchPanel->isVisible() )
return;
if ( firstAvailableText.isEmpty() ) {
@ -2227,14 +2167,6 @@ void ArticleView::performFtsFindOperation( bool backwards )
QWebEnginePage::FindFlags flags( 0 );
if ( ftsSearchMatchCase )
flags |= QWebEnginePage::FindCaseSensitively;
// Restore saved highlighted selection
webview->page()->runJavaScript(
QString( "var sel=window.getSelection();sel.removeAllRanges();sel.addRange(%1);_=0;" ).arg( rangeVarName ) );
if ( backwards ) {
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
webview->findText( firstAvailableText,

View file

@ -49,9 +49,8 @@ class ArticleView: public QWidget
QAction pasteAction, articleUpAction, articleDownAction, goBackAction, goForwardAction, selectCurrentArticleAction,
copyAsTextAction, inspectAction;
bool searchIsOpened;
bool expandOptionalParts;
QString rangeVarName;
/// An action used to create Anki notes.
QAction sendToAnkiAction{ tr( "&Create Anki note" ), this };
@ -85,8 +84,6 @@ class ArticleView: public QWidget
/// Search in results of full-text search
QString firstAvailableText;
QStringList uniqueMatches;
bool ftsSearchIsOpened = false;
bool ftsSearchMatchCase = false;
QString delayedHighlightText;
@ -363,7 +360,6 @@ private slots:
void on_searchText_returnPressed();
void on_searchCloseButton_clicked();
void on_searchCaseSensitive_clicked();
void on_highlightAllButton_clicked();
void on_ftsSearchPrevious_clicked();
void on_ftsSearchNext_clicked();
@ -412,17 +408,13 @@ private:
bool eventFilter( QObject * obj, QEvent * ev ) override;
void performFindOperation( bool restart, bool backwards, bool checkHighlight = false );
void performFindOperation( bool backwards );
/// Returns the comma-separated list of dictionary ids which should be muted
/// for the given group. If there are none, returns empty string.
QString getMutedForGroup( unsigned group );
QStringList getMutedDictionaries( unsigned group );
protected:
// We need this to hide the search bar when we're showed
void showEvent( QShowEvent * ) override;
};
class ResourceToSaveHandler: public QObject

View file

@ -980,13 +980,8 @@ void MainWindow::refreshTranslateLine()
// Visually mark the input line to mark if there's no results
bool setMark = wordFinder.getResults().empty() && !wordFinder.wasSearchUncertain();
if ( translateLine->property( "noResults" ).toBool() != setMark ) {
translateLine->setProperty( "noResults", setMark );
Utils::Widget::setNoResultColor( translateLine, setMark );
}
}
void MainWindow::clipboardChange( QClipboard::Mode m )
{
@ -2345,11 +2340,7 @@ void MainWindow::updateSuggestionList( QString const & newValue )
ui.wordList->unsetCursor();
// Reset the noResults mark if it's on right now
if ( translateLine->property( "noResults" ).toBool() ) {
translateLine->setProperty( "noResults", false );
Utils::Widget::setNoResultColor( translateLine, false );
}
return;
}

View file

@ -20,11 +20,6 @@ SearchPanel::SearchPanel( QWidget * parent ):
next->setText( tr( "&Next" ) );
next->setShortcut( QKeySequence( tr( "Ctrl+G" ) ) );
highlightAll = new QCheckBox( this );
highlightAll->setIcon( QIcon( ":/icons/highlighter.png" ) );
highlightAll->setText( tr( "Highlight &all" ) );
highlightAll->setChecked( true );
caseSensitive = new QCheckBox( this );
caseSensitive->setText( tr( "&Case Sensitive" ) );
@ -38,7 +33,6 @@ SearchPanel::SearchPanel( QWidget * parent ):
auto * buttonsRow = new QHBoxLayout();
buttonsRow->addWidget( previous );
buttonsRow->addWidget( next );
buttonsRow->addWidget( highlightAll );
buttonsRow->addWidget( caseSensitive );
buttonsRow->addStretch();

View file

@ -16,7 +16,6 @@ public:
QPushButton * close;
QPushButton * previous;
QPushButton * next;
QCheckBox * highlightAll;
QCheckBox * caseSensitive;
};