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

KPIMTextedit Library

  • sources
  • kde-4.14
  • kdepimlibs
  • 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 if ( d->spellCheckingEnabled ) {
147  Highlighter::highlightBlock( text );
148  return; //setCurrentBlockState already done in Highlighter::highlightBlock
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 }
KPIMTextEdit::EMailQuoteHighlighter::highlightBlock
virtual void highlightBlock(const QString &text)
Reimplemented to highlight quote blocks.
Definition: emailquotehighlighter.cpp:127
QString::append
QString & append(QChar ch)
QColor::name
QString name() const
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KPIMTextEdit::EMailQuoteHighlighter::EMailQuoteHighlighter
EMailQuoteHighlighter(TextEdit *textEdit, const QColor &normalColor=Qt::black, const QColor &quoteDepth1=QColor(0x00, 0x80, 0x00), const QColor &quoteDepth2=QColor(0x00, 0x80, 0x00), const QColor &quoteDepth3=QColor(0x00, 0x80, 0x00), const QColor &misspelledColor=Qt::red)
Constructor.
Definition: emailquotehighlighter.cpp:35
QString::remove
QString & remove(int position, int n)
QList::const_iterator
QRegExp
KPIMTextEdit::EMailQuoteHighlighter::unsetMisspelled
virtual void unsetMisspelled(int start, int count)
Reimplemented, the base version sets the text color to black, which is not what we want...
Definition: emailquotehighlighter.cpp:153
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
KPIMTextEdit::EMailQuoteHighlighter::highlightText
static QString highlightText(const QString &text, const QColor &quoteDepth1=QColor(0x00, 0x80, 0x00), const QColor &quoteDepth2=QColor(0x00, 0x80, 0x00), const QColor &quoteDepth3=QColor(0x00, 0x80, 0x00))
Use this static method to get a text consisting of multiple lines highligted.
Definition: emailquotehighlighter.cpp:63
KPIMTextEdit::EMailQuoteHighlighter::setQuoteColor
void setQuoteColor(const QColor &normalColor, const QColor &quoteDepth1, const QColor &quoteDepth2, const QColor &quoteDepth3, const QColor &misspelledColor=Qt::red)
Sets the colors used for highlighting quoted text and spelling mistakes.
Definition: emailquotehighlighter.cpp:106
QString
QColor
QStringList
KPIMTextEdit::EMailQuoteHighlighter::setMisspelled
virtual void setMisspelled(int start, int count)
Reimplemented to set the color of the misspelled word to a color defined by setQuoteColor().
Definition: emailquotehighlighter.cpp:159
QLatin1Char
QString::mid
QString mid(int position, int n) const
KPIMTextEdit::EMailQuoteHighlighter::toggleSpellHighlighting
void toggleSpellHighlighting(bool on)
Turns spellcheck highlighting on or off.
Definition: emailquotehighlighter.cpp:119
QLatin1String
KPIMTextEdit::TextEdit
Special textedit that provides additional features which are useful for PIM applications like mail cl...
Definition: textedit.h:84
KPIMTextEdit::EMailQuoteHighlighter::highlightParagraph
static QString highlightParagraph(const QString &text, const QColor &quoteDepth1=QColor(0x00, 0x80, 0x00), const QColor &quoteDepth2=QColor(0x00, 0x80, 0x00), const QColor &quoteDepth3=QColor(0x00, 0x80, 0x00))
Use this static method to get proper highlighting for a single line.
Definition: emailquotehighlighter.cpp:80
QString::length
int length() const
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:23 by doxygen 1.8.7 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 API Reference

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

Search



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