From d985e9525b19d83d6087746035f91a9cef3b5d59 Mon Sep 17 00:00:00 2001 From: Altair Wei Date: Mon, 31 Jul 2023 18:50:01 +0800 Subject: [PATCH] NEW: JSRepl highlight search --- src/jsplugin/JSRepl.cpp | 28 ++++++++++++++++++++++++++-- src/jsplugin/JSRepl.h | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/jsplugin/JSRepl.cpp b/src/jsplugin/JSRepl.cpp index ac10f477..7d34dba3 100644 --- a/src/jsplugin/JSRepl.cpp +++ b/src/jsplugin/JSRepl.cpp @@ -228,7 +228,11 @@ void JSReplSearchBox::findNext() QTextCursor cursor = textEdit->textCursor(); cursor.movePosition(QTextCursor::Start); textEdit->setTextCursor(cursor); - textEdit->find(text); + found = textEdit->find(text); + } + + if (found) { + highlight(textEdit->textCursor()); } } @@ -241,7 +245,11 @@ void JSReplSearchBox::findPrevious() QTextCursor cursor = textEdit->textCursor(); cursor.movePosition(QTextCursor::End); textEdit->setTextCursor(cursor); - textEdit->find(text, QTextDocument::FindBackward); + found = textEdit->find(text, QTextDocument::FindBackward); + } + + if (found) { + highlight(textEdit->textCursor()); } } @@ -249,3 +257,19 @@ void JSReplSearchBox::setFocusOnLineEdit() { lineEdit->setFocus(); } + +void JSReplSearchBox::highlight(const QTextCursor &cursor) +{ + QList extraSelections; + + QTextEdit::ExtraSelection selection; + QColor lineColor = QColor(Qt::yellow).lighter(160); + + selection.format.setBackground(lineColor); + selection.format.setProperty(QTextFormat::FullWidthSelection, true); + selection.cursor = cursor; + selection.cursor.clearSelection(); + extraSelections.append(selection); + + textEdit->setExtraSelections(extraSelections); +} diff --git a/src/jsplugin/JSRepl.h b/src/jsplugin/JSRepl.h index 2bc35ab7..bc233528 100644 --- a/src/jsplugin/JSRepl.h +++ b/src/jsplugin/JSRepl.h @@ -87,6 +87,8 @@ public slots: void findPrevious(); private: + void highlight(const QTextCursor &cursor); + QTextEdit *textEdit; QLineEdit *lineEdit; };