Falkon Develop
Cross-platform Qt-based web browser
searchtoolbar.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "searchtoolbar.h"
19#include "webview.h"
20#include "webpage.h"
21#include "lineedit.h"
22#include "ui_searchtoolbar.h"
23#include "iconprovider.h"
24
25#include <QKeyEvent>
26#include <QShortcut>
27
29 : QWidget(parent)
30 , ui(new Ui::SearchToolbar)
31 , m_view(view)
32 , m_findFlags{}
33{
34 setAttribute(Qt::WA_DeleteOnClose);
35 ui->setupUi(this);
36
37 ui->closeButton->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_DialogCloseButton));
38 ui->next->setShortcut(QKeySequence(QSL("Ctrl+G")));
39 ui->previous->setShortcut(QKeySequence(QSL("Ctrl+Shift+G")));
40
41 ui->resultsInfo->hide();
42 connect(view->page(), &QWebEnginePage::findTextFinished, this, &SearchToolBar::showSearchResults);
43
44 connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
45 connect(ui->lineEdit, &QLineEdit::textEdited, this, &SearchToolBar::findNext);
46 connect(ui->next, &QAbstractButton::clicked, this, &SearchToolBar::findNext);
47 connect(ui->previous, &QAbstractButton::clicked, this, &SearchToolBar::findPrevious);
48 connect(ui->caseSensitive, &QAbstractButton::clicked, this, &SearchToolBar::caseSensitivityChanged);
49
50 auto* findNextAction = new QShortcut(QKeySequence(QSL("F3")), this);
51 connect(findNextAction, &QShortcut::activated, this, &SearchToolBar::findNext);
52
53 auto* findPreviousAction = new QShortcut(QKeySequence(QSL("Shift+F3")), this);
54 connect(findPreviousAction, &QShortcut::activated, this, &SearchToolBar::findPrevious);
55
56 parent->installEventFilter(this);
57}
58
60{
61 // Show only essentials widget + set minimum width
62 ui->caseSensitive->hide();
63 ui->horizontalLayout->setSpacing(2);
64 ui->horizontalLayout->setContentsMargins(2, 6, 2, 6);
65 setMinimumWidth(260);
66}
67
69{
70 ui->lineEdit->setFocus();
71}
72
74{
75 hide();
76 searchText(QString());
77 m_view->setFocus();
78 deleteLater();
79}
80
82{
83 m_findFlags = {};
85
86 searchText(ui->lineEdit->text());
87}
88
90{
91 m_findFlags = QWebEnginePage::FindBackward;
93
94 searchText(ui->lineEdit->text());
95}
96
98{
99 if (ui->caseSensitive->isChecked()) {
100 m_findFlags = m_findFlags | QWebEnginePage::FindCaseSensitively;
101 }
102 else {
103 m_findFlags = m_findFlags & ~QWebEnginePage::FindCaseSensitively;
104 }
105}
106
108{
110
111 searchText(QString());
112 searchText(ui->lineEdit->text());
113}
114
115void SearchToolBar::setText(const QString &text)
116{
117 ui->lineEdit->setText(text);
118}
119
120void SearchToolBar::searchText(const QString &text)
121{
122 m_searchRequests++;
123 QPointer<SearchToolBar> guard = this;
124 m_view->findText(text, m_findFlags, [=](QWebEngineFindTextResult result) {
125 bool found = result.numberOfMatches() > 0;
126 if (!guard) {
127 return;
128 }
129 if (--m_searchRequests != 0) {
130 return;
131 }
132 if (ui->lineEdit->text().isEmpty())
133 found = true;
134
135 ui->lineEdit->setProperty("notfound", QVariant(!found));
136 ui->lineEdit->style()->unpolish(ui->lineEdit);
137 ui->lineEdit->style()->polish(ui->lineEdit);
138
139 // Clear selection
140 m_view->page()->runJavaScript(QSL("window.getSelection().empty();"), WebPage::SafeJsWorld);
141 });
142}
143
144void SearchToolBar::showSearchResults(const QWebEngineFindTextResult &result)
145{
146 if (result.numberOfMatches() == 0) {
147 ui->resultsInfo->hide();
148 return;
149 }
150
151 ui->resultsInfo->setText(tr("%1 of %2").arg(
152 QString::number(result.activeMatch()), QString::number(result.numberOfMatches())));
153 ui->resultsInfo->show();
154}
155
156bool SearchToolBar::eventFilter(QObject* obj, QEvent* event)
157{
158 Q_UNUSED(obj);
159
160 if (event->type() == QEvent::KeyPress) {
161 auto *ke = static_cast<QKeyEvent*>(event);
162 switch (ke->key()) {
163 case Qt::Key_Escape:
164 close();
165 break;
166 case Qt::Key_Enter:
167 case Qt::Key_Return:
168 if (ke->modifiers() & Qt::ShiftModifier) {
169 findPrevious();
170 } else {
171 findNext();
172 }
173 break;
174 default:
175 break;
176 }
177 }
178
179 return false;
180}
181
183{
184 delete ui;
185}
static IconProvider * instance()
SearchToolBar(WebView *view, QWidget *parent=nullptr)
void searchText(const QString &text)
void caseSensitivityChanged()
void showSearchResults(const QWebEngineFindTextResult &result)
void showMinimalInPopupWindow()
void setText(const QString &text)
~SearchToolBar() override
void updateFindFlags()
void focusSearchLine()
bool eventFilter(QObject *obj, QEvent *event) override
@ SafeJsWorld
Definition: webpage.h:45
WebPage * page() const
Definition: webview.cpp:132
#define QSL(x)
Definition: qzcommon.h:40