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 if(! p) {
00195 p = prev;
00196 continue;
00197 }
00198 p->setPrev( prev );
00199 }
00200 p = p->prev();
00201 }
00202 textdoc->invalidate();
00203 if(posOfToc)
00204 {
00205 posOfTable=new KoTextCursor( textdoc );
00206 posOfTable->setParag(posOfToc );
00207 posOfTable->setIndex( 0 );
00208 }
00209
00210 return posOfTable;
00211 }
00212
00213 KoParagStyle * KWInsertTOCCommand::findOrCreateTOCStyle( KWTextFrameSet *fs, int depth )
00214 {
00215
00216 QString name;
00217 QString displayName;
00218 if ( depth >= 0 ) {
00219
00220 name = QString( "Contents_Head_%1" ).arg( depth+1 );
00221 displayName = i18n( "Contents Head %1" ).arg( depth+1 );
00222 } else {
00223 name = "Contents_Title";
00224 displayName = i18n( "Contents Title" );
00225 }
00226 KoParagStyle * style = fs->kWordDocument()->styleCollection()->findStyle( name );
00227 if ( !style )
00228 {
00229 style = new KoParagStyle( name );
00230 style->setDisplayName( displayName );
00231 style->format().setBold( ( ( depth==-1) || ( depth==0 ) ) ? true : false );
00232 style->format().setPointSize( depth==-1 ? 20 : 12 );
00233 if ( depth == -1 )
00234 {
00235 style->paragLayout().topBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00236 style->paragLayout().bottomBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00237
00238
00239 style->paragLayout().leftBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00240 style->paragLayout().rightBorder = KoBorder( Qt::black, KoBorder::SOLID, 1 );
00241 style->paragLayout().alignment = Qt::AlignHCenter;
00242 }
00243 else
00244 {
00245 KoTabulatorList tabList;
00246 KoTabulator tab;
00247
00248 tab.ptPos = KoUnit::fromUserValue( floor( KoUnit::toMM( fs->frame( 0 )->width() ) ), KoUnit::unit("mm") );
00249
00250 tab.type = T_RIGHT;
00251 tab.filling = TF_DOTS;
00252 tab.ptWidth = 0.5;
00253 tabList.append( tab );
00254 style->paragLayout().setTabList( tabList );
00255 style->paragLayout().margins[QStyleSheetItem::MarginLeft] = KoUnit::fromUserValue( (depth*4.5), KoUnit::unit("mm") );
00256 }
00257 style = fs->kWordDocument()->styleCollection()->addStyle( style );
00258 fs->kWordDocument()->updateAllStyleLists();
00259 }
00260 return style;
00261 }