KTextAddons

autocorrectiontextedit.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "autocorrectiontextedit.h"
8#include <TextAutoCorrectionCore/AutoCorrection>
9
10#include <QKeyEvent>
11
12using namespace TextAutoCorrectionWidgets;
13
14class TextAutoCorrectionWidgets::AutoCorrectionTextEditPrivate
15{
16public:
17 AutoCorrectionTextEditPrivate()
18 : mAutoCorrection(new TextAutoCorrectionCore::AutoCorrection())
19 {
20 }
21
22 ~AutoCorrectionTextEditPrivate()
23 {
24 if (mNeedToDelete) {
25 delete mAutoCorrection;
26 }
27 }
28
29 TextAutoCorrectionCore::AutoCorrection *mAutoCorrection = nullptr;
30 bool mNeedToDelete = true;
31};
32
33AutoCorrectionTextEdit::AutoCorrectionTextEdit(QWidget *parent)
34 : QTextEdit(parent)
35 , d(new TextAutoCorrectionWidgets::AutoCorrectionTextEditPrivate)
36{
37}
38
39AutoCorrectionTextEdit::~AutoCorrectionTextEdit() = default;
40
41void AutoCorrectionTextEdit::setAutocorrection(TextAutoCorrectionCore::AutoCorrection *autocorrect)
42{
43 d->mNeedToDelete = false;
44 delete d->mAutoCorrection;
45 d->mAutoCorrection = autocorrect;
46}
47
48TextAutoCorrectionCore::AutoCorrection *AutoCorrectionTextEdit::autocorrection() const
49{
50 return d->mAutoCorrection;
51}
52
53void AutoCorrectionTextEdit::setAutocorrectionLanguage(const QString &language)
54{
55 TextAutoCorrectionCore::AutoCorrectionSettings *settings = d->mAutoCorrection->autoCorrectionSettings();
56 settings->setLanguage(language);
57 d->mAutoCorrection->setAutoCorrectionSettings(settings);
58}
59
60static bool isSpecial(const QTextCharFormat &charFormat)
61{
62 return charFormat.isFrameFormat() || charFormat.isImageFormat() || charFormat.isListFormat() || charFormat.isTableFormat()
63 || charFormat.isTableCellFormat();
64}
65
66void AutoCorrectionTextEdit::keyPressEvent(QKeyEvent *e)
67{
68 if (d->mAutoCorrection && d->mAutoCorrection->autoCorrectionSettings()->isEnabledAutoCorrection()) {
69 if ((e->key() == Qt::Key_Space) || (e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) {
70 if (!textCursor().hasSelection()) {
71 const QTextCharFormat initialTextFormat = textCursor().charFormat();
72 const bool richText = acceptRichText();
73 int position = textCursor().position();
74 const bool addSpace = d->mAutoCorrection->autocorrect(richText, *document(), position);
75 QTextCursor cur = textCursor();
76 cur.setPosition(position);
77 const bool spacePressed = (e->key() == Qt::Key_Space);
78 const QChar insertChar = spacePressed ? QLatin1Char(' ') : QLatin1Char('\n');
79 if (richText && !isSpecial(initialTextFormat)) {
80 if (addSpace || !spacePressed) {
81 cur.insertText(insertChar, initialTextFormat);
82 }
83 } else {
84 if (addSpace || !spacePressed) {
85 cur.insertText(insertChar);
86 }
87 }
88 setTextCursor(cur);
89 return;
90 }
91 }
92 }
94}
95
96#include "moc_autocorrectiontextedit.cpp"
int key() const const
Key_Space
QTextCharFormat charFormat() const const
void insertText(const QString &text)
int position() const const
void setPosition(int pos, MoveMode m)
virtual void keyPressEvent(QKeyEvent *e) override
void setTextCursor(const QTextCursor &cursor)
QTextCursor textCursor() const const
bool isFrameFormat() const const
bool isImageFormat() const const
bool isListFormat() const const
bool isTableCellFormat() const const
bool isTableFormat() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.