• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • 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, 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 &quoteDepth1,
00065                                               const QColor &quoteDepth2,
00066                                               const QColor &quoteDepth3 )
00067 {
00068   const QStringList splitList = text.split( QLatin1Char( '\n' ) );
00069   QString result;
00070   QStringList::const_iterator it = splitList.constBegin();
00071   while ( it != splitList.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 &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   }
00134 
00135   if ( simplified.startsWith( QLatin1String( ">>>" ) ) ) {
00136     setFormat( 0, text.length(), d->col3 );
00137   } else if ( simplified.startsWith( QLatin1String( ">>" ) ) ) {
00138     setFormat( 0, text.length(), d->col2 );
00139   } else if ( simplified.startsWith( QLatin1String( ">" ) ) ) {
00140     setFormat( 0, text.length(), d->col1 );
00141   } else if ( d->parent->isLineQuoted( text ) ) {
00142     setFormat( 0, text.length(), d->col1 ); // FIXME: custom quote prefix
00143                                             // can't handle multiple levels
00144   } else {
00145     if ( d->spellCheckingEnabled && checkerEnabledByDefault() ) {
00146       Highlighter::highlightBlock( text );
00147     }
00148   }
00149   setCurrentBlockState( 0 );
00150 }
00151 
00152 void EMailQuoteHighlighter::unsetMisspelled( int start, int count )
00153 {
00154   Q_UNUSED( start );
00155   Q_UNUSED( count );
00156 }
00157 
00158 void EMailQuoteHighlighter::setMisspelled( int start, int count )
00159 {
00160   setMisspelledColor( d->misspelledColor );
00161   Sonnet::Highlighter::setMisspelled( start, count );
00162 }
00163 
00164 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:56:51 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPIMTextedit Library

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

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal