KoPathPointMoveCommand.cpp 2.57 KB
Newer Older
1
/* This file is part of the KDE project
2
 * Copyright (C) 2006,2008 Jan Hambrecht <jaham@gmx.net>
3
 * Copyright (C) 2006,2007 Thorsten Zachmann <zachmann@kde.org>
4
 * Copyright (C) 2007 Thomas Zander <zander@kde.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */

#include "KoPathPointMoveCommand.h"
23
#include "KoPathPoint.h"
24 25
#include <klocale.h>

26
KoPathPointMoveCommand::KoPathPointMoveCommand(const QList<KoPathPointData> &pointData, const QPointF &offset, QUndoCommand *parent)
27 28 29
        : QUndoCommand(parent)
        , m_offset(offset)
        , m_undoCalled(true)
30
{
31
    setText(i18n("Move points"));
32

33 34
    foreach( const KoPathPointData &data, pointData ) {
        m_points[data.pathShape].insert( data.pointIndex );
35
    }
36 37 38 39
}

void KoPathPointMoveCommand::redo()
{
40
    QUndoCommand::redo();
41
    if (! m_undoCalled)
42
        return;
43 44 45
    
    applyOffset( m_offset );
    
46
    m_undoCalled = false;
47 48 49 50
}

void KoPathPointMoveCommand::undo()
{
51
    QUndoCommand::undo();
52
    if (m_undoCalled)
53
        return;
54 55 56
    
    applyOffset( -1.0 * m_offset );

57
    m_undoCalled = true;
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71
void KoPathPointMoveCommand::applyOffset( const QPointF &offset )
{
    QMap<KoPathShape*, QSet<KoPathPointIndex> >::iterator it(m_points.begin());
    for (; it != m_points.end(); ++it) {
        KoPathShape * path = it.key();
        // transform offset from document to shape coordinate system
        QPointF shapeOffset = path->documentToShape(offset) - path->documentToShape(QPointF());
        QMatrix matrix;
        matrix.translate(shapeOffset.x(), shapeOffset.y());
        
        // repaint old bounding rect
        path->update();
Jan Hambrecht's avatar
Jan Hambrecht committed
72
        foreach(const KoPathPointIndex & index, it.value()) {
73 74 75 76 77 78 79 80 81
            KoPathPoint * p = path->pointByIndex(index);
            if ( p )
                p->map(matrix, true);
        }
        path->normalize();
        // repaint new bounding rect
        path->update();
    }
}