From e6fd3d0318d0f53c51cf4cc87ccdc8f9741957e7 Mon Sep 17 00:00:00 2001 From: Robert Bieber Date: Fri, 23 Jul 2010 07:31:53 +0000 Subject: Theme Editor: Switched back to Lorenzo Bettini's find/replace dialog (with some modifications) as he changed the license to LGPL v2.1 git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27528 a1c6a512-1295-4272-9138-f99709370657 --- .../qtfindreplacedialog/findreplaceform.cpp | 218 +++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 utils/themeeditor/qtfindreplacedialog/findreplaceform.cpp (limited to 'utils/themeeditor/qtfindreplacedialog/findreplaceform.cpp') diff --git a/utils/themeeditor/qtfindreplacedialog/findreplaceform.cpp b/utils/themeeditor/qtfindreplacedialog/findreplaceform.cpp new file mode 100644 index 0000000000..2859e92faa --- /dev/null +++ b/utils/themeeditor/qtfindreplacedialog/findreplaceform.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2009 Lorenzo Bettini + * See COPYING file that comes with this distribution + */ + +#include +#include +#include +#include + +#include "varianteditor.h" +#include "findreplaceform.h" +#include "ui_findreplaceform.h" + +#define TEXT_TO_FIND "textToFind" +#define TEXT_TO_REPLACE "textToReplace" +#define DOWN_RADIO "downRadio" +#define UP_RADIO "upRadio" +#define CASE_CHECK "caseCheck" +#define WHOLE_CHECK "wholeCheck" +#define REGEXP_CHECK "regexpCheck" + +FindReplaceForm::FindReplaceForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::FindReplaceForm), textEdit(0) +{ + ui->setupUi(this); + + ui->errorLabel->setText(""); + + connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(textToFindChanged())); + connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(validateRegExp(QString))); + + connect(ui->regexCheckBox, SIGNAL(toggled(bool)), this, SLOT(regexpSelected(bool))); + + connect(ui->findButton, SIGNAL(clicked()), this, SLOT(find())); + connect(ui->closeButton, SIGNAL(clicked()), parent, SLOT(close())); + + connect(ui->replaceButton, SIGNAL(clicked()), this, SLOT(replace())); + connect(ui->replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAll())); +} + +FindReplaceForm::~FindReplaceForm() +{ + delete ui; + if(textEdit) + delete textEdit; +} + +void FindReplaceForm::hideReplaceWidgets() { + ui->replaceLabel->setVisible(false); + ui->textToReplace->setVisible(false); + ui->replaceButton->setVisible(false); + ui->replaceAllButton->setVisible(false); +} + +void FindReplaceForm::setTextEdit(QTextEdit *textEdit_) { + textEdit = new VariantEditor(textEdit_); + textEdit->connectToSetEnabled(ui->replaceButton); + textEdit->connectToSetEnabled(ui->replaceAllButton); +} + +void FindReplaceForm::setTextEdit(QPlainTextEdit *textEdit_) +{ + textEdit = new VariantEditor(textEdit_); + textEdit->connectToSetEnabled(ui->replaceButton); + textEdit->connectToSetEnabled(ui->replaceAllButton); +} + +void FindReplaceForm::changeEvent(QEvent *e) +{ + QWidget::changeEvent(e); + switch (e->type()) { + case QEvent::LanguageChange: + ui->retranslateUi(this); + break; + default: + break; + } +} + +void FindReplaceForm::textToFindChanged() { + ui->findButton->setEnabled(ui->textToFind->text().size() > 0); +} + +void FindReplaceForm::regexpSelected(bool sel) { + if (sel) + validateRegExp(ui->textToFind->text()); + else + validateRegExp(""); +} + +void FindReplaceForm::validateRegExp(const QString &text) { + if (!ui->regexCheckBox->isChecked() || text.size() == 0) { + ui->errorLabel->setText(""); + return; // nothing to validate + } + + QRegExp reg(text, + (ui->caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive)); + + if (reg.isValid()) { + showError(""); + } else { + showError(reg.errorString()); + } +} + +void FindReplaceForm::showError(const QString &error) { + if (error == "") { + ui->errorLabel->setText(""); + } else { + ui->errorLabel->setText("" + + error + + ""); + } +} + +void FindReplaceForm::showMessage(const QString &message) { + if (message == "") { + ui->errorLabel->setText(""); + } else { + ui->errorLabel->setText("" + + message + + ""); + } +} + +void FindReplaceForm::find() { + find(ui->downRadioButton->isChecked()); +} + +void FindReplaceForm::find(bool next) { + if (!textEdit) + return; // TODO: show some warning? + + // backward search + bool back = !next; + + const QString &toSearch = ui->textToFind->text(); + + bool result = false; + + QTextDocument::FindFlags flags; + + if (back) + flags |= QTextDocument::FindBackward; + if (ui->caseCheckBox->isChecked()) + flags |= QTextDocument::FindCaseSensitively; + if (ui->wholeCheckBox->isChecked()) + flags |= QTextDocument::FindWholeWords; + + if (ui->regexCheckBox->isChecked()) { + QRegExp reg(toSearch, + (ui->caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive)); + + qDebug() << "searching for regexp: " << reg.pattern(); + + textCursor = textEdit->document()->find(reg, textCursor, flags); + textEdit->setTextCursor(textCursor); + result = (!textCursor.isNull()); + } else { + qDebug() << "searching for: " << toSearch; + + result = textEdit->find(toSearch, flags); + } + + if (result) { + showError(""); + } else { + showError(tr("no match found")); + // move to the beginning of the document for the next find + textEdit->setTextCursor(QTextCursor(textEdit->document()->begin())); + } +} + +void FindReplaceForm::replace() { + if (!textEdit->textCursor().hasSelection()) { + find(); + } else { + textEdit->textCursor().insertText(ui->textToReplace->text()); + find(); + } +} + +void FindReplaceForm::replaceAll() { + int i=0; + while (textEdit->textCursor().hasSelection()){ + textEdit->textCursor().insertText(ui->textToReplace->text()); + find(); + i++; + } + showMessage(tr("Replaced %1 occurrence(s)").arg(i)); +} + +void FindReplaceForm::writeSettings(QSettings &settings, const QString &prefix) { + settings.beginGroup(prefix); + settings.setValue(TEXT_TO_FIND, ui->textToFind->text()); + settings.setValue(TEXT_TO_REPLACE, ui->textToReplace->text()); + settings.setValue(DOWN_RADIO, ui->downRadioButton->isChecked()); + settings.setValue(UP_RADIO, ui->upRadioButton->isChecked()); + settings.setValue(CASE_CHECK, ui->caseCheckBox->isChecked()); + settings.setValue(WHOLE_CHECK, ui->wholeCheckBox->isChecked()); + settings.setValue(REGEXP_CHECK, ui->regexCheckBox->isChecked()); + settings.endGroup(); +} + +void FindReplaceForm::readSettings(QSettings &settings, const QString &prefix) { + settings.beginGroup(prefix); + ui->textToFind->setText(settings.value(TEXT_TO_FIND, "").toString()); + ui->textToReplace->setText(settings.value(TEXT_TO_REPLACE, "").toString()); + ui->downRadioButton->setChecked(settings.value(DOWN_RADIO, true).toBool()); + ui->upRadioButton->setChecked(settings.value(UP_RADIO, false).toBool()); + ui->caseCheckBox->setChecked(settings.value(CASE_CHECK, false).toBool()); + ui->wholeCheckBox->setChecked(settings.value(WHOLE_CHECK, false).toBool()); + ui->regexCheckBox->setChecked(settings.value(REGEXP_CHECK, false).toBool()); + settings.endGroup(); +} -- cgit v1.2.3