KTextAddons

converttext.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "converttext.h"
8#include <QChar>
9#include <QString>
10
11using namespace TextUtils;
12
13// code from kitinerary/src/lib/stringutil.cpp
14QString ConvertText::normalize(QStringView str)
15{
16 QString out;
17 out.reserve(str.size());
18 for (const auto c : str) {
19 // case folding
20 const auto n = c.toCaseFolded();
21
22 // if the character has a canonical decomposition use that and skip the
23 // combining diacritic markers following it
24 // see https://en.wikipedia.org/wiki/Unicode_equivalence
25 // see https://en.wikipedia.org/wiki/Combining_character
26 if (n.decompositionTag() == QChar::Canonical) {
27 out.push_back(n.decomposition().at(0));
28 }
29 // handle compatibility compositions such as ligatures
30 // see https://en.wikipedia.org/wiki/Unicode_compatibility_characters
31 else if (n.decompositionTag() == QChar::Compat && n.isLetter() && n.script() == QChar::Script_Latin) {
32 out.append(n.decomposition());
33 } else {
34 out.push_back(n);
35 }
36 }
37 return out;
38}
39
40void ConvertText::upperCase(QTextCursor &cursor)
41{
42 if (cursor.hasSelection()) {
43 const QString newText = cursor.selectedText().toUpper();
44 cursor.insertText(newText);
45 }
46}
47
48void ConvertText::lowerCase(QTextCursor &cursor)
49{
50 if (cursor.hasSelection()) {
51 const QString newText = cursor.selectedText().toLower();
52 cursor.insertText(newText);
53 }
54}
55
56void ConvertText::sentenceCase(QTextCursor &cursor)
57{
58 if (cursor.hasSelection()) {
59 QString newText = cursor.selectedText();
60 const int nbChar(newText.length());
61 for (int i = 0; i < nbChar; ++i) {
62 if (i == 0 && newText.at(0).isLetter()) {
63 newText.replace(0, 1, newText.at(0).toUpper());
64 } else if (newText.at(i) == QChar::ParagraphSeparator || newText.at(i) == QChar::LineSeparator) {
65 ++i;
66 if (i < nbChar) {
67 if (newText.at(i).isLetter()) {
68 newText.replace(i, 1, newText.at(i).toUpper());
69 }
70 }
71 }
72 }
73 cursor.insertText(newText);
74 }
75}
76
77void ConvertText::reverseCase(QTextCursor &cursor)
78{
79 if (cursor.hasSelection()) {
80 const QString newText = cursor.selectedText();
81 QString reverseCaseText;
82 const int nbChar(newText.length());
83 for (int i = 0; i < nbChar; ++i) {
84 QChar charVal = newText.at(i);
85 if (charVal.isLetter()) {
86 if (charVal.isLower()) {
87 charVal = charVal.toUpper();
88 } else {
89 charVal = charVal.toLower();
90 }
91 }
92 reverseCaseText += charVal;
93 }
94 cursor.insertText(reverseCaseText);
95 }
96}
ParagraphSeparator
bool isLetter(char32_t ucs4)
bool isLower(char32_t ucs4)
char32_t toLower(char32_t ucs4)
char32_t toUpper(char32_t ucs4)
QString & append(QChar ch)
const QChar at(qsizetype position) const const
qsizetype length() const const
void push_back(QChar ch)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
void reserve(qsizetype size)
QString toLower() const const
QString toUpper() const const
qsizetype size() const const
bool hasSelection() const const
void insertText(const QString &text)
QString selectedText() const const
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.