• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KPIMTextedit Library

  • kpimtextedit
emailquotehighlighter.cpp
1 
21 #include "emailquotehighlighter.h"
22 
23 #include "textedit.h"
24 
25 namespace KPIMTextEdit {
26 
27 class EMailQuoteHighlighter::EMailQuoteHighlighterPrivate
28 {
29  public:
30  QColor col1, col2, col3, misspelledColor;
31  bool spellCheckingEnabled;
32  TextEdit *parent;
33 };
34 
35 EMailQuoteHighlighter::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 
59 EMailQuoteHighlighter::~EMailQuoteHighlighter()
60 {
61 }
62 
63 QString 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 
80 QString 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( ">" ), 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 
106 void 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 
119 void EMailQuoteHighlighter::toggleSpellHighlighting( bool on )
120 {
121  if ( on != d->spellCheckingEnabled ) {
122  d->spellCheckingEnabled = on;
123  rehighlight();
124  }
125 }
126 
127 void 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 {
147  if ( d->spellCheckingEnabled && checkerEnabledByDefault() ) {
148  Highlighter::highlightBlock( text );
149  }
150  }
151  setCurrentBlockState( 0 );
152 }
153 
154 void EMailQuoteHighlighter::unsetMisspelled( int start, int count )
155 {
156  Q_UNUSED( start );
157  Q_UNUSED( count );
158 }
159 
160 void EMailQuoteHighlighter::setMisspelled( int start, int count )
161 {
162  setMisspelledColor( d->misspelledColor );
163  Sonnet::Highlighter::setMisspelled( start, count );
164 }
165 
166 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:00 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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