karbon

vgroupcmd.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, The Karbon Developers
00003    Copyright (C) 2002, The Karbon Developers
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <klocale.h>
00022 
00023 #include "vgroup.h"
00024 #include "vgroupcmd.h"
00025 #include "vselection.h"
00026 #include "vdocument.h"
00027 #include "vlayer.h"
00028 
00029 VGroupCmd::VGroupCmd( VDocument *doc )
00030     : VCommand( doc, i18n( "Group Objects" ), "14_group" )
00031 {
00032     m_selection = document()->selection()->clone();
00033 
00034     m_group = 0L;
00035 }
00036 
00037 VGroupCmd::~VGroupCmd()
00038 {
00039     delete( m_selection );
00040 }
00041 
00042 void
00043 VGroupCmd::execute()
00044 {
00045     m_group = new VGroup( document()->activeLayer() );
00046 
00047     VObjectListIterator itr( m_selection->objects() );
00048     for ( ; itr.current() ; ++itr )
00049     {
00050         // remove from corresponding parent
00051         VGroup *parent = dynamic_cast<VGroup*>( itr.current()->parent() );
00052         if( parent )
00053             parent->take( *itr.current() );
00054 
00055         m_group->append( itr.current() );
00056     }
00057 
00058     document()->append( m_group );
00059     document()->selection()->clear();
00060     document()->selection()->append( m_group );
00061 
00062     setSuccess( true );
00063 }
00064 
00065 void
00066 VGroupCmd::unexecute()
00067 {
00068     if( ! m_group ) 
00069         return;
00070 
00071     document()->selection()->clear();
00072 
00073     VObjectListIterator itr( m_group->objects() );
00074     for ( ; itr.current() ; ++itr )
00075     {
00076         // TODO : remove from corresponding VLayer
00077         document()->selection()->append( itr.current() );
00078     }
00079 
00080     VGroup* parent;
00081     if( ( parent = dynamic_cast<VGroup*>( m_group->parent() ) ) )
00082     {
00083         // unregister from parent:
00084         parent->take( *m_group );
00085 
00086         // inform all objects in this group about their new parent
00087         VObjectListIterator itr = m_selection->objects();
00088 
00089         for ( ; itr.current() ; ++itr )
00090         {
00091             parent->append( itr.current() );
00092         }
00093 
00094         m_group->clear();
00095         m_group->setState( VObject::deleted );
00096     }
00097     
00098     setSuccess( false );
00099 }
00100 
KDE Home | KDE Accessibility Home | Description of Access Keys