00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 #ifdef HAVE_LIBKSPELL2
00024
00025 #include "kobgspellcheck.h"
00026 #include "kobgspellcheck.moc"
00027 #include "kotextparag.h"
00028
00029 #include "kospell.h"
00030
00031 #include "kotextobject.h"
00032 #include "kotextdocument.h"
00033
00034
00035 #include <kspell2/backgroundchecker.h>
00036 #include <kspell2/broker.h>
00037 #include <kspell2/dictionary.h>
00038 #include <kspell2/settings.h>
00039 #include <kspell2/filter.h>
00040 using namespace KSpell2;
00041
00042 #include <klocale.h>
00043 #include <kdebug.h>
00044 #include <kdeversion.h>
00045 #include <qtimer.h>
00046 #include <qptrdict.h>
00047
00048
00049
00050 class KoBgSpellCheck::Private
00051 {
00052 public:
00053 int marked;
00054 KoSpell *backSpeller;
00055 QPtrDict<KoTextParag> paragCache;
00056 bool startupChecking;
00057 };
00058
00059 static const int delayAfterMarked = 10;
00060
00061 KoBgSpellCheck::KoBgSpellCheck( const Broker::Ptr& broker, QObject *parent,
00062 const char *name )
00063 : QObject( parent, name )
00064 {
00065 #ifdef DEBUG_BGSPELLCHECKING
00066 kdDebug(32500) << "KoBgSpellCheck::KoBgSpellCheck " << this << endl;
00067 #endif
00068 d = new Private;
00069 d->startupChecking = false;
00070 d->marked = 0;
00071 d->backSpeller = new KoSpell( broker, this, "KoSpell" );
00072
00073 connect( d->backSpeller, SIGNAL(misspelling(const QString&, int)),
00074 SLOT(spellCheckerMisspelling(const QString &, int )) );
00075 connect( d->backSpeller, SIGNAL(done()),
00076 SLOT(spellCheckerDone()) );
00077 connect( d->backSpeller, SIGNAL(aboutToFeedText()),
00078 SLOT(slotClearPara()) );
00079 }
00080
00081 KoBgSpellCheck::~KoBgSpellCheck()
00082 {
00083 delete d; d = 0;
00084 }
00085
00086 void KoBgSpellCheck::registerNewTextObject( KoTextObject *obj )
00087 {
00088 Q_ASSERT( obj );
00089
00090 connect( obj, SIGNAL(paragraphCreated(KoTextParag*)),
00091 SLOT(slotParagraphCreated(KoTextParag*)) );
00092 connect( obj, SIGNAL(paragraphModified(KoTextParag*, int, int, int)),
00093 SLOT(slotParagraphModified(KoTextParag*, int, int, int)) );
00094 connect( obj, SIGNAL(paragraphDeleted(KoTextParag*)),
00095 SLOT(slotParagraphDeleted(KoTextParag*)) );
00096 }
00097
00098 void KoBgSpellCheck::setEnabled( bool b )
00099 {
00100 d->backSpeller->settings()->setBackgroundCheckerEnabled( b );
00101 if ( b )
00102 start();
00103 else
00104 stop();
00105 }
00106
00107 bool KoBgSpellCheck::enabled() const
00108 {
00109 return d->backSpeller->settings()->backgroundCheckerEnabled();
00110 }
00111
00112 void KoBgSpellCheck::start()
00113 {
00114 if ( !enabled() )
00115 return;
00116
00117 d->startupChecking = true;
00118 d->marked = 0;
00119 KoTextIterator *itr = createWholeDocIterator();
00120 d->backSpeller->check( itr );
00121 d->backSpeller->start();
00122 }
00123
00124 void KoBgSpellCheck::spellCheckerMisspelling( const QString &old, int pos )
00125 {
00126 KoTextParag* parag = d->backSpeller->currentParag();
00127 #ifdef DEBUG_BGSPELLCHECKING
00128 kdDebug(32500) << "KoBgSpellCheck::spellCheckerMisspelling parag=" << parag
00129 << " (id=" << parag->paragId() << ", length="
00130 << parag->length() << ") pos=" << pos << " length="
00131 << old.length() << endl;
00132 #endif
00133 markWord( parag, pos, old.length(), true );
00134
00135
00136 parag->document()->emitRepaintChanged();
00137
00138 if ( d->startupChecking && d->marked > delayAfterMarked ) {
00139 d->marked = 0;
00140 QTimer::singleShot( 1000, this, SLOT(checkerContinue()) );
00141 } else {
00142 if ( d->startupChecking )
00143 ++d->marked;
00144 checkerContinue();
00145 }
00146 }
00147
00148 void KoBgSpellCheck::markWord( KoTextParag* parag, int pos, int length, bool misspelled )
00149 {
00150 if ( pos >= parag->length() ) {
00151 kdDebug(32500) << "markWord: " << pos << " is out of parag (length=" << parag->length() << ")" << endl;
00152 return;
00153 }
00154
00155 KoTextStringChar *ch = parag->at( pos );
00156 KoTextFormat format( *ch->format() );
00157 format.setMisspelled( misspelled );
00158 #ifdef DEBUG_BGSPELLCHECKING
00159 kdDebug(32500) << "markWord: changing mark from " << pos << " length=" << length << " misspelled=" << misspelled << endl;
00160 #endif
00161 parag->setFormat( pos, length, &format, true, KoTextFormat::Misspelled );
00162 parag->setChanged( true );
00163
00164 }
00165
00166 void KoBgSpellCheck::checkerContinue()
00167 {
00168 d->backSpeller->continueChecking();
00169 }
00170
00171 void KoBgSpellCheck::spellCheckerDone()
00172 {
00173 d->startupChecking = false;
00174
00175 if ( d->paragCache.isEmpty() )
00176 return;
00177
00178 QPtrDictIterator<KoTextParag> itr( d->paragCache );
00179 KoTextParag *parag = d->paragCache.take( itr.currentKey() );
00180 #ifdef DEBUG_BGSPELLCHECKING
00181 kdDebug(32500) << "spellCheckerDone : " << parag << ", cache = "<< d->paragCache.count() <<endl;
00182 #endif
00183 d->backSpeller->check( parag );
00184 }
00185
00186 void KoBgSpellCheck::stop()
00187 {
00188 #ifdef DEBUG_BGSPELLCHECKING
00189 kdDebug(32500) << "KoBgSpellCheck::stopSpellChecking" << endl;
00190 #endif
00191 d->backSpeller->stop();
00192 }
00193
00194 void KoBgSpellCheck::slotParagraphCreated( KoTextParag* parag )
00195 {
00196 if ( d->backSpeller->check( parag ) ) {
00197 d->paragCache.insert( parag, parag );
00198 }
00199 }
00200
00201 void KoBgSpellCheck::slotParagraphModified( KoTextParag* parag, int ,
00202 int pos, int length )
00203 {
00204
00205 if ( d->backSpeller->checking() ) {
00206 d->paragCache.insert( parag, parag );
00207 return;
00208 }
00209 #ifdef DEBUG_BGSPELLCHECKING
00210 kdDebug(32500) << "Para modified " << parag << " pos = "<<pos<<", length = "<< length <<endl;
00211 #endif
00212
00213 #if KDE_VERSION > KDE_MAKE_VERSION(3,3,0)
00214 if ( length < 10 ) {
00215 QString str = parag->string()->stringToSpellCheck();
00217 Filter filter;
00218 filter.setBuffer( str );
00219
00220 filter.setCurrentPosition( QMAX( 0, pos - 2 ) );
00221 int curPos = filter.currentPosition();
00222 filter.setSettings( d->backSpeller->settings() );
00223
00224
00225
00226
00227 markWord( parag, curPos, parag->length() - curPos, false );
00228
00229 for ( Word w = filter.nextWord(); !w.end; w = filter.nextWord() ) {
00230 bool misspelling = !d->backSpeller->checkWord( w.word );
00231
00232 markWord( parag, w.start, w.word.length(), misspelling );
00233 }
00234 if ( parag->hasChanged() )
00235 parag->document()->emitRepaintChanged();
00236 #else
00237 if ( length < 3 ) {
00238 QString word;
00239 int start;
00240 bool misspelled = !d->backSpeller->checkWordInParagraph( parag, pos,
00241 word, start );
00242 markWord( parag, start, word.length(), misspelled );
00243 parag->document()->emitRepaintChanged();
00244 #endif
00245 } else
00246 {
00247 d->backSpeller->check( parag );
00248 }
00249 }
00250
00251 void KoBgSpellCheck::slotParagraphDeleted( KoTextParag* parag )
00252 {
00253 d->paragCache.take( parag );
00254 if ( parag == d->backSpeller->currentParag() )
00255 d->backSpeller->slotCurrentParagraphDeleted();
00256 }
00257
00258 void KoBgSpellCheck::slotClearPara()
00259 {
00260 KoTextParag *parag = d->backSpeller->currentParag();
00261
00262
00263
00264
00265
00266
00267
00268 KoTextStringChar *ch = parag->at( 0 );
00269 KoTextFormat format( *ch->format() );
00270 format.setMisspelled( false );
00271 #ifdef DEBUG_BGSPELLCHECKING
00272 kdDebug(32500) << "clearPara: resetting mark on paragraph " << parag->paragId() << endl;
00273 #endif
00274 parag->setFormat( 0, parag->length()-1, &format, true,
00275 KoTextFormat::Misspelled );
00276 parag->setChanged( true );
00277 parag->document()->emitRepaintChanged();
00278 }
00279
00280 KSpell2::Settings * KoBgSpellCheck::settings() const
00281 {
00282 return d->backSpeller->settings();
00283 }
00284
00285 #endif