diff options
Diffstat (limited to 'utils/themeeditor/findreplace/findreplaceform.cpp')
-rw-r--r-- | utils/themeeditor/findreplace/findreplaceform.cpp | 222 |
1 files changed, 0 insertions, 222 deletions
diff --git a/utils/themeeditor/findreplace/findreplaceform.cpp b/utils/themeeditor/findreplace/findreplaceform.cpp deleted file mode 100644 index cd2700f033..0000000000 --- a/utils/themeeditor/findreplace/findreplaceform.cpp +++ /dev/null | |||
@@ -1,222 +0,0 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
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 | ||
12 | * | ||
13 | **************************************************************************** | ||
14 | * | ||
15 | * Copyright (C) 2009 Lorenzo Bettini <http://www.lorenzobettini.it> | ||
16 | * See COPYING file that comes with this distribution | ||
17 | */ | ||
18 | |||
19 | #include <QtGui> | ||
20 | #include <QTextEdit> | ||
21 | #include <QRegExp> | ||
22 | #include <QSettings> | ||
23 | |||
24 | #include "findreplaceform.h" | ||
25 | #include "ui_findreplaceform.h" | ||
26 | |||
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" | ||
34 | |||
35 | FindReplaceForm::FindReplaceForm(QWidget *parent) : | ||
36 | QWidget(parent), | ||
37 | ui(new Ui::FindReplaceForm), textEdit(0) | ||
38 | { | ||
39 | ui->setupUi(this); | ||
40 | |||
41 | ui->errorLabel->setText(""); | ||
42 | |||
43 | connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(textToFindChanged())); | ||
44 | connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(validateRegExp(QString))); | ||
45 | |||
46 | connect(ui->regexCheckBox, SIGNAL(toggled(bool)), this, SLOT(regexpSelected(bool))); | ||
47 | |||
48 | connect(ui->findButton, SIGNAL(clicked()), this, SLOT(find())); | ||
49 | connect(ui->closeButton, SIGNAL(clicked()), parent, SLOT(close())); | ||
50 | |||
51 | connect(ui->replaceButton, SIGNAL(clicked()), this, SLOT(replace())); | ||
52 | connect(ui->replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAll())); | ||
53 | } | ||
54 | |||
55 | FindReplaceForm::~FindReplaceForm() | ||
56 | { | ||
57 | delete ui; | ||
58 | } | ||
59 | |||
60 | void FindReplaceForm::hideReplaceWidgets() { | ||
61 | ui->replaceLabel->setVisible(false); | ||
62 | ui->textToReplace->setVisible(false); | ||
63 | ui->replaceButton->setVisible(false); | ||
64 | ui->replaceAllButton->setVisible(false); | ||
65 | } | ||
66 | |||
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))); | ||
71 | } | ||
72 | |||
73 | void FindReplaceForm::changeEvent(QEvent *e) | ||
74 | { | ||
75 | QWidget::changeEvent(e); | ||
76 | switch (e->type()) { | ||
77 | case QEvent::LanguageChange: | ||
78 | ui->retranslateUi(this); | ||
79 | break; | ||
80 | default: | ||
81 | break; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | void FindReplaceForm::textToFindChanged() { | ||
86 | ui->findButton->setEnabled(ui->textToFind->text().size() > 0); | ||
87 | } | ||
88 | |||
89 | void FindReplaceForm::regexpSelected(bool sel) { | ||
90 | if (sel) | ||
91 | validateRegExp(ui->textToFind->text()); | ||
92 | else | ||
93 | validateRegExp(""); | ||
94 | } | ||
95 | |||
96 | void FindReplaceForm::validateRegExp(const QString &text) { | ||
97 | if (!ui->regexCheckBox->isChecked() || text.size() == 0) { | ||
98 | ui->errorLabel->setText(""); | ||
99 | return; // nothing to validate | ||
100 | } | ||
101 | |||
102 | QRegExp reg(text, | ||
103 | (ui->caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive)); | ||
104 | |||
105 | if (reg.isValid()) { | ||
106 | showError(""); | ||
107 | } else { | ||
108 | showError(reg.errorString()); | ||
109 | } | ||
110 | } | ||
111 | |||
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>"); | ||
119 | } | ||
120 | } | ||
121 | |||
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>"); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | void FindReplaceForm::find() { | ||
133 | find(ui->downRadioButton->isChecked()); | ||
134 | } | ||
135 | |||
136 | void FindReplaceForm::find(bool next) { | ||
137 | if (!textEdit) | ||
138 | return; // TODO: show some warning? | ||
139 | |||
140 | // backward search | ||
141 | bool back = !next; | ||
142 | |||
143 | const QString &toSearch = ui->textToFind->text(); | ||
144 | |||
145 | bool result = false; | ||
146 | |||
147 | QTextDocument::FindFlags flags; | ||
148 | |||
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; | ||
155 | |||
156 | if (ui->regexCheckBox->isChecked()) { | ||
157 | QRegExp reg(toSearch, | ||
158 | (ui->caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive)); | ||
159 | |||
160 | qDebug() << "searching for regexp: " << reg.pattern(); | ||
161 | |||
162 | textCursor = textEdit->document()->find(reg, textCursor, flags); | ||
163 | textEdit->setTextCursor(textCursor); | ||
164 | result = (!textCursor.isNull()); | ||
165 | } else { | ||
166 | qDebug() << "searching for: " << toSearch; | ||
167 | |||
168 | result = textEdit->find(toSearch, flags); | ||
169 | } | ||
170 | |||
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); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | void FindReplaceForm::replace() { | ||
182 | if (!textEdit->textCursor().hasSelection()) { | ||
183 | find(); | ||
184 | } else { | ||
185 | textEdit->textCursor().insertText(ui->textToReplace->text()); | ||
186 | find(); | ||
187 | } | ||
188 | } | ||
189 | |||
190 | void FindReplaceForm::replaceAll() { | ||
191 | int i=0; | ||
192 | while (textEdit->textCursor().hasSelection()){ | ||
193 | textEdit->textCursor().insertText(ui->textToReplace->text()); | ||
194 | find(); | ||
195 | i++; | ||
196 | } | ||
197 | showMessage(tr("Replaced %1 occurrence(s)").arg(i)); | ||
198 | } | ||
199 | |||
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(); | ||
210 | } | ||
211 | |||
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(); | ||
222 | } | ||