KPIMTextedit Library
emailquotehighlighter.cpp
00001 00021 #include "emailquotehighlighter.h" 00022 00023 #include "textedit.h" 00024 #include <kdeversion.h> 00025 00026 namespace KPIMTextEdit { 00027 00028 class EMailQuoteHighlighter::EMailQuoteHighlighterPrivate 00029 { 00030 public: 00031 QColor col1, col2, col3, misspelledColor; 00032 bool spellCheckingEnabled; 00033 TextEdit *parent; 00034 }; 00035 00036 EMailQuoteHighlighter::EMailQuoteHighlighter( TextEdit *textEdit, 00037 const QColor &normalColor, 00038 const QColor "eDepth1, 00039 const QColor "eDepth2, 00040 const QColor "eDepth3, 00041 const QColor &misspelledColor ) 00042 : Highlighter( textEdit, textEdit->configFile() ), 00043 d( new EMailQuoteHighlighterPrivate() ) 00044 { 00045 Q_UNUSED( normalColor ); 00046 // Don't automatically disable the spell checker, for example because there 00047 // are too many misspelled words. That would also disable quote highlighting. 00048 // FIXME: disable this spell checking! 00049 setAutomatic( false ); 00050 00051 setActive( true ); 00052 d->col1 = quoteDepth1; 00053 d->col2 = quoteDepth2; 00054 d->col3 = quoteDepth3; 00055 d->misspelledColor = misspelledColor; 00056 d->spellCheckingEnabled = false; 00057 d->parent = textEdit; 00058 } 00059 00060 EMailQuoteHighlighter::~EMailQuoteHighlighter() 00061 { 00062 } 00063 00064 QString EMailQuoteHighlighter::highlightText( const QString &text, 00065 const QColor "eDepth1, 00066 const QColor "eDepth2, 00067 const QColor "eDepth3 ) 00068 { 00069 const QStringList splitted = text.split( QLatin1Char('\n') ); 00070 QString result; 00071 QStringList::const_iterator it = splitted.constBegin(); 00072 while ( it != splitted.constEnd() ) { 00073 result.append( highlightParagraph(( *it ) + QLatin1Char('\n'), 00074 quoteDepth1, quoteDepth2, quoteDepth3 ) ); 00075 ++it; 00076 } 00077 return result; 00078 } 00079 00080 QString EMailQuoteHighlighter::highlightParagraph( const QString& text, 00081 const QColor "eDepth1, 00082 const QColor "eDepth2, 00083 const QColor "eDepth3 ) 00084 { 00085 QString simplified = text; 00086 simplified = simplified.remove( QRegExp( QLatin1String( "\\s" ) ) ) 00087 .replace( QLatin1Char( '|' ), QLatin1Char( '>' ) ) 00088 .replace( QLatin1String( ">"), QLatin1String( ">" )); 00089 00090 while ( simplified.startsWith( QLatin1String(">>>>") ) ) 00091 simplified = simplified.mid( 3 ); 00092 00093 QString result( QLatin1String("<font color=\"%1\">%2</font>") ); 00094 if ( simplified.startsWith( QLatin1String( ">>>" ) ) ) { 00095 return result.arg( quoteDepth3.name(), text); 00096 } else if ( simplified.startsWith( QLatin1String( ">>" ) ) ) { 00097 return result.arg( quoteDepth2.name(), text); 00098 } else if ( simplified.startsWith( QLatin1String( ">" ) ) ) { 00099 return result.arg( quoteDepth1.name(), text); 00100 } 00101 00102 return text; 00103 } 00104 00105 void EMailQuoteHighlighter::setQuoteColor( const QColor &normalColor, 00106 const QColor "eDepth1, 00107 const QColor "eDepth2, 00108 const QColor "eDepth3, 00109 const QColor &misspelledColor ) 00110 { 00111 Q_UNUSED( normalColor ); 00112 d->col1 = quoteDepth1; 00113 d->col2 = quoteDepth2; 00114 d->col3 = quoteDepth3; 00115 d->misspelledColor = misspelledColor; 00116 } 00117 00118 void EMailQuoteHighlighter::toggleSpellHighlighting( bool on ) 00119 { 00120 if ( on != d->spellCheckingEnabled ) { 00121 d->spellCheckingEnabled = on; 00122 rehighlight(); 00123 } 00124 } 00125 00126 void EMailQuoteHighlighter::highlightBlock( const QString & text ) 00127 { 00128 QString simplified = text; 00129 simplified = simplified.remove( QRegExp( QLatin1String( "\\s" ) ) ) 00130 .replace( QLatin1Char( '|' ), QLatin1Char( '>' ) ); 00131 00132 while ( simplified.startsWith( QLatin1String(">>>>") ) ) 00133 simplified = simplified.mid( 3 ); 00134 if ( simplified.startsWith( QLatin1String(">>>") ) ) 00135 setFormat( 0, text.length(), d->col3 ); 00136 else if ( simplified.startsWith( QLatin1String(">>") ) ) 00137 setFormat( 0, text.length(), d->col2 ); 00138 else if ( simplified.startsWith( QLatin1String(">") ) ) 00139 setFormat( 0, text.length(), d->col1 ); 00140 else if ( d->parent->isLineQuoted( text ) ) { 00141 setFormat( 0, text.length(), d->col1 ); // FIXME: custom quote prefix can't handle multiple levels 00142 } 00143 else 00144 { 00145 if ( d->spellCheckingEnabled 00146 #if KDE_IS_VERSION(4,4,93) 00147 && checkerEnabledByDefault() 00148 #endif 00149 ) 00150 Highlighter::highlightBlock( text ); 00151 } 00152 setCurrentBlockState( 0 ); 00153 } 00154 00155 void EMailQuoteHighlighter::unsetMisspelled( int start, int count ) 00156 { 00157 Q_UNUSED( start ) 00158 Q_UNUSED( count ) 00159 } 00160 00161 void EMailQuoteHighlighter::setMisspelled( int start, int count ) 00162 { 00163 setMisspelledColor( d->misspelledColor ); 00164 Sonnet::Highlighter::setMisspelled( start, count ); 00165 } 00166 00167 }