Pimcommon

lineeditwithautocorrection.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "autocorrection/widgets/lineeditwithautocorrection.h"
8#include <QKeyEvent>
9#include <TextAutoCorrectionCore/AutoCorrection>
10
11using namespace PimCommon;
12class PimCommon::LineEditWithAutoCorrectionPrivate
13{
14public:
15 LineEditWithAutoCorrectionPrivate()
16 : mAutoCorrection(new TextAutoCorrectionCore::AutoCorrection())
17 {
18 }
19
20 ~LineEditWithAutoCorrectionPrivate()
21 {
22 if (mNeedToDeleteAutoCorrection) {
23 delete mAutoCorrection;
24 }
25 }
26 TextAutoCorrectionCore::AutoCorrection *mAutoCorrection = nullptr;
27 bool mNeedToDeleteAutoCorrection = true;
28};
29
30LineEditWithAutoCorrection::LineEditWithAutoCorrection(QWidget *parent, const QString &configFile)
31 : PimCommon::SpellCheckLineEdit(parent, configFile)
32 , d(new PimCommon::LineEditWithAutoCorrectionPrivate)
33{
34}
35
36LineEditWithAutoCorrection::~LineEditWithAutoCorrection() = default;
37
38TextAutoCorrectionCore::AutoCorrection *LineEditWithAutoCorrection::autocorrection() const
39{
40 return d->mAutoCorrection;
41}
42
43void LineEditWithAutoCorrection::setAutocorrection(TextAutoCorrectionCore::AutoCorrection *autocorrect)
44{
45 d->mNeedToDeleteAutoCorrection = false;
46 delete d->mAutoCorrection;
47 d->mAutoCorrection = autocorrect;
48}
49
50void LineEditWithAutoCorrection::setAutocorrectionLanguage(const QString &language)
51{
52 TextAutoCorrectionCore::AutoCorrectionSettings *settings = d->mAutoCorrection->autoCorrectionSettings();
53 settings->setLanguage(language);
54 d->mAutoCorrection->setAutoCorrectionSettings(settings);
55}
56
57void LineEditWithAutoCorrection::keyPressEvent(QKeyEvent *e)
58{
59 if (d->mAutoCorrection && d->mAutoCorrection->autoCorrectionSettings()->isEnabledAutoCorrection()) {
60 if ((e->key() == Qt::Key_Space) || (e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) {
61 if (!textCursor().hasSelection()) {
62 int position = textCursor().position();
63 // no Html format in subject. => false
64 const bool addSpace = d->mAutoCorrection->autocorrect(false, *document(), position);
65 QTextCursor cur = textCursor();
66 cur.setPosition(position);
67 if (e->key() == Qt::Key_Space) {
68 if (addSpace) {
69 cur.insertText(QStringLiteral(" "));
70 setTextCursor(cur);
71 }
72 return;
73 }
74 }
75 }
76 }
77 PimCommon::SpellCheckLineEdit::keyPressEvent(e);
78}
79
80#include "moc_lineeditwithautocorrection.cpp"
The SpellCheckLineEdit class.
folderdialogacltab.h
int key() const const
Key_Space
void insertText(const QString &text)
int position() const const
void setPosition(int pos, MoveMode m)
void setTextCursor(const QTextCursor &cursor)
QTextCursor textCursor() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.