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"
8using namespace Qt::Literals::StringLiterals;
9
10#include "richtextcomposer.h"
11#include <QRegularExpression>
12using namespace KPIMTextEdit;
13
14class Q_DECL_HIDDEN KPIMTextEdit::RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighterPrivate
15{
16public:
17 RichTextComposer *parent = nullptr;
18 QColor col1;
19 QColor col2;
20 QColor col3;
21 QColor misspelledColor;
22 bool spellCheckingEnabled = false;
23};
24
25RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighter(RichTextComposer *textEdit,
26 const QColor &normalColor,
27 const QColor &quoteDepth1,
28 const QColor &quoteDepth2,
29 const QColor &quoteDepth3,
30 const QColor &misspelledColor)
31 : Sonnet::Highlighter(textEdit)
32 , d(new KPIMTextEdit::RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighterPrivate())
33{
34 Q_UNUSED(normalColor)
35 // Don't automatically disable the spell checker, for example because there
36 // are too many misspelled words. That would also disable quote highlighting.
37 // FIXME: disable this spell checking!
38 setAutomatic(false);
39
40 setActive(true);
41 d->col1 = quoteDepth1;
42 d->col2 = quoteDepth2;
43 d->col3 = quoteDepth3;
44 d->misspelledColor = misspelledColor;
45 d->spellCheckingEnabled = false;
46 d->parent = textEdit;
47}
48
49RichTextComposerEmailQuoteHighlighter::~RichTextComposerEmailQuoteHighlighter() = default;
50
51void RichTextComposerEmailQuoteHighlighter::setQuoteColor(const QColor &normalColor,
52 const QColor &quoteDepth1,
53 const QColor &quoteDepth2,
54 const QColor &quoteDepth3,
55 const QColor &misspelledColor)
56{
57 Q_UNUSED(normalColor)
58 d->col1 = quoteDepth1;
59 d->col2 = quoteDepth2;
60 d->col3 = quoteDepth3;
61 d->misspelledColor = misspelledColor;
62}
63
64void RichTextComposerEmailQuoteHighlighter::toggleSpellHighlighting(bool on)
65{
66 if (on != d->spellCheckingEnabled) {
67 d->spellCheckingEnabled = on;
69 }
70}
71
72void RichTextComposerEmailQuoteHighlighter::highlightBlock(const QString &text)
73{
74 QString simplified = text;
75 simplified.remove(QRegularExpression(QStringLiteral("\\s"))).replace(QLatin1Char('|'), QLatin1Char('>'));
76
77 while (simplified.startsWith(">>>>"_L1)) {
78 simplified.remove(0, 3);
79 }
80
81 if (simplified.startsWith(">>>"_L1)) {
82 setFormat(0, text.length(), d->col3);
83 } else if (simplified.startsWith(">>"_L1)) {
84 setFormat(0, text.length(), d->col2);
85 } else if (simplified.startsWith(">"_L1)) {
86 setFormat(0, text.length(), d->col1);
87 } else if (d->parent->isLineQuoted(text)) {
88 setFormat(0, text.length(), d->col1); // FIXME: custom quote prefix
89 // can't handle multiple levels
90 } else if (d->spellCheckingEnabled) {
91 Highlighter::highlightBlock(text);
92 return; // setCurrentBlockState already done in Highlighter::highlightBlock
93 }
95}
96
97void RichTextComposerEmailQuoteHighlighter::unsetMisspelled(int start, int count)
98{
99 Q_UNUSED(start)
100 Q_UNUSED(count)
101}
102
103void RichTextComposerEmailQuoteHighlighter::setMisspelled(int start, int count)
104{
105 setMisspelledColor(d->misspelledColor);
106 Sonnet::Highlighter::setMisspelled(start, count);
107}
108
109#include "moc_richtextcomposeremailquotehighlighter.cpp"
The RichTextComposer class.
void setMisspelledColor(const QColor &color)
Q_SCRIPTABLE Q_NOREPLY void start()
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 Mon Nov 4 2024 16:35:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.