cosmetic fixes to make it validate more, kmcomposerui.rc and karmui.rc still do not...
[kdepim.git] / libkdepim / kemailquotinghighter.cpp
blob453e8cb7b0a585d465aa23d245d4487619967e45
1 /**
2 * kemailquotinghighter.cpp
4 * Copyright (C) 2006 Laurent Montel <montel@kde.org>
5 * Copyright (C) 2008 Thomas McGuire <mcguire@kde.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA
23 #include "kemailquotinghighter.h"
25 #include <KColorScheme>
26 #include <KConfig>
27 #include <KDebug>
28 #include <KLocale>
29 #include <sonnet/speller.h>
30 #include <kdeversion.h>
32 #include <QTextEdit>
33 #include <QTimer>
34 #include <QColor>
35 #include <QHash>
36 #include <QTextCursor>
38 namespace KPIM {
39 class KEMailQuotingHighlighter::KEmailQuotingHighlighterPrivate
41 public:
42 QColor col1, col2, col3, misspelledColor;
43 bool spellCheckingEnabled;
46 KEMailQuotingHighlighter::KEMailQuotingHighlighter( QTextEdit *textEdit,
47 const QColor &normalColor,
48 const QColor &quoteDepth1,
49 const QColor &quoteDepth2,
50 const QColor &quoteDepth3,
51 const QColor &misspelledColor )
52 : Highlighter( textEdit ),
53 d( new KEmailQuotingHighlighterPrivate() )
55 Q_UNUSED( normalColor );
56 // Don't automatically disable the spell checker, for example because there
57 // are too many misspelled words. That would also disable quote highlighting.
58 setAutomatic( false );
60 setActive( true );
61 d->col1 = quoteDepth1;
62 d->col2 = quoteDepth2;
63 d->col3 = quoteDepth3;
64 d->misspelledColor = misspelledColor;
65 d->spellCheckingEnabled = false;
68 KEMailQuotingHighlighter::~KEMailQuotingHighlighter()
70 delete d;
73 void KEMailQuotingHighlighter::setQuoteColor( const QColor &normalColor,
74 const QColor &quoteDepth1,
75 const QColor &quoteDepth2,
76 const QColor &quoteDepth3,
77 const QColor &misspelledColor )
79 Q_UNUSED( normalColor );
80 d->col1 = quoteDepth1;
81 d->col2 = quoteDepth2;
82 d->col3 = quoteDepth3;
83 d->misspelledColor = misspelledColor;
86 void KEMailQuotingHighlighter::toggleSpellHighlighting( bool on )
88 if ( on != d->spellCheckingEnabled ) {
89 d->spellCheckingEnabled = on;
90 rehighlight();
94 void KEMailQuotingHighlighter::highlightBlock( const QString & text )
96 QString simplified = text;
97 simplified = simplified.replace( QRegExp( "\\s" ), QString() )
98 .replace( '|', QLatin1String(">") );
99 while ( simplified.startsWith( QLatin1String(">>>>") ) )
100 simplified = simplified.mid( 3 );
101 if ( simplified.startsWith( QLatin1String(">>>") ) ||
102 simplified.startsWith( QLatin1String("> > >") ) )
103 setFormat( 0, text.length(), d->col1 );
104 else if ( simplified.startsWith( QLatin1String(">>") ) ||
105 simplified.startsWith( QLatin1String("> >") ) )
106 setFormat( 0, text.length(), d->col2 );
107 else if ( simplified.startsWith( QLatin1String(">") ) )
108 setFormat( 0, text.length(), d->col3 );
109 else
111 if ( d->spellCheckingEnabled )
112 Highlighter::highlightBlock( text );
114 setCurrentBlockState( 0 );
117 void KEMailQuotingHighlighter::unsetMisspelled( int start, int count )
119 Q_UNUSED( start )
120 Q_UNUSED( count )
123 void KEMailQuotingHighlighter::setMisspelled( int start, int count )
125 #if KDE_IS_VERSION( 4, 1, 63 )
126 setMisspelledColor( d->misspelledColor );
127 #endif
128 Sonnet::Highlighter::setMisspelled( start, count );