F:/KPlato/koffice/libs/kotext/KoTextCommand.cpp

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

Généré le Wed Nov 22 23:41:09 2006 pour KPlato par  doxygen 1.5.1-p1