Skip to content

Commit

Permalink
Add a checkable push button to the search bar that allows to highlight
Browse files Browse the repository at this point in the history
a specific string in a website. It uses the newly added API that comes
with Qt 4.6 / WebKit trunk.
  • Loading branch information
Jakub Wieczorek committed Jul 2, 2009
1 parent ac121e1 commit a8dd9fd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/searchbanner.ui
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@
</spacer>
</item>
<item>
<widget class="QLabel" name="searchInfo">
<widget class="QLabel" name="searchInfo"/>
</item>
<item>
<widget class="QToolButton" name="highlightAllButton">
<property name="text">
<string notr="true">TextLabel</string>
<string>Highlight All</string>
</property>
<property name="visible">
<bool>false</bool>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
Expand Down
24 changes: 23 additions & 1 deletion src/webviewsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ WebViewSearch::WebViewSearch(QWebView *webView, QWidget *parent)
: SearchBar(parent)
{
setSearchObject(webView);
#if QT_VERSION >= 0x040600 || defined(WEBKIT_TRUNK)
ui.highlightAllButton->setVisible(true);
connect(ui.highlightAllButton, SIGNAL(toggled(bool)),
this, SLOT(highlightAll()));
connect(ui.searchLineEdit, SIGNAL(textEdited(const QString &)),
this, SLOT(highlightAll()));
#endif
}

void WebViewSearch::findNext()
Expand All @@ -44,17 +51,32 @@ void WebViewSearch::findPrevious()
find(QWebPage::FindBackward | QWebPage::FindWrapsAroundDocument);
}

#if QT_VERSION >= 0x040600 || defined(WEBKIT_TRUNK)
void WebViewSearch::highlightAll()
{
webView()->findText(QString(), QWebPage::HighlightAllOccurrences);

if (ui.highlightAllButton->isChecked())
find(QWebPage::HighlightAllOccurrences);
}
#endif

void WebViewSearch::find(QWebPage::FindFlags flags)
{
QString searchString = ui.searchLineEdit->text();
if (!searchObject() || searchString.isEmpty())
return;
QString infoString;
if (!((QWebView*)searchObject())->findText(searchString, flags))
if (!webView()->findText(searchString, flags))
infoString = tr("Not Found");
ui.searchInfo->setText(infoString);
}

QWebView *WebViewSearch::webView() const
{
return qobject_cast<QWebView*>(searchObject());
}

WebViewWithSearch::WebViewWithSearch(WebView *webView, QWidget *parent)
: QWidget(parent)
, m_webView(webView)
Expand Down
4 changes: 4 additions & 0 deletions src/webviewsearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ class WebViewSearch : public SearchBar
public slots:
void findNext();
void findPrevious();
#if QT_VERSION >= 0x040600 || defined(WEBKIT_TRUNK)
void highlightAll();
#endif

private:
void find(QWebPage::FindFlags flags);
QWebView *webView() const;
};

#include "webview.h"
Expand Down

0 comments on commit a8dd9fd

Please sign in to comment.