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
23 #include "kemailquotinghighter.h"
25 #include <KColorScheme>
29 #include <sonnet/speller.h>
30 #include <kdeversion.h>
36 #include <QTextCursor>
39 class KEMailQuotingHighlighter::KEmailQuotingHighlighterPrivate
42 QColor col1
, col2
, col3
, misspelledColor
;
43 bool spellCheckingEnabled
;
46 KEMailQuotingHighlighter::KEMailQuotingHighlighter( QTextEdit
*textEdit
,
47 const QColor
&normalColor
,
48 const QColor
"eDepth1
,
49 const QColor
"eDepth2
,
50 const QColor
"eDepth3
,
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 );
61 d
->col1
= quoteDepth1
;
62 d
->col2
= quoteDepth2
;
63 d
->col3
= quoteDepth3
;
64 d
->misspelledColor
= misspelledColor
;
65 d
->spellCheckingEnabled
= false;
68 KEMailQuotingHighlighter::~KEMailQuotingHighlighter()
73 void KEMailQuotingHighlighter::setQuoteColor( const QColor
&normalColor
,
74 const QColor
"eDepth1
,
75 const QColor
"eDepth2
,
76 const QColor
"eDepth3
,
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
;
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
);
111 if ( d
->spellCheckingEnabled
)
112 Highlighter::highlightBlock( text
);
114 setCurrentBlockState( 0 );
117 void KEMailQuotingHighlighter::unsetMisspelled( int start
, int count
)
123 void KEMailQuotingHighlighter::setMisspelled( int start
, int count
)
125 #if KDE_IS_VERSION( 4, 1, 63 )
126 setMisspelledColor( d
->misspelledColor
);
128 Sonnet::Highlighter::setMisspelled( start
, count
);