KTextAddons

findutils.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Igor Poboiko <igor.poboiko@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "findutils.h"
8#include <TextUtils/ConvertText>
9
10#include <QDebug>
11#include <QPlainTextEdit>
12#include <QRegularExpression>
13#include <QTextCursor>
14#include <QTextEdit>
15
16using namespace TextCustomEditor;
17QTextDocument::FindFlags FindUtils::convertTextEditFindFlags(TextEditFindBarBase::FindFlags textEditFlags)
18{
20 if (textEditFlags & TextEditFindBarBase::FindBackward) {
22 }
23 if (textEditFlags & TextEditFindBarBase::FindCaseSensitively) {
25 }
26 if (textEditFlags & TextEditFindBarBase::FindWholeWords) {
28 }
29 return flags;
30}
31
32int FindUtils::replaceAll(QTextEdit *view, const QString &str, const QString &replaceStr, TextEditFindBarBase::FindFlags searchOptions)
33{
34 auto document = view->document();
35 int count = 0;
36 // Ignoring FindBackward when replacing all
37 const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(searchOptions) & ~QTextDocument::FindBackward;
38 if (searchOptions & TextEditFindBarBase::FindFlag::FindRespectDiacritics) {
39 view->textCursor().beginEditBlock();
40 QTextCursor c(document);
41 while (!c.isNull()) {
42 c = document->find(str, c, flags);
43 if (!c.isNull()) {
44 c.insertText(replaceStr);
45 count++;
46 } else {
47 break;
48 }
49 }
50 view->textCursor().endEditBlock();
51 } else {
52 const QString toPlainTextWithoutRespectDiacritics{TextUtils::ConvertText::normalize(view->toPlainText())};
53 const QString searchStrWithoutRespectDiacritics{TextUtils::ConvertText::normalize(str)};
54
55 QTextDocument documentWithoutRespectDiacritics(toPlainTextWithoutRespectDiacritics);
56 QTextCursor documentWithoutRespectDiacriticsTextCursor(&documentWithoutRespectDiacritics);
57 documentWithoutRespectDiacriticsTextCursor.setPosition(0);
58
59 view->textCursor().beginEditBlock();
60 QTextCursor c(document);
61
62 while (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
63 documentWithoutRespectDiacriticsTextCursor =
64 documentWithoutRespectDiacritics.find(searchStrWithoutRespectDiacritics, documentWithoutRespectDiacriticsTextCursor, flags);
65 if (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
66 c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionStart());
67 c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionEnd(), QTextCursor::KeepAnchor);
68 c.insertText(replaceStr);
69 documentWithoutRespectDiacriticsTextCursor.insertText(replaceStr);
70 count++;
71 } else {
72 break;
73 }
74 }
75 view->textCursor().endEditBlock();
76 }
77 return count;
78}
79
80int FindUtils::replaceAll(QPlainTextEdit *view, const QString &str, const QString &replaceStr, TextEditFindBarBase::FindFlags searchOptions)
81{
82 auto document = view->document();
83 int count = 0;
84 // Ignoring FindBackward when replacing all
85 const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(searchOptions) & ~QTextDocument::FindBackward;
86 if (searchOptions & TextEditFindBarBase::FindFlag::FindRespectDiacritics) {
87 view->textCursor().beginEditBlock();
88 QTextCursor c(document);
89 while (!c.isNull()) {
90 c = document->find(str, c, flags);
91 if (!c.isNull()) {
92 c.insertText(replaceStr);
93 count++;
94 } else {
95 break;
96 }
97 }
98 view->textCursor().endEditBlock();
99 } else {
100 const QString toPlainTextWithoutRespectDiacritics{TextUtils::ConvertText::normalize(view->toPlainText())};
101 const QString searchStrWithoutRespectDiacritics{TextUtils::ConvertText::normalize(str)};
102
103 QTextDocument documentWithoutRespectDiacritics(toPlainTextWithoutRespectDiacritics);
104 QTextCursor documentWithoutRespectDiacriticsTextCursor(&documentWithoutRespectDiacritics);
105 documentWithoutRespectDiacriticsTextCursor.setPosition(0);
106
107 view->textCursor().beginEditBlock();
108 QTextCursor c(document);
109
110 while (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
111 documentWithoutRespectDiacriticsTextCursor =
112 documentWithoutRespectDiacritics.find(searchStrWithoutRespectDiacritics, documentWithoutRespectDiacriticsTextCursor, flags);
113 if (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
114 c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionStart());
115 c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionEnd(), QTextCursor::KeepAnchor);
116 c.insertText(replaceStr);
117 documentWithoutRespectDiacriticsTextCursor.insertText(replaceStr);
118 count++;
119 } else {
120 break;
121 }
122 }
123 view->textCursor().endEditBlock();
124 }
125 return count;
126}
127
128int FindUtils::replaceAll(QTextDocument *document, const QRegularExpression &regExp, const QString &replaceWidget, TextEditFindBarBase::FindFlags searchOptions)
129{
130 QTextCursor c(document);
131 c.beginEditBlock();
132 int count = 0;
133 // Ignoring FindBackward when replacing all
134 const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(searchOptions) & ~QTextDocument::FindBackward;
135 while (!c.isNull()) {
136 c = document->find(regExp, c, flags);
137 if (!c.isNull()) {
138 // find() selects found text, and insertText() replaces selection
139 c.insertText(replaceWidget);
140 count++;
141 } else {
142 break;
143 }
144 }
145 c.endEditBlock();
146 return count;
147}
148
149bool FindUtils::find(QPlainTextEdit *view, const QString &searchText, QTextDocument::FindFlags searchOptions)
150{
151 const QString text = TextUtils::ConvertText::normalize(view->document()->toPlainText());
152 QTextDocument doc(text);
153 QTextCursor c(&doc);
154 QTextCursor docCusor(view->textCursor());
155 c.setPosition(docCusor.position());
156 // qDebug() << " docCusor.position() " << docCusor.position();
157 c = doc.find(TextUtils::ConvertText::normalize(searchText), c, searchOptions);
158 if (!c.isNull()) {
159 // qDebug() << " c.selectionStart() " << c.selectionStart() << "c.selectionEnd() " << c.selectionEnd();
160 if (searchOptions & QTextDocument::FindBackward) {
161 docCusor.setPosition(c.selectionEnd());
162 docCusor.setPosition(c.selectionStart(), QTextCursor::KeepAnchor);
163 } else {
164 docCusor.setPosition(c.selectionStart());
165 docCusor.setPosition(c.selectionEnd(), QTextCursor::KeepAnchor);
166 }
167 view->setTextCursor(docCusor);
168 view->ensureCursorVisible();
169 return true;
170 }
171 return false;
172}
173
174bool FindUtils::find(QTextEdit *view, const QString &searchText, QTextDocument::FindFlags searchOptions)
175{
176 const QString text = TextUtils::ConvertText::normalize(view->document()->toPlainText());
177 QTextDocument doc(text);
178 QTextCursor c(&doc);
179 QTextCursor docCusor(view->textCursor());
180 c.setPosition(docCusor.position());
181 // qDebug() << " docCusor.position() " << docCusor.position();
182 c = doc.find(TextUtils::ConvertText::normalize(searchText), c, searchOptions);
183 if (!c.isNull()) {
184 // qDebug() << " c.selectionStart() " << c.selectionStart() << "c.selectionEnd() " << c.selectionEnd();
185 if (searchOptions & QTextDocument::FindBackward) {
186 docCusor.setPosition(c.selectionEnd());
187 docCusor.setPosition(c.selectionStart(), QTextCursor::KeepAnchor);
188 } else {
189 docCusor.setPosition(c.selectionStart());
190 docCusor.setPosition(c.selectionEnd(), QTextCursor::KeepAnchor);
191 }
192 view->setTextCursor(docCusor);
193 view->ensureCursorVisible();
194 return true;
195 }
196 return false;
197}
QTextDocument * document() const const
void ensureCursorVisible()
void setTextCursor(const QTextCursor &cursor)
QTextCursor textCursor() const const
QString toPlainText() const const
void beginEditBlock()
void endEditBlock()
QTextCursor find(const QRegularExpression &expr, const QTextCursor &cursor, FindFlags options) const const
QString toPlainText() const const
void ensureCursorVisible()
void setTextCursor(const QTextCursor &cursor)
QTextCursor textCursor() const const
QString toPlainText() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.