2011-03-19 00:28:37 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-19 00:28:37 +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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "sourceviewersearch.h"
|
|
|
|
#include "ui_sourceviewersearch.h"
|
|
|
|
#include "sourceviewer.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "iconprovider.h"
|
2012-03-15 19:35:37 +01:00
|
|
|
#include "plaineditwithlines.h"
|
2011-03-19 00:28:37 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QShortcut>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
SourceViewerSearch::SourceViewerSearch(SourceViewer* parent)
|
|
|
|
: AnimatedWidget(AnimatedWidget::Up)
|
|
|
|
, m_sourceViewer(parent)
|
|
|
|
, ui(new Ui::SourceViewerSearch)
|
2011-03-19 00:28:37 +01:00
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2011-08-02 16:19:20 +02:00
|
|
|
ui->setupUi(widget());
|
2011-09-30 21:44:18 +02:00
|
|
|
ui->closeButton->setIcon(IconProvider::standardIcon(QStyle::SP_DialogCloseButton));
|
2011-03-19 00:28:37 +01:00
|
|
|
|
2011-09-30 21:44:18 +02:00
|
|
|
ui->next->setIcon(IconProvider::standardIcon(QStyle::SP_ArrowForward));
|
2011-03-19 00:28:37 +01:00
|
|
|
|
2011-09-30 21:44:18 +02:00
|
|
|
ui->previous->setIcon(IconProvider::standardIcon(QStyle::SP_ArrowBack));
|
2011-03-19 00:28:37 +01:00
|
|
|
ui->lineEdit->setFocus();
|
|
|
|
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(hide()));
|
2011-03-19 13:14:43 +01:00
|
|
|
connect(ui->lineEdit, SIGNAL(textEdited(QString)), this, SLOT(next()));
|
|
|
|
connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(next()));
|
2011-03-19 00:28:37 +01:00
|
|
|
connect(ui->next, SIGNAL(clicked()), this, SLOT(next()));
|
|
|
|
connect(ui->previous, SIGNAL(clicked()), this, SLOT(previous()));
|
2011-08-02 16:19:20 +02:00
|
|
|
|
2011-12-22 21:09:04 +01:00
|
|
|
QShortcut* findNextAction = new QShortcut(QKeySequence("F3"), this);
|
|
|
|
connect(findNextAction, SIGNAL(activated()), this, SLOT(next()));
|
|
|
|
|
2011-12-22 23:45:55 +01:00
|
|
|
QShortcut* findPreviousAction = new QShortcut(QKeySequence("Shift+F3"), this);
|
|
|
|
connect(findPreviousAction, SIGNAL(activated()), this, SLOT(previous()));
|
|
|
|
|
2011-08-02 16:19:20 +02:00
|
|
|
startAnimation();
|
2011-11-06 12:05:16 +01:00
|
|
|
qApp->installEventFilter(this);
|
2011-03-19 00:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewerSearch::activateLineEdit()
|
|
|
|
{
|
|
|
|
ui->lineEdit->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewerSearch::next()
|
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
bool found = find(0);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!found) {
|
2011-03-19 00:28:37 +01:00
|
|
|
m_sourceViewer->sourceEdit()->moveCursor(QTextCursor::Start);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
ui->lineEdit->setProperty("notfound", !found);
|
|
|
|
|
|
|
|
ui->lineEdit->style()->unpolish(ui->lineEdit);
|
|
|
|
ui->lineEdit->style()->polish(ui->lineEdit);
|
2011-03-19 00:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewerSearch::previous()
|
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
bool found = find(QTextDocument::FindBackward);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!found) {
|
2011-03-19 00:28:37 +01:00
|
|
|
m_sourceViewer->sourceEdit()->moveCursor(QTextCursor::Start);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
ui->lineEdit->setProperty("notfound", !found);
|
|
|
|
|
|
|
|
ui->lineEdit->style()->unpolish(ui->lineEdit);
|
|
|
|
ui->lineEdit->style()->polish(ui->lineEdit);
|
2011-03-19 00:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SourceViewerSearch::find(QTextDocument::FindFlags flags)
|
|
|
|
{
|
|
|
|
QString string = ui->lineEdit->text();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (string.isEmpty()) {
|
2011-03-19 00:28:37 +01:00
|
|
|
return true;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-19 00:28:37 +01:00
|
|
|
if (string != m_lastSearchedString) {
|
|
|
|
QTextCursor cursor = m_sourceViewer->sourceEdit()->textCursor();
|
|
|
|
cursor.setPosition(cursor.selectionStart());
|
|
|
|
cursor.clearSelection();
|
|
|
|
m_sourceViewer->sourceEdit()->setTextCursor(cursor);
|
|
|
|
m_lastSearchedString = string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_sourceViewer->sourceEdit()->find(string, flags)) {
|
|
|
|
QTextCursor cursor = m_sourceViewer->sourceEdit()->textCursor();
|
2011-11-06 17:01:23 +01:00
|
|
|
m_sourceViewer->sourceEdit()->moveCursor((flags == QTextDocument::FindBackward) ? QTextCursor::End : QTextCursor::Start);
|
|
|
|
if (!m_sourceViewer->sourceEdit()->find(string, flags)) {
|
2011-03-19 00:28:37 +01:00
|
|
|
cursor.clearSelection();
|
|
|
|
m_sourceViewer->sourceEdit()->setTextCursor(cursor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
bool SourceViewerSearch::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
|
|
|
|
hide();
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-19 00:28:37 +01:00
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
return AnimatedWidget::eventFilter(obj, event);
|
|
|
|
}
|