19#include "KoTextIterator.h"
20#include "KoTextParag.h"
21#include "KoTextView.h"
22#include <kfinddialog.h>
38 Q_ASSERT( !lstObjects.isEmpty() );
46 if (
options & KFindDialog::FromCursor )
49 m_firstParag = textView->cursor()->parag();
50 m_firstIndex = textView->cursor()->index();
53 m_options &= ~KFindDialog::FromCursor;
54 kdWarning(32500) <<
"FromCursor specified, but no textview?" << endl;
58 bool forw = ! (
options & KFindDialog::FindBackwards );
61 if ( textView && (
options & KFindDialog::SelectedText ) )
64 KoTextCursor c1 = textObj->
textDocument()->selectionStartCursor( KoTextDocument::Standard );
65 KoTextCursor c2 = textObj->
textDocument()->selectionEndCursor( KoTextDocument::Standard );
68 m_firstParag = forw ? c1.parag() : c2.parag();
69 m_firstIndex = forw ? c1.index() : c2.index();
71 m_lastParag = forw ? c2.parag() : c1.parag();
72 m_lastIndex = forw ? c2.index() : c1.index();
74 m_lstObjects.append( textObj );
75 m_currentTextObj = m_lstObjects.begin();
80 m_lstObjects = lstObjects;
81 if ( textView && (
options & KFindDialog::FromCursor) )
87 while( m_lstObjects.first() != textView->textObject() ) {
89 m_lstObjects.pop_front();
90 m_lstObjects.push_back( textobj );
91 if ( m_lstObjects.first() == initialFirst ) {
92 kdWarning(32500) <<
"Didn't manage to find " << textView->textObject() <<
" in the list of textobjects!!!" << endl;
97 while( m_lstObjects.last() != textView->textObject() ) {
99 m_lstObjects.pop_back();
100 m_lstObjects.push_front( textobj );
101 if ( m_lstObjects.first() == initialFirst ) {
102 kdWarning(32500) <<
"Didn't manage to find " << textView->textObject() <<
" in the list of textobjects!!!" << endl;
109 KoTextParag* firstParag = m_lstObjects.first()->textDocument()->firstParag();
111 KoTextParag* lastParag = m_lstObjects.last()->textDocument()->lastParag();
112 int lastIndex = lastParag->length()-1;
115 m_firstParag = forw ? firstParag : lastParag;
116 m_firstIndex = forw ? firstIndex : lastIndex;
119 m_lastParag = forw ? lastParag : firstParag;
120 m_lastIndex = forw ? lastIndex : firstIndex;
121 m_currentTextObj = forw ? m_lstObjects.begin() : m_lstObjects.fromLast();
124 assert( *m_currentTextObj );
125 assert( m_firstParag );
126 assert( m_lastParag );
127 Q_ASSERT( (*m_currentTextObj)->isVisible() );
128 m_currentParag = m_firstParag;
130 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;
131 TQValueList<KoTextObject *>::Iterator it = m_lstObjects.begin();
132 for( ; it != m_lstObjects.end(); ++it )
133 kdDebug(32500) << (*it) <<
" " << (*it)->name() << endl;
135 Q_ASSERT( (*m_currentTextObj)->textDocument() == m_currentParag->textDocument() );
136 Q_ASSERT( (forw?m_lstObjects.last():m_lstObjects.first())->textDocument() == m_lastParag->textDocument() );
138 connectTextObjects();
143 if( m_lstObjects.isEmpty() )
145 m_currentParag = m_firstParag;
146 bool forw = ! ( m_options & KFindDialog::FindBackwards );
147 Q_ASSERT( ! (m_options & KFindDialog::FromCursor) );
148 if ( (m_options & KFindDialog::FromCursor) || forw )
149 m_currentTextObj = m_lstObjects.begin();
151 m_currentTextObj = m_lstObjects.fromLast();
152 if ( !(*m_currentTextObj)->isVisible() )
155 if ( m_currentParag )
156 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;
158 kdDebug(32500) <<
"KoTextIterator::restart - nowhere to go!" << endl;
162void KoTextIterator::connectTextObjects()
164 TQValueList<KoTextObject *>::Iterator it = m_lstObjects.begin();
165 for( ; it != m_lstObjects.end(); ++it ) {
166 connect( (*it), TQ_SIGNAL( paragraphDeleted( KoTextParag* ) ),
167 this, TQ_SLOT( slotParagraphDeleted( KoTextParag* ) ) );
168 connect( (*it), TQ_SIGNAL( paragraphModified( KoTextParag*,
int,
int,
int ) ),
169 this, TQ_SLOT( slotParagraphModified( KoTextParag*,
int,
int,
int ) ) );
177void KoTextIterator::slotParagraphModified( KoTextParag* parag,
int modifyType,
int pos,
int length )
179 if ( parag == m_currentParag )
183void KoTextIterator::slotParagraphDeleted( KoTextParag* parag )
186 kdDebug(32500) <<
"KoTextIterator::slotParagraphDeleted " << parag <<
" (" << parag->paragId() <<
")" << endl;
191 if ( parag == m_lastParag )
193 if ( m_lastParag->prev() ) {
194 m_lastParag = m_lastParag->prev();
195 m_lastIndex = m_lastParag->length()-1;
197 m_lastParag = m_lastParag->next();
201 if ( parag == m_firstParag )
203 if ( m_firstParag->prev() ) {
204 m_firstParag = m_firstParag->prev();
205 m_firstIndex = m_firstParag->length()-1;
207 m_firstParag = m_firstParag->next();
211 if ( parag == m_currentParag )
217 if ( m_currentParag )
218 kdDebug(32500) <<
"KoTextIterator: firstParag:" << m_firstParag <<
" (" << m_firstParag->paragId() <<
") - lastParag:" << m_lastParag <<
" (" << m_lastParag->paragId() <<
") m_currentParag:" << m_currentParag <<
" (" << m_currentParag->paragId() <<
")" << endl;
225 if ( !m_currentParag ) {
226 kdDebug(32500) << k_funcinfo <<
" called past the end" << endl;
229 if ( m_currentParag == m_lastParag ) {
232 kdDebug(32500) <<
"KoTextIterator++: done, after last parag " << m_lastParag << endl;
236 bool forw = ! ( m_options & KFindDialog::FindBackwards );
237 KoTextParag* parag = forw ? m_currentParag->next() : m_currentParag->prev();
240 m_currentParag = parag;
247 if ( m_currentParag )
248 kdDebug(32500) <<
"KoTextIterator++ (" << *m_currentTextObj <<
"," <<
249 m_currentParag->paragId() <<
")" << endl;
251 kdDebug(32500) <<
"KoTextIterator++ (at end)" << endl;
255void KoTextIterator::nextTextObject()
257 bool forw = ! ( m_options & KFindDialog::FindBackwards );
261 if ( m_currentTextObj == m_lstObjects.end() )
264 m_currentParag = (*m_currentTextObj)->textDocument()->firstParag();
266 if ( m_currentTextObj == m_lstObjects.begin() )
271 m_currentParag = (*m_currentTextObj)->textDocument()->lastParag();
276 while ( m_currentParag && !(*m_currentTextObj)->isVisible() );
278 if ( m_currentParag )
279 kdDebug(32500) << k_funcinfo <<
" m_currentTextObj=" << (*m_currentTextObj) << endl;
286 return m_currentParag == 0L;
301 Q_ASSERT( m_currentParag );
302 Q_ASSERT( m_currentParag->string() );
303 TQString str = m_currentParag->string()->toString();
304 str.truncate( str.length() - 1 );
305 bool forw = ! ( m_options & KFindDialog::FindBackwards );
306 if ( m_currentParag == m_firstParag )
308 if ( m_firstParag == m_lastParag )
309 return forw ? qMakePair( m_firstIndex, str.mid( m_firstIndex, m_lastIndex - m_firstIndex ) )
310 : qMakePair( m_lastIndex, str.mid( m_lastIndex, m_firstIndex - m_lastIndex ) );
312 return forw ? qMakePair( m_firstIndex, str.mid( m_firstIndex ) )
313 : qMakePair( 0, str.left( m_firstIndex ) );
315 if ( m_currentParag == m_lastParag )
317 return forw ? qMakePair( 0, str.left( m_lastIndex ) )
318 : qMakePair( m_lastIndex, str.mid( m_lastIndex ) );
321 return qMakePair( 0, str );
327 bool forw = ! ( m_options & KFindDialog::FindBackwards );
328 int strLength = m_currentParag->string()->length() - 1;
329 if ( m_currentParag == m_firstParag )
331 if ( m_firstParag == m_lastParag )
332 return m_firstIndex < m_lastIndex;
334 return forw ? m_firstIndex < strLength
337 if ( m_currentParag == m_lastParag )
338 return forw ? m_lastIndex > 0
339 : m_lastIndex < strLength;
340 return strLength > 0;
347 bool wasBack = (m_options & KFindDialog::FindBackwards);
348 bool isBack = (
options & KFindDialog::FindBackwards);
349 if ( wasBack != isBack )
351 tqSwap( m_firstParag, m_lastParag );
352 tqSwap( m_firstIndex, m_lastIndex );
353 if ( m_currentParag == 0 )
356 kdDebug(32500) << k_funcinfo <<
"was done -> reinit" << endl;
361 bool wasFromCursor = (m_options & KFindDialog::FromCursor);
362 bool isFromCursor = (
options & KFindDialog::FromCursor);
365 if ( wasFromCursor && !isFromCursor )
370 if ( ! (
options & KFindDialog::SelectedText ) )
374 KoTextParag* firstParag = m_lstObjects.first()->textDocument()->firstParag();
376 KoTextParag* lastParag = m_lstObjects.last()->textDocument()->lastParag();
377 int lastIndex = lastParag->length()-1;
378 m_firstParag = (!isBack) ? firstParag : lastParag;
379 m_firstIndex = (!isBack) ? firstIndex : lastIndex;
381 kdDebug(32500) <<
"setOptions: FromCursor removed. New m_firstParag=" << m_firstParag <<
" (" << m_firstParag->paragId() <<
") isBack=" << isBack << endl;
389#include "KoTextIterator.moc"
TQPair< int, TQString > currentTextAndIndex() const
void currentParagraphDeleted()
Emitted when the current paragraph has been deleted by the user.
int options() const
Return the options currently used by the iterator.
void restart()
Restart from the beginning - assumes same parameters given to init.
int currentStartIndex() const
Where in currentParag() does currentText() start?
void setOptions(int options)
Change options during iteration.
void operator++()
Go to next paragraph that we must iterate over.
void init(const TQValueList< KoTextObject * > &lstObjects, KoTextView *textView, int options)
The search direction (forward or backward) is handled in a bit of a tricky way.
void currentParagraphModified(int modifyType, int pos, int length)
Emitted when the current paragraph has been modified by the user.
TQString currentText() const
The KoTextObject is the high-level object that contains a KoTextDocument (the list of paragraphs),...
KoTextDocument * textDocument() const
Return the text document contained in this KoTextObject.
Object that is created to edit a KoTextObject.