1/**
2 * Copyright (C) 2006 Laurent Montel <montel@kde.org>
3 * Copyright (C) 2008 Thomas McGuire <mcguire@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
21#include "emailquotehighlighter.h"
22
23#include "textedit.h"
24
25namespace KPIMTextEdit {
26
27class EMailQuoteHighlighter::EMailQuoteHighlighterPrivate
28{
29 public:
30 QColor col1, col2, col3, misspelledColor;
31 bool spellCheckingEnabled;
32 TextEdit *parent;
33};
34
35EMailQuoteHighlighter::EMailQuoteHighlighter( TextEdit *textEdit,
36 const QColor &normalColor,
37 const QColor &quoteDepth1,
38 const QColor &quoteDepth2,
39 const QColor &quoteDepth3,
40 const QColor &misspelledColor )
41 : Highlighter( textEdit, textEdit->configFile() ),
42 d( new EMailQuoteHighlighterPrivate() )
43{
44 Q_UNUSED( normalColor );
45 // Don't automatically disable the spell checker, for example because there
46 // are too many misspelled words. That would also disable quote highlighting.
47 // FIXME: disable this spell checking!
48 setAutomatic( false );
49
50 setActive( true );
51 d->col1 = quoteDepth1;
52 d->col2 = quoteDepth2;
53 d->col3 = quoteDepth3;
54 d->misspelledColor = misspelledColor;
55 d->spellCheckingEnabled = false;
56 d->parent = textEdit;
57}
58
59EMailQuoteHighlighter::~EMailQuoteHighlighter()
60{
61}
62
63QString EMailQuoteHighlighter::highlightText( const QString &text,
64 const QColor &quoteDepth1,
65 const QColor &quoteDepth2,
66 const QColor &quoteDepth3 )
67{
68 const QStringList splitList = text.split( QLatin1Char( '\n' ) );
69 QString result;
70 QStringList::const_iterator it = splitList.constBegin();
71 QStringList::const_iterator end = splitList.constEnd();
72 while ( it != end ) {
73 result.append( highlightParagraph( ( *it ) + QLatin1Char( '\n' ),
74 quoteDepth1, quoteDepth2, quoteDepth3 ) );
75 ++it;
76 }
77 return result;
78}
79
80QString EMailQuoteHighlighter::highlightParagraph( const QString &text,
81 const QColor &quoteDepth1,
82 const QColor &quoteDepth2,
83 const QColor &quoteDepth3 )
84{
85 QString simplified = text;
86 simplified = simplified.remove( QRegExp( QLatin1String( "\\s" ) ) ).
87 replace( QLatin1Char( '|' ), QLatin1Char( '>' ) ).
88 replace( QLatin1String( "&gt;" ), QLatin1String( ">" ) );
89
90 while ( simplified.startsWith( QLatin1String( ">>>>" ) ) ) {
91 simplified = simplified.mid( 3 );
92 }
93
94 QString result( QLatin1String( "<font color=\"%1\">%2</font>" ) );
95 if ( simplified.startsWith( QLatin1String( ">>>" ) ) ) {
96 return result.arg( quoteDepth3.name(), text );
97 } else if ( simplified.startsWith( QLatin1String( ">>" ) ) ) {
98 return result.arg( quoteDepth2.name(), text );
99 } else if ( simplified.startsWith( QLatin1String( ">" ) ) ) {
100 return result.arg( quoteDepth1.name(), text );
101 }
102
103 return text;
104}
105
106void EMailQuoteHighlighter::setQuoteColor( const QColor &normalColor,
107 const QColor &quoteDepth1,
108 const QColor &quoteDepth2,
109 const QColor &quoteDepth3,
110 const QColor &misspelledColor )
111{
112 Q_UNUSED( normalColor );
113 d->col1 = quoteDepth1;
114 d->col2 = quoteDepth2;
115 d->col3 = quoteDepth3;
116 d->misspelledColor = misspelledColor;
117}
118
119void EMailQuoteHighlighter::toggleSpellHighlighting( bool on )
120{
121 if ( on != d->spellCheckingEnabled ) {
122 d->spellCheckingEnabled = on;
123 rehighlight();
124 }
125}
126
127void EMailQuoteHighlighter::highlightBlock( const QString &text )
128{
129 QString simplified = text;
130 simplified = simplified.remove( QRegExp( QLatin1String( "\\s" ) ) ).
131 replace( QLatin1Char( '|' ), QLatin1Char( '>' ) );
132
133 while ( simplified.startsWith( QLatin1String( ">>>>" ) ) ) {
134 simplified = simplified.mid( 3 );
135 }
136
137 if ( simplified.startsWith( QLatin1String( ">>>" ) ) ) {
138 setFormat( 0, text.length(), d->col3 );
139 } else if ( simplified.startsWith( QLatin1String( ">>" ) ) ) {
140 setFormat( 0, text.length(), d->col2 );
141 } else if ( simplified.startsWith( QLatin1String( ">" ) ) ) {
142 setFormat( 0, text.length(), d->col1 );
143 } else if ( d->parent->isLineQuoted( text ) ) {
144 setFormat( 0, text.length(), d->col1 ); // FIXME: custom quote prefix
145 // can't handle multiple levels
146 } else if ( d->spellCheckingEnabled ) {
147 Highlighter::highlightBlock( text );
148 return; //setCurrentBlockState already done in Highlighter::highlightBlock
149 }
150 setCurrentBlockState( 0 );
151}
152
153void EMailQuoteHighlighter::unsetMisspelled( int start, int count )
154{
155 Q_UNUSED( start );
156 Q_UNUSED( count );
157}
158
159void EMailQuoteHighlighter::setMisspelled( int start, int count )
160{
161 setMisspelledColor( d->misspelledColor );
162 Sonnet::Highlighter::setMisspelled( start, count );
163}
164
165}
166