2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-10 12:36:41 +01:00
|
|
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "searchtoolbar.h"
|
2018-02-05 11:41:15 +01:00
|
|
|
#include "webview.h"
|
2015-08-30 10:56:07 +02:00
|
|
|
#include "webpage.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "lineedit.h"
|
2011-08-02 16:19:20 +02:00
|
|
|
#include "ui_searchtoolbar.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "iconprovider.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-08-31 15:19:07 +02:00
|
|
|
#include <QKeyEvent>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QShortcut>
|
|
|
|
|
2013-01-25 19:52:30 +01:00
|
|
|
SearchToolBar::SearchToolBar(WebView* view, QWidget* parent)
|
2017-01-21 17:42:14 +01:00
|
|
|
: QWidget(parent)
|
2011-11-06 17:01:23 +01:00
|
|
|
, ui(new Ui::SearchToolbar)
|
2013-01-25 19:52:30 +01:00
|
|
|
, m_view(view)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_findFlags(0)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-08-02 16:19:20 +02:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2017-01-21 17:42:14 +01:00
|
|
|
ui->setupUi(this);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2015-01-27 11:01:52 +01:00
|
|
|
ui->closeButton->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_DialogCloseButton));
|
|
|
|
ui->next->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_ArrowDown));
|
2017-12-03 10:13:10 +01:00
|
|
|
ui->next->setShortcut(QKeySequence("Ctrl+G"));
|
2015-01-27 11:01:52 +01:00
|
|
|
ui->previous->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_ArrowUp));
|
2017-12-03 10:13:10 +01:00
|
|
|
ui->previous->setShortcut(QKeySequence("Ctrl+Shift+G"));
|
2011-08-02 16:19:20 +02:00
|
|
|
|
2017-01-21 17:42:14 +01:00
|
|
|
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
|
2018-03-10 15:04:39 +01:00
|
|
|
connect(ui->lineEdit, SIGNAL(textEdited(QString)), this, SLOT(findNext()));
|
2012-02-07 18:37:44 +01:00
|
|
|
connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(findNext()));
|
2011-08-02 16:19:20 +02:00
|
|
|
connect(ui->next, SIGNAL(clicked()), this, SLOT(findNext()));
|
|
|
|
connect(ui->previous, SIGNAL(clicked()), this, SLOT(findPrevious()));
|
2011-12-22 23:33:35 +01:00
|
|
|
connect(ui->caseSensitive, SIGNAL(clicked()), this, SLOT(caseSensitivityChanged()));
|
2011-11-06 12:05:16 +01:00
|
|
|
|
2011-12-22 21:09:04 +01:00
|
|
|
QShortcut* findNextAction = new QShortcut(QKeySequence("F3"), this);
|
|
|
|
connect(findNextAction, SIGNAL(activated()), this, SLOT(findNext()));
|
|
|
|
|
2011-12-22 23:45:55 +01:00
|
|
|
QShortcut* findPreviousAction = new QShortcut(QKeySequence("Shift+F3"), this);
|
|
|
|
connect(findPreviousAction, SIGNAL(activated()), this, SLOT(findPrevious()));
|
|
|
|
|
2013-01-25 19:52:30 +01:00
|
|
|
parent->installEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2013-02-10 11:25:44 +01:00
|
|
|
void SearchToolBar::showMinimalInPopupWindow()
|
|
|
|
{
|
|
|
|
// Show only essentials widget + set minimum width
|
|
|
|
ui->caseSensitive->hide();
|
|
|
|
ui->results->hide();
|
|
|
|
ui->horizontalLayout->setSpacing(2);
|
|
|
|
ui->horizontalLayout->setContentsMargins(2, 6, 2, 6);
|
|
|
|
setMinimumWidth(260);
|
|
|
|
}
|
|
|
|
|
2012-04-19 15:58:23 +02:00
|
|
|
void SearchToolBar::focusSearchLine()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-19 15:58:23 +02:00
|
|
|
ui->lineEdit->setFocus();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:42:14 +01:00
|
|
|
void SearchToolBar::close()
|
2012-01-16 16:28:41 +01:00
|
|
|
{
|
2017-01-21 17:42:14 +01:00
|
|
|
hide();
|
2012-08-27 16:01:22 +02:00
|
|
|
searchText(QString());
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->setFocus();
|
2017-01-21 17:42:14 +01:00
|
|
|
deleteLater();
|
2012-01-16 16:28:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void SearchToolBar::findNext()
|
|
|
|
{
|
2015-01-27 11:01:52 +01:00
|
|
|
m_findFlags = 0;
|
2011-12-22 23:33:35 +01:00
|
|
|
updateFindFlags();
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-12-22 23:33:35 +01:00
|
|
|
searchText(ui->lineEdit->text());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchToolBar::findPrevious()
|
|
|
|
{
|
2015-01-27 11:01:52 +01:00
|
|
|
m_findFlags = QWebEnginePage::FindBackward;
|
2011-12-22 23:33:35 +01:00
|
|
|
updateFindFlags();
|
|
|
|
|
2011-08-02 16:19:20 +02:00
|
|
|
searchText(ui->lineEdit->text());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 23:33:35 +01:00
|
|
|
void SearchToolBar::updateFindFlags()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-12-22 23:33:35 +01:00
|
|
|
if (ui->caseSensitive->isChecked()) {
|
2015-01-27 11:01:52 +01:00
|
|
|
m_findFlags = m_findFlags | QWebEnginePage::FindCaseSensitively;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-12-22 23:33:35 +01:00
|
|
|
else {
|
2015-01-27 11:01:52 +01:00
|
|
|
m_findFlags = m_findFlags & ~QWebEnginePage::FindCaseSensitively;
|
2011-12-22 23:33:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchToolBar::caseSensitivityChanged()
|
|
|
|
{
|
|
|
|
updateFindFlags();
|
|
|
|
|
2015-08-30 10:57:08 +02:00
|
|
|
searchText(QString());
|
2011-12-22 23:33:35 +01:00
|
|
|
searchText(ui->lineEdit->text());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:04:39 +01:00
|
|
|
void SearchToolBar::setText(const QString &text)
|
|
|
|
{
|
|
|
|
ui->lineEdit->setText(text);
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void SearchToolBar::searchText(const QString &text)
|
|
|
|
{
|
2018-12-24 14:05:29 +01:00
|
|
|
m_searchRequests++;
|
2017-08-25 16:11:40 +02:00
|
|
|
QPointer<SearchToolBar> guard = this;
|
|
|
|
m_view->findText(text, m_findFlags, [=](bool found) {
|
|
|
|
if (!guard) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-24 14:05:29 +01:00
|
|
|
if (--m_searchRequests != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-30 10:56:07 +02:00
|
|
|
if (ui->lineEdit->text().isEmpty())
|
|
|
|
found = true;
|
2012-07-27 18:55:55 +02:00
|
|
|
|
2015-08-30 10:56:07 +02:00
|
|
|
if (!found)
|
|
|
|
ui->results->setText(tr("No results found."));
|
|
|
|
else
|
|
|
|
ui->results->clear();
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2015-08-30 10:56:07 +02:00
|
|
|
ui->lineEdit->setProperty("notfound", QVariant(!found));
|
|
|
|
ui->lineEdit->style()->unpolish(ui->lineEdit);
|
|
|
|
ui->lineEdit->style()->polish(ui->lineEdit);
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2015-08-30 10:56:07 +02:00
|
|
|
// Clear selection
|
2018-01-10 12:36:41 +01:00
|
|
|
m_view->page()->runJavaScript(QSL("window.getSelection().empty();"), WebPage::SafeJsWorld);
|
2015-08-30 10:56:07 +02:00
|
|
|
});
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-08-02 16:19:20 +02:00
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
bool SearchToolBar::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
{
|
2012-04-19 15:58:23 +02:00
|
|
|
Q_UNUSED(obj);
|
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
|
2017-01-21 17:42:14 +01:00
|
|
|
close();
|
2011-11-06 12:05:16 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 15:58:23 +02:00
|
|
|
return false;
|
2011-11-06 12:05:16 +01:00
|
|
|
}
|
|
|
|
|
2011-08-02 16:19:20 +02:00
|
|
|
SearchToolBar::~SearchToolBar()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|