karbon
valigncmd.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
|