File indexing completed on 2025-02-02 04:14:49

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
0003  * SPDX-FileCopyrightText: 2006 Jan Hambrecht <jaham@gmx.net>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "KoShapeAlignCommand.h"
0009 #include "KoShape.h"
0010 #include "KoShapeGroup.h"
0011 #include "commands/KoShapeMoveCommand.h"
0012 
0013 #include <klocalizedstring.h>
0014 // #include <FlakeDebug.h>
0015 
0016 class Q_DECL_HIDDEN KoShapeAlignCommand::Private
0017 {
0018 public:
0019     Private() : command(0) {}
0020     ~Private() {
0021         delete command;
0022     }
0023     KoShapeMoveCommand *command;
0024 };
0025 
0026 KoShapeAlignCommand::KoShapeAlignCommand(const QList<KoShape*> &shapes, Align align, const QRectF &boundingRect, KUndo2Command *parent)
0027         : KUndo2Command(parent),
0028         d(new Private())
0029 {
0030     QList<QPointF> previousPositions;
0031     QList<QPointF> newPositions;
0032     QPointF position;
0033     QPointF delta;
0034     QRectF bRect;
0035     Q_FOREACH (KoShape *shape, shapes) {
0036 //   if (dynamic_cast<KoShapeGroup*> (shape))
0037 //       debugFlake <<"Found Group";
0038 //   else if (dynamic_cast<KoShapeContainer*> (shape))
0039 //       debugFlake <<"Found Container";
0040 //   else
0041 //       debugFlake <<"Found shape";
0042         position = shape->absolutePosition();
0043         previousPositions  << position;
0044         bRect = shape->absoluteOutlineRect();
0045         switch (align) {
0046         case HorizontalLeftAlignment:
0047             delta = QPointF(boundingRect.left(), bRect.y()) - bRect.topLeft();
0048             break;
0049         case HorizontalCenterAlignment:
0050             delta = QPointF(boundingRect.center().x() - bRect.width() / 2, bRect.y()) - bRect.topLeft();
0051             break;
0052         case HorizontalRightAlignment:
0053             delta = QPointF(boundingRect.right() - bRect.width(), bRect.y()) - bRect.topLeft();
0054             break;
0055         case VerticalTopAlignment:
0056             delta = QPointF(bRect.x(), boundingRect.top()) - bRect.topLeft();
0057             break;
0058         case VerticalCenterAlignment:
0059             delta = QPointF(bRect.x(), boundingRect.center().y() - bRect.height() / 2) - bRect.topLeft();
0060             break;
0061         case VerticalBottomAlignment:
0062             delta = QPointF(bRect.x(), boundingRect.bottom() - bRect.height()) - bRect.topLeft();
0063             break;
0064         };
0065         newPositions  << position + delta;
0066 //debugFlake <<"-> moving" <<  position.x() <<"," << position.y() <<" to" <<
0067 //        (position + delta).x() << ", " << (position+delta).y() << endl;
0068     }
0069     d->command = new KoShapeMoveCommand(shapes, previousPositions, newPositions);
0070 
0071     setText(kundo2_i18n("Align shapes"));
0072 }
0073 
0074 KoShapeAlignCommand::~KoShapeAlignCommand()
0075 {
0076     delete d;
0077 }
0078 
0079 void KoShapeAlignCommand::redo()
0080 {
0081     KUndo2Command::redo();
0082     d->command->redo();
0083 }
0084 
0085 void KoShapeAlignCommand::undo()
0086 {
0087     KUndo2Command::undo();
0088     d->command->undo();
0089 }