highlighter.cpp

00001 
00022 #include "highlighter.h"
00023 #include "broker.h"
00024 #include "dictionary.h"
00025 #include "settings.h"
00026 
00027 #include <kconfig.h>
00028 #include <kdebug.h>
00029 
00030 #include <qtextedit.h>
00031 #include <qtimer.h>
00032 #include <qcolor.h>
00033 #include <qdict.h>
00034 
00035 namespace KSpell2 {
00036 
00037 class Highlighter::Private
00038 {
00039 public:
00040     Filter     *filter;
00041     Broker::Ptr broker;
00042     Dictionary *dict;
00043     QDict<Dictionary>  dictCache;
00044 };
00045 
00046 Highlighter::Highlighter( QTextEdit *textEdit,
00047                           const QString& configFile,
00048                           Filter *filter)
00049     : QSyntaxHighlighter( textEdit )
00050 {
00051     d = new Private;
00052     d->filter = filter;
00053     if ( !configFile.isEmpty() )
00054         d->broker = Broker::openBroker( KSharedConfig::openConfig( configFile ) );
00055     else
00056         d->broker = Broker::openBroker();
00057 
00058     d->filter->setSettings( d->broker->settings() );
00059     d->dict   = d->broker->dictionary();
00060     Q_ASSERT( d->dict );
00061     d->dictCache.insert( d->broker->settings()->defaultLanguage(),
00062                          d->dict );
00063 }
00064 
00065 Highlighter::~Highlighter()
00066 {
00067     delete d; d = 0;
00068 }
00069 
00070 int Highlighter::highlightParagraph( const QString& text,
00071                                      int endStateOfLastPara )
00072 {
00073     Q_UNUSED( endStateOfLastPara );
00074     int para, index;
00075     textEdit()->getCursorPosition( &para, &index );
00076     const int lengthPosition = text.length() - 1;
00077 
00078     if ( index != lengthPosition ||
00079          ( lengthPosition > 0 && !text[lengthPosition-1].isLetter() ) ) {
00080         d->filter->setBuffer( text );
00081         Word w = d->filter->nextWord();
00082         while ( !w.end ) {
00083             if ( !d->dict->check( w.word ) ) {
00084                 setMisspelled( w.start, w.word.length() );
00085             } else
00086                 unsetMisspelled( w.start, w.word.length() );
00087             w = d->filter->nextWord();
00088         }
00089     }
00090     //QTimer::singleShot( 0, this, SLOT(checkWords()) );
00091 
00092     return 0;
00093 }
00094 
00095 Filter *Highlighter::currentFilter() const
00096 {
00097     return d->filter;
00098 }
00099 
00100 void Highlighter::setCurrentFilter( Filter *filter )
00101 {
00102     d->filter = filter;
00103     d->filter->setSettings( d->broker->settings() );
00104 }
00105 
00106 QString Highlighter::currentLanguage() const
00107 {
00108     return d->dict->language();
00109 }
00110 
00111 void Highlighter::setCurrentLanguage( const QString& lang )
00112 {
00113     if ( !d->dictCache.find( lang ) ) {
00114         Dictionary *dict = d->broker->dictionary( lang );
00115         if ( dict ) {
00116             d->dictCache.insert( lang, dict );
00117         } else {
00118             kdDebug()<<"No dictionary for \""
00119                      <<lang
00120                      <<"\" staying with the current language."
00121                      <<endl;
00122             return;
00123         }
00124     }
00125     d->dict = d->dictCache[lang];
00126 }
00127 
00128 void Highlighter::setMisspelled( int start, int count )
00129 {
00130     setFormat( start , count, Qt::red );
00131 }
00132 
00133 void Highlighter::unsetMisspelled( int start, int count )
00134 {
00135     setFormat( start, count, Qt::black );
00136 }
00137 
00138 /*
00139 void Highlighter::checkWords()
00140 {
00141     Word w = d->filter->nextWord();
00142     if ( !w.end ) {
00143         if ( !d->dict->check( w.word ) ) {
00144             setFormat( w.start, w.word.length(),
00145                        Qt::red );
00146         }
00147     }
00148 }*/
00149 
00150 }
KDE Home | KDE Accessibility Home | Description of Access Keys