lib Library API Documentation

kospell.cc

00001 /* This file is part of KOffice
00002    Copyright (C) 2004 Zack Rusin <zack@kde.org>
00003 
00004    Original version written by David Sweet <dsweet@kde.org> and
00005          Wolfram Diestel <wolfram@steloj.de>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License version 2 as published by the Free Software Foundation.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include "config.h"
00023 #ifdef HAVE_LIBKSPELL2
00024 #include "kospell.h"
00025 
00026 #include "kotextobject.h"
00027 #include "kotextparag.h"
00028 #include "kotextiterator.h"
00029 
00030 #include <kspell2/broker.h>
00031 #include <kspell2/filter.h>
00032 
00033 #include <kglobal.h>
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036 
00037 #include <qtimer.h>
00038 
00039 using namespace KSpell2;
00040 
00041 class KoSpell::Private
00042 {
00043 public:
00044     KoTextIterator *itr;
00045     KoTextParag    *parag;
00046     bool            dialog;
00047     bool            needsIncrement;
00048     KoTextDocument *lastTxtDocument;
00049 };
00050 
00051 KoSpell::KoSpell( const Broker::Ptr& broker,  QObject *parent,
00052                   const char *name )
00053     : BackgroundChecker( broker, parent, name )
00054 {
00055     d = new Private;
00056     d->parag = 0;
00057     d->itr = 0;
00058     d->dialog = false;
00059     d->needsIncrement = false;
00060     d->lastTxtDocument = 0;
00061 }
00062 
00063 KoSpell::~KoSpell()
00064 {
00065     delete d;
00066 }
00067 
00068 bool KoSpell::check( KoTextIterator *itr, bool dialog )
00069 {
00070     bool ret = false;
00071 
00072     if ( !itr )
00073         return ret;
00074 
00075     d->itr = itr;
00076     connect( d->itr, SIGNAL( currentParagraphDeleted() ), SLOT( slotCurrentParagraphDeleted() ) );
00077     d->lastTxtDocument = d->itr->currentTextObject()->textDocument();
00078     d->needsIncrement = false;
00079     ret = !d->itr->atEnd();
00080     d->dialog = dialog;
00081 
00082     return ret;
00083 }
00084 
00085 bool KoSpell::check( KoTextParag *parag )
00086 {
00087     if ( checking() || !parag )
00088         return false;
00089 
00090     d->parag = parag;
00091     d->lastTxtDocument = d->parag->textDocument();
00092 
00093     start();
00094 
00095     return true;
00096 }
00097 
00098 bool KoSpell::checkWordInParagraph( KoTextParag *parag, int pos,
00099                                     QString& foundWord, int& start )
00100 {
00101     if ( !parag ) {
00102         start = -1;
00103         return false;
00104     }
00105 
00106     d->parag = parag;
00107     d->lastTxtDocument = d->parag->textDocument();
00108     QString str = parag->string()->stringToSpellCheck();
00110     Filter filter;
00111     filter.setBuffer( str );
00112     filter.setSettings( broker()->settings() );
00113     Word w = filter.wordAtPosition( pos );
00114     foundWord = w.word;
00115     start = w.start;
00116 #ifdef DEBUG_SPELL
00117     kdDebug()<<"XXXXX WORD IS " << w.word << ", pos = "<< pos
00118              << ", start = "<< w.start <<endl;
00119 #endif
00120     return checkWord( w.word );
00121 }
00122 
00123 QString KoSpell::getMoreText()
00124 {
00125 #ifdef DEBUG_SPELL
00126     kdDebug()<<"here 1 dialog = " << d->dialog << ", itr = "
00127              << d->itr << ", atEnd = "
00128              << ( ( d->itr ) ? d->itr->atEnd() : true )
00129              << endl;
00130 #endif
00131 
00132     if ( d->needsIncrement && d->itr && !d->itr->atEnd() ) {
00133         ++( *d->itr );
00134         if ( !d->itr->atEnd() )
00135             d->lastTxtDocument = d->itr->currentTextObject()->textDocument();
00136     }
00137 
00138     bool iteratorAtEnd = d->itr && d->itr->atEnd();
00139 
00140     if ( !d->dialog && ( !d->itr || iteratorAtEnd ) ) {
00141         QString str = d->parag ? d->parag->string()->stringToSpellCheck() : QString::null;
00142         if ( !str.isEmpty() )
00143             emit aboutToFeedText();
00144         return str;
00145     }
00146 
00147     if ( iteratorAtEnd )
00148         return QString::null;
00149 
00150     d->needsIncrement = true;
00151 
00152     QString text = d->itr->currentText();
00153     d->parag = d->itr->currentParag();
00154 
00155     emit aboutToFeedText();
00156     //kdDebug()<<"here 2"<<endl;
00157     while ( !d->dialog && d->parag ) {
00158         if ( d->parag->string()->needsSpellCheck() &&
00159              d->parag->length() > 1 )
00160             break;
00161         ++(*d->itr);
00162         if ( d->itr->atEnd() ) {
00163             d->needsIncrement = false;
00164             return QString::null;
00165         }
00166         d->parag = d->itr->currentParag();
00167         d->lastTxtDocument = d->parag->textDocument();
00168         text = d->itr->currentText();
00169     }
00170 
00171     d->parag->string()->setNeedsSpellCheck( false );
00172 
00173     return text;
00174 }
00175 
00176 void KoSpell::finishedCurrentFeed()
00177 {
00178     emit paragraphChecked( d->parag );
00179 }
00180 
00181 KoTextParag  *KoSpell::currentParag() const
00182 {
00183     return d->parag;
00184 }
00185 
00186 KoTextObject *KoSpell::currentTextObject() const
00187 {
00188     if ( d->itr && !d->itr->atEnd() )
00189         return d->itr->currentTextObject();
00190     return 0;
00191 }
00192 
00193 int KoSpell::currentStartIndex() const
00194 {
00195     if ( d->itr && !d->itr->atEnd() )
00196         return d->itr->currentStartIndex();
00197     return 0;
00198 }
00199 
00200 void KoSpell::slotCurrentParagraphDeleted()
00201 {
00202     stop();
00203     if ( d->itr ) {
00204         d->needsIncrement = false;
00205         d->parag = d->itr->currentParag();
00206         start();
00207     } else {
00208         d->parag = 0;
00209     }
00210 }
00211 
00212 bool KoSpell::checking() const
00213 {
00214 #ifdef DEBUG_SPELL
00215     kdDebug()<< d->itr << ", "
00216              << ( ( d->itr ) ? d->itr->atEnd() : false ) << ", "
00217              << filter()->atEnd()
00218              << endl;
00219 #endif
00220     if ( d->itr ) {
00221         if ( d->itr->atEnd() &&
00222              filter()->atEnd() )
00223             return false;
00224         else
00225             return true;
00226     } else
00227         return !filter()->atEnd();
00228 }
00229 
00230 KoTextDocument * KoSpell::textDocument() const
00231 {
00232     return d->lastTxtDocument;
00233 }
00234 
00235 KSpell2::Settings * KoSpell::settings() const
00236 {
00237     return broker()->settings();
00238 }
00239 
00240 #include "kospell.moc"
00241 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:07 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003