KTextAddons

autocorrectionlineedit.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 "autocorrectionlineedit.h"
8#include <QKeyEvent>
9#include <QTextCursor>
10#include <TextAutoCorrectionCore/AutoCorrection>
11
12using namespace TextAutoCorrectionWidgets;
13using namespace TextAutoCorrectionCore;
14class TextAutoCorrectionWidgets::AutoCorrectionLineEditPrivate
15{
16public:
17 AutoCorrectionLineEditPrivate()
18 : mAutoCorrection(new TextAutoCorrectionCore::AutoCorrection())
19 {
20 }
21
22 ~AutoCorrectionLineEditPrivate()
23 {
24 if (mNeedToDeleteAutoCorrection) {
25 delete mAutoCorrection;
26 }
27 }
28
29 TextAutoCorrectionCore::AutoCorrection *mAutoCorrection = nullptr;
30 bool mNeedToDeleteAutoCorrection = true;
31};
32
33AutoCorrectionLineEdit::AutoCorrectionLineEdit(QWidget *parent)
34 : QPlainTextEdit(parent)
35 , d(new TextAutoCorrectionWidgets::AutoCorrectionLineEditPrivate)
36{
37 setTabChangesFocus(true);
38 // widget may not be resized vertically
40 setLineWrapMode(NoWrap);
41 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43 document()->adjustSize();
44 document()->setDocumentMargin(2);
45}
46
47AutoCorrectionLineEdit::~AutoCorrectionLineEdit() = default;
48
49TextAutoCorrectionCore::AutoCorrection *AutoCorrectionLineEdit::autocorrection() const
50{
51 return d->mAutoCorrection;
52}
53
54void AutoCorrectionLineEdit::setAutocorrection(AutoCorrection *autocorrect)
55{
56 d->mNeedToDeleteAutoCorrection = false;
57 delete d->mAutoCorrection;
58 d->mAutoCorrection = autocorrect;
59}
60
61void AutoCorrectionLineEdit::setAutocorrectionLanguage(const QString &language)
62{
63 TextAutoCorrectionCore::AutoCorrectionSettings *settings = d->mAutoCorrection->autoCorrectionSettings();
64 settings->setLanguage(language);
65 d->mAutoCorrection->setAutoCorrectionSettings(settings);
66}
67
68void AutoCorrectionLineEdit::keyPressEvent(QKeyEvent *e)
69{
70 if (d->mAutoCorrection && d->mAutoCorrection->autoCorrectionSettings()->isEnabledAutoCorrection()) {
71 if ((e->key() == Qt::Key_Space) || (e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) {
72 if (!textCursor().hasSelection()) {
73 int position = textCursor().position();
74 // no Html format in subject. => false
75 const bool addSpace = d->mAutoCorrection->autocorrect(false, *document(), position);
76 QTextCursor cur = textCursor();
77 cur.setPosition(position);
78 if (e->key() == Qt::Key_Space) {
79 if (addSpace) {
80 cur.insertText(QStringLiteral(" "));
81 setTextCursor(cur);
82 }
83 return;
84 }
85 }
86 }
87 }
89}
90
91#include "moc_autocorrectionlineedit.cpp"
int key() const const
QTextDocument * document() const const
virtual void keyPressEvent(QKeyEvent *e) override
void setTextCursor(const QTextCursor &cursor)
QTextCursor textCursor() const const
Key_Space
ScrollBarAlwaysOff
void insertText(const QString &text)
int position() const const
void setPosition(int pos, MoveMode m)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:47:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.