Commit 532adacc authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

It will allow to autotest code

parent beda2a3d
Pipeline #143939 passed with stages
in 2 minutes and 40 seconds
......@@ -31,19 +31,34 @@ QTextDocument::FindFlags FindUtils::convertTextEditFindFlags(TextEditFindBarBase
return flags;
}
int FindUtils::replaceAll(QTextDocument *document, const TextFindWidget *findWidget, const QString &replaceWidget)
int FindUtils::replaceAll(QTextDocument *document, const QString &str, const QString &replaceWidget, QTextDocument::FindFlags searchOptions)
{
QTextCursor c(document);
c.beginEditBlock();
int count = 0;
// Ignoring FindBackward when replacing all
const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(findWidget->searchOptions()) & ~QTextDocument::FindBackward;
const QTextDocument::FindFlags flags = searchOptions & ~QTextDocument::FindBackward;
while (!c.isNull()) {
if (findWidget->isRegularExpression()) {
c = document->find(findWidget->searchRegularExpression(), c, flags);
} else {
c = document->find(findWidget->searchText(), c, flags);
c = document->find(str, c, flags);
if (!c.isNull()) {
// find() selects found text, and insertText() replaces selection
c.insertText(replaceWidget);
count++;
}
}
c.endEditBlock();
return count;
}
int FindUtils::replaceAll(QTextDocument *document, const QRegularExpression &regExp, const QString &replaceWidget, QTextDocument::FindFlags searchOptions)
{
QTextCursor c(document);
c.beginEditBlock();
int count = 0;
// Ignoring FindBackward when replacing all
const QTextDocument::FindFlags flags = searchOptions & ~QTextDocument::FindBackward;
while (!c.isNull()) {
c = document->find(regExp, c, flags);
if (!c.isNull()) {
// find() selects found text, and insertText() replaces selection
c.insertText(replaceWidget);
......
......@@ -27,8 +27,9 @@ namespace FindUtils
*
* @return number of replacements done
*/
Q_REQUIRED_RESULT int replaceAll(QTextDocument *document, const TextFindWidget *findWidget, const QString &replaceWidget);
Q_REQUIRED_RESULT int replaceAll(QTextDocument *document, const QString &str, const QString &replaceWidget, QTextDocument::FindFlags searchOptions);
Q_REQUIRED_RESULT int
replaceAll(QTextDocument *document, const QRegularExpression &regExp, const QString &replaceWidget, QTextDocument::FindFlags searchOptions);
Q_REQUIRED_RESULT QString normalize(QStringView str);
Q_REQUIRED_RESULT QChar normalize(QChar c);
Q_REQUIRED_RESULT bool find(QPlainTextEdit *view, const QString &searchText, QTextDocument::FindFlags searchOptions);
......
......@@ -111,6 +111,13 @@ void PlainTextEditFindBar::slotReplaceText()
void PlainTextEditFindBar::slotReplaceAllText()
{
const int count = FindUtils::replaceAll(d->mView->document(), mFindWidget, mReplaceWidget->replaceLineEdit()->text());
int count = 0;
const QString replaceStr{mReplaceWidget->replaceLineEdit()->text()};
const QTextDocument::FindFlags searchOptions{FindUtils::convertTextEditFindFlags(mFindWidget->searchOptions())};
if (mFindWidget->isRegularExpression()) {
count = FindUtils::replaceAll(d->mView->document(), mFindWidget->searchRegularExpression(), replaceStr, searchOptions);
} else {
count = FindUtils::replaceAll(d->mView->document(), mFindWidget->searchText(), replaceStr, searchOptions);
}
Q_EMIT displayMessageIndicator(i18np("%1 replacement made", "%1 replacements made", count));
}
......@@ -101,6 +101,13 @@ void RichTextEditFindBar::slotReplaceText()
void RichTextEditFindBar::slotReplaceAllText()
{
const int count = FindUtils::replaceAll(d->mView->document(), mFindWidget, mReplaceWidget->replaceLineEdit()->text());
int count = 0;
const QString replaceStr{mReplaceWidget->replaceLineEdit()->text()};
const QTextDocument::FindFlags searchOptions{FindUtils::convertTextEditFindFlags(mFindWidget->searchOptions())};
if (mFindWidget->isRegularExpression()) {
count = FindUtils::replaceAll(d->mView->document(), mFindWidget->searchRegularExpression(), replaceStr, searchOptions);
} else {
count = FindUtils::replaceAll(d->mView->document(), mFindWidget->searchText(), replaceStr, searchOptions);
}
Q_EMIT displayMessageIndicator(i18np("%1 replacement made", "%1 replacements made", count));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment