diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ texteditor/plaintexteditor/plaintexteditor.cpp texteditor/plaintexteditor/plaintexteditorwidget.cpp texteditor/plaintexteditor/plaintextsyntaxspellcheckinghighlighter.cpp + texteditor/commonwidget/findutils.cpp texteditor/commonwidget/textfindreplacewidget.cpp texteditor/commonwidget/texteditfindbarbase.cpp texteditor/commonwidget/textgotolinewidget.cpp diff --git a/src/texteditor/commonwidget/findutils.h b/src/texteditor/commonwidget/findutils.h new file mode 100644 --- /dev/null +++ b/src/texteditor/commonwidget/findutils.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2020 Igor Poboiko + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef FINDUTILS_H +#define FINDUTILS_H + +#include + +class QTextDocument; + +namespace KPIMTextEdit { + +class TextFindWidget; +class TextReplaceWidget; + +namespace FindUtils { + /** + * Replaces all occurences of a search string provided by @p findWidget + * by a string provided by @p replaceWidget in a @p document + * + * @return number of replacements done + */ + Q_REQUIRED_RESULT int replaceAll(QTextDocument *document, const TextFindWidget *findWidget, const TextReplaceWidget *replaceWidget); +} + +}; // namespace KPIMTextEdit + + +#endif // FINDUTILS_H diff --git a/src/texteditor/commonwidget/findutils.cpp b/src/texteditor/commonwidget/findutils.cpp new file mode 100644 --- /dev/null +++ b/src/texteditor/commonwidget/findutils.cpp @@ -0,0 +1,51 @@ +/* + Copyright (C) 2020 Igor Poboiko + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "findutils.h" +#include "textfindreplacewidget.h" + +#include +#include +#include +#include + +using namespace KPIMTextEdit; + +int FindUtils::replaceAll(QTextDocument *document, const TextFindWidget *findWidget, const TextReplaceWidget *replaceWidget) +{ + QTextCursor c(document); + c.beginEditBlock(); + int count = 0; + while (!c.isNull()) { + // Ignoring FindBackward when replacing all + QTextDocument::FindFlags flags = findWidget->searchOptions() & ~QTextDocument::FindBackward; + if (findWidget->isRegularExpression()) { + c = document->find(findWidget->searchRegExp(), c, flags); + } else { + c = document->find(findWidget->searchText(), c, flags); + } + if (!c.isNull()) { + // find() selects found text, and insertText() replaces selection + c.insertText(replaceWidget->replaceLineEdit()->text()); + count++; + } + } + c.endEditBlock(); + return count; +} diff --git a/src/texteditor/plaintexteditor/plaintexteditfindbar.cpp b/src/texteditor/plaintexteditor/plaintexteditfindbar.cpp --- a/src/texteditor/plaintexteditor/plaintexteditfindbar.cpp +++ b/src/texteditor/plaintexteditor/plaintexteditfindbar.cpp @@ -19,9 +19,11 @@ #include "plaintexteditfindbar.h" #include "texteditor/commonwidget/textfindreplacewidget.h" +#include "texteditor/commonwidget/findutils.h" #include #include +#include #include #include @@ -122,12 +124,6 @@ void PlainTextEditFindBar::slotReplaceAllText() { - QString newText; - if (mFindWidget->isRegularExpression()) { - newText = d->mView->toPlainText().replace(mFindWidget->searchRegExp(), mReplaceWidget->replaceLineEdit()->text()); - } else { - newText = d->mView->toPlainText().replace(mFindWidget->searchText(), mReplaceWidget->replaceLineEdit()->text()); - } - d->mView->selectAll(); - d->mView->insertPlainText(newText); + const int count = FindUtils::replaceAll(d->mView->document(), mFindWidget, mReplaceWidget); + Q_EMIT displayMessageIndicator(i18np("%1 replacement made", "%1 replacements made", count)); } diff --git a/src/texteditor/richtexteditor/richtexteditfindbar.cpp b/src/texteditor/richtexteditor/richtexteditfindbar.cpp --- a/src/texteditor/richtexteditor/richtexteditfindbar.cpp +++ b/src/texteditor/richtexteditor/richtexteditfindbar.cpp @@ -19,9 +19,11 @@ #include "richtexteditfindbar.h" #include "texteditor/commonwidget/textfindreplacewidget.h" +#include "texteditor/commonwidget/findutils.h" #include #include +#include #include #include @@ -122,12 +124,6 @@ void RichTextEditFindBar::slotReplaceAllText() { - QString newText; - if (mFindWidget->isRegularExpression()) { - newText = d->mView->toPlainText().replace(mFindWidget->searchRegExp(), mReplaceWidget->replaceLineEdit()->text()); - } else { - newText = d->mView->toPlainText().replace(mFindWidget->searchText(), mReplaceWidget->replaceLineEdit()->text()); - } - d->mView->selectAll(); - d->mView->insertPlainText(newText); + const int count = FindUtils::replaceAll(d->mView->document(), mFindWidget, mReplaceWidget); + Q_EMIT displayMessageIndicator(i18np("%1 replacement made", "%1 replacements made", count)); }