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

View file

@ -49,9 +49,8 @@ class ArticleView: public QWidget
QAction pasteAction, articleUpAction, articleDownAction, goBackAction, goForwardAction, selectCurrentArticleAction, QAction pasteAction, articleUpAction, articleDownAction, goBackAction, goForwardAction, selectCurrentArticleAction,
copyAsTextAction, inspectAction; copyAsTextAction, inspectAction;
bool searchIsOpened;
bool expandOptionalParts; bool expandOptionalParts;
QString rangeVarName;
/// An action used to create Anki notes. /// An action used to create Anki notes.
QAction sendToAnkiAction{ tr( "&Create Anki note" ), this }; QAction sendToAnkiAction{ tr( "&Create Anki note" ), this };
@ -85,8 +84,6 @@ class ArticleView: public QWidget
/// Search in results of full-text search /// Search in results of full-text search
QString firstAvailableText; QString firstAvailableText;
QStringList uniqueMatches; QStringList uniqueMatches;
bool ftsSearchIsOpened = false;
bool ftsSearchMatchCase = false;
QString delayedHighlightText; QString delayedHighlightText;
@ -363,7 +360,6 @@ private slots:
void on_searchText_returnPressed(); void on_searchText_returnPressed();
void on_searchCloseButton_clicked(); void on_searchCloseButton_clicked();
void on_searchCaseSensitive_clicked(); void on_searchCaseSensitive_clicked();
void on_highlightAllButton_clicked();
void on_ftsSearchPrevious_clicked(); void on_ftsSearchPrevious_clicked();
void on_ftsSearchNext_clicked(); void on_ftsSearchNext_clicked();
@ -412,17 +408,13 @@ private:
bool eventFilter( QObject * obj, QEvent * ev ) override; 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 /// Returns the comma-separated list of dictionary ids which should be muted
/// for the given group. If there are none, returns empty string. /// for the given group. If there are none, returns empty string.
QString getMutedForGroup( unsigned group ); QString getMutedForGroup( unsigned group );
QStringList getMutedDictionaries( 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 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 // Visually mark the input line to mark if there's no results
bool setMark = wordFinder.getResults().empty() && !wordFinder.wasSearchUncertain(); bool setMark = wordFinder.getResults().empty() && !wordFinder.wasSearchUncertain();
if ( translateLine->property( "noResults" ).toBool() != setMark ) {
translateLine->setProperty( "noResults", setMark );
Utils::Widget::setNoResultColor( translateLine, setMark ); Utils::Widget::setNoResultColor( translateLine, setMark );
} }
}
void MainWindow::clipboardChange( QClipboard::Mode m ) void MainWindow::clipboardChange( QClipboard::Mode m )
{ {
@ -2345,11 +2340,7 @@ void MainWindow::updateSuggestionList( QString const & newValue )
ui.wordList->unsetCursor(); ui.wordList->unsetCursor();
// Reset the noResults mark if it's on right now // Reset the noResults mark if it's on right now
if ( translateLine->property( "noResults" ).toBool() ) {
translateLine->setProperty( "noResults", false );
Utils::Widget::setNoResultColor( translateLine, false ); Utils::Widget::setNoResultColor( translateLine, false );
}
return; return;
} }

View file

@ -20,11 +20,6 @@ SearchPanel::SearchPanel( QWidget * parent ):
next->setText( tr( "&Next" ) ); next->setText( tr( "&Next" ) );
next->setShortcut( QKeySequence( tr( "Ctrl+G" ) ) ); 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 = new QCheckBox( this );
caseSensitive->setText( tr( "&Case Sensitive" ) ); caseSensitive->setText( tr( "&Case Sensitive" ) );
@ -38,7 +33,6 @@ SearchPanel::SearchPanel( QWidget * parent ):
auto * buttonsRow = new QHBoxLayout(); auto * buttonsRow = new QHBoxLayout();
buttonsRow->addWidget( previous ); buttonsRow->addWidget( previous );
buttonsRow->addWidget( next ); buttonsRow->addWidget( next );
buttonsRow->addWidget( highlightAll );
buttonsRow->addWidget( caseSensitive ); buttonsRow->addWidget( caseSensitive );
buttonsRow->addStretch(); buttonsRow->addStretch();

View file

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