File indexing completed on 2024-11-10 04:01:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Boudewijn Rempt <boud@kde.org>
0003  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 #ifndef KIS_MOVE_COMMAND_COMMON_H
0008 #define KIS_MOVE_COMMAND_COMMON_H
0009 
0010 #include <QPoint>
0011 #include <klocalizedstring.h>
0012 #include "kundo2command.h"
0013 #include "kritaimage_export.h"
0014 #include "kis_types.h"
0015 
0016 /**
0017  * KisMoveCommandCommon is a general template for a command that moves
0018  * entities capable of setX() and setY() actions. Generally in Krita
0019  * you should now move the device itself, only the node containing
0020  * that device. But the case of the selections is a bit special, so we
0021  * move them separately.
0022  */
0023 template <class ObjectSP>
0024 class KRITAIMAGE_EXPORT KisMoveCommandCommon : public KUndo2Command
0025 {
0026 public:
0027     KisMoveCommandCommon(ObjectSP object, const QPoint& oldPos, const QPoint& newPos, KUndo2Command *parent = 0)
0028         : KUndo2Command(kundo2_i18n("Move"), parent),
0029           m_oldPos(oldPos),
0030           m_newPos(newPos),
0031           m_object(object)
0032     {
0033     }
0034 
0035     void redo() override {
0036         moveTo(m_newPos);
0037     }
0038 
0039     void undo() override {
0040         moveTo(m_oldPos);
0041     }
0042 
0043 private:
0044     void moveTo(const QPoint& pos) {
0045         /**
0046          * FIXME: Hack alert:
0047          * Our iterators don't have guarantees on thread-safety
0048          * when the offset varies. When it is fixed, remove the locking.
0049          * see: KisIterator::stressTest(), KisToolMove::mousePressEvent()
0050          */
0051         m_object->setX(pos.x());
0052         m_object->setY(pos.y());
0053     }
0054 
0055 private:
0056     QPoint m_oldPos;
0057     QPoint m_newPos;
0058 
0059 protected:
0060     ObjectSP m_object;
0061 };
0062 
0063 #endif