F:/KPlato/koffice/libs/kotext/KoBgSpellCheck.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Zack Rusin <zack@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 // #define DEBUG_BGSPELLCHECKING
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     // Repaint immediately, since the checking is timer-based (slow), it looks
00137     // slow (chunky) if we only repaint once a paragraph is completely done.
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; // not yet
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     // don't repaint here, in the slotParagraphModified case we want to repaint only once at the end
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 /*ParagModifyType*/,
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         // pos - 1 wasn't enough for the case a splitting a word into two misspelled halves
00236         filter.setCurrentPosition( qMax( 0, pos - 2 ) );
00237         int curPos = filter.currentPosition(); // Filter adjusted it by going back to the last word
00238         //kDebug() << "str='" << str << "' set position " << qMax(0, pos-2) << " got back curPos=" << curPos << endl;
00239         filter.setSettings( d->backSpeller->settings() );
00240 
00241         // Tricky: KSpell2::Filter::nextWord's behavior makes the for() loop skip ignored words,
00242         // so it doesn't mark them as OK... So we need to clear the marks everywhere first.
00243         // To avoid flickering the repainting is only done once, after checking the parag.
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             //kDebug()<<"Word = \""<< w.word<< "\" , misspelled = "<<misspelling<<endl;
00249             markWord( parag, w.start, w.word.length(), misspelling );
00250         }
00251         if ( parag->hasChanged() ) // always true currently
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     // don't do it here, let KoTextIterator do that after adjusting itself better...
00266     //if ( parag == d->backSpeller->currentParag() )
00267     //    d->backSpeller->slotCurrentParagraphDeleted();
00268 }
00269 
00270 void KoBgSpellCheck::slotClearPara()
00271 {
00272     KoTextParag *parag = d->backSpeller->currentParag();
00273 
00274     // We remove any misspelled format from the paragraph
00275     // - otherwise we'd never notice words being ok again :)
00276     // (e.g. due to adding a word to the ignore list, not due to editing)
00277     //
00278     // TODO: do this all only if there was a format with 'misspelled' in the paragraph,
00279     // to minimize repaints
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         // When typing a letter into an existing word and then going somewhere else,
00307         // we need to re-check that word - after moving d->intra* out of the way of course.
00308         slotParagraphModified( oldIntraWordParag, 0 /*unused*/, oldIntraWordPosition, 1 );
00309     }
00310 }

Généré le Wed Nov 22 23:41:07 2006 pour KPlato par  doxygen 1.5.1-p1