1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-14 11:02:19 +01:00
falkonOfficial/src/lib/webview/searchtoolbar.cpp

179 lines
5.0 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2013 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"
#include "tabbedwebview.h"
2011-03-02 16:57:41 +01:00
#include "lineedit.h"
#include "ui_searchtoolbar.h"
#include "iconprovider.h"
2011-03-02 16:57:41 +01:00
2012-08-31 15:19:07 +02:00
#include <QKeyEvent>
#include <QShortcut>
SearchToolBar::SearchToolBar(WebView* view, QWidget* parent)
: AnimatedWidget(AnimatedWidget::Up, 300, parent)
, ui(new Ui::SearchToolbar)
, m_view(view)
, m_findFlags(0)
2011-03-02 16:57:41 +01:00
{
setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(widget());
2011-03-02 16:57:41 +01:00
ui->closeButton->setIcon(qIconProvider->standardIcon(QStyle::SP_DialogCloseButton));
ui->next->setIcon(qIconProvider->standardIcon(QStyle::SP_ArrowForward));
ui->previous->setIcon(qIconProvider->standardIcon(QStyle::SP_ArrowBack));
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(hide()));
connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(findNext()));
connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(findNext()));
connect(ui->next, SIGNAL(clicked()), this, SLOT(findNext()));
connect(ui->previous, SIGNAL(clicked()), this, SLOT(findPrevious()));
connect(ui->highligh, SIGNAL(clicked()), this, SLOT(highlightChanged()));
connect(ui->caseSensitive, SIGNAL(clicked()), this, SLOT(caseSensitivityChanged()));
startAnimation();
QShortcut* findNextAction = new QShortcut(QKeySequence("F3"), this);
connect(findNextAction, SIGNAL(activated()), this, SLOT(findNext()));
QShortcut* findPreviousAction = new QShortcut(QKeySequence("Shift+F3"), this);
connect(findPreviousAction, SIGNAL(activated()), this, SLOT(findPrevious()));
parent->installEventFilter(this);
}
void SearchToolBar::setWebView(WebView* view)
{
m_view = view;
2011-03-02 16:57:41 +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);
}
void SearchToolBar::focusSearchLine()
2011-03-02 16:57:41 +01:00
{
ui->lineEdit->setFocus();
2011-03-02 16:57:41 +01:00
}
void SearchToolBar::hide()
{
AnimatedWidget::hide();
searchText(QString());
m_view->setFocus();
}
2011-03-02 16:57:41 +01:00
void SearchToolBar::findNext()
{
m_findFlags = QWebPage::FindWrapsAroundDocument;
updateFindFlags();
2011-03-02 16:57:41 +01:00
searchText(ui->lineEdit->text());
2011-03-02 16:57:41 +01:00
}
void SearchToolBar::findPrevious()
{
m_findFlags = QWebPage::FindBackward | QWebPage::FindWrapsAroundDocument;
updateFindFlags();
searchText(ui->lineEdit->text());
2011-03-02 16:57:41 +01:00
}
void SearchToolBar::updateFindFlags()
2011-03-02 16:57:41 +01:00
{
if (ui->caseSensitive->isChecked()) {
m_findFlags = m_findFlags | QWebPage::FindCaseSensitively;
}
else {
m_findFlags = m_findFlags & ~QWebPage::FindCaseSensitively;
2011-03-02 16:57:41 +01:00
}
}
void SearchToolBar::highlightChanged()
{
if (ui->highligh->isChecked()) {
m_view->findText(ui->lineEdit->text(), m_findFlags | QWebPage::HighlightAllOccurrences);
2011-03-02 16:57:41 +01:00
}
else {
m_view->findText(QString(), QWebPage::HighlightAllOccurrences);
}
}
void SearchToolBar::caseSensitivityChanged()
{
updateFindFlags();
searchText(ui->lineEdit->text());
2011-03-02 16:57:41 +01:00
}
void SearchToolBar::searchText(const QString &text)
{
// Clear highlighting on page
m_view->findText(QString(), QWebPage::HighlightAllOccurrences);
bool found = m_view->findText(text, m_findFlags);
if (text.isEmpty()) {
found = true;
}
if (ui->highligh->isChecked()) {
m_findFlags = QWebPage::HighlightAllOccurrences;
updateFindFlags();
m_view->findText(text, m_findFlags);
}
else {
m_view->findText(QString(), QWebPage::HighlightAllOccurrences);
}
if (!found) {
ui->results->setText(tr("No results found."));
}
else {
ui->results->clear();
}
ui->lineEdit->setProperty("notfound", QVariant(!found));
ui->lineEdit->style()->unpolish(ui->lineEdit);
ui->lineEdit->style()->polish(ui->lineEdit);
2011-03-02 16:57:41 +01:00
}
bool SearchToolBar::eventFilter(QObject* obj, QEvent* event)
{
Q_UNUSED(obj);
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
hide();
}
return false;
}
SearchToolBar::~SearchToolBar()
{
delete ui;
}