libs/flake

KoPathPointMoveCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006,2008-2009 Jan Hambrecht <jaham@gmx.net>
00003  * Copyright (C) 2006,2007 Thorsten Zachmann <zachmann@kde.org>
00004  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "KoPathPointMoveCommand.h"
00023 #include "KoPathPoint.h"
00024 #include <klocale.h>
00025 
00026 KoPathPointMoveCommand::KoPathPointMoveCommand(const QList<KoPathPointData> &pointData, const QPointF &offset, QUndoCommand *parent)
00027     : QUndoCommand(parent)
00028     , m_undoCalled(true)
00029 {
00030     setText(i18n("Move points"));
00031 
00032     foreach( const KoPathPointData &data, pointData ) {
00033         if (!m_points.contains(data)) {
00034             m_points[data] = offset;
00035             m_paths.insert(data.pathShape);
00036         }
00037     }
00038 }
00039 
00040 KoPathPointMoveCommand::KoPathPointMoveCommand(const QList<KoPathPointData> &pointData, const QList<QPointF> &offsets, QUndoCommand *parent)
00041     : QUndoCommand(parent)
00042     , m_undoCalled(true)
00043 {
00044     Q_ASSERT(pointData.count() == offsets.count());
00045 
00046     setText(i18n("Move points"));
00047 
00048     uint dataCount = pointData.count();
00049     for (uint i = 0; i < dataCount; ++i) {
00050         const KoPathPointData & data = pointData[i];
00051         if (!m_points.contains(data)) {
00052             m_points[data] = offsets[i];
00053             m_paths.insert(data.pathShape);
00054         }
00055     }
00056 }
00057 
00058 void KoPathPointMoveCommand::redo()
00059 {
00060     QUndoCommand::redo();
00061     if (! m_undoCalled)
00062         return;
00063 
00064     applyOffset( 1.0 );
00065 
00066     m_undoCalled = false;
00067 }
00068 
00069 void KoPathPointMoveCommand::undo()
00070 {
00071     QUndoCommand::undo();
00072     if (m_undoCalled)
00073         return;
00074 
00075     applyOffset( -1.0 );
00076 
00077     m_undoCalled = true;
00078 }
00079 
00080 void KoPathPointMoveCommand::applyOffset( qreal factor )
00081 {
00082     foreach(KoPathShape * path, m_paths) {
00083         // repaint old bounding rect
00084         path->update();
00085     }
00086 
00087     QMap<KoPathPointData, QPointF>::iterator it(m_points.begin());
00088     for (; it != m_points.end(); ++it) {
00089         KoPathShape * path = it.key().pathShape;
00090         // transform offset from document to shape coordinate system
00091         QPointF shapeOffset = path->documentToShape(factor*it.value()) - path->documentToShape(QPointF());
00092         QMatrix matrix;
00093         matrix.translate(shapeOffset.x(), shapeOffset.y());
00094 
00095         KoPathPoint * p = path->pointByIndex(it.key().pointIndex);
00096         if ( p )
00097             p->map(matrix, true);
00098     }
00099 
00100     foreach(KoPathShape * path, m_paths) {
00101         path->normalize();
00102         // repaint new bounding rect
00103         path->update();
00104     }
00105 }