File indexing completed on 2024-12-15 04:51:47
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "richtexteditwithautocorrection.h" 0008 0009 #include <TextAutoCorrectionCore/AutoCorrection> 0010 0011 #include <QKeyEvent> 0012 0013 using namespace NoteShared; 0014 0015 class NoteShared::RichTextEditWithAutoCorrectionPrivate 0016 { 0017 public: 0018 RichTextEditWithAutoCorrectionPrivate() 0019 : mAutoCorrection(new TextAutoCorrectionCore::AutoCorrection()) 0020 { 0021 } 0022 0023 ~RichTextEditWithAutoCorrectionPrivate() 0024 { 0025 if (mNeedToDelete) { 0026 delete mAutoCorrection; 0027 } 0028 } 0029 TextAutoCorrectionCore::AutoCorrection *mAutoCorrection = nullptr; 0030 bool mNeedToDelete = true; 0031 }; 0032 0033 RichTextEditWithAutoCorrection::RichTextEditWithAutoCorrection(QWidget *parent) 0034 : TextCustomEditor::RichTextEditor(parent) 0035 , d(new NoteShared::RichTextEditWithAutoCorrectionPrivate) 0036 { 0037 } 0038 0039 RichTextEditWithAutoCorrection::~RichTextEditWithAutoCorrection() = default; 0040 0041 void RichTextEditWithAutoCorrection::setAutocorrection(TextAutoCorrectionCore::AutoCorrection *autocorrect) 0042 { 0043 d->mNeedToDelete = false; 0044 delete d->mAutoCorrection; 0045 d->mAutoCorrection = autocorrect; 0046 } 0047 0048 TextAutoCorrectionCore::AutoCorrection *RichTextEditWithAutoCorrection::autocorrection() const 0049 { 0050 return d->mAutoCorrection; 0051 } 0052 0053 void RichTextEditWithAutoCorrection::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 RichTextEditWithAutoCorrection::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 TextCustomEditor::RichTextEditor::keyPressEvent(e); 0094 } 0095 0096 #include "moc_richtexteditwithautocorrection.cpp"