24#include "KoBgSpellCheck.h"
25#include "KoBgSpellCheck.moc"
26#include "KoTextParag.h"
30#include "KoTextObject.h"
31#include "KoTextDocument.h"
34#include <tdespell2/backgroundchecker.h>
35#include <tdespell2/broker.h>
36#include <tdespell2/dictionary.h>
37#include <tdespell2/settings.h>
38#include <tdespell2/filter.h>
39using namespace KSpell2;
48class KoBgSpellCheck::Private
53 TQPtrDict<KoTextParag> paragCache;
55 KoTextParag* intraWordParag;
56 int intraWordPosition;
59static const int delayAfterMarked = 10;
61KoBgSpellCheck::KoBgSpellCheck(
const Broker::Ptr& broker, TQObject *parent,
63 : TQObject( parent,
name )
65#ifdef DEBUG_BGSPELLCHECKING
66 kdDebug(32500) <<
"KoBgSpellCheck::KoBgSpellCheck " <<
this << endl;
69 d->startupChecking =
false;
71 d->intraWordParag = 0;
72 d->intraWordPosition = 0;
73 d->backSpeller =
new KoSpell( broker,
this,
"KoSpell" );
75 connect( d->backSpeller, TQ_SIGNAL(misspelling(
const TQString&,
int)),
76 TQ_SLOT(spellCheckerMisspelling(
const TQString &,
int )) );
77 connect( d->backSpeller, TQ_SIGNAL(done()),
78 TQ_SLOT(spellCheckerDone()) );
79 connect( d->backSpeller, TQ_SIGNAL(aboutToFeedText()),
80 TQ_SLOT(slotClearPara()) );
83KoBgSpellCheck::~KoBgSpellCheck()
88void KoBgSpellCheck::registerNewTextObject(
KoTextObject *obj )
92 connect( obj, TQ_SIGNAL(paragraphCreated(KoTextParag*)),
93 TQ_SLOT(slotParagraphCreated(KoTextParag*)) );
94 connect( obj, TQ_SIGNAL(paragraphModified(KoTextParag*,
int,
int,
int)),
95 TQ_SLOT(slotParagraphModified(KoTextParag*,
int,
int,
int)) );
96 connect( obj, TQ_SIGNAL(paragraphDeleted(KoTextParag*)),
97 TQ_SLOT(slotParagraphDeleted(KoTextParag*)) );
100void KoBgSpellCheck::setEnabled(
bool b )
102 d->backSpeller->settings()->setBackgroundCheckerEnabled( b );
109bool KoBgSpellCheck::enabled()
const
111 return d->backSpeller->settings()->backgroundCheckerEnabled();
114void KoBgSpellCheck::start()
119 d->startupChecking =
true;
121 KoTextIterator *itr = createWholeDocIterator();
122 d->backSpeller->check( itr );
123 d->backSpeller->start();
126void KoBgSpellCheck::spellCheckerMisspelling(
const TQString &old,
int pos )
128 KoTextParag* parag = d->backSpeller->currentParag();
129#ifdef DEBUG_BGSPELLCHECKING
130 kdDebug(32500) <<
"KoBgSpellCheck::spellCheckerMisspelling parag=" << parag
131 <<
" (id=" << parag->paragId() <<
", length="
132 << parag->length() <<
") pos=" << pos <<
" length="
133 << old.length() << endl;
135 markWord( parag, pos, old.length(),
true );
138 parag->document()->emitRepaintChanged();
140 if ( d->startupChecking && d->marked > delayAfterMarked ) {
142 TQTimer::singleShot( 1000,
this, TQ_SLOT(checkerContinue()) );
144 if ( d->startupChecking )
150void KoBgSpellCheck::markWord( KoTextParag* parag,
int pos,
int length,
bool misspelled )
152 if ( pos >= parag->length() ) {
153 kdDebug(32500) <<
"markWord: " << pos <<
" is out of parag (length=" << parag->length() <<
")" << endl;
156 if ( misspelled && parag == d->intraWordParag &&
157 d->intraWordPosition >= pos &&
158 d->intraWordPosition < pos+length ) {
159#ifdef DEBUG_BGSPELLCHECKING
160 kdDebug(32500) <<
"markWord: " << parag <<
" " << pos <<
" to " << pos+length <<
" - word being edited" << endl;
165 KoTextStringChar *ch = parag->at( pos );
166 KoTextFormat format( *ch->format() );
167 format.setMisspelled( misspelled );
168#ifdef DEBUG_BGSPELLCHECKING
169 kdDebug(32500) <<
"markWord: changing mark from " << pos <<
" length=" << length <<
" misspelled=" << misspelled << endl;
171 parag->setFormat( pos, length, &format,
true, KoTextFormat::Misspelled );
172 parag->setChanged(
true );
176void KoBgSpellCheck::checkerContinue()
179 d->backSpeller->continueChecking();
182void KoBgSpellCheck::spellCheckerDone()
184 d->startupChecking =
false;
186 if ( d->paragCache.isEmpty() )
189 TQPtrDictIterator<KoTextParag> itr( d->paragCache );
190 KoTextParag *parag = d->paragCache.take( itr.currentKey() );
191#ifdef DEBUG_BGSPELLCHECKING
192 kdDebug(32500) <<
"spellCheckerDone : " << parag <<
", cache = "<< d->paragCache.count() <<endl;
194 d->backSpeller->check( parag );
197void KoBgSpellCheck::stop()
199#ifdef DEBUG_BGSPELLCHECKING
200 kdDebug(32500) <<
"KoBgSpellCheck::stopSpellChecking" << endl;
202 d->backSpeller->stop();
205void KoBgSpellCheck::slotParagraphCreated( KoTextParag* parag )
207 parag->string()->setNeedsSpellCheck(
true );
210 if ( !d->backSpeller->check( parag ) ) {
211 d->paragCache.insert( parag, parag );
215void KoBgSpellCheck::slotParagraphModified( KoTextParag* parag,
int ,
216 int pos,
int length )
218 parag->string()->setNeedsSpellCheck(
true );
222 if ( d->backSpeller->checking() ) {
223 d->paragCache.insert( parag, parag );
226#ifdef DEBUG_BGSPELLCHECKING
227 kdDebug(32500) <<
"Para modified " << parag <<
" pos = "<<pos<<
", length = "<< length <<endl;
231 TQString str = parag->string()->stringToSpellCheck();
234 filter.setBuffer( str );
236 filter.setCurrentPosition( TQMAX( 0, pos - 2 ) );
237 int curPos = filter.currentPosition();
239 filter.setSettings( d->backSpeller->settings() );
244 markWord( parag, curPos, parag->length() - curPos,
false );
246 for ( Word w = filter.nextWord(); !w.end; w = filter.nextWord() ) {
247 bool misspelling = !d->backSpeller->checkWord( w.word );
249 markWord( parag, w.start, w.word.length(), misspelling );
251 if ( parag->hasChanged() )
252 parag->document()->emitRepaintChanged();
255 d->backSpeller->check( parag );
259void KoBgSpellCheck::slotParagraphDeleted( KoTextParag* parag )
261 d->paragCache.take( parag );
262 if ( parag == d->intraWordParag )
263 d->intraWordParag = 0;
270void KoBgSpellCheck::slotClearPara()
272 KoTextParag *parag = d->backSpeller->currentParag();
280 KoTextStringChar *ch = parag->at( 0 );
281 KoTextFormat format( *ch->format() );
282 format.setMisspelled(
false );
283#ifdef DEBUG_BGSPELLCHECKING
284 kdDebug(32500) <<
"clearPara: resetting mark on paragraph " << parag->paragId() << endl;
286 parag->setFormat( 0, parag->length()-1, &format,
true,
287 KoTextFormat::Misspelled );
288 parag->setChanged(
true );
289 parag->document()->emitRepaintChanged();
292KSpell2::Settings * KoBgSpellCheck::settings()
const
294 return d->backSpeller->settings();
297void KoBgSpellCheck::setIntraWordEditing( KoTextParag* parag,
int index )
299 KoTextParag* oldIntraWordParag = d->intraWordParag;
300 int oldIntraWordPosition = d->intraWordPosition;
302 d->intraWordParag = parag;
303 d->intraWordPosition = index;
305 if ( oldIntraWordParag && !parag ) {
308 slotParagraphModified( oldIntraWordParag, 0 , oldIntraWordPosition, 1 );
KOffice spell checking object Used for both spell-checking-with-a-dialog (directly) and background sp...
The KoTextObject is the high-level object that contains a KoTextDocument (the list of paragraphs),...
const char * name(StdAction id)