00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoTextCommand.h"
00021 #include "KoTextObject.h"
00022 #include "KoTextParag.h"
00023 #include "KoVariable.h"
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026
00027
00028 void KoTextCommand::execute()
00029 {
00030 m_textobj->redo();
00031 }
00032
00033
00034 void KoTextCommand::unexecute()
00035 {
00036 m_textobj->undo();
00037 }
00038
00039 KoTextDeleteCommand::KoTextDeleteCommand(
00040 KoTextDocument *d, int i, int idx, const QMemArray<KoTextStringChar> &str,
00041 const CustomItemsMap & customItemsMap,
00042 const QValueList<KoParagLayout> &oldParagLayouts )
00043 : KoTextDocDeleteCommand( d, i, idx, str ),
00044 m_oldParagLayouts( oldParagLayouts ),
00045 m_customItemsMap( customItemsMap )
00046 {
00047 Q_ASSERT( id >= 0 );
00048 }
00049
00050 KoTextCursor * KoTextDeleteCommand::execute( KoTextCursor *c )
00051 {
00052 KoTextParag *s = doc ? doc->paragAt( id ) : parag;
00053 if ( !s ) {
00054 kdWarning() << "can't locate parag at " << id << ", last parag: " << doc->lastParag()->paragId() << endl;
00055 return 0;
00056 }
00057 cursor.setParag( s );
00058 cursor.setIndex( index );
00059 int len = text.size();
00060
00061
00062 for ( int i = 0; i < len; ++i )
00063 {
00064 KoTextStringChar * ch = cursor.parag()->at( cursor.index() );
00065 if ( ch->isCustom() )
00066 {
00067 ch->customItem()->setDeleted( true );
00068 cursor.parag()->removeCustomItem(cursor.index());
00069 }
00070 cursor.gotoRight();
00071 }
00072
00073 return KoTextDocDeleteCommand::execute(c);
00074 }
00075
00076 KoTextCursor * KoTextDeleteCommand::unexecute( KoTextCursor *c )
00077 {
00078
00079 KoTextCursor * cr = KoTextDocDeleteCommand::unexecute(c);
00080
00081 KoTextParag *s = doc ? doc->paragAt( id ) : parag;
00082 if ( !s ) {
00083 kdWarning() << "can't locate parag at " << id << ", last parag: " << (doc ? doc->lastParag()->paragId() : -1) << endl;
00084 return 0;
00085 }
00086 cursor.setParag( s );
00087 cursor.setIndex( index );
00088
00089 m_customItemsMap.insertItems( cursor, text.size() );
00090
00091
00092 QValueList<KoParagLayout>::Iterator lit = m_oldParagLayouts.begin();
00093 kdDebug(32500) << "KoTextDeleteCommand::unexecute " << m_oldParagLayouts.count() << " parag layouts. First parag=" << s->paragId() << endl;
00094 Q_ASSERT( id == s->paragId() );
00095 KoTextParag *p = s;
00096 while ( p ) {
00097 if ( lit != m_oldParagLayouts.end() )
00098 {
00099 kdDebug(32500) << "KoTextDeleteCommand::unexecute applying paraglayout to parag " << p->paragId() << endl;
00100 p->setParagLayout( *lit );
00101 }
00102 else
00103 break;
00104
00105
00106 p = p->next();
00107 ++lit;
00108 }
00109 return cr;
00110 }
00111
00112 KoTextParagCommand::KoTextParagCommand( KoTextDocument *d, int fParag, int lParag,
00113 const QValueList<KoParagLayout> &oldParagLayouts,
00114 KoParagLayout newParagLayout,
00115 int flags,
00116 QStyleSheetItem::Margin margin, bool borderOutline )
00117 : KoTextDocCommand( d ), firstParag( fParag ), lastParag( lParag ), m_oldParagLayouts( oldParagLayouts ),
00118 m_newParagLayout( newParagLayout ), m_flags( flags ), m_margin( margin ), m_borderOutline( borderOutline )
00119 {
00120 Q_ASSERT( fParag >= 0 );
00121 Q_ASSERT( lParag >= 0 );
00122 }
00123
00124 KoTextCursor * KoTextParagCommand::execute( KoTextCursor *c )
00125 {
00126
00127 KoTextParag *p = doc->paragAt( firstParag );
00128 if ( !p )
00129 {
00130 kdWarning() << "KoTextParagCommand::execute paragraph " << firstParag << "not found" << endl;
00131 return c;
00132 }
00133 while ( p ) {
00134 if ( ( m_flags & KoParagLayout::Margins ) && m_margin != (QStyleSheetItem::Margin)-1 )
00135 p->setMargin( static_cast<QStyleSheetItem::Margin>(m_margin), m_newParagLayout.margins[m_margin] );
00136 else
00137 {
00138 p->setParagLayout( m_newParagLayout, m_flags );
00139 if ( (m_flags & KoParagLayout::Borders) && m_borderOutline)
00140 {
00141 KoBorder tmpBorder;
00142 tmpBorder.setPenWidth(0);
00143 p->setTopBorder(tmpBorder);
00144 p->setBottomBorder(tmpBorder);
00145 }
00146 }
00147 if ( p->paragId() == lastParag )
00148 break;
00149 p = p->next();
00150 }
00151 if ( (m_flags & KoParagLayout::Borders) && m_borderOutline)
00152 {
00153 p->setBottomBorder( m_newParagLayout.bottomBorder);
00154 doc->paragAt( firstParag )->setTopBorder( m_newParagLayout.topBorder);
00155 }
00156
00157
00158
00159 c->setParag( p );
00160 c->setIndex( p->length()-1 );
00161 return c;
00162 }
00163
00164 KoTextCursor * KoTextParagCommand::unexecute( KoTextCursor *c )
00165 {
00166 kdDebug(32500) << "KoTextParagCommand::unexecute" << endl;
00167 KoTextParag *p = doc->paragAt( firstParag );
00168 if ( !p )
00169 {
00170 kdDebug(32500) << "KoTextParagCommand::unexecute paragraph " << firstParag << "not found" << endl;
00171 return c;
00172 }
00173 QValueList<KoParagLayout>::Iterator lit = m_oldParagLayouts.begin();
00174 while ( p ) {
00175 if ( lit == m_oldParagLayouts.end() )
00176 {
00177 kdDebug(32500) << "KoTextParagCommand::unexecute m_oldParagLayouts not big enough!" << endl;
00178 break;
00179 }
00180 if ( m_flags & KoParagLayout::Margins && m_margin != (QStyleSheetItem::Margin)-1 )
00181 p->setMargin( static_cast<QStyleSheetItem::Margin>(m_margin), (*lit).margins[m_margin] );
00182 else
00183 {
00184 p->setParagLayout( *lit, m_flags );
00185 }
00186 if ( p->paragId() == lastParag )
00187 break;
00188 p = p->next();
00189 ++lit;
00190 }
00191
00192 c->setParag( p );
00193 c->setIndex( p->length()-1 );
00194 return c;
00195 }
00196
00198
00199 KoParagFormatCommand::KoParagFormatCommand( KoTextDocument *d, int fParag, int lParag,
00200 const QValueList<KoTextFormat *> &oldFormats,
00201 KoTextFormat * newFormat )
00202 : KoTextDocCommand( d ), firstParag( fParag ), lastParag( lParag ), m_oldFormats( oldFormats ),
00203 m_newFormat( newFormat )
00204 {
00205 QValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin();
00206 for ( ; lit != m_oldFormats.end() ; ++lit )
00207 (*lit)->addRef();
00208 }
00209
00210 KoParagFormatCommand::~KoParagFormatCommand()
00211 {
00212 QValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin();
00213 for ( ; lit != m_oldFormats.end() ; ++lit )
00214 (*lit)->removeRef();
00215 }
00216
00217 KoTextCursor * KoParagFormatCommand::execute( KoTextCursor *c )
00218 {
00219 KoTextParag *p = doc->paragAt( firstParag );
00220 if ( !p )
00221 {
00222 kdDebug(32500) << "KoTextParagCommand::execute paragraph " << firstParag << "not found" << endl;
00223 return c;
00224 }
00225 while ( p ) {
00226 p->setFormat( m_newFormat );
00227 p->invalidate(0);
00228 if ( p->paragId() == lastParag )
00229 break;
00230 p = p->next();
00231 }
00232 return c;
00233 }
00234
00235 KoTextCursor * KoParagFormatCommand::unexecute( KoTextCursor *c )
00236 {
00237 kdDebug(32500) << "KoParagFormatCommand::unexecute" << endl;
00238 KoTextParag *p = doc->paragAt( firstParag );
00239 if ( !p )
00240 {
00241 kdDebug(32500) << "KoParagFormatCommand::unexecute paragraph " << firstParag << "not found" << endl;
00242 return c;
00243 }
00244 QValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin();
00245 while ( p ) {
00246 if ( lit == m_oldFormats.end() )
00247 {
00248 kdDebug(32500) << "KoParagFormatCommand::unexecute m_oldFormats not big enough!" << endl;
00249 break;
00250 }
00251 p->setFormat( (*lit) );
00252 if ( p->paragId() == lastParag )
00253 break;
00254 p = p->next();
00255 ++lit;
00256 }
00257 return c;
00258 }
00259
00260 KoTextFormatCommand::KoTextFormatCommand(KoTextDocument *d, int sid, int sidx, int eid, int eidx, const QMemArray<KoTextStringChar> &old, const KoTextFormat *f, int fl )
00261 : KoTextDocFormatCommand(d, sid, sidx, eid, eidx, old, f, fl)
00262 {
00263 }
00264
00265
00266 KoTextFormatCommand::~KoTextFormatCommand()
00267 {
00268 }
00269
00270 void KoTextFormatCommand::resizeCustomItems()
00271 {
00272 KoTextParag *sp = doc->paragAt( startId );
00273 KoTextParag *ep = doc->paragAt( endId );
00274 if ( !sp || !ep )
00275 return;
00276
00277 KoTextCursor start( doc );
00278 start.setParag( sp );
00279 start.setIndex( startIndex );
00280 KoTextCursor end( doc );
00281 end.setParag( ep );
00282 end.setIndex( endIndex );
00283
00284 doc->setSelectionStart( KoTextDocument::Temp, &start );
00285 doc->setSelectionEnd( KoTextDocument::Temp, &end );
00286
00287
00288
00289 if ( start.parag() == end.parag() )
00290 {
00291 QString text = start.parag()->string()->toString().mid( start.index(), end.index() - start.index() );
00292 for ( int i = start.index(); i < end.index(); ++i )
00293 {
00294 if( start.parag()->at(i)->isCustom())
00295 {
00296 start.parag()->at(i)->customItem()->resize();
00297 }
00298 }
00299 }
00300 else
00301 {
00302 int i;
00303 QString text = start.parag()->string()->toString().mid( start.index(), start.parag()->length() - 1 - start.index() );
00304 for ( i = start.index(); i < start.parag()->length(); ++i )
00305 if( start.parag()->at(i)->isCustom())
00306 {
00307 start.parag()->at(i)->customItem()->resize();
00308 }
00309
00310 KoTextParag *p = start.parag()->next();
00311 while ( p && p != end.parag() )
00312 {
00313 text = p->string()->toString().left( p->length() - 1 );
00314 for ( i = 0; i < p->length(); ++i )
00315 {
00316 if( p->at(i)->isCustom())
00317 {
00318 p->at(i)->customItem()->resize();
00319 }
00320 }
00321 p = p->next();
00322 }
00323 text = end.parag()->string()->toString().left( end.index() );
00324 for ( i = 0; i < end.index(); ++i )
00325 {
00326 if( end.parag()->at(i)->isCustom())
00327 {
00328 end.parag()->at(i)->customItem()->resize();
00329 }
00330 }
00331 }
00332 }
00333
00334 KoTextCursor *KoTextFormatCommand::execute( KoTextCursor *c )
00335 {
00336 c = KoTextDocFormatCommand::execute( c );
00337 resizeCustomItems();
00338 return c;
00339 }
00340
00341 KoTextCursor *KoTextFormatCommand::unexecute( KoTextCursor *c )
00342 {
00343 kdDebug(32500) << "KoTextFormatCommand::unexecute c:" << c << " index:" << c->index() << endl;
00344 c = KoTextDocFormatCommand::unexecute( c );
00345 kdDebug(32500) << "KoTextFormatCommand::unexecute after KoTextFormatCommand c:" << c << " index:" << c->index() << endl;
00346 resizeCustomItems();
00347 return c;
00348 }
00349
00351
00352 KoChangeVariableSubType::KoChangeVariableSubType(
00353 short int _oldValue, short int _newValue,
00354 KoVariable *var):
00355 KCommand(),
00356 m_newValue(_newValue),
00357 m_oldValue(_oldValue),
00358 m_var(var)
00359 {
00360 }
00361
00362 void KoChangeVariableSubType::execute()
00363 {
00364 Q_ASSERT(m_var);
00365 m_var->setVariableSubType(m_newValue);
00366 m_var->recalcAndRepaint();
00367 }
00368
00369 void KoChangeVariableSubType::unexecute()
00370 {
00371 Q_ASSERT(m_var);
00372 m_var->setVariableSubType(m_oldValue);
00373 m_var->recalcAndRepaint();
00374 }
00375
00376 QString KoChangeVariableSubType::name() const
00377 {
00378 return i18n( "Change Variable Subtype" );
00379 }
00380
00382
00383 KoChangeVariableFormatProperties::KoChangeVariableFormatProperties(
00384 const QString &_oldValue, const QString &_newValue,
00385 KoVariable *var)
00386 : KCommand(),
00387 m_newValue(_newValue),
00388 m_oldValue(_oldValue),
00389 m_var(var)
00390 {
00391 }
00392
00393 void KoChangeVariableFormatProperties::execute()
00394 {
00395 Q_ASSERT(m_var);
00396
00397 KoVariableFormatCollection* coll = m_var->variableColl()->formatCollection();
00398 m_var->setVariableFormat( coll->format( m_var->variableFormat()->getKey( m_newValue ) ) );
00399 m_var->recalcAndRepaint();
00400 }
00401
00402 void KoChangeVariableFormatProperties::unexecute()
00403 {
00404 Q_ASSERT(m_var);
00405
00406 KoVariableFormatCollection* coll = m_var->variableColl()->formatCollection();
00407 m_var->setVariableFormat( coll->format( m_var->variableFormat()->getKey( m_oldValue ) ) );
00408 m_var->recalcAndRepaint();
00409 }
00410
00411 QString KoChangeVariableFormatProperties::name() const
00412 {
00413 return i18n( "Change Variable Format" );
00414 }