lib Library API Documentation

kocommand.cc

00001 /* This file is part of the KDE project 00002 Copyright (C) 2001 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "kocommand.h" 00021 #include "kotextobject.h" 00022 #include "kovariable.h" 00023 #include <kdebug.h> 00024 #include <klocale.h> 00025 00026 // This is automatically called by KCommandHistory's redo action when redo is activated 00027 void KoTextCommand::execute() 00028 { 00029 m_textobj->redo(); 00030 } 00031 00032 // This is automatically called by KCommandHistory's undo action when undo is activated 00033 void KoTextCommand::unexecute() 00034 { 00035 m_textobj->undo(); 00036 } 00037 00038 KoTextDeleteCommand::KoTextDeleteCommand( 00039 KoTextDocument *d, int i, int idx, const QMemArray<KoTextStringChar> &str, 00040 const CustomItemsMap & customItemsMap, 00041 const QValueList<KoParagLayout> &oldParagLayouts ) 00042 : KoTextDocDeleteCommand( d, i, idx, str ), 00043 m_oldParagLayouts( oldParagLayouts ), 00044 m_customItemsMap( customItemsMap ) 00045 { 00046 Q_ASSERT( id >= 0 ); 00047 } 00048 00049 KoTextCursor * KoTextDeleteCommand::execute( KoTextCursor *c ) 00050 { 00051 KoTextParag *s = doc ? doc->paragAt( id ) : parag; 00052 if ( !s ) { 00053 kdWarning() << "can't locate parag at " << id << ", last parag: " << doc->lastParag()->paragId() << endl; 00054 return 0; 00055 } 00056 cursor.setParag( s ); 00057 cursor.setIndex( index ); 00058 int len = text.size(); 00059 // Detach from custom items. They are already in the map, and we don't 00060 // want them to be deleted 00061 for ( int i = 0; i < len; ++i ) 00062 { 00063 KoTextStringChar * ch = cursor.parag()->at( cursor.index() ); 00064 if ( ch->isCustom() ) 00065 { 00066 ch->customItem()->setDeleted( true ); 00067 cursor.parag()->removeCustomItem(cursor.index()); 00068 } 00069 cursor.gotoRight(); 00070 } 00071 00072 return KoTextDocDeleteCommand::execute(c); 00073 } 00074 00075 KoTextCursor * KoTextDeleteCommand::unexecute( KoTextCursor *c ) 00076 { 00077 // Let QRichText re-create the text and formatting 00078 KoTextCursor * cr = KoTextDocDeleteCommand::unexecute(c); 00079 00080 KoTextParag *s = doc ? doc->paragAt( id ) : parag; 00081 if ( !s ) { 00082 kdWarning() << "can't locate parag at " << id << ", last parag: " << doc->lastParag()->paragId() << endl; 00083 return 0; 00084 } 00085 cursor.setParag( s ); 00086 cursor.setIndex( index ); 00087 // Set any custom item that we had 00088 m_customItemsMap.insertItems( cursor, text.size() ); 00089 00090 // Now restore the parag layouts (i.e. libkotext specific stuff) 00091 QValueList<KoParagLayout>::Iterator lit = m_oldParagLayouts.begin(); 00092 kdDebug(32500) << "KoTextDeleteCommand::unexecute " << m_oldParagLayouts.count() << " parag layouts. First parag=" << s->paragId() << endl; 00093 Q_ASSERT( id == s->paragId() ); 00094 KoTextParag *p = s; 00095 while ( p ) { 00096 if ( lit != m_oldParagLayouts.end() ) 00097 { 00098 kdDebug(32500) << "KoTextDeleteCommand::unexecute applying paraglayout to parag " << p->paragId() << endl; 00099 p->setParagLayout( *lit ); 00100 } 00101 else 00102 break; 00103 //if ( s == cr->parag() ) 00104 // break; 00105 p = p->next(); 00106 ++lit; 00107 } 00108 return cr; 00109 } 00110 00111 KoTextParagCommand::KoTextParagCommand( KoTextDocument *d, int fParag, int lParag, 00112 const QValueList<KoParagLayout> &oldParagLayouts, 00113 KoParagLayout newParagLayout, 00114 int flags, 00115 QStyleSheetItem::Margin margin, bool borderOutline ) 00116 : KoTextDocCommand( d ), firstParag( fParag ), lastParag( lParag ), m_oldParagLayouts( oldParagLayouts ), 00117 m_newParagLayout( newParagLayout ), m_flags( flags ), m_margin( margin ), m_borderOutline( borderOutline ) 00118 { 00119 Q_ASSERT( fParag >= 0 ); 00120 Q_ASSERT( lParag >= 0 ); 00121 } 00122 00123 KoTextCursor * KoTextParagCommand::execute( KoTextCursor *c ) 00124 { 00125 //kdDebug(32500) << "KoTextParagCommand::execute" << endl; 00126 KoTextParag *p = doc->paragAt( firstParag ); 00127 if ( !p ) 00128 { 00129 kdWarning() << "KoTextParagCommand::execute paragraph " << firstParag << "not found" << endl; 00130 return c; 00131 } 00132 while ( p ) { 00133 if ( ( m_flags & KoParagLayout::Margins ) && m_margin != (QStyleSheetItem::Margin)-1 ) // all 00134 p->setMargin( static_cast<QStyleSheetItem::Margin>(m_margin), m_newParagLayout.margins[m_margin] ); 00135 else 00136 { 00137 p->setParagLayout( m_newParagLayout, m_flags ); 00138 if ( (m_flags & KoParagLayout::Borders) && m_borderOutline) 00139 { 00140 KoBorder tmpBorder; 00141 tmpBorder.setPenWidth(0); 00142 p->setTopBorder(tmpBorder); 00143 p->setBottomBorder(tmpBorder); 00144 } 00145 } 00146 if ( p->paragId() == lastParag ) 00147 break; 00148 p = p->next(); 00149 } 00150 if ( (m_flags & KoParagLayout::Borders) && m_borderOutline) 00151 { 00152 p->setBottomBorder( m_newParagLayout.bottomBorder); 00153 doc->paragAt( firstParag )->setTopBorder( m_newParagLayout.topBorder); 00154 } 00155 00156 //kdDebug(32500) << "KoTextParagCommand::execute done" << endl; 00157 // Set cursor to end of selection. Like in KoTextFormatCommand::[un]execute... 00158 c->setParag( p ); 00159 c->setIndex( p->length()-1 ); 00160 return c; 00161 } 00162 00163 KoTextCursor * KoTextParagCommand::unexecute( KoTextCursor *c ) 00164 { 00165 kdDebug(32500) << "KoTextParagCommand::unexecute" << endl; 00166 KoTextParag *p = doc->paragAt( firstParag ); 00167 if ( !p ) 00168 { 00169 kdDebug(32500) << "KoTextParagCommand::unexecute paragraph " << firstParag << "not found" << endl; 00170 return c; 00171 } 00172 QValueList<KoParagLayout>::Iterator lit = m_oldParagLayouts.begin(); 00173 while ( p ) { 00174 if ( lit == m_oldParagLayouts.end() ) 00175 { 00176 kdDebug(32500) << "KoTextParagCommand::unexecute m_oldParagLayouts not big enough!" << endl; 00177 break; 00178 } 00179 if ( m_flags & KoParagLayout::Margins && m_margin != (QStyleSheetItem::Margin)-1 ) // just one 00180 p->setMargin( static_cast<QStyleSheetItem::Margin>(m_margin), (*lit).margins[m_margin] ); 00181 else 00182 { 00183 p->setParagLayout( *lit, m_flags ); 00184 } 00185 if ( p->paragId() == lastParag ) 00186 break; 00187 p = p->next(); 00188 ++lit; 00189 } 00190 // Set cursor to end of selection. Like in KoTextFormatCommand::[un]execute... 00191 c->setParag( p ); 00192 c->setIndex( p->length()-1 ); 00193 return c; 00194 } 00195 00197 00198 KoParagFormatCommand::KoParagFormatCommand( KoTextDocument *d, int fParag, int lParag, 00199 const QValueList<KoTextFormat *> &oldFormats, 00200 KoTextFormat * newFormat ) 00201 : KoTextDocCommand( d ), firstParag( fParag ), lastParag( lParag ), m_oldFormats( oldFormats ), 00202 m_newFormat( newFormat ) 00203 { 00204 QValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin(); 00205 for ( ; lit != m_oldFormats.end() ; ++lit ) 00206 (*lit)->addRef(); 00207 } 00208 00209 KoParagFormatCommand::~KoParagFormatCommand() 00210 { 00211 QValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin(); 00212 for ( ; lit != m_oldFormats.end() ; ++lit ) 00213 (*lit)->removeRef(); 00214 } 00215 00216 KoTextCursor * KoParagFormatCommand::execute( KoTextCursor *c ) 00217 { 00218 KoTextParag *p = doc->paragAt( firstParag ); 00219 if ( !p ) 00220 { 00221 kdDebug(32500) << "KoTextParagCommand::execute paragraph " << firstParag << "not found" << endl; 00222 return c; 00223 } 00224 while ( p ) { 00225 p->setFormat( m_newFormat ); 00226 p->invalidate(0); 00227 if ( p->paragId() == lastParag ) 00228 break; 00229 p = p->next(); 00230 } 00231 return c; 00232 } 00233 00234 KoTextCursor * KoParagFormatCommand::unexecute( KoTextCursor *c ) 00235 { 00236 kdDebug(32500) << "KoParagFormatCommand::unexecute" << endl; 00237 KoTextParag *p = doc->paragAt( firstParag ); 00238 if ( !p ) 00239 { 00240 kdDebug(32500) << "KoParagFormatCommand::unexecute paragraph " << firstParag << "not found" << endl; 00241 return c; 00242 } 00243 QValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin(); 00244 while ( p ) { 00245 if ( lit == m_oldFormats.end() ) 00246 { 00247 kdDebug(32500) << "KoParagFormatCommand::unexecute m_oldFormats not big enough!" << endl; 00248 break; 00249 } 00250 p->setFormat( (*lit) ); 00251 if ( p->paragId() == lastParag ) 00252 break; 00253 p = p->next(); 00254 ++lit; 00255 } 00256 return c; 00257 } 00258 00259 KoTextFormatCommand::KoTextFormatCommand(KoTextDocument *d, int sid, int sidx, int eid, int eidx, const QMemArray<KoTextStringChar> &old, const KoTextFormat *f, int fl ) 00260 : KoTextDocFormatCommand(d, sid, sidx, eid, eidx, old, f, fl) 00261 { 00262 } 00263 00264 00265 KoTextFormatCommand::~KoTextFormatCommand() 00266 { 00267 } 00268 00269 void KoTextFormatCommand::resizeCustomItems() 00270 { 00271 KoTextParag *sp = doc->paragAt( startId ); 00272 KoTextParag *ep = doc->paragAt( endId ); 00273 if ( !sp || !ep ) 00274 return; 00275 00276 KoTextCursor start( doc ); 00277 start.setParag( sp ); 00278 start.setIndex( startIndex ); 00279 KoTextCursor end( doc ); 00280 end.setParag( ep ); 00281 end.setIndex( endIndex ); 00282 00283 doc->setSelectionStart( KoTextDocument::Temp, &start ); 00284 doc->setSelectionEnd( KoTextDocument::Temp, &end ); 00285 00286 // TODO use the visitor pattern (some 'ResizeCustomItemVisitor') 00287 00288 if ( start.parag() == end.parag() ) 00289 { 00290 QString text = start.parag()->string()->toString().mid( start.index(), end.index() - start.index() ); 00291 for ( int i = start.index(); i < end.index(); ++i ) 00292 { 00293 if( start.parag()->at(i)->isCustom()) 00294 { 00295 start.parag()->at(i)->customItem()->resize(); 00296 } 00297 } 00298 } 00299 else 00300 { 00301 int i; 00302 QString text = start.parag()->string()->toString().mid( start.index(), start.parag()->length() - 1 - start.index() ); 00303 for ( i = start.index(); i < start.parag()->length(); ++i ) 00304 if( start.parag()->at(i)->isCustom()) 00305 { 00306 start.parag()->at(i)->customItem()->resize(); 00307 } 00308 00309 KoTextParag *p = start.parag()->next(); 00310 while ( p && p != end.parag() ) 00311 { 00312 text = p->string()->toString().left( p->length() - 1 ); 00313 for ( i = 0; i < p->length(); ++i ) 00314 { 00315 if( p->at(i)->isCustom()) 00316 { 00317 p->at(i)->customItem()->resize(); 00318 } 00319 } 00320 p = p->next(); 00321 } 00322 text = end.parag()->string()->toString().left( end.index() ); 00323 for ( i = 0; i < end.index(); ++i ) 00324 { 00325 if( end.parag()->at(i)->isCustom()) 00326 { 00327 end.parag()->at(i)->customItem()->resize(); 00328 } 00329 } 00330 } 00331 } 00332 00333 KoTextCursor *KoTextFormatCommand::execute( KoTextCursor *c ) 00334 { 00335 c = KoTextDocFormatCommand::execute( c ); 00336 resizeCustomItems(); 00337 return c; 00338 } 00339 00340 KoTextCursor *KoTextFormatCommand::unexecute( KoTextCursor *c ) 00341 { 00342 kdDebug(32500) << "KoTextFormatCommand::unexecute c:" << c << " index:" << c->index() << endl; 00343 c = KoTextDocFormatCommand::unexecute( c ); 00344 kdDebug(32500) << "KoTextFormatCommand::unexecute after KoTextFormatCommand c:" << c << " index:" << c->index() << endl; 00345 resizeCustomItems(); 00346 return c; 00347 } 00348 00350 00351 KoChangeVariableSubType::KoChangeVariableSubType( 00352 short int _oldValue, short int _newValue, 00353 KoVariable *var): 00354 KCommand(), 00355 m_newValue(_newValue), 00356 m_oldValue(_oldValue), 00357 m_var(var) 00358 { 00359 } 00360 00361 void KoChangeVariableSubType::execute() 00362 { 00363 Q_ASSERT(m_var); 00364 m_var->setVariableSubType(m_newValue); 00365 m_var->recalcAndRepaint(); 00366 } 00367 00368 void KoChangeVariableSubType::unexecute() 00369 { 00370 Q_ASSERT(m_var); 00371 m_var->setVariableSubType(m_oldValue); 00372 m_var->recalcAndRepaint(); 00373 } 00374 00375 QString KoChangeVariableSubType::name() const 00376 { 00377 return i18n( "Change Variable Subtype" ); 00378 } 00379 00381 00382 KoChangeVariableFormatProperties::KoChangeVariableFormatProperties( 00383 const QString &_oldValue, const QString &_newValue, 00384 KoVariable *var) 00385 : KCommand(), 00386 m_newValue(_newValue), 00387 m_oldValue(_oldValue), 00388 m_var(var) 00389 { 00390 } 00391 00392 void KoChangeVariableFormatProperties::execute() 00393 { 00394 Q_ASSERT(m_var); 00395 // Wrong! m_var->variableFormat()->setFormatProperties( m_newValue ); 00396 KoVariableFormatCollection* coll = m_var->variableColl()->formatCollection(); 00397 m_var->setVariableFormat( coll->format( m_var->variableFormat()->getKey( m_newValue ) ) ); 00398 m_var->recalcAndRepaint(); 00399 } 00400 00401 void KoChangeVariableFormatProperties::unexecute() 00402 { 00403 Q_ASSERT(m_var); 00404 // Wrong! m_var->variableFormat()->setFormatProperties( m_oldValue ); 00405 KoVariableFormatCollection* coll = m_var->variableColl()->formatCollection(); 00406 m_var->setVariableFormat( coll->format( m_var->variableFormat()->getKey( m_oldValue ) ) ); 00407 m_var->recalcAndRepaint(); 00408 } 00409 00410 QString KoChangeVariableFormatProperties::name() const 00411 { 00412 return i18n( "Change Variable Format" ); 00413 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Sep 24 18:22:23 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003