Theme Editor: Added an edit menu with a find/replace function (copied from an LGPL...
[kugel-rb.git] / utils / themeeditor / findreplace / findreplaceform.cpp
blobcd2700f0336fae8752e9b4733e2506c7d4a00bba
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * This file has been copied from Lorenzo Bettini, with minor modifications
11 * made available under the LGPL version 3, as the original file was licensed
13 ****************************************************************************
15 * Copyright (C) 2009 Lorenzo Bettini <http://www.lorenzobettini.it>
16 * See COPYING file that comes with this distribution
19 #include <QtGui>
20 #include <QTextEdit>
21 #include <QRegExp>
22 #include <QSettings>
24 #include "findreplaceform.h"
25 #include "ui_findreplaceform.h"
27 #define TEXT_TO_FIND "textToFind"
28 #define TEXT_TO_REPLACE "textToReplace"
29 #define DOWN_RADIO "downRadio"
30 #define UP_RADIO "upRadio"
31 #define CASE_CHECK "caseCheck"
32 #define WHOLE_CHECK "wholeCheck"
33 #define REGEXP_CHECK "regexpCheck"
35 FindReplaceForm::FindReplaceForm(QWidget *parent) :
36 QWidget(parent),
37 ui(new Ui::FindReplaceForm), textEdit(0)
39 ui->setupUi(this);
41 ui->errorLabel->setText("");
43 connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(textToFindChanged()));
44 connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(validateRegExp(QString)));
46 connect(ui->regexCheckBox, SIGNAL(toggled(bool)), this, SLOT(regexpSelected(bool)));
48 connect(ui->findButton, SIGNAL(clicked()), this, SLOT(find()));
49 connect(ui->closeButton, SIGNAL(clicked()), parent, SLOT(close()));
51 connect(ui->replaceButton, SIGNAL(clicked()), this, SLOT(replace()));
52 connect(ui->replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAll()));
55 FindReplaceForm::~FindReplaceForm()
57 delete ui;
60 void FindReplaceForm::hideReplaceWidgets() {
61 ui->replaceLabel->setVisible(false);
62 ui->textToReplace->setVisible(false);
63 ui->replaceButton->setVisible(false);
64 ui->replaceAllButton->setVisible(false);
67 void FindReplaceForm::setTextEdit(QPlainTextEdit *textEdit_) {
68 textEdit = textEdit_;
69 connect(textEdit, SIGNAL(copyAvailable(bool)), ui->replaceButton, SLOT(setEnabled(bool)));
70 connect(textEdit, SIGNAL(copyAvailable(bool)), ui->replaceAllButton, SLOT(setEnabled(bool)));
73 void FindReplaceForm::changeEvent(QEvent *e)
75 QWidget::changeEvent(e);
76 switch (e->type()) {
77 case QEvent::LanguageChange:
78 ui->retranslateUi(this);
79 break;
80 default:
81 break;
85 void FindReplaceForm::textToFindChanged() {
86 ui->findButton->setEnabled(ui->textToFind->text().size() > 0);
89 void FindReplaceForm::regexpSelected(bool sel) {
90 if (sel)
91 validateRegExp(ui->textToFind->text());
92 else
93 validateRegExp("");
96 void FindReplaceForm::validateRegExp(const QString &text) {
97 if (!ui->regexCheckBox->isChecked() || text.size() == 0) {
98 ui->errorLabel->setText("");
99 return; // nothing to validate
102 QRegExp reg(text,
103 (ui->caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive));
105 if (reg.isValid()) {
106 showError("");
107 } else {
108 showError(reg.errorString());
112 void FindReplaceForm::showError(const QString &error) {
113 if (error == "") {
114 ui->errorLabel->setText("");
115 } else {
116 ui->errorLabel->setText("<span style=\" font-weight:600; color:#ff0000;\">" +
117 error +
118 "</span>");
122 void FindReplaceForm::showMessage(const QString &message) {
123 if (message == "") {
124 ui->errorLabel->setText("");
125 } else {
126 ui->errorLabel->setText("<span style=\" font-weight:600; color:green;\">" +
127 message +
128 "</span>");
132 void FindReplaceForm::find() {
133 find(ui->downRadioButton->isChecked());
136 void FindReplaceForm::find(bool next) {
137 if (!textEdit)
138 return; // TODO: show some warning?
140 // backward search
141 bool back = !next;
143 const QString &toSearch = ui->textToFind->text();
145 bool result = false;
147 QTextDocument::FindFlags flags;
149 if (back)
150 flags |= QTextDocument::FindBackward;
151 if (ui->caseCheckBox->isChecked())
152 flags |= QTextDocument::FindCaseSensitively;
153 if (ui->wholeCheckBox->isChecked())
154 flags |= QTextDocument::FindWholeWords;
156 if (ui->regexCheckBox->isChecked()) {
157 QRegExp reg(toSearch,
158 (ui->caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive));
160 qDebug() << "searching for regexp: " << reg.pattern();
162 textCursor = textEdit->document()->find(reg, textCursor, flags);
163 textEdit->setTextCursor(textCursor);
164 result = (!textCursor.isNull());
165 } else {
166 qDebug() << "searching for: " << toSearch;
168 result = textEdit->find(toSearch, flags);
171 if (result) {
172 showError("");
173 } else {
174 showError(tr("no match found"));
175 // move to the beginning of the document for the next find
176 textCursor.setPosition(0);
177 textEdit->setTextCursor(textCursor);
181 void FindReplaceForm::replace() {
182 if (!textEdit->textCursor().hasSelection()) {
183 find();
184 } else {
185 textEdit->textCursor().insertText(ui->textToReplace->text());
186 find();
190 void FindReplaceForm::replaceAll() {
191 int i=0;
192 while (textEdit->textCursor().hasSelection()){
193 textEdit->textCursor().insertText(ui->textToReplace->text());
194 find();
195 i++;
197 showMessage(tr("Replaced %1 occurrence(s)").arg(i));
200 void FindReplaceForm::writeSettings(QSettings &settings, const QString &prefix) {
201 settings.beginGroup(prefix);
202 settings.setValue(TEXT_TO_FIND, ui->textToFind->text());
203 settings.setValue(TEXT_TO_REPLACE, ui->textToReplace->text());
204 settings.setValue(DOWN_RADIO, ui->downRadioButton->isChecked());
205 settings.setValue(UP_RADIO, ui->upRadioButton->isChecked());
206 settings.setValue(CASE_CHECK, ui->caseCheckBox->isChecked());
207 settings.setValue(WHOLE_CHECK, ui->wholeCheckBox->isChecked());
208 settings.setValue(REGEXP_CHECK, ui->regexCheckBox->isChecked());
209 settings.endGroup();
212 void FindReplaceForm::readSettings(QSettings &settings, const QString &prefix) {
213 settings.beginGroup(prefix);
214 ui->textToFind->setText(settings.value(TEXT_TO_FIND, "").toString());
215 ui->textToReplace->setText(settings.value(TEXT_TO_REPLACE, "").toString());
216 ui->downRadioButton->setChecked(settings.value(DOWN_RADIO, true).toBool());
217 ui->upRadioButton->setChecked(settings.value(UP_RADIO, false).toBool());
218 ui->caseCheckBox->setChecked(settings.value(CASE_CHECK, false).toBool());
219 ui->wholeCheckBox->setChecked(settings.value(WHOLE_CHECK, false).toBool());
220 ui->regexCheckBox->setChecked(settings.value(REGEXP_CHECK, false).toBool());
221 settings.endGroup();