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