karbon
valigncmd.cc
00001 /* This file is doc of the KDE project 00002 Copyright (C) 2001, 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 00021 #include <klocale.h> 00022 00023 #include "valigncmd.h" 00024 #include "vtransformcmd.h" 00025 #include "vdocument.h" 00026 #include "vselection.h" 00027 00028 #include <kdebug.h> 00029 00030 VAlignCmd::VAlignCmd( VDocument *doc, Align align ) 00031 : VCommand( doc, i18n( "Align Objects" ) ), m_align( align ) 00032 { 00033 m_trafoCmds.setAutoDelete( true ); 00034 } 00035 00036 VAlignCmd::~VAlignCmd() 00037 { 00038 } 00039 00040 void 00041 VAlignCmd::execute() 00042 { 00043 if( document()->selection()->objects().count() == 0 ) 00044 return; 00045 double dx, dy; 00046 KoRect bbox; 00047 KoRect r; 00048 if( document()->selection()->objects().count() == 1 ) 00049 r = document()->boundingBox(); 00050 else 00051 r = document()->selection()->boundingBox(); 00052 VObjectList objs = document()->selection()->objects(); 00053 VObjectListIterator itr( objs ); 00054 VTranslateCmd *trafoCmd = 0L; 00055 for( ; itr.current() ; ++itr ) 00056 { 00057 document()->selection()->clear(); 00058 bbox = itr.current()->boundingBox(); 00059 switch( m_align ) 00060 { 00061 case ALIGN_HORIZONTAL_LEFT : dx = r.topLeft().x() - bbox.topLeft().x(); dy = 0; break; 00062 case ALIGN_HORIZONTAL_CENTER: dx = r.center().x() - bbox.center().x(); dy = 0; break; 00063 case ALIGN_HORIZONTAL_RIGHT : dx = r.topRight().x() - bbox.topRight().x(); dy = 0; break; 00064 case ALIGN_VERTICAL_TOP : dx = 0; dy = r.bottomRight().y() - bbox.bottomRight().y(); break; 00065 case ALIGN_VERTICAL_CENTER : dx = 0; dy = r.center().y() - bbox.center().y(); break; 00066 case ALIGN_VERTICAL_BOTTOM : dx = 0; dy = r.topLeft().y() - bbox.topLeft().y(); break; 00067 }; 00068 document()->selection()->append( itr.current() ); 00069 trafoCmd = new VTranslateCmd( document(), dx, dy ); 00070 m_trafoCmds.append( trafoCmd ); 00071 trafoCmd->execute(); 00072 } 00073 itr.toFirst(); 00074 for( ; itr.current() ; ++itr ) 00075 document()->selection()->append( itr.current() ); 00076 setSuccess( true ); 00077 } 00078 00079 void 00080 VAlignCmd::unexecute() 00081 { 00082 QPtrListIterator<VTranslateCmd> itr( m_trafoCmds ); 00083 for( ; itr.current() ; ++itr ) 00084 itr.current()->unexecute(); 00085 setSuccess( false ); 00086 } 00087

