File indexing completed on 2024-09-15 03:46:01
0001 /* 0002 SPDX-FileCopyrightText: 1999-2006 Éric Bischoff <ebischoff@nerim.net> 0003 SPDX-FileCopyrightText: 2007 Albert Astals Cid <aacid@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 /* Action stored in the undo buffer */ 0009 0010 #include "action.h" 0011 0012 #include <QGraphicsScene> 0013 0014 #include "todraw.h" 0015 0016 ActionAdd::ActionAdd(ToDraw *item, QGraphicsScene *scene) 0017 : m_item(item), m_scene(scene), m_done(false), m_shouldAdd(false) 0018 { 0019 // First m_shouldAdd is false since it was already added 0020 // in the playground code 0021 } 0022 0023 ActionAdd::~ActionAdd() 0024 { 0025 if (!m_done) delete m_item; 0026 } 0027 0028 void ActionAdd::redo() 0029 { 0030 if (m_shouldAdd) { 0031 m_scene->addItem(m_item); 0032 } 0033 m_done = true; 0034 m_shouldAdd = true; 0035 } 0036 0037 void ActionAdd::undo() 0038 { 0039 m_scene->removeItem(m_item); 0040 m_done = false; 0041 } 0042 0043 0044 0045 ActionRemove::ActionRemove(ToDraw *item, const QPointF &oldPos, QGraphicsScene *scene) 0046 : m_item(item), m_scene(scene), m_done(true) 0047 { 0048 m_oldPos = QPointF(oldPos.x() / scene->width(), oldPos.y() / scene->height()); 0049 } 0050 0051 ActionRemove::~ActionRemove() 0052 { 0053 if (m_done) delete m_item; 0054 } 0055 0056 void ActionRemove::redo() 0057 { 0058 m_scene->removeItem(m_item); 0059 m_done = true; 0060 } 0061 0062 void ActionRemove::undo() 0063 { 0064 m_item->setPos(m_oldPos.x() * m_scene->width(), m_oldPos.y() * m_scene->height()); 0065 m_scene->addItem(m_item); 0066 m_done = false; 0067 } 0068 0069 0070 0071 ActionMove::ActionMove(ToDraw *item, const QPointF &oldPos, int zValue, QGraphicsScene *scene) 0072 : m_item(item), m_zValue(zValue), m_scene(scene) 0073 { 0074 m_oldPos = QPointF(oldPos.x() / scene->width(), oldPos.y() / scene->height()); 0075 m_newPos = QPointF(m_item->pos().x() / scene->width(), m_item->pos().y() / scene->height()); 0076 } 0077 0078 void ActionMove::redo() 0079 { 0080 qreal zValue = m_item->zValue(); 0081 m_item->setPos(m_newPos.x() * m_scene->width(), m_newPos.y() * m_scene->height()); 0082 m_item->setZValue(m_zValue); 0083 m_zValue = zValue; 0084 } 0085 0086 void ActionMove::undo() 0087 { 0088 qreal zValue = m_item->zValue(); 0089 m_item->setPos(m_oldPos.x() * m_scene->width(), m_oldPos.y() * m_scene->height()); 0090 m_item->setZValue(m_zValue); 0091 m_zValue = zValue; 0092 }