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 <tdeversion.h>
44#include <tqtimer.h>
45#include <tqptrdict.h>
46
47// #define DEBUG_BGSPELLCHECKING
48
49class KoBgSpellCheck::Private
50{
51public:
52 int marked;
53 KoSpell *backSpeller;
54 TQPtrDict<KoTextParag> paragCache;
55 bool startupChecking;
56 KoTextParag* intraWordParag;
57 int intraWordPosition;
58};
59
60static const int delayAfterMarked = 10;
61
62KoBgSpellCheck::KoBgSpellCheck( const Broker::Ptr& broker, TQObject *parent,
63 const char *name )
64 : TQObject( parent, name )
65{
66#ifdef DEBUG_BGSPELLCHECKING
67 kdDebug(32500) << "KoBgSpellCheck::KoBgSpellCheck " << this << endl;
68#endif
69 d = new Private;
70 d->startupChecking = false;
71 d->marked = 0;
72 d->intraWordParag = 0;
73 d->intraWordPosition = 0;
74 d->backSpeller = new KoSpell( broker, this, "KoSpell" );
75
76 connect( d->backSpeller, TQ_SIGNAL(misspelling(const TQString&, int)),
77 TQ_SLOT(spellCheckerMisspelling(const TQString &, int )) );
78 connect( d->backSpeller, TQ_SIGNAL(done()),
79 TQ_SLOT(spellCheckerDone()) );
80 connect( d->backSpeller, TQ_SIGNAL(aboutToFeedText()),
81 TQ_SLOT(slotClearPara()) );
82}
83
84KoBgSpellCheck::~KoBgSpellCheck()
85{
86 delete d; d = 0;
87}
88
89void KoBgSpellCheck::registerNewTextObject( KoTextObject *obj )
90{
91 Q_ASSERT( obj );
92
93 connect( obj, TQ_SIGNAL(paragraphCreated(KoTextParag*)),
94 TQ_SLOT(slotParagraphCreated(KoTextParag*)) );
95 connect( obj, TQ_SIGNAL(paragraphModified(KoTextParag*, int, int, int)),
96 TQ_SLOT(slotParagraphModified(KoTextParag*, int, int, int)) );
97 connect( obj, TQ_SIGNAL(paragraphDeleted(KoTextParag*)),
98 TQ_SLOT(slotParagraphDeleted(KoTextParag*)) );
99}
100
101void KoBgSpellCheck::setEnabled( bool b )
102{
103 d->backSpeller->settings()->setBackgroundCheckerEnabled( b );
104 if ( b )
105 start();
106 else
107 stop();
108}
109
110bool KoBgSpellCheck::enabled() const
111{
112 return d->backSpeller->settings()->backgroundCheckerEnabled();
113}
114
115void KoBgSpellCheck::start()
116{
117 if ( !enabled() )
118 return;
119
120 d->startupChecking = true;
121 d->marked = 0;
122 KoTextIterator *itr = createWholeDocIterator();
123 d->backSpeller->check( itr );
124 d->backSpeller->start();
125}
126
127void KoBgSpellCheck::spellCheckerMisspelling( const TQString &old, int pos )
128{
129 KoTextParag* parag = d->backSpeller->currentParag();
130#ifdef DEBUG_BGSPELLCHECKING
131 kdDebug(32500) << "KoBgSpellCheck::spellCheckerMisspelling parag=" << parag
132 << " (id=" << parag->paragId() << ", length="
133 << parag->length() << ") pos=" << pos << " length="
134 << old.length() << endl;
135#endif
136 markWord( parag, pos, old.length(), true );
137 // Repaint immediately, since the checking is timer-based (slow), it looks
138 // slow (chunky) if we only repaint once a paragraph is completely done.
139 parag->document()->emitRepaintChanged();
140
141 if ( d->startupChecking && d->marked > delayAfterMarked ) {
142 d->marked = 0;
143 TQTimer::singleShot( 1000, this, TQ_SLOT(checkerContinue()) );
144 } else {
145 if ( d->startupChecking )
146 ++d->marked;
147 checkerContinue();
148 }
149}
150
151void KoBgSpellCheck::markWord( KoTextParag* parag, int pos, int length, bool misspelled )
152{
153 if ( pos >= parag->length() ) {
154 kdDebug(32500) << "markWord: " << pos << " is out of parag (length=" << parag->length() << ")" << endl;
155 return;
156 }
157 if ( misspelled && parag == d->intraWordParag &&
158 d->intraWordPosition >= pos &&
159 d->intraWordPosition < pos+length ) {
160#ifdef DEBUG_BGSPELLCHECKING
161 kdDebug(32500) << "markWord: " << parag << " " << pos << " to " << pos+length << " - word being edited" << endl;
162#endif
163 return; // not yet
164 }
165
166 KoTextStringChar *ch = parag->at( pos );
167 KoTextFormat format( *ch->format() );
168 format.setMisspelled( misspelled );
169#ifdef DEBUG_BGSPELLCHECKING
170 kdDebug(32500) << "markWord: changing mark from " << pos << " length=" << length << " misspelled=" << misspelled << endl;
171#endif
172 parag->setFormat( pos, length, &format, true, KoTextFormat::Misspelled );
173 parag->setChanged( true );
174 // don't repaint here, in the slotParagraphModified case we want to repaint only once at the end
175}
176
177void KoBgSpellCheck::checkerContinue()
178{
179 if(enabled())
180 d->backSpeller->continueChecking();
181}
182
183void KoBgSpellCheck::spellCheckerDone()
184{
185 d->startupChecking = false;
186
187 if ( d->paragCache.isEmpty() )
188 return;
189
190 TQPtrDictIterator<KoTextParag> itr( d->paragCache );
191 KoTextParag *parag = d->paragCache.take( itr.currentKey() );
192#ifdef DEBUG_BGSPELLCHECKING
193 kdDebug(32500) << "spellCheckerDone : " << parag << ", cache = "<< d->paragCache.count() <<endl;
194#endif
195 d->backSpeller->check( parag );
196}
197
198void KoBgSpellCheck::stop()
199{
200#ifdef DEBUG_BGSPELLCHECKING
201 kdDebug(32500) << "KoBgSpellCheck::stopSpellChecking" << endl;
202#endif
203 d->backSpeller->stop();
204}
205
206void KoBgSpellCheck::slotParagraphCreated( KoTextParag* parag )
207{
208 parag->string()->setNeedsSpellCheck( true );
209 if ( !enabled() )
210 return;
211 if ( !d->backSpeller->check( parag ) ) {
212 d->paragCache.insert( parag, parag );
213 }
214}
215
216void KoBgSpellCheck::slotParagraphModified( KoTextParag* parag, int /*ParagModifyType*/,
217 int pos, int length )
218{
219 parag->string()->setNeedsSpellCheck( true );
220 if ( !enabled() )
221 return;
222
223 if ( d->backSpeller->checking() ) {
224 d->paragCache.insert( parag, parag );
225 return;
226 }
227#ifdef DEBUG_BGSPELLCHECKING
228 kdDebug(32500) << "Para modified " << parag << " pos = "<<pos<<", length = "<< length <<endl;
229#endif
230
231#if TDE_VERSION > TDE_MAKE_VERSION(3,3,0)
232 if ( length < 10 ) {
233 TQString str = parag->string()->stringToSpellCheck();
235 Filter filter;
236 filter.setBuffer( str );
237 // pos - 1 wasn't enough for the case a splitting a word into two misspelled halves
238 filter.setCurrentPosition( TQMAX( 0, pos - 2 ) );
239 int curPos = filter.currentPosition(); // Filter adjusted it by going back to the last word
240 //kdDebug() << "str='" << str << "' set position " << TQMAX(0, pos-2) << " got back curPos=" << curPos << endl;
241 filter.setSettings( d->backSpeller->settings() );
242
243 // Tricky: KSpell2::Filter::nextWord's behavior makes the for() loop skip ignored words,
244 // so it doesn't mark them as OK... So we need to clear the marks everywhere first.
245 // To avoid flickering the repainting is only done once, after checking the parag.
246 markWord( parag, curPos, parag->length() - curPos, false );
247
248 for ( Word w = filter.nextWord(); !w.end; w = filter.nextWord() ) {
249 bool misspelling = !d->backSpeller->checkWord( w.word );
250 //kdDebug()<<"Word = \""<< w.word<< "\" , misspelled = "<<misspelling<<endl;
251 markWord( parag, w.start, w.word.length(), misspelling );
252 }
253 if ( parag->hasChanged() ) // always true currently
254 parag->document()->emitRepaintChanged();
255#else
256 if ( length < 3 ) {
257 TQString word;
258 int start;
259 bool misspelled = !d->backSpeller->checkWordInParagraph( parag, pos,
260 word, start );
261 markWord( parag, start, word.length(), misspelled );
262 parag->document()->emitRepaintChanged();
263#endif
264 } else
265 {
266 d->backSpeller->check( parag );
267 }
268}
269
270void KoBgSpellCheck::slotParagraphDeleted( KoTextParag* parag )
271{
272 d->paragCache.take( parag );
273 if ( parag == d->intraWordParag )
274 d->intraWordParag = 0;
275
276 // don't do it here, let KoTextIterator do that after adjusting itself better...
277 //if ( parag == d->backSpeller->currentParag() )
278 // d->backSpeller->slotCurrentParagraphDeleted();
279}
280
281void KoBgSpellCheck::slotClearPara()
282{
283 KoTextParag *parag = d->backSpeller->currentParag();
284
285 // We remove any misspelled format from the paragraph
286 // - otherwise we'd never notice words being ok again :)
287 // (e.g. due to adding a word to the ignore list, not due to editing)
288 //
289 // TODO: do this all only if there was a format with 'misspelled' in the paragraph,
290 // to minimize repaints
291 KoTextStringChar *ch = parag->at( 0 );
292 KoTextFormat format( *ch->format() );
293 format.setMisspelled( false );
294#ifdef DEBUG_BGSPELLCHECKING
295 kdDebug(32500) << "clearPara: resetting mark on paragraph " << parag->paragId() << endl;
296#endif
297 parag->setFormat( 0, parag->length()-1, &format, true,
298 KoTextFormat::Misspelled );
299 parag->setChanged( true );
300 parag->document()->emitRepaintChanged();
301}
302
303KSpell2::Settings * KoBgSpellCheck::settings() const
304{
305 return d->backSpeller->settings();
306}
307
308void KoBgSpellCheck::setIntraWordEditing( KoTextParag* parag, int index )
309{
310 KoTextParag* oldIntraWordParag = d->intraWordParag;
311 int oldIntraWordPosition = d->intraWordPosition;
312
313 d->intraWordParag = parag;
314 d->intraWordPosition = index;
315
316 if ( oldIntraWordParag && !parag ) {
317 // When typing a letter into an existing word and then going somewhere else,
318 // we need to re-check that word - after moving d->intra* out of the way of course.
319 slotParagraphModified( oldIntraWordParag, 0 /*unused*/, oldIntraWordPosition, 1 );
320 }
321}
KOffice spell checking object Used for both spell-checking-with-a-dialog (directly) and background sp...
Definition KoSpell.h:46
Each character (KoTextStringChar) points to a KoTextFormat that defines the formatting of that charac...
A convenient way to iterate over paragraphs, possibly in multiple textobjects, with many options (fro...
The KoTextObject is the high-level object that contains a KoTextDocument (the list of paragraphs),...
const char * name(StdAction id)