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 "kotextparag.h"
00023 #include "kovariable.h"
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 
00027 // This is automatically called by KCommandHistory's redo action when redo is activated
00028 void KoTextCommand::execute()
00029 {
00030     m_textobj->redo();
00031 }
00032 
00033 // This is automatically called by KCommandHistory's undo action when undo is activated
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     // Detach from custom items. They are already in the map, and we don't
00061     // want them to be deleted
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     // Let QRichText re-create the text and formatting
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->lastParag()->paragId() << endl;
00084         return 0;
00085     }
00086     cursor.setParag( s );
00087     cursor.setIndex( index );
00088     // Set any custom item that we had
00089     m_customItemsMap.insertItems( cursor, text.size() );
00090 
00091     // Now restore the parag layouts (i.e. libkotext specific stuff)
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         //if ( s == cr->parag() )
00105         //    break;
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     //kdDebug(32500) << "KoTextParagCommand::execute" << endl;
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 ) // all
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     //kdDebug(32500) << "KoTextParagCommand::execute done" << endl;
00158     // Set cursor to end of selection. Like in KoTextFormatCommand::[un]execute...
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 ) // just one
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     // Set cursor to end of selection. Like in KoTextFormatCommand::[un]execute...
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     // TODO use the visitor pattern (some 'ResizeCustomItemVisitor')
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     // Wrong! m_var->variableFormat()->setFormatProperties( m_newValue );
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     // Wrong! m_var->variableFormat()->setFormatProperties( m_oldValue );
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 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:39:59 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003