KPimTextEdit

richtextcomposeremailquotehighlighter.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "richtextcomposeremailquotehighlighter.h"
8#include "richtextcomposer.h"
9#include <QRegularExpression>
10using namespace KPIMTextEdit;
11
12class Q_DECL_HIDDEN KPIMTextEdit::RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighterPrivate
13{
14public:
15 RichTextComposer *parent = nullptr;
16 QColor col1;
17 QColor col2;
18 QColor col3;
19 QColor misspelledColor;
20 bool spellCheckingEnabled = false;
21};
22
23RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighter(RichTextComposer *textEdit,
24 const QColor &normalColor,
25 const QColor &quoteDepth1,
26 const QColor &quoteDepth2,
27 const QColor &quoteDepth3,
28 const QColor &misspelledColor)
29 : Sonnet::Highlighter(textEdit)
30 , d(new KPIMTextEdit::RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighterPrivate())
31{
32 Q_UNUSED(normalColor)
33 // Don't automatically disable the spell checker, for example because there
34 // are too many misspelled words. That would also disable quote highlighting.
35 // FIXME: disable this spell checking!
36 setAutomatic(false);
37
38 setActive(true);
39 d->col1 = quoteDepth1;
40 d->col2 = quoteDepth2;
41 d->col3 = quoteDepth3;
42 d->misspelledColor = misspelledColor;
43 d->spellCheckingEnabled = false;
44 d->parent = textEdit;
45}
46
47RichTextComposerEmailQuoteHighlighter::~RichTextComposerEmailQuoteHighlighter() = default;
48
49void RichTextComposerEmailQuoteHighlighter::setQuoteColor(const QColor &normalColor,
50 const QColor &quoteDepth1,
51 const QColor &quoteDepth2,
52 const QColor &quoteDepth3,
53 const QColor &misspelledColor)
54{
56 d->col1 = quoteDepth1;
57 d->col2 = quoteDepth2;
58 d->col3 = quoteDepth3;
59 d->misspelledColor = misspelledColor;
60}
61
62void RichTextComposerEmailQuoteHighlighter::toggleSpellHighlighting(bool on)
63{
64 if (on != d->spellCheckingEnabled) {
65 d->spellCheckingEnabled = on;
67 }
68}
69
70void RichTextComposerEmailQuoteHighlighter::highlightBlock(const QString &text)
71{
72 QString simplified = text;
73 simplified.remove(QRegularExpression(QStringLiteral("\\s"))).replace(QLatin1Char('|'), QLatin1Char('>'));
74
75 while (simplified.startsWith(QLatin1StringView(">>>>"))) {
76 simplified.remove(0, 3);
77 }
78
79 if (simplified.startsWith(QLatin1StringView(">>>"))) {
80 setFormat(0, text.length(), d->col3);
81 } else if (simplified.startsWith(QLatin1StringView(">>"))) {
82 setFormat(0, text.length(), d->col2);
83 } else if (simplified.startsWith(QLatin1StringView(">"))) {
84 setFormat(0, text.length(), d->col1);
85 } else if (d->parent->isLineQuoted(text)) {
86 setFormat(0, text.length(), d->col1); // FIXME: custom quote prefix
87 // can't handle multiple levels
88 } else if (d->spellCheckingEnabled) {
89 Highlighter::highlightBlock(text);
90 return; // setCurrentBlockState already done in Highlighter::highlightBlock
91 }
93}
94
95void RichTextComposerEmailQuoteHighlighter::unsetMisspelled(int start, int count)
96{
98 Q_UNUSED(count)
99}
100
101void RichTextComposerEmailQuoteHighlighter::setMisspelled(int start, int count)
102{
103 setMisspelledColor(d->misspelledColor);
104 Sonnet::Highlighter::setMisspelled(start, count);
105}
106
107#include "moc_richtextcomposeremailquotehighlighter.cpp"
The RichTextComposer class.
void setMisspelledColor(const QColor &color)
Q_SCRIPTABLE Q_NOREPLY void start()
T qobject_cast(QObject *object)
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
void setCurrentBlockState(int newState)
void setFormat(int start, int count, const QColor &color)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.