From a8801d15c56eaef7d3ac65d4616dbf8f26966972 Mon Sep 17 00:00:00 2001 From: kh1 <qt-info@nokia.com> Date: Tue, 30 Mar 2010 16:10:05 +0200 Subject: [PATCH] Implement text highlight support after full text search. --- src/plugins/help/helpviewer.h | 2 +- src/plugins/help/helpviewer_qtb.cpp | 42 +++++++++++++++++++++++------ src/plugins/help/helpviewer_qwv.cpp | 8 +++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/plugins/help/helpviewer.h b/src/plugins/help/helpviewer.h index 489185d8efa..9ad9e516f0e 100644 --- a/src/plugins/help/helpviewer.h +++ b/src/plugins/help/helpviewer.h @@ -83,7 +83,7 @@ public: bool isBackwardAvailable() const; bool findText(const QString &text, Find::IFindSupport::FindFlags flags, - bool incremental); + bool incremental, bool fromSearch); static QString AboutBlankPage; static QString PageNotFoundMessage; diff --git a/src/plugins/help/helpviewer_qtb.cpp b/src/plugins/help/helpviewer_qtb.cpp index b56a2bed5d2..56b6687b49e 100644 --- a/src/plugins/help/helpviewer_qtb.cpp +++ b/src/plugins/help/helpviewer_qtb.cpp @@ -53,6 +53,13 @@ HelpViewer::HelpViewer(qreal zoom, QWidget *parent) : QTextBrowser(parent) , d(new HelpViewerPrivate(zoom)) { + QPalette p = palette(); + p.setColor(QPalette::Inactive, QPalette::Highlight, + p.color(QPalette::Active, QPalette::Highlight)); + p.setColor(QPalette::Inactive, QPalette::HighlightedText, + p.color(QPalette::Active, QPalette::HighlightedText)); + setPalette(p); + installEventFilter(this); document()->setDocumentMargin(8); @@ -173,16 +180,16 @@ bool HelpViewer::isBackwardAvailable() const } bool HelpViewer::findText(const QString &text, IFindSupport::FindFlags flags, - bool incremental) + bool incremental, bool fromSearch) { QTextDocument *doc = document(); QTextCursor cursor = textCursor(); - if (!doc || cursor.isNull()) return false; + const int position = cursor.selectionStart(); if (incremental) - cursor.setPosition(cursor.selectionStart()); + cursor.setPosition(position); QTextDocument::FindFlags f = IFindSupport::textDocumentFlagsForFindFlags(flags); QTextCursor found = doc->find(text, cursor, f); @@ -192,14 +199,33 @@ bool HelpViewer::findText(const QString &text, IFindSupport::FindFlags flags, else cursor.movePosition(QTextCursor::End); found = doc->find(text, cursor, f); - if (found.isNull()) { - return false; + } + + if (fromSearch) { + cursor.beginEditBlock(); + viewport()->setUpdatesEnabled(false); + + QTextCharFormat marker; + marker.setForeground(Qt::red); + cursor.movePosition(QTextCursor::Start); + setTextCursor(cursor); + + while (find(text)) { + QTextCursor hit = textCursor(); + hit.mergeCharFormat(marker); } + + viewport()->setUpdatesEnabled(true); + cursor.endEditBlock(); } - if (!found.isNull()) { - setTextCursor(found); + + bool cursorIsNull = found.isNull(); + if (cursorIsNull) { + found = textCursor(); + found.setPosition(position); } - return true; + setTextCursor(found); + return cursorIsNull; } // -- public slots diff --git a/src/plugins/help/helpviewer_qwv.cpp b/src/plugins/help/helpviewer_qwv.cpp index 5e463719a41..efe608ec4a5 100644 --- a/src/plugins/help/helpviewer_qwv.cpp +++ b/src/plugins/help/helpviewer_qwv.cpp @@ -140,8 +140,8 @@ QNetworkReply *HelpNetworkAccessManager::createRequest(Operation /*op*/, } const QHelpEngineCore &engine = HelpManager::helpEngineCore(); - const QByteArray &data = engine.findFile(url).isValid() - ? engine.fileData(url) : HelpViewer::PageNotFoundMessage.arg(url.toString()).toUtf8(); + const QByteArray &data = engine.findFile(url).isValid() ? engine.fileData(url) + : HelpViewer::PageNotFoundMessage.arg(url.toString()).toUtf8(); return new HelpNetworkReply(request, data, mimeType); } @@ -329,9 +329,9 @@ bool HelpViewer::isBackwardAvailable() const } bool HelpViewer::findText(const QString &text, IFindSupport::FindFlags flags, - bool incremental) + bool incremental, bool fromSearch) { - Q_UNUSED(incremental) + Q_UNUSED((incremental && fromSearch)) QWebPage::FindFlags options = QWebPage::FindWrapsAroundDocument; if (flags & Find::IFindSupport::FindBackward) options |= QWebPage::FindBackward; -- GitLab