File indexing completed on 2024-05-12 04:58:31
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "searchtoolbar.h" 0019 #include "webview.h" 0020 #include "webpage.h" 0021 #include "lineedit.h" 0022 #include "ui_searchtoolbar.h" 0023 #include "iconprovider.h" 0024 0025 #include <QKeyEvent> 0026 #include <QShortcut> 0027 0028 SearchToolBar::SearchToolBar(WebView* view, QWidget* parent) 0029 : QWidget(parent) 0030 , ui(new Ui::SearchToolbar) 0031 , m_view(view) 0032 , m_findFlags{} 0033 { 0034 setAttribute(Qt::WA_DeleteOnClose); 0035 ui->setupUi(this); 0036 0037 ui->closeButton->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_DialogCloseButton)); 0038 ui->next->setShortcut(QKeySequence(QSL("Ctrl+G"))); 0039 ui->previous->setShortcut(QKeySequence(QSL("Ctrl+Shift+G"))); 0040 0041 ui->resultsInfo->hide(); 0042 connect(view->page(), &QWebEnginePage::findTextFinished, this, &SearchToolBar::showSearchResults); 0043 0044 connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close())); 0045 connect(ui->lineEdit, &QLineEdit::textEdited, this, &SearchToolBar::findNext); 0046 connect(ui->next, &QAbstractButton::clicked, this, &SearchToolBar::findNext); 0047 connect(ui->previous, &QAbstractButton::clicked, this, &SearchToolBar::findPrevious); 0048 connect(ui->caseSensitive, &QAbstractButton::clicked, this, &SearchToolBar::caseSensitivityChanged); 0049 0050 auto* findNextAction = new QShortcut(QKeySequence(QSL("F3")), this); 0051 connect(findNextAction, &QShortcut::activated, this, &SearchToolBar::findNext); 0052 0053 auto* findPreviousAction = new QShortcut(QKeySequence(QSL("Shift+F3")), this); 0054 connect(findPreviousAction, &QShortcut::activated, this, &SearchToolBar::findPrevious); 0055 0056 parent->installEventFilter(this); 0057 } 0058 0059 void SearchToolBar::showMinimalInPopupWindow() 0060 { 0061 // Show only essentials widget + set minimum width 0062 ui->caseSensitive->hide(); 0063 ui->horizontalLayout->setSpacing(2); 0064 ui->horizontalLayout->setContentsMargins(2, 6, 2, 6); 0065 setMinimumWidth(260); 0066 } 0067 0068 void SearchToolBar::focusSearchLine() 0069 { 0070 ui->lineEdit->setFocus(); 0071 } 0072 0073 void SearchToolBar::close() 0074 { 0075 hide(); 0076 searchText(QString()); 0077 m_view->setFocus(); 0078 deleteLater(); 0079 } 0080 0081 void SearchToolBar::findNext() 0082 { 0083 m_findFlags = {}; 0084 updateFindFlags(); 0085 0086 searchText(ui->lineEdit->text()); 0087 } 0088 0089 void SearchToolBar::findPrevious() 0090 { 0091 m_findFlags = QWebEnginePage::FindBackward; 0092 updateFindFlags(); 0093 0094 searchText(ui->lineEdit->text()); 0095 } 0096 0097 void SearchToolBar::updateFindFlags() 0098 { 0099 if (ui->caseSensitive->isChecked()) { 0100 m_findFlags = m_findFlags | QWebEnginePage::FindCaseSensitively; 0101 } 0102 else { 0103 m_findFlags = m_findFlags & ~QWebEnginePage::FindCaseSensitively; 0104 } 0105 } 0106 0107 void SearchToolBar::caseSensitivityChanged() 0108 { 0109 updateFindFlags(); 0110 0111 searchText(QString()); 0112 searchText(ui->lineEdit->text()); 0113 } 0114 0115 void SearchToolBar::setText(const QString &text) 0116 { 0117 ui->lineEdit->setText(text); 0118 } 0119 0120 void SearchToolBar::searchText(const QString &text) 0121 { 0122 m_searchRequests++; 0123 QPointer<SearchToolBar> guard = this; 0124 m_view->findText(text, m_findFlags, [=](QWebEngineFindTextResult result) { 0125 bool found = result.numberOfMatches() > 0; 0126 if (!guard) { 0127 return; 0128 } 0129 if (--m_searchRequests != 0) { 0130 return; 0131 } 0132 if (ui->lineEdit->text().isEmpty()) 0133 found = true; 0134 0135 ui->lineEdit->setProperty("notfound", QVariant(!found)); 0136 ui->lineEdit->style()->unpolish(ui->lineEdit); 0137 ui->lineEdit->style()->polish(ui->lineEdit); 0138 0139 // Clear selection 0140 m_view->page()->runJavaScript(QSL("window.getSelection().empty();"), WebPage::SafeJsWorld); 0141 }); 0142 } 0143 0144 void SearchToolBar::showSearchResults(const QWebEngineFindTextResult &result) 0145 { 0146 if (result.numberOfMatches() == 0) { 0147 ui->resultsInfo->hide(); 0148 return; 0149 } 0150 0151 ui->resultsInfo->setText(tr("%1 of %2").arg( 0152 QString::number(result.activeMatch()), QString::number(result.numberOfMatches()))); 0153 ui->resultsInfo->show(); 0154 } 0155 0156 bool SearchToolBar::eventFilter(QObject* obj, QEvent* event) 0157 { 0158 Q_UNUSED(obj); 0159 0160 if (event->type() == QEvent::KeyPress) { 0161 auto *ke = static_cast<QKeyEvent*>(event); 0162 switch (ke->key()) { 0163 case Qt::Key_Escape: 0164 close(); 0165 break; 0166 case Qt::Key_Enter: 0167 case Qt::Key_Return: 0168 if (ke->modifiers() & Qt::ShiftModifier) { 0169 findPrevious(); 0170 } else { 0171 findNext(); 0172 } 0173 break; 0174 default: 0175 break; 0176 } 0177 } 0178 0179 return false; 0180 } 0181 0182 SearchToolBar::~SearchToolBar() 0183 { 0184 delete ui; 0185 }