summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoBgSpellCheck.cpp
blob: ae2f3668ef18216d5cf603ed541a8ed3c49894d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* This file is part of the KDE project
   Copyright (C) 2004 Zack Rusin <zack@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "KoBgSpellCheck.h"
#include "KoBgSpellCheck.moc"
#include "KoTextParag.h"

#include "KoSpell.h"

#include "KoTextObject.h"
#include "KoTextDocument.h"


#include <kspell2/backgroundchecker.h>
#include <kspell2/broker.h>
#include <kspell2/dictionary.h>
#include <kspell2/settings.h>
#include <kspell2/filter.h>
using namespace KSpell2;

#include <klocale.h>
#include <kdebug.h>
#include <kdeversion.h>
#include <tqtimer.h>
#include <tqptrdict.h>

// #define DEBUG_BGSPELLCHECKING

class KoBgSpellCheck::Private
{
public:
    int marked;
    KoSpell *backSpeller;
    TQPtrDict<KoTextParag> paragCache;
    bool startupChecking;
    KoTextParag* intraWordParag;
    int intraWordPosition;
};

static const int delayAfterMarked = 10;

KoBgSpellCheck::KoBgSpellCheck( const Broker::Ptr& broker, TQObject *tqparent,
                                const char *name )
    : TQObject( tqparent, name )
{
#ifdef DEBUG_BGSPELLCHECKING
    kdDebug(32500) << "KoBgSpellCheck::KoBgSpellCheck " << this << endl;
#endif
    d = new Private;
    d->startupChecking = false;
    d->marked = 0;
    d->intraWordParag = 0;
    d->intraWordPosition = 0;
    d->backSpeller = new KoSpell( broker, this, "KoSpell" );

    connect( d->backSpeller, TQT_SIGNAL(misspelling(const TQString&, int)),
             TQT_SLOT(spellCheckerMisspelling(const TQString &, int )) );
    connect( d->backSpeller, TQT_SIGNAL(done()),
             TQT_SLOT(spellCheckerDone()) );
    connect( d->backSpeller, TQT_SIGNAL(aboutToFeedText()),
             TQT_SLOT(slotClearPara()) );
}

KoBgSpellCheck::~KoBgSpellCheck()
{
    delete d; d = 0;
}

void KoBgSpellCheck::registerNewTextObject( KoTextObject *obj )
{
    Q_ASSERT( obj );

    connect( obj, TQT_SIGNAL(paragraphCreated(KoTextParag*)),
             TQT_SLOT(slotParagraphCreated(KoTextParag*)) );
    connect( obj, TQT_SIGNAL(paragraphModified(KoTextParag*, int, int, int)),
             TQT_SLOT(slotParagraphModified(KoTextParag*, int, int, int)) );
    connect( obj, TQT_SIGNAL(paragraphDeleted(KoTextParag*)),
             TQT_SLOT(slotParagraphDeleted(KoTextParag*)) );
}

void KoBgSpellCheck::setEnabled( bool b )
{
    d->backSpeller->settings()->setBackgroundCheckerEnabled( b );
    if ( b )
        start();
    else
        stop();
}

bool KoBgSpellCheck::enabled() const
{
    return d->backSpeller->settings()->backgroundCheckerEnabled();
}

void KoBgSpellCheck::start()
{
    if ( !enabled() )
        return;

    d->startupChecking = true;
    d->marked = 0;
    KoTextIterator *itr = createWholeDocIterator();
    d->backSpeller->check( itr );
    d->backSpeller->start();
}

void KoBgSpellCheck::spellCheckerMisspelling( const TQString &old, int pos )
{
    KoTextParag* parag = d->backSpeller->currentParag();
#ifdef DEBUG_BGSPELLCHECKING
    kdDebug(32500) << "KoBgSpellCheck::spellCheckerMisspelling parag=" << parag
                   << " (id=" << parag->paragId() << ", length="
                   << parag->length() << ") pos=" << pos << " length="
                   << old.length() << endl;
#endif
    markWord( parag, pos, old.length(), true );
    // Repaint immediately, since the checking is timer-based (slow), it looks
    // slow (chunky) if we only tqrepaint once a paragraph is completely done.
    parag->document()->emitRepaintChanged();

    if ( d->startupChecking && d->marked > delayAfterMarked ) {
        d->marked = 0;
        TQTimer::singleShot( 1000, this, TQT_SLOT(checkerContinue()) );
    } else {
        if ( d->startupChecking )
            ++d->marked;
        checkerContinue();
    }
}

void KoBgSpellCheck::markWord( KoTextParag* parag, int pos, int length, bool misspelled )
{
    if ( pos >= parag->length() ) {
        kdDebug(32500) << "markWord: " << pos << " is out of parag (length=" << parag->length() << ")" << endl;
        return;
    }
    if ( misspelled && parag == d->intraWordParag &&
         d->intraWordPosition >= pos &&
         d->intraWordPosition < pos+length ) {
#ifdef DEBUG_BGSPELLCHECKING
        kdDebug(32500) << "markWord: " << parag << " " << pos << " to " << pos+length << " - word being edited" << endl;
#endif
        return; // not yet
    }

    KoTextStringChar *ch = parag->at( pos );
    KoTextFormat format( *ch->format() );
    format.setMisspelled( misspelled );
#ifdef DEBUG_BGSPELLCHECKING
    kdDebug(32500) << "markWord: changing mark from " << pos << " length=" << length << " misspelled=" << misspelled << endl;
#endif
    parag->setFormat( pos, length, &format, true, KoTextFormat::Misspelled );
    parag->setChanged( true );
    // don't tqrepaint here, in the slotParagraphModified case we want to tqrepaint only once at the end
}

void KoBgSpellCheck::checkerContinue()
{
    if(enabled())
        d->backSpeller->continueChecking();
}

void KoBgSpellCheck::spellCheckerDone()
{
    d->startupChecking = false;

    if ( d->paragCache.isEmpty() )
        return;

    TQPtrDictIterator<KoTextParag> itr( d->paragCache );
    KoTextParag *parag = d->paragCache.take( itr.currentKey() );
#ifdef DEBUG_BGSPELLCHECKING
    kdDebug(32500) << "spellCheckerDone : " << parag << ", cache = "<< d->paragCache.count() <<endl;
#endif
    d->backSpeller->check( parag );
}

void KoBgSpellCheck::stop()
{
#ifdef DEBUG_BGSPELLCHECKING
  kdDebug(32500) << "KoBgSpellCheck::stopSpellChecking" << endl;
#endif
  d->backSpeller->stop();
}

void KoBgSpellCheck::slotParagraphCreated( KoTextParag* parag )
{
    parag->string()->setNeedsSpellCheck( true );
    if ( !enabled() )
        return;
    if ( !d->backSpeller->check( parag ) ) {
        d->paragCache.insert( parag, parag );
    }
}

void KoBgSpellCheck::slotParagraphModified( KoTextParag* parag, int /*ParagModifyType*/,
                                            int pos, int length )
{
    parag->string()->setNeedsSpellCheck( true );
    if ( !enabled() )
        return;

    if ( d->backSpeller->checking() ) {
        d->paragCache.insert( parag, parag );
        return;
    }
#ifdef DEBUG_BGSPELLCHECKING
    kdDebug(32500) << "Para modified " << parag << " pos = "<<pos<<", length = "<< length <<endl;
#endif

#if KDE_VERSION > KDE_MAKE_VERSION(3,3,0)
    if ( length < 10 ) {
        TQString str = parag->string()->stringToSpellCheck();
        /// ##### do we really need to create a Filter every time?
        Filter filter;
        filter.setBuffer( str );
        // pos - 1 wasn't enough for the case a splitting a word into two misspelled halves
        filter.setCurrentPosition( TQMAX( 0, pos - 2 ) );
        int curPos = filter.currentPosition(); // Filter adjusted it by going back to the last word
        //kdDebug() << "str='" << str << "' set position " << TQMAX(0, pos-2) << " got back curPos=" << curPos << endl;
        filter.setSettings( d->backSpeller->settings() );

        // Tricky: KSpell2::Filter::nextWord's behavior makes the for() loop skip ignored words,
        // so it doesn't mark them as OK... So we need to clear the marks everywhere first.
        // To avoid flickering the repainting is only done once, after checking the parag.
        markWord( parag, curPos, parag->length() - curPos, false );

        for ( Word w = filter.nextWord(); !w.end; w = filter.nextWord() ) {
            bool misspelling = !d->backSpeller->checkWord( w.word );
            //kdDebug()<<"Word = \""<< w.word<< "\" , misspelled = "<<misspelling<<endl;
            markWord( parag, w.start, w.word.length(), misspelling );
        }
        if ( parag->hasChanged() ) // always true currently
            parag->document()->emitRepaintChanged();
#else
    if ( length < 3 ) {
        TQString word;
        int start;
        bool misspelled = !d->backSpeller->checkWordInParagraph( parag, pos,
                                                                 word, start );
        markWord( parag, start, word.length(), misspelled );
        parag->document()->emitRepaintChanged();
#endif
    } else
    {
        d->backSpeller->check( parag );
    }
}

void KoBgSpellCheck::slotParagraphDeleted( KoTextParag* parag )
{
    d->paragCache.take( parag );
    if ( parag == d->intraWordParag )
        d->intraWordParag = 0;

    // don't do it here, let KoTextIterator do that after adjusting itself better...
    //if ( parag == d->backSpeller->currentParag() )
    //    d->backSpeller->slotCurrentParagraphDeleted();
}

void KoBgSpellCheck::slotClearPara()
{
    KoTextParag *parag = d->backSpeller->currentParag();

    // We remove any misspelled format from the paragraph
    // - otherwise we'd never notice words being ok again :)
    // (e.g. due to adding a word to the ignore list, not due to editing)
    //
    // TODO: do this all only if there was a format with 'misspelled' in the paragraph,
    // to minimize repaints
    KoTextStringChar *ch = parag->at( 0 );
    KoTextFormat format( *ch->format() );
    format.setMisspelled( false );
#ifdef DEBUG_BGSPELLCHECKING
    kdDebug(32500) << "clearPara: resetting mark on paragraph " << parag->paragId() << endl;
#endif
    parag->setFormat( 0, parag->length()-1, &format, true,
                      KoTextFormat::Misspelled );
    parag->setChanged( true );
    parag->document()->emitRepaintChanged();
}

KSpell2::Settings * KoBgSpellCheck::settings() const
{
    return d->backSpeller->settings();
}

void KoBgSpellCheck::setIntraWordEditing( KoTextParag* parag, int index )
{
    KoTextParag* oldIntraWordParag = d->intraWordParag;
    int oldIntraWordPosition = d->intraWordPosition;

    d->intraWordParag = parag;
    d->intraWordPosition = index;

    if ( oldIntraWordParag && !parag ) {
        // When typing a letter into an existing word and then going somewhere else,
        // we need to re-check that word - after moving d->intra* out of the way of course.
        slotParagraphModified( oldIntraWordParag, 0 /*unused*/, oldIntraWordPosition, 1 );
    }
}