1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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
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
) :
37 ui(new Ui::FindReplaceForm
), textEdit(0)
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()
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_
) {
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
);
77 case QEvent::LanguageChange
:
78 ui
->retranslateUi(this);
85 void FindReplaceForm::textToFindChanged() {
86 ui
->findButton
->setEnabled(ui
->textToFind
->text().size() > 0);
89 void FindReplaceForm::regexpSelected(bool sel
) {
91 validateRegExp(ui
->textToFind
->text());
96 void FindReplaceForm::validateRegExp(const QString
&text
) {
97 if (!ui
->regexCheckBox
->isChecked() || text
.size() == 0) {
98 ui
->errorLabel
->setText("");
99 return; // nothing to validate
103 (ui
->caseCheckBox
->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive
));
108 showError(reg
.errorString());
112 void FindReplaceForm::showError(const QString
&error
) {
114 ui
->errorLabel
->setText("");
116 ui
->errorLabel
->setText("<span style=\" font-weight:600; color:#ff0000;\">" +
122 void FindReplaceForm::showMessage(const QString
&message
) {
124 ui
->errorLabel
->setText("");
126 ui
->errorLabel
->setText("<span style=\" font-weight:600; color:green;\">" +
132 void FindReplaceForm::find() {
133 find(ui
->downRadioButton
->isChecked());
136 void FindReplaceForm::find(bool next
) {
138 return; // TODO: show some warning?
143 const QString
&toSearch
= ui
->textToFind
->text();
147 QTextDocument::FindFlags flags
;
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());
166 qDebug() << "searching for: " << toSearch
;
168 result
= textEdit
->find(toSearch
, flags
);
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()) {
185 textEdit
->textCursor().insertText(ui
->textToReplace
->text());
190 void FindReplaceForm::replaceAll() {
192 while (textEdit
->textCursor().hasSelection()){
193 textEdit
->textCursor().insertText(ui
->textToReplace
->text());
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());
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());