File indexing completed on 2026-07-19 08:32:55

0001 /*
0002   SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "autocorrectiontextedit.h"
0008 #include <TextAutoCorrectionCore/AutoCorrection>
0009 
0010 #include <QKeyEvent>
0011 
0012 using namespace TextAutoCorrectionWidgets;
0013 
0014 class TextAutoCorrectionWidgets::AutoCorrectionTextEditPrivate
0015 {
0016 public:
0017     AutoCorrectionTextEditPrivate()
0018         : mAutoCorrection(new TextAutoCorrectionCore::AutoCorrection())
0019     {
0020     }
0021 
0022     ~AutoCorrectionTextEditPrivate()
0023     {
0024         if (mNeedToDelete) {
0025             delete mAutoCorrection;
0026         }
0027     }
0028 
0029     TextAutoCorrectionCore::AutoCorrection *mAutoCorrection = nullptr;
0030     bool mNeedToDelete = true;
0031 };
0032 
0033 AutoCorrectionTextEdit::AutoCorrectionTextEdit(QWidget *parent)
0034     : QTextEdit(parent)
0035     , d(new TextAutoCorrectionWidgets::AutoCorrectionTextEditPrivate)
0036 {
0037 }
0038 
0039 AutoCorrectionTextEdit::~AutoCorrectionTextEdit() = default;
0040 
0041 void AutoCorrectionTextEdit::setAutocorrection(TextAutoCorrectionCore::AutoCorrection *autocorrect)
0042 {
0043     d->mNeedToDelete = false;
0044     delete d->mAutoCorrection;
0045     d->mAutoCorrection = autocorrect;
0046 }
0047 
0048 TextAutoCorrectionCore::AutoCorrection *AutoCorrectionTextEdit::autocorrection() const
0049 {
0050     return d->mAutoCorrection;
0051 }
0052 
0053 void AutoCorrectionTextEdit::setAutocorrectionLanguage(const QString &language)
0054 {
0055     TextAutoCorrectionCore::AutoCorrectionSettings *settings = d->mAutoCorrection->autoCorrectionSettings();
0056     settings->setLanguage(language);
0057     d->mAutoCorrection->setAutoCorrectionSettings(settings);
0058 }
0059 
0060 static bool isSpecial(const QTextCharFormat &charFormat)
0061 {
0062     return charFormat.isFrameFormat() || charFormat.isImageFormat() || charFormat.isListFormat() || charFormat.isTableFormat()
0063         || charFormat.isTableCellFormat();
0064 }
0065 
0066 void AutoCorrectionTextEdit::keyPressEvent(QKeyEvent *e)
0067 {
0068     if (d->mAutoCorrection && d->mAutoCorrection->autoCorrectionSettings()->isEnabledAutoCorrection()) {
0069         if ((e->key() == Qt::Key_Space) || (e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) {
0070             if (!textCursor().hasSelection()) {
0071                 const QTextCharFormat initialTextFormat = textCursor().charFormat();
0072                 const bool richText = acceptRichText();
0073                 int position = textCursor().position();
0074                 const bool addSpace = d->mAutoCorrection->autocorrect(richText, *document(), position);
0075                 QTextCursor cur = textCursor();
0076                 cur.setPosition(position);
0077                 const bool spacePressed = (e->key() == Qt::Key_Space);
0078                 const QChar insertChar = spacePressed ? QLatin1Char(' ') : QLatin1Char('\n');
0079                 if (richText && !isSpecial(initialTextFormat)) {
0080                     if (addSpace || !spacePressed) {
0081                         cur.insertText(insertChar, initialTextFormat);
0082                     }
0083                 } else {
0084                     if (addSpace || !spacePressed) {
0085                         cur.insertText(insertChar);
0086                     }
0087                 }
0088                 setTextCursor(cur);
0089                 return;
0090             }
0091         }
0092     }
0093     QTextEdit::keyPressEvent(e);
0094 }
0095 
0096 #include "moc_autocorrectiontextedit.cpp"