libs/flake

KoShapeAlignCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2006 Jan Hambrecht <jaham@gmx.net>
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 "KoShapeAlignCommand.h"
00022 #include "KoShape.h"
00023 #include "KoShapeGroup.h"
00024 #include "commands/KoShapeMoveCommand.h"
00025 
00026 #include <klocale.h>
00027 // #include <kdebug.h>
00028 
00029 class KoShapeAlignCommand::Private
00030 {
00031 public:
00032     Private() : command(0) {}
00033     ~Private() {
00034         delete command;
00035     }
00036     KoShapeMoveCommand *command;
00037 };
00038 
00039 KoShapeAlignCommand::KoShapeAlignCommand(const QList<KoShape*> &shapes, Align align, QRectF boundingRect,
00040         QUndoCommand *parent)
00041         : QUndoCommand(parent),
00042         d(new Private())
00043 {
00044     QList<QPointF> previousPositions;
00045     QList<QPointF> newPositions;
00046     QPointF position;
00047     QPointF delta;
00048     QRectF bRect;
00049     foreach(KoShape *shape, shapes) {
00050 //   if (dynamic_cast<KoShapeGroup*> (shape))
00051 //       kDebug(30006) <<"Found Group";
00052 //   else if (dynamic_cast<KoShapeContainer*> (shape))
00053 //       kDebug(30006) <<"Found Container";
00054 //   else
00055 //       kDebug(30006) <<"Found shape";
00056         position = shape->position();
00057         previousPositions  << position;
00058         bRect = shape->boundingRect();
00059         switch (align) {
00060         case HorizontalLeftAlignment:
00061             delta = QPointF(boundingRect.left(), bRect.y()) - bRect.topLeft();
00062             break;
00063         case HorizontalCenterAlignment:
00064             delta = QPointF(boundingRect.center().x() - bRect.width() / 2, bRect.y()) - bRect.topLeft();
00065             break;
00066         case HorizontalRightAlignment:
00067             delta = QPointF(boundingRect.right() - bRect.width(), bRect.y()) - bRect.topLeft();
00068             break;
00069         case VerticalTopAlignment:
00070             delta = QPointF(bRect.x(), boundingRect.top()) - bRect.topLeft();
00071             break;
00072         case VerticalCenterAlignment:
00073             delta = QPointF(bRect.x(), boundingRect.center().y() - bRect.height() / 2) - bRect.topLeft();
00074             break;
00075         case VerticalBottomAlignment:
00076             delta = QPointF(bRect.x(), boundingRect.bottom() - bRect.height()) - bRect.topLeft();
00077             break;
00078         };
00079         newPositions  << position + delta;
00080 //kDebug(30006) <<"-> moving" <<  position.x() <<"," << position.y() <<" to" <<
00081 //        (position + delta).x() << ", " << (position+delta).y() << endl;
00082     }
00083     d->command = new KoShapeMoveCommand(shapes, previousPositions, newPositions);
00084 
00085     setText(i18n("Align shapes"));
00086 }
00087 
00088 KoShapeAlignCommand::~KoShapeAlignCommand()
00089 {
00090     delete d;
00091 }
00092 
00093 void KoShapeAlignCommand::redo()
00094 {
00095     QUndoCommand::redo();
00096     d->command->redo();
00097 }
00098 
00099 void KoShapeAlignCommand::undo()
00100 {
00101     QUndoCommand::undo();
00102     d->command->undo();
00103 }