diff options
| author | Robert Bieber <robby@bieberphoto.com> | 2010-06-26 07:59:23 +0000 |
|---|---|---|
| committer | Robert Bieber <robby@bieberphoto.com> | 2010-06-26 07:59:23 +0000 |
| commit | be70fd89be787e2b24604f9ba785b87c1f8f1d22 (patch) | |
| tree | 5b7ec98c48d2a5d2dc5078007142d2e92c09a8a2 /utils/themeeditor/findreplace/findreplaceform.cpp | |
| parent | 5300c7014d602c57fcae7f6619f5138d83ba33c0 (diff) | |
| download | rockbox-be70fd89be787e2b24604f9ba785b87c1f8f1d22.zip rockbox-be70fd89be787e2b24604f9ba785b87c1f8f1d22.tar.gz rockbox-be70fd89be787e2b24604f9ba785b87c1f8f1d22.tar.bz2 rockbox-be70fd89be787e2b24604f9ba785b87c1f8f1d22.tar.xz | |
Theme Editor: Added an edit menu with a find/replace function (copied from an LGPL library)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27137 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/themeeditor/findreplace/findreplaceform.cpp')
| -rw-r--r-- | utils/themeeditor/findreplace/findreplaceform.cpp | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/utils/themeeditor/findreplace/findreplaceform.cpp b/utils/themeeditor/findreplace/findreplaceform.cpp new file mode 100644 index 0000000..cd2700f --- /dev/null +++ b/utils/themeeditor/findreplace/findreplaceform.cpp @@ -0,0 +1,222 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * This file has been copied from Lorenzo Bettini, with minor modifications + * made available under the LGPL version 3, as the original file was licensed + * + **************************************************************************** + * + * Copyright (C) 2009 Lorenzo Bettini <http://www.lorenzobettini.it> + * See COPYING file that comes with this distribution + */ + +#include <QtGui> +#include <QTextEdit> +#include <QRegExp> +#include <QSettings> + +#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; +} + +void FindReplaceForm::hideReplaceWidgets() { + ui->replaceLabel->setVisible(false); + ui->textToReplace->setVisible(false); + ui->replaceButton->setVisible(false); + ui->replaceAllButton->setVisible(false); +} + +void FindReplaceForm::setTextEdit(QPlainTextEdit *textEdit_) { + textEdit = textEdit_; + connect(textEdit, SIGNAL(copyAvailable(bool)), ui->replaceButton, SLOT(setEnabled(bool))); + connect(textEdit, SIGNAL(copyAvailable(bool)), ui->replaceAllButton, SLOT(setEnabled(bool))); +} + +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("<span style=\" font-weight:600; color:#ff0000;\">" + + error + + "</span>"); + } +} + +void FindReplaceForm::showMessage(const QString &message) { + if (message == "") { + ui->errorLabel->setText(""); + } else { + ui->errorLabel->setText("<span style=\" font-weight:600; color:green;\">" + + message + + "</span>"); + } +} + +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 + textCursor.setPosition(0); + textEdit->setTextCursor(textCursor); + } +} + +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(); +} |