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         // TODO : this doesn't work for objects inside groups!
00096         VLayerListIterator litr( document()->layers() );
00097         while( !selection.objects().isEmpty() && litr.current() )
00098         {
00099             for ( ; litr.current(); ++litr )
00100             {
00101                 if( litr.current()->state() == VObject::deleted )
00102                     continue;
00103                 VObjectList objects = litr.current()->objects();
00104                 VObjectList todo;
00105                 VObjectListIterator objectItr( objects );
00106                 // find all selected VObjects that are in the current layer
00107                 for ( ; objectItr.current(); ++objectItr )
00108                 {
00109                     VObjectListIterator selectionItr( selection.objects() );
00110                     for ( ; selectionItr.current() ; ++selectionItr )
00111                     {
00112                         if( objectItr.current() == selectionItr.current() )
00113                         {
00114                             if( m_state == up )
00115                                 todo.prepend( objectItr.current() );
00116                             else
00117                                 todo.append( objectItr.current() );
00118                         }
00119                     }
00120                 }
00121 
00122                 kdDebug(38000) << "todo.count() : " << todo.count() << endl;
00123 
00124                 // we have found the affected vobjects in this vlayer
00125                 VObjectListIterator todoItr( todo );
00126                 for ( ; todoItr.current(); ++todoItr )
00127                 {
00128                     if( m_state == up )
00129                         litr.current()->upwards( *todoItr.current() );
00130                     else
00131                         litr.current()->downwards( *todoItr.current() );
00132                     // remove from selection
00133                     selection.take( *todoItr.current() );
00134                     // make sure object stays selected
00135                     todoItr.current()->setState( VObject::selected );
00136                 }
00137             }
00138         }
00139     }
00140     setSuccess( true );
00141 }
00142 
00143 void
00144 VZOrderCmd::unexecute()
00145 {
00146     if( m_state == sendToBack )
00147     {
00148         m_state = bringToFront;
00149         execute();
00150         m_state = sendToBack;
00151     }
00152     else if( m_state == bringToFront )
00153     {
00154         m_state = sendToBack;
00155         execute();
00156         m_state = bringToFront;
00157     }
00158     else if( m_state == up )
00159     {
00160         m_state = down;
00161         execute();
00162         m_state = up;
00163     }
00164     else if( m_state == down )
00165     {
00166         m_state = up;
00167         execute();
00168         m_state = down;
00169     }
00170     setSuccess( false );
00171 }
00172 
KDE Home | KDE Accessibility Home | Description of Access Keys