1 /* This file is part of the KDE project
2  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2006 Jan Hambrecht <jaham@gmx.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "KoShapeMoveCommand.h"
22 
23 #include <KoShape.h>
24 #include <KoShapeContainer.h>
25 #include <KoShapeContainerModel.h>
26 #include <KoShapeAnchor.h>
27 
28 #include <klocalizedstring.h>
29 
30 class Q_DECL_HIDDEN KoShapeMoveCommand::Private
31 {
32 public:
33     QList<KoShape*> shapes;
34     QVector<QPointF> previousPositions, newPositions, previousOffsets, newOffsets;
35 };
36 
KoShapeMoveCommand(const QList<KoShape * > & shapes,const QVector<QPointF> & previousPositions,const QVector<QPointF> & newPositions,KUndo2Command * parent)37 KoShapeMoveCommand::KoShapeMoveCommand(const QList<KoShape*> &shapes,
38                                        const QVector<QPointF> &previousPositions, const QVector<QPointF> &newPositions,
39                                        KUndo2Command *parent)
40     : KUndo2Command(parent),
41     d(new Private())
42 {
43     d->shapes = shapes;
44     d->previousPositions = previousPositions;
45     d->newPositions = newPositions;
46     Q_ASSERT(d->shapes.count() == d->previousPositions.count());
47     Q_ASSERT(d->shapes.count() == d->newPositions.count());
48 
49     setText(kundo2_i18n("Move shapes"));
50 }
51 
KoShapeMoveCommand(const QList<KoShape * > & shapes,const QVector<QPointF> & previousPositions,const QVector<QPointF> & newPositions,const QVector<QPointF> & previousOffsets,const QVector<QPointF> & newOffsets,KUndo2Command * parent)52 KoShapeMoveCommand::KoShapeMoveCommand(const QList<KoShape*> &shapes,
53                                        const QVector<QPointF> &previousPositions, const QVector<QPointF> &newPositions,
54                                        const QVector<QPointF> &previousOffsets, const QVector<QPointF> &newOffsets,
55                                        KUndo2Command *parent)
56         : KUndo2Command(parent),
57         d(new Private())
58 {
59     d->shapes = shapes;
60     d->previousPositions = previousPositions;
61     d->newPositions = newPositions;
62     d->previousOffsets = previousOffsets;
63     d->newOffsets = newOffsets;
64     Q_ASSERT(d->shapes.count() == d->previousPositions.count());
65     Q_ASSERT(d->shapes.count() == d->newPositions.count());
66 
67     setText(kundo2_i18n("Move shapes"));
68 }
69 
~KoShapeMoveCommand()70 KoShapeMoveCommand::~KoShapeMoveCommand()
71 {
72     delete d;
73 }
74 
redo()75 void KoShapeMoveCommand::redo()
76 {
77     KUndo2Command::redo();
78     for (int i = 0; i < d->shapes.count(); i++) {
79         d->shapes.at(i)->update();
80         if (d->shapes.at(i)->anchor() && !d->newOffsets.isEmpty()) {
81             d->shapes.at(i)->anchor()->setOffset(d->newOffsets.at(i));
82         }
83         d->shapes.at(i)->setPosition(d->newPositions.at(i));
84         d->shapes.at(i)->update();
85     }
86 }
87 
undo()88 void KoShapeMoveCommand::undo()
89 {
90     KUndo2Command::undo();
91     for (int i = 0; i < d->shapes.count(); i++) {
92         d->shapes.at(i)->update();
93         if (d->shapes.at(i)->anchor() && !d->previousOffsets.isEmpty()) {
94             d->shapes.at(i)->anchor()->setOffset(d->previousOffsets.at(i));
95         }
96         d->shapes.at(i)->setPosition(d->previousPositions.at(i));
97         d->shapes.at(i)->update();
98     }
99 }
100 
101 /// update newPositions list with new positions.
setNewPositions(const QVector<QPointF> & newPositions)102 void KoShapeMoveCommand::setNewPositions(const QVector<QPointF> &newPositions)
103 {
104     d->newPositions = newPositions;
105 }
106