2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 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"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.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)
|
2011-11-06 17:01:23 +01:00
|
|
|
: AnimatedWidget(AnimatedWidget::Up, 300, parent)
|
|
|
|
, 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);
|
|
|
|
ui->setupUi(widget());
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
ui->closeButton->setIcon(IconProvider::standardIcon(QStyle::SP_DialogCloseButton));
|
|
|
|
ui->next->setIcon(IconProvider::standardIcon(QStyle::SP_ArrowDown));
|
|
|
|
ui->previous->setIcon(IconProvider::standardIcon(QStyle::SP_ArrowUp));
|
2011-08-02 16:19:20 +02:00
|
|
|
|
|
|
|
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(hide()));
|
2011-12-22 23:33:35 +01:00
|
|
|
connect(ui->lineEdit, SIGNAL(textChanged(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->highligh, SIGNAL(clicked()), this, SLOT(highlightChanged()));
|
|
|
|
connect(ui->caseSensitive, SIGNAL(clicked()), this, SLOT(caseSensitivityChanged()));
|
2011-08-02 16:19:20 +02:00
|
|
|
startAnimation();
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchToolBar::setWebView(WebView* view)
|
|
|
|
{
|
|
|
|
m_view = view;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-10 11:25:44 +01:00
|
|
|
void SearchToolBar::showMinimalInPopupWindow()
|
|
|
|
{
|
|
|
|
// Show only essentials widget + set minimum width
|
|
|
|
ui->highligh->hide();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-16 16:28:41 +01:00
|
|
|
void SearchToolBar::hide()
|
|
|
|
{
|
|
|
|
AnimatedWidget::hide();
|
2012-08-27 16:01:22 +02:00
|
|
|
|
|
|
|
searchText(QString());
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->setFocus();
|
2012-01-16 16:28:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void SearchToolBar::findNext()
|
|
|
|
{
|
2011-12-22 23:33:35 +01:00
|
|
|
m_findFlags = QWebPage::FindWrapsAroundDocument;
|
|
|
|
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()
|
|
|
|
{
|
2011-12-22 23:33:35 +01:00
|
|
|
m_findFlags = QWebPage::FindBackward | QWebPage::FindWrapsAroundDocument;
|
|
|
|
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()) {
|
|
|
|
m_findFlags = m_findFlags | QWebPage::FindCaseSensitively;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-12-22 23:33:35 +01:00
|
|
|
m_findFlags = m_findFlags & ~QWebPage::FindCaseSensitively;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-12-22 23:33:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchToolBar::highlightChanged()
|
|
|
|
{
|
|
|
|
if (ui->highligh->isChecked()) {
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->findText(ui->lineEdit->text(), m_findFlags | QWebPage::HighlightAllOccurrences);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-12-22 23:33:35 +01:00
|
|
|
else {
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->findText(QString(), QWebPage::HighlightAllOccurrences);
|
2011-12-22 23:33:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchToolBar::caseSensitivityChanged()
|
|
|
|
{
|
|
|
|
updateFindFlags();
|
|
|
|
|
|
|
|
searchText(ui->lineEdit->text());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchToolBar::searchText(const QString &text)
|
|
|
|
{
|
2012-07-27 21:23:41 +02:00
|
|
|
// Clear highlighting on page
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->findText(QString(), QWebPage::HighlightAllOccurrences);
|
2012-07-27 21:23:41 +02:00
|
|
|
|
2013-01-25 19:52:30 +01:00
|
|
|
bool found = m_view->findText(text, m_findFlags);
|
2012-07-27 18:55:55 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (text.isEmpty()) {
|
2011-09-11 19:15:06 +02:00
|
|
|
found = true;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2011-12-22 23:33:35 +01:00
|
|
|
if (ui->highligh->isChecked()) {
|
|
|
|
m_findFlags = QWebPage::HighlightAllOccurrences;
|
|
|
|
updateFindFlags();
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->findText(text, m_findFlags);
|
2011-12-22 23:33:35 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-01-25 19:52:30 +01:00
|
|
|
m_view->findText(QString(), QWebPage::HighlightAllOccurrences);
|
2011-12-22 23:33:35 +01:00
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!found) {
|
2011-08-02 16:19:20 +02:00
|
|
|
ui->results->setText(tr("No results found."));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-08-02 16:19:20 +02:00
|
|
|
ui->results->clear();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
|
2012-06-26 11:49:39 +02:00
|
|
|
ui->lineEdit->setProperty("notfound", QVariant(!found));
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
ui->lineEdit->style()->unpolish(ui->lineEdit);
|
|
|
|
ui->lineEdit->style()->polish(ui->lineEdit);
|
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) {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|