lib

KoSpell.cpp
1/* This file is part of KOffice
2 Copyright (C) 2004 Zack Rusin <zack@kde.org>
3
4 Original version written by David Sweet <dsweet@kde.org> and
5 Wolfram Diestel <wolfram@steloj.de>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License version 2 as published by the Free Software Foundation.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "config.h"
23#include "KoSpell.h"
24
25#include "KoTextObject.h"
26#include "KoTextParag.h"
27#include "KoTextIterator.h"
28
29#include <tdespell2/broker.h>
30#include <tdespell2/filter.h>
31
32#include <tdeglobal.h>
33#include <tdelocale.h>
34#include <kdebug.h>
35
36#include <tqtimer.h>
37
38//#define DEBUG_SPELL
39
40using namespace KSpell2;
41
42class KoSpell::Private
43{
44public:
45 KoTextIterator *itr;
46 KoTextParag *parag;
47 bool dialog;
48 bool needsIncrement;
49 KoTextDocument *lastTxtDocument;
50};
51
52KoSpell::KoSpell( const Broker::Ptr& broker, TQObject *parent,
53 const char *name )
54 : BackgroundChecker( broker, parent, name )
55{
56 d = new Private;
57 d->parag = 0;
58 d->itr = 0;
59 d->dialog = false;
60 d->needsIncrement = false;
61 d->lastTxtDocument = 0;
62}
63
65{
66 delete d;
67}
68
69bool KoSpell::check( KoTextIterator *itr, bool dialog )
70{
71 bool ret = false;
72
73 if ( !itr )
74 return ret;
75
76 d->itr = itr;
77 connect( d->itr, TQ_SIGNAL( currentParagraphDeleted() ), TQ_SLOT( slotCurrentParagraphDeleted() ) );
78 d->lastTxtDocument = d->itr->currentTextObject()->textDocument();
79 d->needsIncrement = false;
80 ret = !d->itr->atEnd();
81 d->dialog = dialog;
82
83 return ret;
84}
85
86bool KoSpell::check( KoTextParag *parag )
87{
88 if ( checking() || !parag )
89 return false;
90
91 d->parag = parag;
92 d->lastTxtDocument = d->parag->textDocument();
93
94 start();
95
96 return true;
97}
98
99bool KoSpell::checkWordInParagraph( KoTextParag *parag, int pos,
100 TQString& foundWord, int& start )
101{
102 if ( !parag ) {
103 start = -1;
104 return false;
105 }
106
107 d->parag = parag;
108 d->lastTxtDocument = d->parag->textDocument();
109 TQString str = parag->string()->stringToSpellCheck();
111 Filter filter;
112 filter.setBuffer( str );
113 filter.setSettings( broker()->settings() );
114 Word w = filter.wordAtPosition( pos );
115 foundWord = w.word;
116 start = w.start;
117#ifdef DEBUG_SPELL
118 kdDebug()<<"KoSpell: WORD IS " << w.word << ", pos = "<< pos
119 << ", start = "<< w.start <<endl;
120#endif
121 return checkWord( w.word );
122}
123
124TQString KoSpell::getMoreText()
125{
126#ifdef DEBUG_SPELL
127 kdDebug()<<"getMoreText: dialog = " << d->dialog << ", itr = "
128 << d->itr << ", atEnd = "
129 << ( ( d->itr ) ? d->itr->atEnd() : true )
130 << " d->parag=" << d->parag
131 << endl;
132#endif
133
134 if ( d->needsIncrement && d->itr && !d->itr->atEnd() ) {
135 ++( *d->itr );
136 if ( !d->itr->atEnd() )
137 d->lastTxtDocument = d->itr->currentTextObject()->textDocument();
138 }
139
140 if ( d->itr && d->itr->atEnd() )
141 return TQString();
142
143 if ( !d->dialog && !d->itr ) {
144 TQString str = d->parag ? d->parag->string()->stringToSpellCheck() : TQString();
145 if ( !str.isEmpty() )
146 emit aboutToFeedText();
147 return str;
148 }
149
150 d->needsIncrement = true;
151
152 TQString text = d->itr->currentText();
153 d->parag = d->itr->currentParag();
154
155 emit aboutToFeedText();
156 //kdDebug()<<"here 2"<<endl;
157 while ( !d->dialog && d->parag ) {
158 if ( d->parag->string()->needsSpellCheck() &&
159 d->parag->length() > 1 )
160 break;
161 ++(*d->itr);
162 if ( d->itr->atEnd() ) {
163 d->needsIncrement = false;
164 return TQString();
165 }
166 d->parag = d->itr->currentParag();
167 d->lastTxtDocument = d->parag->textDocument();
168 text = d->itr->currentText();
169 }
170
171 d->parag->string()->setNeedsSpellCheck( false );
172
173 return text;
174}
175
176void KoSpell::finishedCurrentFeed()
177{
178 emit paragraphChecked( d->parag );
179}
180
181KoTextParag *KoSpell::currentParag() const
182{
183 return d->parag;
184}
185
186KoTextObject *KoSpell::currentTextObject() const
187{
188 if ( d->itr && !d->itr->atEnd() )
189 return d->itr->currentTextObject();
190 return 0;
191}
192
193int KoSpell::currentStartIndex() const
194{
195 if ( d->itr && !d->itr->atEnd() )
196 return d->itr->currentStartIndex();
197 return 0;
198}
199
200void KoSpell::slotCurrentParagraphDeleted()
201{
202#ifdef DEBUG_SPELL
203 kdDebug() << "KoSpell::slotCurrentParagraphDeleted itr=" << d->itr << endl;
204#endif
205 stop();
206 if ( d->itr ) {
207 d->needsIncrement = false;
208 d->parag = d->itr->currentParag();
209#ifdef DEBUG_SPELL
210 kdDebug() << "KoSpell::slotCurrentParagraphDeleted d->parag=" << d->parag << endl;
211#endif
212 start();
213 } else {
214 d->parag = 0;
215 }
216}
217
219{
220#ifdef DEBUG_SPELL
221 kdDebug()<< "KoSpell::checking: itr=" << d->itr
222 << ", atEnd=" << ( ( d->itr ) ? d->itr->atEnd() : false )
223 << ", filter()->atEnd()=" << filter()->atEnd()
224 << endl;
225#endif
226 if ( d->itr ) {
227 if ( d->itr->atEnd() &&
228 filter()->atEnd() )
229 return false;
230 else
231 return true;
232 } else
233 return !filter()->atEnd();
234}
235
236KoTextDocument * KoSpell::textDocument() const
237{
238 return d->lastTxtDocument;
239}
240
241KSpell2::Settings * KoSpell::settings() const
242{
243 return broker()->settings();
244}
245
246#include "KoSpell.moc"
bool checking() const
Returns whether the speller is already checking something.
Definition KoSpell.cpp:218
void paragraphChecked(KoTextParag *)
Emitted after a paragraph has been checked.
virtual bool check(KoTextIterator *itr, bool dialog=false)
Spellchecks a buffer of many words in plain text format.
Definition KoSpell.cpp:69
KSpell2::Settings * settings() const
Returns the Settings object used by the broker.
Definition KoSpell.cpp:241
virtual ~KoSpell()
The destructor instructs ISpell/ASpell to write out the personal dictionary and then terminates ISpel...
Definition KoSpell.cpp:64
virtual bool checkWordInParagraph(KoTextParag *parag, int pos, TQString &word, int &start)
Definition KoSpell.cpp:99
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)