KoShapeTransformCommand.cpp 2.91 KB
Newer Older
1 2
/* This file is part of the KDE project
 * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
3
 * Copyright (C) 2007 Thomas Zander <zander@kde.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

21 22
#include "kis_command_ids.h"

23 24 25
#include "KoShapeTransformCommand.h"
#include "KoShape.h"

26
#include <QList>
27
#include <QTransform>
28

Boudewijn Rempt's avatar
Boudewijn Rempt committed
29
#include <FlakeDebug.h>
30

31
class Q_DECL_HIDDEN KoShapeTransformCommand::Private
32
{
33 34 35
public:
    Private(const QList<KoShape*> &list) : shapes(list) { }
    QList<KoShape*> shapes;
36 37
    QList<QTransform> oldState;
    QList<QTransform> newState;
38 39
};

Alexander Potashev's avatar
Alexander Potashev committed
40 41
KoShapeTransformCommand::KoShapeTransformCommand(const QList<KoShape*> &shapes, const QList<QTransform> &oldState, const QList<QTransform> &newState, KUndo2Command * parent)
        : KUndo2Command(parent),
42
        d(new Private(shapes))
43
{
44 45
    Q_ASSERT(shapes.count() == oldState.count());
    Q_ASSERT(shapes.count() == newState.count());
46 47
    d->oldState = oldState;
    d->newState = newState;
48 49
}

Laurent Montel's avatar
Laurent Montel committed
50 51 52 53 54
KoShapeTransformCommand::~KoShapeTransformCommand()
{
    delete d;
}

55 56
void KoShapeTransformCommand::redo()
{
Alexander Potashev's avatar
Alexander Potashev committed
57
    KUndo2Command::redo();
58 59

    const int shapeCount = d->shapes.count();
60
    for (int i = 0; i < shapeCount; ++i) {
61
        KoShape * shape = d->shapes[i];
62
        const QRectF oldDirtyRect = shape->boundingRect();
63
        shape->setTransformation(d->newState[i]);
64
        shape->updateAbsolute(oldDirtyRect | shape->boundingRect());
65 66 67 68 69
    }
}

void KoShapeTransformCommand::undo()
{
Alexander Potashev's avatar
Alexander Potashev committed
70
    KUndo2Command::undo();
71

72
    const int shapeCount = d->shapes.count();
73
    for (int i = 0; i < shapeCount; ++i) {
74
        KoShape * shape = d->shapes[i];
75
        const QRectF oldDirtyRect = shape->boundingRect();
76
        shape->setTransformation(d->oldState[i]);
77
        shape->updateAbsolute(oldDirtyRect | shape->boundingRect());
78 79
    }
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

int KoShapeTransformCommand::id() const
{
    return KisCommandUtils::TransformShapeId;
}

bool KoShapeTransformCommand::mergeWith(const KUndo2Command *command)
{
    const KoShapeTransformCommand *other = dynamic_cast<const KoShapeTransformCommand*>(command);

    if (!other ||
        other->d->shapes != d->shapes ||
        other->text() != text()) {

        return false;
    }

    d->newState = other->d->newState;
    return true;
}