karbon

vreplacingcmd.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, The Karbon Developers
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 
00021 #include "vreplacingcmd.h"
00022 #include "vselection.h"
00023 #include "vdocument.h"
00024 
00025 VReplacingCmd::VReplacingCmd( VDocument* doc, const QString& name )
00026     : VCommand( doc, name )
00027 {
00028     // Set members.
00029     m_oldObjects = doc ? document()->selection()->clone() : 0L;
00030     m_newObjects = 0L;
00031 }
00032 
00033 VReplacingCmd::~VReplacingCmd()
00034 {
00035     delete( m_oldObjects );
00036     delete( m_newObjects );
00037 }
00038 
00039 void
00040 VReplacingCmd::execute()
00041 {
00042     // Did we have at least once a success? Otherwise we don't get inserted
00043     // into the command history.
00044     bool successful = false;
00045 
00046 
00047     // Create new shapes if they don't exist yet.
00048     if( !m_newObjects )
00049     {
00050         m_newObjects = new VSelection();
00051 
00052         // Pointer to temporary object.
00053         VObject* newObject;
00054 
00055         VObjectListIterator itr( m_oldObjects->objects() );
00056         VObjectList rejects;
00057 
00058         for( ; itr.current(); ++itr )
00059         {
00060             // Clone object and visit the clone.
00061             newObject = itr.current()->clone();
00062 
00063             // Success.
00064             if( visit( *newObject ) )
00065             {
00066                 successful = true;
00067 
00068                 // Insert new shape right before old shape.
00069                 itr.current()->parent()->insertInfrontOf(
00070                     newObject, itr.current() );
00071 
00072                 // Add new shape to list of new objects.
00073                 m_newObjects->append( newObject );
00074             }
00075             // No success.
00076             else
00077             {
00078                 rejects.append( itr.current() );
00079                 // Delete temporary object.
00080                 delete( newObject );
00081             }
00082         }
00083         VObjectListIterator jtr( rejects );
00084         for( ; jtr.current(); ++jtr )
00085         {
00086             // Don't consider this object in the future anymore.
00087             m_oldObjects->take( *jtr.current() );
00088         }
00089     }
00090 
00091     // Nothing to do.
00092     if( m_newObjects->objects().count() == 0 )
00093         return;
00094 
00095 
00096     VObjectListIterator itr( m_oldObjects->objects() );
00097 
00098     // Hide old objects.
00099     for( ; itr.current(); ++itr )
00100     {
00101         document()->selection()->take( *itr.current() );
00102         itr.current()->setState( VObject::deleted );
00103     }
00104 
00105     // Show new objects.
00106     for( itr = m_newObjects->objects(); itr.current(); ++itr )
00107     {
00108         itr.current()->setState( VObject::normal );
00109         document()->selection()->append( itr.current() );
00110     }
00111 
00112 
00113     // Tell command history wether we had success at least once.
00114     setSuccess( successful );
00115 }
00116 
00117 void
00118 VReplacingCmd::unexecute()
00119 {
00120     // Nothing to do.
00121     if( m_newObjects->objects().count() == 0 )
00122         return;
00123 
00124 
00125     VObjectListIterator itr( m_oldObjects->objects() );
00126 
00127     // Show old objects.
00128     for( ; itr.current(); ++itr )
00129     {
00130         itr.current()->setState( VObject::normal );
00131         document()->selection()->append( itr.current() );
00132     }
00133 
00134     // Hide new objects.
00135     for( itr = m_newObjects->objects(); itr.current(); ++itr )
00136     {
00137         document()->selection()->take( *itr.current() );
00138         itr.current()->setState( VObject::deleted );
00139     }
00140 
00141 
00142     // Reset success for command history.
00143     setSuccess( false );
00144 }
00145 
KDE Home | KDE Accessibility Home | Description of Access Keys