diff --git a/src/lib/webtab/searchtoolbar.cpp b/src/lib/webtab/searchtoolbar.cpp index bdf1ef0a..73bcfa2b 100644 --- a/src/lib/webtab/searchtoolbar.cpp +++ b/src/lib/webtab/searchtoolbar.cpp @@ -1,169 +1,188 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "searchtoolbar.h" #include "webview.h" #include "webpage.h" #include "lineedit.h" #include "ui_searchtoolbar.h" #include "iconprovider.h" #include #include SearchToolBar::SearchToolBar(WebView* view, QWidget* parent) : QWidget(parent) , ui(new Ui::SearchToolbar) , m_view(view) , m_findFlags(0) { setAttribute(Qt::WA_DeleteOnClose); ui->setupUi(this); ui->closeButton->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_DialogCloseButton)); ui->next->setShortcut(QKeySequence("Ctrl+G")); ui->previous->setShortcut(QKeySequence("Ctrl+Shift+G")); + ui->resultsInfo->hide(); +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(5, 14, 0) + connect(view->page(), &QWebEnginePage::findTextFinished, this, &SearchToolBar::showSearchResults); +#endif + connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close())); connect(ui->lineEdit, &QLineEdit::textEdited, this, &SearchToolBar::findNext); connect(ui->next, &QAbstractButton::clicked, this, &SearchToolBar::findNext); connect(ui->previous, &QAbstractButton::clicked, this, &SearchToolBar::findPrevious); connect(ui->caseSensitive, &QAbstractButton::clicked, this, &SearchToolBar::caseSensitivityChanged); QShortcut* findNextAction = new QShortcut(QKeySequence("F3"), this); connect(findNextAction, &QShortcut::activated, this, &SearchToolBar::findNext); QShortcut* findPreviousAction = new QShortcut(QKeySequence("Shift+F3"), this); connect(findPreviousAction, &QShortcut::activated, this, &SearchToolBar::findPrevious); parent->installEventFilter(this); } void SearchToolBar::showMinimalInPopupWindow() { // Show only essentials widget + set minimum width ui->caseSensitive->hide(); ui->horizontalLayout->setSpacing(2); ui->horizontalLayout->setContentsMargins(2, 6, 2, 6); setMinimumWidth(260); } void SearchToolBar::focusSearchLine() { ui->lineEdit->setFocus(); } void SearchToolBar::close() { hide(); searchText(QString()); m_view->setFocus(); deleteLater(); } void SearchToolBar::findNext() { m_findFlags = 0; updateFindFlags(); searchText(ui->lineEdit->text()); } void SearchToolBar::findPrevious() { m_findFlags = QWebEnginePage::FindBackward; updateFindFlags(); searchText(ui->lineEdit->text()); } void SearchToolBar::updateFindFlags() { if (ui->caseSensitive->isChecked()) { m_findFlags = m_findFlags | QWebEnginePage::FindCaseSensitively; } else { m_findFlags = m_findFlags & ~QWebEnginePage::FindCaseSensitively; } } void SearchToolBar::caseSensitivityChanged() { updateFindFlags(); searchText(QString()); searchText(ui->lineEdit->text()); } void SearchToolBar::setText(const QString &text) { ui->lineEdit->setText(text); } void SearchToolBar::searchText(const QString &text) { m_searchRequests++; QPointer guard = this; m_view->findText(text, m_findFlags, [=](bool found) { if (!guard) { return; } if (--m_searchRequests != 0) { return; } if (ui->lineEdit->text().isEmpty()) found = true; ui->lineEdit->setProperty("notfound", QVariant(!found)); ui->lineEdit->style()->unpolish(ui->lineEdit); ui->lineEdit->style()->polish(ui->lineEdit); // Clear selection m_view->page()->runJavaScript(QSL("window.getSelection().empty();"), WebPage::SafeJsWorld); }); } +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(5, 14, 0) +void SearchToolBar::showSearchResults(const QWebEngineFindTextResult &result) +{ + if (result.numberOfMatches() == 0) { + ui->resultsInfo->hide(); + return; + } + + ui->resultsInfo->setText(tr("%1 of %2").arg( + QString::number(result.activeMatch()), QString::number(result.numberOfMatches()))); + ui->resultsInfo->show(); +} +#endif + bool SearchToolBar::eventFilter(QObject* obj, QEvent* event) { Q_UNUSED(obj); if (event->type() == QEvent::KeyPress) { QKeyEvent *ke = static_cast(event); switch (ke->key()) { case Qt::Key_Escape: close(); break; case Qt::Key_Enter: case Qt::Key_Return: if (ke->modifiers() & Qt::ShiftModifier) { findPrevious(); } else { findNext(); } break; default: break; } } return false; } SearchToolBar::~SearchToolBar() { delete ui; } diff --git a/src/lib/webtab/searchtoolbar.h b/src/lib/webtab/searchtoolbar.h index d018c74c..b2d4ac23 100644 --- a/src/lib/webtab/searchtoolbar.h +++ b/src/lib/webtab/searchtoolbar.h @@ -1,66 +1,75 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2010-2017 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #ifndef SEARCHTOOLBAR_H #define SEARCHTOOLBAR_H #include +#include + +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(5, 14, 0) +#include +#endif #include "qzcommon.h" namespace Ui { class SearchToolbar; } class QLineEdit; class WebView; class LineEdit; class FALKON_EXPORT SearchToolBar : public QWidget { Q_OBJECT public: explicit SearchToolBar(WebView* view, QWidget* parent = 0); ~SearchToolBar(); void showMinimalInPopupWindow(); void focusSearchLine(); bool eventFilter(QObject* obj, QEvent* event) override; public Q_SLOTS: void setText(const QString &text); void searchText(const QString &text); void updateFindFlags(); void caseSensitivityChanged(); void findNext(); void findPrevious(); +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(5, 14, 0) + void showSearchResults(const QWebEngineFindTextResult &result); +#endif + void close(); private: Ui::SearchToolbar* ui; WebView* m_view; QWebEnginePage::FindFlags m_findFlags; int m_searchRequests = 0; }; #endif // SEARCHTOOLBAR_H diff --git a/src/lib/webtab/searchtoolbar.ui b/src/lib/webtab/searchtoolbar.ui index 1c760b6c..358554b8 100644 --- a/src/lib/webtab/searchtoolbar.ui +++ b/src/lib/webtab/searchtoolbar.ui @@ -1,113 +1,120 @@ SearchToolbar 0 0 855 40 4 4 0 0 true 100 0 16777215 16777215 Find... true Find the next match for the current search phrase &Next Qt::ToolButtonTextBesideIcon Find the previous match for the current search phrase &Previous Qt::ToolButtonTextBesideIcon &Match Case true + + + + + + + FocusSelectLineEdit QLineEdit
focusselectlineedit.h
MacToolButton QToolButton
mactoolbutton.h