KTextAddons

richtexteditfindbar.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "richtexteditfindbar.h"
8#include "widgets/findutils.h"
9#include "widgets/textfindreplacewidget.h"
10
11#include <KLocalizedString>
12#include <TextUtils/ConvertText>
13
14#include <QLineEdit>
15#include <QRegularExpression>
16#include <QTextEdit>
17
18using namespace TextCustomEditor;
19class Q_DECL_HIDDEN TextCustomEditor::RichTextEditFindBarPrivate
20{
21public:
22 RichTextEditFindBarPrivate(QTextEdit *view)
23 : mView(view)
24 {
25 }
26
27 QTextEdit *const mView;
28};
29
30RichTextEditFindBar::RichTextEditFindBar(QTextEdit *view, QWidget *parent)
31 : TextEditFindBarBase(parent)
32 , d(new TextCustomEditor::RichTextEditFindBarPrivate(view))
33{
34}
35
36RichTextEditFindBar::~RichTextEditFindBar() = default;
37
38void RichTextEditFindBar::slotSearchText(bool backward, bool isAutoSearch)
39{
40 d->mView->moveCursor(QTextCursor::Start);
41 searchText(backward, isAutoSearch);
42}
43
44bool RichTextEditFindBar::viewIsReadOnly() const
45{
46 return d->mView->isReadOnly();
47}
48
49bool RichTextEditFindBar::documentIsEmpty() const
50{
51 return d->mView->document()->isEmpty();
52}
53
54bool RichTextEditFindBar::searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions)
55{
56 bool found = false;
57 if (searchOptions & TextEditFindBarBase::FindRespectDiacritics) {
58 found = d->mView->find(text, FindUtils::convertTextEditFindFlags(searchOptions));
59 } else {
60 found = FindUtils::find(d->mView, text, FindUtils::convertTextEditFindFlags(searchOptions));
61 }
62 mFindWidget->setFoundMatch(found);
63 return found;
64}
65
66bool RichTextEditFindBar::searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions)
67{
68 const bool found = d->mView->find(regExp, FindUtils::convertTextEditFindFlags(searchOptions));
69 mFindWidget->setFoundMatch(found);
70 return found;
71}
72
73void RichTextEditFindBar::autoSearchMoveCursor()
74{
75 QTextCursor cursor = d->mView->textCursor();
76 cursor.setPosition(cursor.selectionStart());
77 d->mView->setTextCursor(cursor);
78}
79
80void RichTextEditFindBar::slotReplaceText()
81{
82 const TextEditFindBarBase::FindFlags searchOptions = mFindWidget->searchOptions();
83 if (d->mView->textCursor().hasSelection()) {
84 if (mFindWidget->isRegularExpression()) {
85 if (d->mView->textCursor().selectedText().contains(mFindWidget->searchRegularExpression())) {
86 d->mView->textCursor().insertText(mReplaceWidget->replaceLineEdit()->text());
87 // search next after replace text.
88 searchText(false, false);
89 }
90 } else {
91 if (searchOptions & TextEditFindBarBase::FindRespectDiacritics) {
92 if (TextUtils::ConvertText::normalize(d->mView->textCursor().selectedText()) == TextUtils::ConvertText::normalize(mFindWidget->searchText())) {
93 d->mView->textCursor().insertText(mReplaceWidget->replaceLineEdit()->text());
94 // search next after replace text.
95 searchText(false, false);
96 } else {
97 if (d->mView->textCursor().selectedText() == mFindWidget->searchText()) {
98 d->mView->textCursor().insertText(mReplaceWidget->replaceLineEdit()->text());
99 // search next after replace text.
100 searchText(false, false);
101 }
102 }
103 }
104 }
105 } else {
106 searchText(false, false);
107 }
108}
109
110void RichTextEditFindBar::slotReplaceAllText()
111{
112 int count = 0;
113 const QString replaceStr{mReplaceWidget->replaceLineEdit()->text()};
114 const TextEditFindBarBase::FindFlags searchOptions{mFindWidget->searchOptions()};
115 if (mFindWidget->isRegularExpression()) {
116 count = FindUtils::replaceAll(d->mView->document(), mFindWidget->searchRegularExpression(), replaceStr, searchOptions);
117 } else {
118 count = FindUtils::replaceAll(d->mView, mFindWidget->searchText(), replaceStr, searchOptions);
119 }
120 Q_EMIT displayMessageIndicator(i18np("%1 replacement made", "%1 replacements made", count));
121}
122
123#include "moc_richtexteditfindbar.cpp"
The TextEditFindBarBase class.
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.