• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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  QString result( QLatin1String( "<font color=\"%1\">%2</font>" ) );
94  if ( simplified.startsWith( QLatin1String( ">>>" ) ) ) {
95  return result.arg( quoteDepth3.name(), text );
96  } else if ( simplified.startsWith( QLatin1String( ">>" ) ) ) {
97  return result.arg( quoteDepth2.name(), text );
98  } else if ( simplified.startsWith( QLatin1String( ">" ) ) ) {
99  return result.arg( quoteDepth1.name(), text );
100  }
101 
102  return text;
103 }
104 
105 void EMailQuoteHighlighter::setQuoteColor( const QColor &normalColor,
106  const QColor &quoteDepth1,
107  const QColor &quoteDepth2,
108  const QColor &quoteDepth3,
109  const QColor &misspelledColor )
110 {
111  Q_UNUSED( normalColor );
112  d->col1 = quoteDepth1;
113  d->col2 = quoteDepth2;
114  d->col3 = quoteDepth3;
115  d->misspelledColor = misspelledColor;
116 }
117 
118 void EMailQuoteHighlighter::toggleSpellHighlighting( bool on )
119 {
120  if ( on != d->spellCheckingEnabled ) {
121  d->spellCheckingEnabled = on;
122  rehighlight();
123  }
124 }
125 
126 void EMailQuoteHighlighter::highlightBlock( const QString &text )
127 {
128  QString simplified = text;
129  simplified = simplified.remove( QRegExp( QLatin1String( "\\s" ) ) ).
130  replace( QLatin1Char( '|' ), QLatin1Char( '>' ) );
131 
132  while ( simplified.startsWith( QLatin1String( ">>>>" ) ) ) {
133  simplified = simplified.mid( 3 );
134  }
135 
136  if ( simplified.startsWith( QLatin1String( ">>>" ) ) ) {
137  setFormat( 0, text.length(), d->col3 );
138  } else if ( simplified.startsWith( QLatin1String( ">>" ) ) ) {
139  setFormat( 0, text.length(), d->col2 );
140  } else if ( simplified.startsWith( QLatin1String( ">" ) ) ) {
141  setFormat( 0, text.length(), d->col1 );
142  } else if ( d->parent->isLineQuoted( text ) ) {
143  setFormat( 0, text.length(), d->col1 ); // FIXME: custom quote prefix
144  // can't handle multiple levels
145  } else {
146  if ( d->spellCheckingEnabled && checkerEnabledByDefault() ) {
147  Highlighter::highlightBlock( text );
148  }
149  }
150  setCurrentBlockState( 0 );
151 }
152 
153 void EMailQuoteHighlighter::unsetMisspelled( int start, int count )
154 {
155  Q_UNUSED( start );
156  Q_UNUSED( count );
157 }
158 
159 void EMailQuoteHighlighter::setMisspelled( int start, int count )
160 {
161  setMisspelledColor( d->misspelledColor );
162  Sonnet::Highlighter::setMisspelled( start, count );
163 }
164 
165 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:47:21 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 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