2010-07-03 06:08:59 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Robert Bieber
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "findreplacedialog.h"
|
|
|
|
#include "ui_findreplacedialog.h"
|
|
|
|
|
|
|
|
#include <QTextBlock>
|
|
|
|
|
|
|
|
FindReplaceDialog::FindReplaceDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
2010-07-03 06:53:06 +00:00
|
|
|
ui(new Ui::FindReplaceDialog), editor(0), textFound()
|
2010-07-03 06:08:59 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
setupUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
FindReplaceDialog::~FindReplaceDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::changeEvent(QEvent *e)
|
|
|
|
{
|
|
|
|
QDialog::changeEvent(e);
|
|
|
|
switch (e->type()) {
|
|
|
|
case QEvent::LanguageChange:
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::closeEvent(QCloseEvent* event)
|
|
|
|
{
|
|
|
|
ui->statusLabel->setText("");
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::setupUI()
|
|
|
|
{
|
2010-07-03 08:23:20 +00:00
|
|
|
QObject::connect(ui->findButton, SIGNAL(clicked()),
|
2010-07-03 06:08:59 +00:00
|
|
|
this, SLOT(find()));
|
2010-07-03 08:23:20 +00:00
|
|
|
QObject::connect(ui->replaceButton, SIGNAL(clicked()),
|
2010-07-03 06:08:59 +00:00
|
|
|
this, SLOT(replace()));
|
2010-07-03 08:23:20 +00:00
|
|
|
QObject::connect(ui->replaceAllButton, SIGNAL(clicked()),
|
2010-07-03 06:08:59 +00:00
|
|
|
this, SLOT(replaceAll()));
|
2010-07-03 08:23:20 +00:00
|
|
|
QObject::connect(ui->closeButton, SIGNAL(clicked()),
|
2010-07-03 06:08:59 +00:00
|
|
|
this, SLOT(close()));
|
|
|
|
QObject::connect(ui->findBox, SIGNAL(textChanged(QString)),
|
|
|
|
this, SLOT(textChanged()));
|
|
|
|
|
|
|
|
textChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::find()
|
|
|
|
{
|
|
|
|
|
|
|
|
if(!editor)
|
|
|
|
return;
|
|
|
|
|
2010-07-03 06:53:06 +00:00
|
|
|
QTextDocument::FindFlags flags = 0;
|
|
|
|
if(ui->caseBox->isChecked())
|
|
|
|
flags |= QTextDocument::FindCaseSensitively;
|
|
|
|
if(ui->backwardsBox->isChecked())
|
|
|
|
flags |= QTextDocument::FindBackward;
|
|
|
|
|
|
|
|
QTextCursor start = textFound.isNull() ? editor->textCursor() : textFound;
|
|
|
|
|
|
|
|
textFound = editor->document()->find(ui->findBox->text(), start, flags);
|
|
|
|
|
|
|
|
if(textFound.isNull() && ui->wrapBox->isChecked())
|
|
|
|
{
|
|
|
|
if(ui->backwardsBox->isChecked())
|
|
|
|
{
|
|
|
|
textFound = editor->document()
|
|
|
|
->find(ui->findBox->text(),
|
|
|
|
editor->document()->toPlainText().length(),
|
|
|
|
flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textFound = editor->document()->find(ui->findBox->text(), 0, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPalette newPal;
|
|
|
|
if(!textFound.isNull())
|
|
|
|
{
|
2010-07-03 08:23:20 +00:00
|
|
|
newPal.setColor(QPalette::Foreground, QColor(0, 150, 0));
|
2010-07-03 06:53:06 +00:00
|
|
|
ui->statusLabel->setPalette(newPal);
|
|
|
|
ui->statusLabel->setText(tr("Match Found"));
|
|
|
|
editor->setTextCursor(textFound);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newPal.setColor(QPalette::Foreground, Qt::red);
|
|
|
|
ui->statusLabel->setPalette(newPal);
|
|
|
|
ui->statusLabel->setText(tr("Match Not Found"));
|
|
|
|
editor->setTextCursor(start);
|
|
|
|
}
|
2010-07-03 06:08:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::replace()
|
|
|
|
{
|
2010-07-03 08:23:20 +00:00
|
|
|
if(textFound.isNull())
|
|
|
|
find();
|
|
|
|
|
|
|
|
if(textFound.isNull())
|
|
|
|
return;
|
2010-07-03 06:08:59 +00:00
|
|
|
|
2010-07-03 08:23:20 +00:00
|
|
|
editor->setTextCursor(textFound);
|
|
|
|
editor->insertPlainText(ui->replaceBox->text());
|
|
|
|
textFound = QTextCursor();
|
2010-07-03 06:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::replaceAll()
|
|
|
|
{
|
|
|
|
|
2010-07-03 08:23:20 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if(!textFound.isNull())
|
|
|
|
{
|
|
|
|
editor->setTextCursor(textFound);
|
|
|
|
editor->insertPlainText(ui->replaceBox->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
find();
|
|
|
|
}while(!textFound.isNull());
|
|
|
|
|
2010-07-03 06:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindReplaceDialog::textChanged()
|
|
|
|
{
|
|
|
|
bool enabled = ui->findBox->text() != "";
|
|
|
|
|
|
|
|
ui->findButton->setEnabled(enabled);
|
|
|
|
ui->replaceButton->setEnabled(enabled);
|
|
|
|
ui->replaceAllButton->setEnabled(enabled);
|
|
|
|
}
|