lib

KoBgSpellCheck.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Zack Rusin <zack@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include "KoBgSpellCheck.h"
25#include "KoBgSpellCheck.moc"
26#include "KoTextParag.h"
27
28#include "KoSpell.h"
29
30#include "KoTextObject.h"
31#include "KoTextDocument.h"
32
33
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;
40
41#include <tdelocale.h>
42#include <kdebug.h>
43#include <tqtimer.h>
44#include <tqptrdict.h>
45
46// #define DEBUG_BGSPELLCHECKING
47
48class KoBgSpellCheck::Private
49{
50public:
51 int marked;
52 KoSpell *backSpeller;
53 TQPtrDict<KoTextParag> paragCache;
54 bool startupChecking;
55 KoTextParag* intraWordParag;
56 int intraWordPosition;
57};
58
59static const int delayAfterMarked = 10;
60
61KoBgSpellCheck::KoBgSpellCheck( const Broker::Ptr& broker, TQObject *parent,
62 const char *name )
63 : TQObject( parent, name )
64{
65#ifdef DEBUG_BGSPELLCHECKING
66 kdDebug(32500) << "KoBgSpellCheck::KoBgSpellCheck " << this << endl;
67#endif
68 d = new Private;
69 d->startupChecking = false;
70 d->marked = 0;
71 d->intraWordParag = 0;
72 d->intraWordPosition = 0;
73 d->backSpeller = new KoSpell( broker, this, "KoSpell" );
74
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()) );
81}
82
83KoBgSpellCheck::~KoBgSpellCheck()
84{
85 delete d; d = 0;
86}
87
88void KoBgSpellCheck::registerNewTextObject( KoTextObject *obj )
89{
90 Q_ASSERT( obj );
91
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*)) );
98}
99
100void KoBgSpellCheck::setEnabled( bool b )
101{
102 d->backSpeller->settings()->setBackgroundCheckerEnabled( b );
103 if ( b )
104 start();
105 else
106 stop();
107}
108
109bool KoBgSpellCheck::enabled() const
110{
111 return d->backSpeller->settings()->backgroundCheckerEnabled();
112}
113
114void KoBgSpellCheck::start()
115{
116 if ( !enabled() )
117 return;
118
119 d->startupChecking = true;
120 d->marked = 0;
121 KoTextIterator *itr = createWholeDocIterator();
122 d->backSpeller->check( itr );
123 d->backSpeller->start();
124}
125
126void KoBgSpellCheck::spellCheckerMisspelling( const TQString &old, int pos )
127{
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;
134#endif
135 markWord( parag, pos, old.length(), true );
136 // Repaint immediately, since the checking is timer-based (slow), it looks
137 // slow (chunky) if we only repaint once a paragraph is completely done.
138 parag->document()->emitRepaintChanged();
139
140 if ( d->startupChecking && d->marked > delayAfterMarked ) {
141 d->marked = 0;
142 TQTimer::singleShot( 1000, this, TQ_SLOT(checkerContinue()) );
143 } else {
144 if ( d->startupChecking )
145 ++d->marked;
146 checkerContinue();
147 }
148}
149
150void KoBgSpellCheck::markWord( KoTextParag* parag, int pos, int length, bool misspelled )
151{
152 if ( pos >= parag->length() ) {
153 kdDebug(32500) << "markWord: " << pos << " is out of parag (length=" << parag->length() << ")" << endl;
154 return;
155 }
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;
161#endif
162 return; // not yet
163 }
164
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;
170#endif
171 parag->setFormat( pos, length, &format, true, KoTextFormat::Misspelled );
172 parag->setChanged( true );
173 // don't repaint here, in the slotParagraphModified case we want to repaint only once at the end
174}
175
176void KoBgSpellCheck::checkerContinue()
177{
178 if(enabled())
179 d->backSpeller->continueChecking();
180}
181
182void KoBgSpellCheck::spellCheckerDone()
183{
184 d->startupChecking = false;
185
186 if ( d->paragCache.isEmpty() )
187 return;
188
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;
193#endif
194 d->backSpeller->check( parag );
195}
196
197void KoBgSpellCheck::stop()
198{
199#ifdef DEBUG_BGSPELLCHECKING
200 kdDebug(32500) << "KoBgSpellCheck::stopSpellChecking" << endl;
201#endif
202 d->backSpeller->stop();
203}
204
205void KoBgSpellCheck::slotParagraphCreated( KoTextParag* parag )
206{
207 parag->string()->setNeedsSpellCheck( true );
208 if ( !enabled() )
209 return;
210 if ( !d->backSpeller->check( parag ) ) {
211 d->paragCache.insert( parag, parag );
212 }
213}
214
215void KoBgSpellCheck::slotParagraphModified( KoTextParag* parag, int /*ParagModifyType*/,
216 int pos, int length )
217{
218 parag->string()->setNeedsSpellCheck( true );
219 if ( !enabled() )
220 return;
221
222 if ( d->backSpeller->checking() ) {
223 d->paragCache.insert( parag, parag );
224 return;
225 }
226#ifdef DEBUG_BGSPELLCHECKING
227 kdDebug(32500) << "Para modified " << parag << " pos = "<<pos<<", length = "<< length <<endl;
228#endif
229
230 if ( length < 10 ) {
231 TQString str = parag->string()->stringToSpellCheck();
233 Filter filter;
234 filter.setBuffer( str );
235 // pos - 1 wasn't enough for the case a splitting a word into two misspelled halves
236 filter.setCurrentPosition( TQMAX( 0, pos - 2 ) );
237 int curPos = filter.currentPosition(); // Filter adjusted it by going back to the last word
238 //kdDebug() << "str='" << str << "' set position " << TQMAX(0, pos-2) << " got back curPos=" << curPos << endl;
239 filter.setSettings( d->backSpeller->settings() );
240
241 // Tricky: KSpell2::Filter::nextWord's behavior makes the for() loop skip ignored words,
242 // so it doesn't mark them as OK... So we need to clear the marks everywhere first.
243 // To avoid flickering the repainting is only done once, after checking the parag.
244 markWord( parag, curPos, parag->length() - curPos, false );
245
246 for ( Word w = filter.nextWord(); !w.end; w = filter.nextWord() ) {
247 bool misspelling = !d->backSpeller->checkWord( w.word );
248 //kdDebug()<<"Word = \""<< w.word<< "\" , misspelled = "<<misspelling<<endl;
249 markWord( parag, w.start, w.word.length(), misspelling );
250 }
251 if ( parag->hasChanged() ) // always true currently
252 parag->document()->emitRepaintChanged();
253 } else
254 {
255 d->backSpeller->check( parag );
256 }
257}
258
259void KoBgSpellCheck::slotParagraphDeleted( KoTextParag* parag )
260{
261 d->paragCache.take( parag );
262 if ( parag == d->intraWordParag )
263 d->intraWordParag = 0;
264
265 // don't do it here, let KoTextIterator do that after adjusting itself better...
266 //if ( parag == d->backSpeller->currentParag() )
267 // d->backSpeller->slotCurrentParagraphDeleted();
268}
269
270void KoBgSpellCheck::slotClearPara()
271{
272 KoTextParag *parag = d->backSpeller->currentParag();
273
274 // We remove any misspelled format from the paragraph
275 // - otherwise we'd never notice words being ok again :)
276 // (e.g. due to adding a word to the ignore list, not due to editing)
277 //
278 // TODO: do this all only if there was a format with 'misspelled' in the paragraph,
279 // to minimize repaints
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;
285#endif
286 parag->setFormat( 0, parag->length()-1, &format, true,
287 KoTextFormat::Misspelled );
288 parag->setChanged( true );
289 parag->document()->emitRepaintChanged();
290}
291
292KSpell2::Settings * KoBgSpellCheck::settings() const
293{
294 return d->backSpeller->settings();
295}
296
297void KoBgSpellCheck::setIntraWordEditing( KoTextParag* parag, int index )
298{
299 KoTextParag* oldIntraWordParag = d->intraWordParag;
300 int oldIntraWordPosition = d->intraWordPosition;
301
302 d->intraWordParag = parag;
303 d->intraWordPosition = index;
304
305 if ( oldIntraWordParag && !parag ) {
306 // When typing a letter into an existing word and then going somewhere else,
307 // we need to re-check that word - after moving d->intra* out of the way of course.
308 slotParagraphModified( oldIntraWordParag, 0 /*unused*/, oldIntraWordPosition, 1 );
309 }
310}
KOffice spell checking object Used for both spell-checking-with-a-dialog (directly) and background sp...
Definition KoSpell.h:46
The KoTextObject is the high-level object that contains a KoTextDocument (the list of paragraphs),...
const char * name(StdAction id)