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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/* This file is part of the KDE project
Copyright (C) 2002-2006 David Faure <faure@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 version 2 as published by the Free Software Foundation.
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.
*/
#include "KoTextIterator.h"
#include "KoTextParag.h"
#include "KoTextView.h"
#include <kfinddialog.h>
#include <kdebug.h>
#include <assert.h>
//#define DEBUG_ITERATOR
/**
* The search direction (forward or backward) is handled in a bit of a tricky way.
* m_firstParag/m_firstIndex is where the search starts, whichever the direction
* m_lastParag/m_lastIndex is where the search ends, whichever the direction
* But the list of textobjects is as given (we assume document order).
* So we go from the first to the last textobject, or from the last to the first textobject.
*/
void KoTextIterator::init( const TQValueList<KoTextObject *> & lstObjects, KoTextView* textView, int options )
{
Q_ASSERT( !lstObjects.isEmpty() );
m_lstObjects.clear();
m_firstParag = 0;
m_firstIndex = 0;
m_options = options;
// 'From Cursor' option
if ( options & KFindDialog::FromCursor )
{
if ( textView ) {
m_firstParag = textView->cursor()->parag();
m_firstIndex = textView->cursor()->index();
} else {
// !? FromCursor option can't work
m_options &= ~KFindDialog::FromCursor;
kdWarning(32500) << "FromCursor specified, but no textview?" << endl;
}
} // no else here !
bool forw = ! ( options & KFindDialog::FindBackwards );
// 'Selected Text' option
if ( textView && ( options & KFindDialog::SelectedText ) )
{
KoTextObject* textObj = textView->textObject();
KoTextCursor c1 = textObj->textDocument()->selectionStartCursor( KoTextDocument::Standard );
KoTextCursor c2 = textObj->textDocument()->selectionEndCursor( KoTextDocument::Standard );
if ( !m_firstParag ) // not from cursor
{
m_firstParag = forw ? c1.parag() : c2.parag();
m_firstIndex = forw ? c1.index() : c2.index();
}
m_lastParag = forw ? c2.parag() : c1.parag();
m_lastIndex = forw ? c2.index() : c1.index();
// Find in the selection only -> only one textobject
m_lstObjects.append( textObj );
m_currentTextObj = m_lstObjects.begin();
}
else
{
// Not "selected text" -> loop through all textobjects
m_lstObjects = lstObjects;
if ( textView && (options & KFindDialog::FromCursor) )
{
KoTextObject* initialFirst = m_lstObjects.first();
// textView->textObject() should be first in m_lstObjects (last when going backwards) !
// Let's ensure this is the case, but without changing the order of the objects.
if ( forw ) {
while( m_lstObjects.first() != textView->textObject() ) {
KoTextObject* textobj = m_lstObjects.front();
m_lstObjects.pop_front();
m_lstObjects.push_back( textobj );
if ( m_lstObjects.first() == initialFirst ) { // safety
kdWarning(32500) << "Didn't manage to find " << textView->textObject() << " in the list of textobjects!!!" << endl;
break;
}
}
} else {
while( m_lstObjects.last() != textView->textObject() ) {
KoTextObject* textobj = m_lstObjects.back();
m_lstObjects.pop_back();
m_lstObjects.push_front( textobj );
if ( m_lstObjects.first() == initialFirst ) { // safety
kdWarning(32500) << "Didn't manage to find " << textView->textObject() << " in the list of textobjects!!!" << endl;
break;
}
}
}
}
KoTextParag* firstParag = m_lstObjects.first()->textDocument()->firstParag();
int firstIndex = 0;
KoTextParag* lastParag = m_lstObjects.last()->textDocument()->lastParag();
int lastIndex = lastParag->length()-1;
if ( !m_firstParag ) // only set this when not 'from cursor'.
{
m_firstParag = forw ? firstParag : lastParag;
m_firstIndex = forw ? firstIndex : lastIndex;
}
// always set the ending point
m_lastParag = forw ? lastParag : firstParag;
m_lastIndex = forw ? lastIndex : firstIndex;
m_currentTextObj = forw ? m_lstObjects.begin() : m_lstObjects.fromLast();
}
assert( *m_currentTextObj ); // all branches set it
assert( m_firstParag );
assert( m_lastParag );
Q_ASSERT( (*m_currentTextObj)->isVisible() );
m_currentParag = m_firstParag;
#ifdef DEBUG_ITERATOR
kdDebug(32500) << "KoTextIterator::init from(" << *m_currentTextObj << "," << m_firstParag->paragId() << ") - to(" << (forw?m_lstObjects.last():m_lstObjects.first()) << "," << m_lastParag->paragId() << "), " << m_lstObjects.count() << " textObjects." << endl;
TQValueList<KoTextObject *>::Iterator it = m_lstObjects.begin();
for( ; it != m_lstObjects.end(); ++it )
kdDebug(32500) << (*it) << " " << (*it)->name() << endl;
#endif
Q_ASSERT( (*m_currentTextObj)->textDocument() == m_currentParag->textDocument() );
Q_ASSERT( (forw?m_lstObjects.last():m_lstObjects.first())->textDocument() == m_lastParag->textDocument() );
connectTextObjects();
}
void KoTextIterator::restart()
{
if( m_lstObjects.isEmpty() )
return;
m_currentParag = m_firstParag;
bool forw = ! ( m_options & KFindDialog::FindBackwards );
Q_ASSERT( ! (m_options & KFindDialog::FromCursor) ); // doesn't make much sense to keep it, right?
if ( (m_options & KFindDialog::FromCursor) || forw )
m_currentTextObj = m_lstObjects.begin();
else
m_currentTextObj = m_lstObjects.fromLast();
if ( !(*m_currentTextObj)->isVisible() )
nextTextObject();
#ifdef DEBUG_ITERATOR
if ( m_currentParag )
kdDebug(32500) << "KoTextIterator::restart from(" << *m_currentTextObj << "," << m_currentParag->paragId() << ") - to(" << (forw?m_lstObjects.last():m_lstObjects.first()) << "," << m_lastParag->paragId() << "), " << m_lstObjects.count() << " textObjects." << endl;
else
kdDebug(32500) << "KoTextIterator::restart - nowhere to go!" << endl;
#endif
}
void KoTextIterator::connectTextObjects()
{
TQValueList<KoTextObject *>::Iterator it = m_lstObjects.begin();
for( ; it != m_lstObjects.end(); ++it ) {
connect( (*it), TQ_SIGNAL( paragraphDeleted( KoTextParag* ) ),
this, TQ_SLOT( slotParagraphDeleted( KoTextParag* ) ) );
connect( (*it), TQ_SIGNAL( paragraphModified( KoTextParag*, int, int, int ) ),
this, TQ_SLOT( slotParagraphModified( KoTextParag*, int, int, int ) ) );
// We don't connect to destroyed(), because for undo/redo purposes,
// we never really delete textdocuments nor textobjects.
// So this is never called.
// Instead the textobject is simply set to invisible, and this is handled by nextTextObject
}
}
void KoTextIterator::slotParagraphModified( KoTextParag* parag, int modifyType, int pos, int length )
{
if ( parag == m_currentParag )
emit currentParagraphModified( modifyType, pos, length );
}
void KoTextIterator::slotParagraphDeleted( KoTextParag* parag )
{
#ifdef DEBUG_ITERATOR
kdDebug(32500) << "KoTextIterator::slotParagraphDeleted " << parag << " (" << parag->paragId() << ")" << endl;
#endif
// Note that the direction doesn't matter here. A begin/end
// at end of parag N or at beginning of parag N+1 is the same,
// and m_firstIndex/m_lastIndex becomes irrelevant, anyway.
if ( parag == m_lastParag )
{
if ( m_lastParag->prev() ) {
m_lastParag = m_lastParag->prev();
m_lastIndex = m_lastParag->length()-1;
} else {
m_lastParag = m_lastParag->next();
m_lastIndex = 0;
}
}
if ( parag == m_firstParag )
{
if ( m_firstParag->prev() ) {
m_firstParag = m_firstParag->prev();
m_firstIndex = m_firstParag->length()-1;
} else {
m_firstParag = m_firstParag->next();
m_firstIndex = 0;
}
}
if ( parag == m_currentParag )
{
operator++();
emit currentParagraphDeleted();
}
#ifdef DEBUG_ITERATOR
if ( m_currentParag )
kdDebug(32500) << "KoTextIterator: firstParag:" << m_firstParag << " (" << m_firstParag->paragId() << ") - lastParag:" << m_lastParag << " (" << m_lastParag->paragId() << ") m_currentParag:" << m_currentParag << " (" << m_currentParag->paragId() << ")" << endl;
#endif
}
// Go to next paragraph that we must iterate over
void KoTextIterator::operator++()
{
if ( !m_currentParag ) {
kdDebug(32500) << k_funcinfo << " called past the end" << endl;
return;
}
if ( m_currentParag == m_lastParag ) {
m_currentParag = 0L;
#ifdef DEBUG_ITERATOR
kdDebug(32500) << "KoTextIterator++: done, after last parag " << m_lastParag << endl;
#endif
return;
}
bool forw = ! ( m_options & KFindDialog::FindBackwards );
KoTextParag* parag = forw ? m_currentParag->next() : m_currentParag->prev();
if ( parag )
{
m_currentParag = parag;
}
else
{
nextTextObject();
}
#ifdef DEBUG_ITERATOR
if ( m_currentParag )
kdDebug(32500) << "KoTextIterator++ (" << *m_currentTextObj << "," <<
m_currentParag->paragId() << ")" << endl;
else
kdDebug(32500) << "KoTextIterator++ (at end)" << endl;
#endif
}
void KoTextIterator::nextTextObject()
{
bool forw = ! ( m_options & KFindDialog::FindBackwards );
do {
if ( forw ) {
++m_currentTextObj;
if ( m_currentTextObj == m_lstObjects.end() )
m_currentParag = 0L; // done
else
m_currentParag = (*m_currentTextObj)->textDocument()->firstParag();
} else {
if ( m_currentTextObj == m_lstObjects.begin() )
m_currentParag = 0L; // done
else
{
--m_currentTextObj;
m_currentParag = (*m_currentTextObj)->textDocument()->lastParag();
}
}
}
// loop in case this new textobject is not visible
while ( m_currentParag && !(*m_currentTextObj)->isVisible() );
#ifdef DEBUG_ITERATOR
if ( m_currentParag )
kdDebug(32500) << k_funcinfo << " m_currentTextObj=" << (*m_currentTextObj) << endl;
#endif
}
bool KoTextIterator::atEnd() const
{
// operator++ sets m_currentParag to 0 when it's done
return m_currentParag == 0L;
}
int KoTextIterator::currentStartIndex() const
{
return currentTextAndIndex().first;
}
TQString KoTextIterator::currentText() const
{
return currentTextAndIndex().second;
}
TQPair<int, TQString> KoTextIterator::currentTextAndIndex() const
{
Q_ASSERT( m_currentParag );
Q_ASSERT( m_currentParag->string() );
TQString str = m_currentParag->string()->toString();
str.truncate( str.length() - 1 ); // remove trailing space
bool forw = ! ( m_options & KFindDialog::FindBackwards );
if ( m_currentParag == m_firstParag )
{
if ( m_firstParag == m_lastParag ) // special case, needs truncating at both ends
return forw ? qMakePair( m_firstIndex, str.mid( m_firstIndex, m_lastIndex - m_firstIndex ) )
: qMakePair( m_lastIndex, str.mid( m_lastIndex, m_firstIndex - m_lastIndex ) );
else
return forw ? qMakePair( m_firstIndex, str.mid( m_firstIndex ) )
: qMakePair( 0, str.left( m_firstIndex ) );
}
if ( m_currentParag == m_lastParag )
{
return forw ? qMakePair( 0, str.left( m_lastIndex ) )
: qMakePair( m_lastIndex, str.mid( m_lastIndex ) );
}
// Not the first parag, nor the last, so we return it all
return qMakePair( 0, str );
}
bool KoTextIterator::hasText() const
{
// Same logic as currentTextAndIndex, but w/o calling it, to avoid all the string copying
bool forw = ! ( m_options & KFindDialog::FindBackwards );
int strLength = m_currentParag->string()->length() - 1;
if ( m_currentParag == m_firstParag )
{
if ( m_firstParag == m_lastParag )
return m_firstIndex < m_lastIndex;
else
return forw ? m_firstIndex < strLength
: m_firstIndex > 0;
}
if ( m_currentParag == m_lastParag )
return forw ? m_lastIndex > 0
: m_lastIndex < strLength;
return strLength > 0;
}
void KoTextIterator::setOptions( int options )
{
if ( m_options != options )
{
bool wasBack = (m_options & KFindDialog::FindBackwards);
bool isBack = (options & KFindDialog::FindBackwards);
if ( wasBack != isBack )
{
tqSwap( m_firstParag, m_lastParag );
tqSwap( m_firstIndex, m_lastIndex );
if ( m_currentParag == 0 ) // done? -> reinit
{
#ifdef DEBUG_ITERATOR
kdDebug(32500) << k_funcinfo << "was done -> reinit" << endl;
#endif
restart();
}
}
bool wasFromCursor = (m_options & KFindDialog::FromCursor);
bool isFromCursor = (options & KFindDialog::FromCursor);
// We can only handle the case where fromcursor got removed.
// If it got added, then we need a textview to take the cursor position from...
if ( wasFromCursor && !isFromCursor )
{
// We also can't handle the "selected text" option here
// It's very hard to have a cursor that's not at the beginning
// or end of the selection, anyway.
if ( ! (options & KFindDialog::SelectedText ) )
{
// Set m_firstParag/m_firstIndex to the beginning of the first object
// (end of last object when going backwards)
KoTextParag* firstParag = m_lstObjects.first()->textDocument()->firstParag();
int firstIndex = 0;
KoTextParag* lastParag = m_lstObjects.last()->textDocument()->lastParag();
int lastIndex = lastParag->length()-1;
m_firstParag = (!isBack) ? firstParag : lastParag;
m_firstIndex = (!isBack) ? firstIndex : lastIndex;
#ifdef DEBUG_ITERATOR
kdDebug(32500) << "setOptions: FromCursor removed. New m_firstParag=" << m_firstParag << " (" << m_firstParag->paragId() << ") isBack=" << isBack << endl;
#endif
}
}
m_options = options;
}
}
#include "KoTextIterator.moc"
|