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