• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 &quoteDepth1,
00038                                               const QColor &quoteDepth2,
00039                                               const QColor &quoteDepth3,
00040                                               const QColor &misspelledColor )
00041     : Highlighter( textEdit ),
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 &quoteDepth1,
00065                        const QColor &quoteDepth2,
00066                        const QColor &quoteDepth3 )
00067 {
00068     QStringList splitted = text.split( QLatin1Char('\n') );
00069     QString result;
00070     QStringList::iterator it = splitted.begin();
00071     while ( it != splitted.end() ) {
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 &quoteDepth1,
00081                             const QColor &quoteDepth2,
00082                             const QColor &quoteDepth3 )
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 &quoteDepth1,
00106                                            const QColor &quoteDepth2,
00107                                            const QColor &quoteDepth3,
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 )
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 }

KPIMTextedit Library

Skip menu "KPIMTextedit Library"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal