File indexing completed on 2024-05-19 05:21:44

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "richtextcomposeremailquotehighlighter.h"
0008 #include "richtextcomposer.h"
0009 #include <QRegularExpression>
0010 using namespace KPIMTextEdit;
0011 
0012 class Q_DECL_HIDDEN KPIMTextEdit::RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighterPrivate
0013 {
0014 public:
0015     RichTextComposer *parent = nullptr;
0016     QColor col1;
0017     QColor col2;
0018     QColor col3;
0019     QColor misspelledColor;
0020     bool spellCheckingEnabled = false;
0021 };
0022 
0023 RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighter(RichTextComposer *textEdit,
0024                                                                              const QColor &normalColor,
0025                                                                              const QColor &quoteDepth1,
0026                                                                              const QColor &quoteDepth2,
0027                                                                              const QColor &quoteDepth3,
0028                                                                              const QColor &misspelledColor)
0029     : Sonnet::Highlighter(textEdit)
0030     , d(new KPIMTextEdit::RichTextComposerEmailQuoteHighlighter::RichTextComposerEmailQuoteHighlighterPrivate())
0031 {
0032     Q_UNUSED(normalColor)
0033     // Don't automatically disable the spell checker, for example because there
0034     // are too many misspelled words. That would also disable quote highlighting.
0035     // FIXME: disable this spell checking!
0036     setAutomatic(false);
0037 
0038     setActive(true);
0039     d->col1 = quoteDepth1;
0040     d->col2 = quoteDepth2;
0041     d->col3 = quoteDepth3;
0042     d->misspelledColor = misspelledColor;
0043     d->spellCheckingEnabled = false;
0044     d->parent = textEdit;
0045 }
0046 
0047 RichTextComposerEmailQuoteHighlighter::~RichTextComposerEmailQuoteHighlighter() = default;
0048 
0049 void RichTextComposerEmailQuoteHighlighter::setQuoteColor(const QColor &normalColor,
0050                                                           const QColor &quoteDepth1,
0051                                                           const QColor &quoteDepth2,
0052                                                           const QColor &quoteDepth3,
0053                                                           const QColor &misspelledColor)
0054 {
0055     Q_UNUSED(normalColor)
0056     d->col1 = quoteDepth1;
0057     d->col2 = quoteDepth2;
0058     d->col3 = quoteDepth3;
0059     d->misspelledColor = misspelledColor;
0060 }
0061 
0062 void RichTextComposerEmailQuoteHighlighter::toggleSpellHighlighting(bool on)
0063 {
0064     if (on != d->spellCheckingEnabled) {
0065         d->spellCheckingEnabled = on;
0066         rehighlight();
0067     }
0068 }
0069 
0070 void RichTextComposerEmailQuoteHighlighter::highlightBlock(const QString &text)
0071 {
0072     QString simplified = text;
0073     simplified.remove(QRegularExpression(QStringLiteral("\\s"))).replace(QLatin1Char('|'), QLatin1Char('>'));
0074 
0075     while (simplified.startsWith(QLatin1StringView(">>>>"))) {
0076         simplified.remove(0, 3);
0077     }
0078 
0079     if (simplified.startsWith(QLatin1StringView(">>>"))) {
0080         setFormat(0, text.length(), d->col3);
0081     } else if (simplified.startsWith(QLatin1StringView(">>"))) {
0082         setFormat(0, text.length(), d->col2);
0083     } else if (simplified.startsWith(QLatin1StringView(">"))) {
0084         setFormat(0, text.length(), d->col1);
0085     } else if (d->parent->isLineQuoted(text)) {
0086         setFormat(0, text.length(), d->col1); // FIXME: custom quote prefix
0087         // can't handle multiple levels
0088     } else if (d->spellCheckingEnabled) {
0089         Highlighter::highlightBlock(text);
0090         return; // setCurrentBlockState already done in Highlighter::highlightBlock
0091     }
0092     setCurrentBlockState(0);
0093 }
0094 
0095 void RichTextComposerEmailQuoteHighlighter::unsetMisspelled(int start, int count)
0096 {
0097     Q_UNUSED(start)
0098     Q_UNUSED(count)
0099 }
0100 
0101 void RichTextComposerEmailQuoteHighlighter::setMisspelled(int start, int count)
0102 {
0103     setMisspelledColor(d->misspelledColor);
0104     Sonnet::Highlighter::setMisspelled(start, count);
0105 }
0106 
0107 #include "moc_richtextcomposeremailquotehighlighter.cpp"