KoShapeMoveStrategy.cpp 2.83 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* This file is part of the KDE project

   Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
   Copyright (C) 2006 Thomas Zander <zander@kde.org>

   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 "KoShapeMoveStrategy.h"

#include <QPainter>
#include <QDebug>
#include <QMouseEvent>

#include "KoCanvasBase.h"
#include "KoShapeManager.h"
#include "KoSelection.h"
#include "KoGfxEvent.h"
#include "KoCommand.h"
#include "kcommand.h"

KoShapeMoveStrategy::KoShapeMoveStrategy( KoTool *tool, KoCanvasBase *canvas, const QPointF &clicked)
: KoInteractionStrategy(tool, canvas)
, m_initialTopLeft(99999, 99999)
, m_start(clicked)
{
    KoSelectionSet selectedObjects = canvas->shapeManager()->selection()->selectedObjects(KoFlake::StrippedSelection);
41
    QRectF boundingRect;
Thomas Zander's avatar
Thomas Zander committed
42 43 44 45 46 47
    foreach(KoShape *shape, selectedObjects) {
        if(shape->isLocked())
            continue;
        m_selectedObjects << shape;
        m_previousPositions << shape->position();
        m_newPositions << shape->position();
48
        boundingRect = boundingRect.unite( shape->boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
49
    }
50
    m_initialTopLeft = boundingRect.topLeft();
Thomas Zander's avatar
Thomas Zander committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
}

void KoShapeMoveStrategy::handleMouseMove(const QPointF &point, Qt::KeyboardModifiers modifiers) {
    m_diff = point - m_start;
    if(m_canvas->snapToGrid() && (modifiers & Qt::ShiftModifier) == 0) {
        QPointF newPos = m_initialTopLeft + m_diff;
        applyGrid(newPos);
        m_diff = newPos - m_initialTopLeft;
    }
    if(modifiers & (Qt::AltModifier | Qt::ControlModifier)) {
        // keep x or y position unchanged
        if(qAbs(m_diff.x()) < qAbs(m_diff.y()))
            m_diff.setX(0);
        else
            m_diff.setY(0);
    }

    int i=0;
    foreach(KoShape *shape, m_selectedObjects) {
        QPointF newPos (m_previousPositions.at(i) + m_diff);
        m_newPositions[i] = newPos;
        shape->repaint();
        shape->setPosition(newPos);
        shape->repaint();
        i++;
    }
}

KCommand* KoShapeMoveStrategy::createCommand() {
    if(m_diff.x() == 0 && m_diff.y() == 0)
        return 0;
    return new KoShapeMoveCommand(m_selectedObjects, m_previousPositions, m_newPositions);
}