1/***************************************************************************
2 * Copyright (C) 1999-2006 by Éric Bischoff <ebischoff@nerim.net> *
3 * Copyright (C) 2007 by Albert Astals Cid <aacid@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
10
11/* Action stored in the undo buffer */
12
13#include "action.h"
14
15#include <QGraphicsScene>
16
17#include "todraw.h"
18
19ActionAdd::ActionAdd(ToDraw *item, QGraphicsScene *scene)
20 : m_item(item), m_scene(scene), m_done(false)
21{
22}
23
24ActionAdd::~ActionAdd()
25{
26 if (!m_done) delete m_item;
27}
28
29void ActionAdd::redo()
30{
31 m_scene->addItem(m_item);
32 m_done = true;
33}
34
35void ActionAdd::undo()
36{
37 m_scene->removeItem(m_item);
38 m_done = false;
39}
40
41
42
43ActionRemove::ActionRemove(ToDraw *item, QGraphicsScene *scene)
44 : m_item(item), m_scene(scene), m_done(true)
45{
46}
47
48ActionRemove::~ActionRemove()
49{
50 if (m_done) delete m_item;
51}
52
53void ActionRemove::redo()
54{
55 if (!m_done)
56 {
57 m_scene->removeItem(m_item);
58 m_done = true;
59 }
60}
61
62void ActionRemove::undo()
63{
64 m_scene->addItem(m_item);
65 m_done = false;
66}
67
68
69
70ActionMove::ActionMove(ToDraw *item, const QPointF &pos, int zValue, QGraphicsScene *scene)
71 : m_item(item), m_zValue(zValue), m_scene(scene)
72{
73 m_pos = QPointF(pos.x() / scene->width(), pos.y() / scene->height());
74}
75
76void ActionMove::redo()
77{
78 QPointF pos = m_item->pos();
79 qreal zValue = m_item->zValue();
80 m_item->setPos(m_pos.x() * m_scene->width(), m_pos.y() * m_scene->height());
81 m_item->setZValue(m_zValue);
82 m_pos = QPointF(pos.x() / m_scene->width(), pos.y() / m_scene->height());
83 m_zValue = zValue;
84}
85
86void ActionMove::undo()
87{
88 redo();
89}
90