karbon

valigncmd.cpp
1/* This file is doc of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20
21#include <tdelocale.h>
22
23#include "valigncmd.h"
24#include "vtransformcmd.h"
25#include "vdocument.h"
26#include "vselection.h"
27
28#include <kdebug.h>
29
30VAlignCmd::VAlignCmd( VDocument *doc, Align align )
31 : VCommand( doc, i18n( "Align Objects" ) ), m_align( align )
32{
33 m_trafoCmds.setAutoDelete( true );
34}
35
36VAlignCmd::~VAlignCmd()
37{
38}
39
40void
42{
43 if( document()->selection()->objects().count() == 0 )
44 return;
45 double dx, dy;
46 KoRect bbox;
47 KoRect r;
48 if( document()->selection()->objects().count() == 1 )
49 r = document()->boundingBox();
50 else
51 r = document()->selection()->boundingBox();
52 VObjectList objs = document()->selection()->objects();
53 VObjectListIterator itr( objs );
54 VTranslateCmd *trafoCmd = 0L;
55 for( ; itr.current() ; ++itr )
56 {
57 document()->selection()->clear();
58 bbox = itr.current()->boundingBox();
59 switch( m_align )
60 {
61 case ALIGN_HORIZONTAL_LEFT : dx = r.topLeft().x() - bbox.topLeft().x(); dy = 0; break;
62 case ALIGN_HORIZONTAL_CENTER: dx = r.center().x() - bbox.center().x(); dy = 0; break;
63 case ALIGN_HORIZONTAL_RIGHT : dx = r.topRight().x() - bbox.topRight().x(); dy = 0; break;
64 case ALIGN_VERTICAL_TOP : dx = 0; dy = r.bottomRight().y() - bbox.bottomRight().y(); break;
65 case ALIGN_VERTICAL_CENTER : dx = 0; dy = r.center().y() - bbox.center().y(); break;
66 case ALIGN_VERTICAL_BOTTOM : dx = 0; dy = r.topLeft().y() - bbox.topLeft().y(); break;
67 };
68 document()->selection()->append( itr.current() );
69 trafoCmd = new VTranslateCmd( document(), dx, dy );
70 m_trafoCmds.append( trafoCmd );
71 trafoCmd->execute();
72 }
73 itr.toFirst();
74 for( ; itr.current() ; ++itr )
75 document()->selection()->append( itr.current() );
76 setSuccess( true );
77}
78
79void
81{
82 TQPtrListIterator<VTranslateCmd> itr( m_trafoCmds );
83 for( ; itr.current() ; ++itr )
84 itr.current()->unexecute();
85 setSuccess( false );
86}
87
The base class for all karbon commands.
Definition vcommand.h:42
virtual void unexecute()
Unexecutes the command.
Definition vcommand.h:75
VDocument * document() const
Returns the document the command works on.
Definition vcommand.h:121
virtual void execute()=0
Executes the command.
All non-visual, static doc info is in here.
Definition vdocument.h:53
VSelection * selection() const
Returns a pointer to the selection.
Definition vdocument.h:238
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
Definition vobject.h:111
const VObjectList & objects() const
Read only access to the selected objects.
Definition vselection.h:136
void append()
Adds all objects to the selection.
void clear()
Removes the references to all objects, not the objects themselves.
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
void setSuccess(bool success=true)
Set the success property.
Definition vvisitor.h:142