00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <KoParagCounter.h>
00021
00022 #include "KWInsertTOCCommand.h"
00023 #include "KWDocument.h"
00024 #include "KWTextFrameSet.h"
00025 #include "KWTextDocument.h"
00026 #include "KWTextParag.h"
00027
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030
00031 KWInsertTOCCommand::KWInsertTOCCommand( KWTextFrameSet * fs, KoTextParag *parag )
00032 : KoTextDocCommand( fs->textDocument() ), m_paragId( parag->paragId() )
00033 {
00034 }
00035
00036 KoTextCursor * KWInsertTOCCommand::execute( KoTextCursor *c )
00037 {
00038 KWTextDocument * textdoc = static_cast<KWTextDocument *>(doc);
00039 KWTextFrameSet * fs = textdoc->textFrameSet();
00040
00041 fs->kWordDocument()->setTocPresent(true);
00042
00043 KoTextParag *insertionParag = textdoc->paragAt( m_paragId );
00044 if ( !insertionParag ) {
00045 qWarning( "KWInsertTOCCommand:: can't locate parag at %d, last parag: %d", m_paragId, textdoc->lastParag()->paragId() );
00046 return c;
00047 }
00048 KWTextParag *body = static_cast<KWTextParag *>( insertionParag );
00049
00050 KWTextParag *parag = static_cast<KWTextParag *>( textdoc->createParag( textdoc, body->prev() , body , true ) );
00051 parag->append( i18n( "Table of Contents" ) );
00052 KoParagStyle * style = findOrCreateTOCStyle( fs, -1 );
00053 parag->setParagLayout( style->paragLayout() );
00054 parag->setFormat( 0, parag->string()->length(), textdoc->formatCollection()->format( &style->format() ) );
00055 parag->setPartOfTableOfContents( true );
00056
00057
00058
00059
00060 KWTextParag *p = static_cast<KWTextParag *>(textdoc->firstParag());
00061
00062 KWTextParag *prevTOCParag = parag;
00063 QMap<KWTextParag *, KWTextParag *> paragMap;
00064 while ( p ) {
00065
00066 if ( p->style() && p->style()->isOutline() )
00067 {
00068 parag = static_cast<KWTextParag *>(textdoc->createParag( textdoc, prevTOCParag , body , true ));
00069 parag->setPartOfTableOfContents( true );
00070 QString txt = p->toString();
00071 parag->append( txt );
00072 prevTOCParag = parag;
00073
00074 paragMap.insert( parag, p );
00075 }
00076 p = static_cast<KWTextParag *>(p->next());
00077 }
00078
00079 kdDebug() << "KWInsertTOCCommand::execute setPageBreaking on " << prevTOCParag << " " << prevTOCParag->paragId() << endl;
00080 prevTOCParag->setPageBreaking( prevTOCParag->pageBreaking() | KWParagLayout::HardFrameBreakAfter );
00081
00082
00083 fs->layout();
00084 fs->updateFrames();
00085
00086
00087
00088
00089 QMap<KWTextParag *, KWTextParag *>::Iterator mapIt = paragMap.begin();
00090 for ( ; mapIt != paragMap.end() ; ++mapIt )
00091 {
00092 KWTextParag * parag = mapIt.key();
00093 KWTextParag * p = mapIt.data();
00094
00095
00096 KoPoint pt;
00097 KWFrame * frame = fs->internalToDocument( QPoint(0, p->rect().top()), pt );
00098 if ( frame )
00099 {
00100 parag->append( "\t" );
00101 parag->append( QString::number( frame->pageNumber() ) );
00102 }
00103
00104
00105 int depth = p->counter() ? p->counter()->depth() : 0;
00106 KoParagStyle * tocStyle = findOrCreateTOCStyle( fs, depth );
00107 parag->setParagLayout( tocStyle->paragLayout() );
00108 parag->setFormat( 0, parag->string()->length(), & tocStyle->format() );
00109 }
00110
00111 prevTOCParag->setPageBreaking( prevTOCParag->pageBreaking() | KWParagLayout::HardFrameBreakAfter );
00112 return c;
00113 }
00114
00115 KoTextCursor *KWInsertTOCCommand::unexecute( KoTextCursor *c )
00116 {
00117 KWTextDocument * textdoc = static_cast<KWTextDocument *>(doc);
00118 KWTextFrameSet * fs = textdoc->textFrameSet();
00119
00120 removeTOC( fs, c, 0L );
00121 fs->kWordDocument()->setTocPresent(false);
00122 return c;
00123 }
00124
00125 KoTextCursor * KWInsertTOCCommand::removeTOC( KWTextFrameSet *fs, KoTextCursor *cursor, KMacroCommand * )
00126 {
00127 KoTextDocument * textdoc = fs->textDocument();
00128
00129 KoTextCursor start( textdoc );
00130 KoTextCursor end( textdoc );
00131
00132 KoTextParag *p = textdoc->lastParag();
00133 KoTextCursor *posOfTable=0L;
00134 KoTextParag *posOfToc=0L;
00135
00136 while ( p )
00137 {
00138 KWTextParag * parag = static_cast<KWTextParag *>(p);
00139 if ( parag->partOfTableOfContents() )
00140 {
00141 kdDebug() << "KWContents::createContents Deleting paragraph " << p << " " << p->paragId() << endl;
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 KoTextParag *prev = p->prev();
00162 KoTextParag *next = p->next();
00163
00164 if ( cursor->parag() == p )
00165 cursor->setParag( next ? next : prev );
00166 delete p;
00167 kdDebug() << "KWInsertTOCCommand::removeTOC " << p << " deleted" << endl;
00168 p = next;
00169 posOfToc = p;
00170 kdDebug() << "KWInsertTOCCommand::removeTOC prev=" << prev << " p=" << p << endl;
00171
00172 if ( prev )
00173 {
00174 prev->setNext( p );
00175 if ( p )
00176 p->setParagId( prev->paragId() + 1 );
00177 else
00178 posOfToc = prev;
00179 }
00180 else
00181 {
00182 textdoc->setFirstParag( p );
00183 if ( p )
00184 {
00185 p->setParagId( 0 );
00186 } else
00187 {
00188 textdoc->clear( true );
00189 cursor->setParag( textdoc->firstParag() );
00190 posOfToc = textdoc->firstParag();
00191 break;
00192 }
00193 }
00194 p->setPrev( prev );
00195 }
00196 p = p->prev();
00197 }
00198 textdoc->invalidate();
00199 if(posOfToc)
00200 {
00201 posOfTable=new KoTextCursor( textdoc );
00202 posOfTable->setParag(posOfToc );
00203 posOfTable->setIndex( 0 );
00204 }
00205
00206 return posOfTable;
00207 }
00208
00209 KoParagStyle * KWInsertTOCCommand::findOrCreateTOCStyle( KWTextFrameSet *fs, int depth )
00210 {
00211
00212 QString name;
00213 QString displayName;
00214 if ( depth >= 0 ) {
00215
00216 name = QString( "Contents_Head_%1" ).arg( depth+1 );
00217 displayName = i18n( "Contents Head %1" ).arg( depth+1 );
00218 } else {
00219 name = "Contents_Title";
00220 displayName = i18n( "Contents Title" );
00221 }
00222 KoParagStyle * style = fs->kWordDocument()->styleCollection()->findStyle( name );
00223 if ( !style )
00224 {
00225 style = new KoParagStyle( name );
00226 style->setDisplayName( displayName );
00227 style->format().setBold( ( ( depth==-1) || ( depth==0 ) ) ? true : false );
00228 style->format().setPointSize( depth==-1 ? 20 : 12 );
00229 if ( depth == -1 )
00230 {
00231 style->paragLayout().topBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00232 style->paragLayout().bottomBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00233
00234
00235 style->paragLayout().leftBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00236 style->paragLayout().rightBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00237 style->paragLayout().alignment = Qt::AlignHCenter;
00238 }
00239 else
00240 {
00241 KoTabulatorList tabList;
00242 KoTabulator tab;
00243
00244 tab.ptPos = KoUnit::fromUserValue( floor( KoUnit::toMM( fs->frame( 0 )->width() ) ), KoUnit::unit("mm") );
00245
00246 tab.type = T_RIGHT;
00247 tab.filling = TF_DOTS;
00248 tab.ptWidth = 0.5;
00249 tabList.append( tab );
00250 style->paragLayout().setTabList( tabList );
00251 style->paragLayout().margins[QStyleSheetItem::MarginLeft] = KoUnit::fromUserValue( (depth*4.5), KoUnit::unit("mm") );
00252 }
00253 style = fs->kWordDocument()->styleCollection()->addStyle( style );
00254 fs->kWordDocument()->updateAllStyleLists();
00255 }
00256 return style;
00257 }