Commit ca089d32 authored by Laurent Montel's avatar Laurent Montel
Browse files

Move class here (used only here)

parent ee1098db
Pipeline #264166 passed with stage
in 1 minute and 29 seconds
......@@ -45,6 +45,9 @@ set(libnoteshared_SRCS
dialog/selectednotefolderdialog.h
widget/notelistwidget.h
alarms/notealarmdialog.h
widget/richtexteditwithautocorrection.cpp
widget/richtexteditwithautocorrection.h
)
ecm_qt_declare_logging_category(libnoteshared_SRCS HEADER noteshared_debug.h IDENTIFIER NOTESHARED_LOG CATEGORY_NAME org.kde.pim.noteshared
......
......@@ -4,12 +4,12 @@
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "noteeditor.h"
#include <PimCommonAutoCorrection/RichTexteditWithAutoCorrection>
#include "widget/richtexteditwithautocorrection.h"
using namespace NoteShared;
NoteEditor::NoteEditor(QWidget *parent)
: KPIMTextEdit::RichTextEditorWidget(new PimCommonAutoCorrection::RichTextEditWithAutoCorrection(parent))
: KPIMTextEdit::RichTextEditorWidget(new NoteShared::RichTextEditWithAutoCorrection(parent))
{
}
......
/*
SPDX-FileCopyrightText: 2013-2022 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "richtexteditwithautocorrection.h"
#include <PimCommonAutoCorrection/AutoCorrection>
#include <QKeyEvent>
using namespace NoteShared;
class NoteShared::RichTextEditWithAutoCorrectionPrivate
{
public:
RichTextEditWithAutoCorrectionPrivate()
: mAutoCorrection(new PimCommonAutoCorrection::AutoCorrection())
{
}
~RichTextEditWithAutoCorrectionPrivate()
{
if (mNeedToDelete) {
delete mAutoCorrection;
}
}
PimCommonAutoCorrection::AutoCorrection *mAutoCorrection = nullptr;
bool mNeedToDelete = true;
};
RichTextEditWithAutoCorrection::RichTextEditWithAutoCorrection(QWidget *parent)
: KPIMTextEdit::RichTextEditor(parent)
, d(new NoteShared::RichTextEditWithAutoCorrectionPrivate)
{
}
RichTextEditWithAutoCorrection::~RichTextEditWithAutoCorrection() = default;
void RichTextEditWithAutoCorrection::setAutocorrection(PimCommonAutoCorrection::AutoCorrection *autocorrect)
{
d->mNeedToDelete = false;
delete d->mAutoCorrection;
d->mAutoCorrection = autocorrect;
}
PimCommonAutoCorrection::AutoCorrection *RichTextEditWithAutoCorrection::autocorrection() const
{
return d->mAutoCorrection;
}
void RichTextEditWithAutoCorrection::setAutocorrectionLanguage(const QString &language)
{
PimCommonAutoCorrection::AutoCorrectionSettings *settings = d->mAutoCorrection->autoCorrectionSettings();
settings->setLanguage(language);
d->mAutoCorrection->setAutoCorrectionSettings(settings);
}
static bool isSpecial(const QTextCharFormat &charFormat)
{
return charFormat.isFrameFormat() || charFormat.isImageFormat() || charFormat.isListFormat() || charFormat.isTableFormat()
|| charFormat.isTableCellFormat();
}
void RichTextEditWithAutoCorrection::keyPressEvent(QKeyEvent *e)
{
if (d->mAutoCorrection && d->mAutoCorrection->autoCorrectionSettings()->isEnabledAutoCorrection()) {
if ((e->key() == Qt::Key_Space) || (e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) {
if (!textCursor().hasSelection()) {
const QTextCharFormat initialTextFormat = textCursor().charFormat();
const bool richText = acceptRichText();
int position = textCursor().position();
const bool addSpace = d->mAutoCorrection->autocorrect(richText, *document(), position);
QTextCursor cur = textCursor();
cur.setPosition(position);
const bool spacePressed = (e->key() == Qt::Key_Space);
const QChar insertChar = spacePressed ? QLatin1Char(' ') : QLatin1Char('\n');
if (richText && !isSpecial(initialTextFormat)) {
if (addSpace || !spacePressed) {
cur.insertText(insertChar, initialTextFormat);
}
} else {
if (addSpace || !spacePressed) {
cur.insertText(insertChar);
}
}
setTextCursor(cur);
return;
}
}
}
KPIMTextEdit::RichTextEditor::keyPressEvent(e);
}
/*
SPDX-FileCopyrightText: 2013-2022 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <KPIMTextEdit/RichTextEditor>
namespace PimCommonAutoCorrection
{
class AutoCorrection;
}
namespace NoteShared
{
class AutoCorrection;
class RichTextEditWithAutoCorrectionPrivate;
/**
* @brief The RichTextEditWithAutoCorrection class
* @author Laurent Montel <montel@kde.org>
*/
class RichTextEditWithAutoCorrection : public KPIMTextEdit::RichTextEditor
{
Q_OBJECT
public:
explicit RichTextEditWithAutoCorrection(QWidget *parent = nullptr);
~RichTextEditWithAutoCorrection() override;
Q_REQUIRED_RESULT PimCommonAutoCorrection::AutoCorrection *autocorrection() const;
void setAutocorrection(PimCommonAutoCorrection::AutoCorrection *autocorrect);
void setAutocorrectionLanguage(const QString &language);
protected:
void keyPressEvent(QKeyEvent *e) override;
private:
std::unique_ptr<RichTextEditWithAutoCorrectionPrivate> const d;
};
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment