karbon

vzordercmd.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, 2003 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 #include <klocale.h>
00021 
00022 #include "vzordercmd.h"
00023 #include "vselection.h"
00024 #include "vdocument.h"
00025 #include "vlayer.h"
00026 
00027 VZOrderCmd::VZOrderCmd( VDocument *doc, VOrder state )
00028     : VCommand( doc, i18n( "Order Selection" ) ), m_state( state )
00029 {
00030     m_selection = document()->selection()->clone();
00031 }
00032 
00033 VZOrderCmd::VZOrderCmd( VDocument *doc, VObject *obj, VOrder state )
00034     : VCommand( doc, i18n( "Order Selection" ) ), m_state( state )
00035 {
00036     m_selection = new VSelection();
00037     m_selection->append( obj );
00038 }
00039 
00040 VZOrderCmd::~VZOrderCmd()
00041 {
00042     delete( m_selection );
00043 }
00044 
00045 void
00046 VZOrderCmd::execute()
00047 {
00048     if( m_state == sendToBack )
00049     {
00050         VObjectListIterator itr( document()->selection()->objects() );
00051         for ( itr.toLast() ; itr.current() ; --itr )
00052         {
00053             // remove from old layer
00054             VObjectList objects;
00055             VLayerListIterator litr( document()->layers() );
00056 
00057             for ( ; litr.current(); ++litr )
00058             {
00059                 objects = litr.current()->objects();
00060                 VObjectListIterator itr2( objects );
00061                 for ( ; itr2.current(); ++itr2 )
00062                     if( itr2.current() == itr.current() )
00063                     {
00064                         litr.current()->sendToBack( *itr2.current() );
00065                         itr2.current()->setState( VObject::selected );
00066                     }
00067             }
00068         }
00069     }
00070     else if( m_state == bringToFront )
00071     {
00072         VObjectListIterator itr( document()->selection()->objects() );
00073         for ( ; itr.current() ; ++itr )
00074         {
00075             // remove from old layer
00076             VObjectList objects;
00077             VLayerListIterator litr( document()->layers() );
00078 
00079             for ( ; litr.current(); ++litr )
00080             {
00081                 objects = litr.current()->objects();
00082                 VObjectListIterator itr2( objects );
00083                 for ( ; itr2.current(); ++itr2 )
00084                     if( itr2.current() == itr.current() )
00085                     {
00086                         litr.current()->bringToFront( *itr2.current() );
00087                         itr2.current()->setState( VObject::selected );
00088                     }
00089             }
00090         }
00091     }
00092     else if( m_state == up || m_state == down )
00093     {
00094         VSelection selection = *m_selection;
00095 
00096         VObjectList objects;
00097 
00098         // TODO : this doesn't work for objects inside groups!
00099         VLayerListIterator litr( document()->layers() );
00100         while( !selection.objects().isEmpty() && litr.current() )
00101         {
00102             for ( ; litr.current(); ++litr )
00103             {
00104                 if( litr.current()->state() == VObject::deleted )
00105                     continue;
00106                 VObjectList todo;
00107                 VObjectListIterator itr( selection.objects() );
00108                 for ( ; itr.current() ; ++itr )
00109                 {
00110                     objects = litr.current()->objects();
00111                     VObjectListIterator itr2( objects );
00112                     // find all selected VObjects that are in the current layer
00113                     for ( ; itr2.current(); ++itr2 )
00114                     {
00115                         if( itr2.current() == itr.current() )
00116                         {
00117                             todo.append( itr.current() );
00118                             // remove from selection
00119                             selection.take( *itr.current() );
00120                         }
00121                     }
00122                 }
00123                 kdDebug(38000) << "todo.count() : " << todo.count() << endl;
00124 
00125                 // we have found the affected vobjects in this vlayer
00126                 VObjectListIterator itr3( todo );
00127                 for ( ; itr3.current(); ++itr3 )
00128                 {
00129                     if( m_state == up )
00130                         litr.current()->upwards( *itr3.current() );
00131                     else
00132                         litr.current()->downwards( *itr3.current() );
00133                     itr3.current()->setState( VObject::selected );
00134                 }
00135             }
00136         }
00137     }
00138     setSuccess( true );
00139 }
00140 
00141 void
00142 VZOrderCmd::unexecute()
00143 {
00144     if( m_state == sendToBack )
00145     {
00146         m_state = bringToFront;
00147         execute();
00148         m_state = sendToBack;
00149     }
00150     else if( m_state == bringToFront )
00151     {
00152         m_state = sendToBack;
00153         execute();
00154         m_state = bringToFront;
00155     }
00156     else if( m_state == up )
00157     {
00158         m_state = down;
00159         execute();
00160         m_state = up;
00161     }
00162     else if( m_state == down )
00163     {
00164         m_state = up;
00165         execute();
00166         m_state = down;
00167     }
00168     setSuccess( false );
00169 }
00170 
KDE Home | KDE Accessibility Home | Description of Access Keys