File: action.cpp

package info (click to toggle)
ktuberling 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 103,256 kB
  • sloc: cpp: 1,569; xml: 226; python: 78; makefile: 8; sh: 2
file content (92 lines) | stat: -rw-r--r-- 2,007 bytes parent folder | download | duplicates (2)
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
41
42
43
44
45
46
47
48
49
50
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
84
85
86
87
88
89
90
91
92
/*
    SPDX-FileCopyrightText: 1999-2006 Éric Bischoff <ebischoff@nerim.net>
    SPDX-FileCopyrightText: 2007 Albert Astals Cid <aacid@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

/* Action stored in the undo buffer */

#include "action.h"

#include <QGraphicsScene>

#include "todraw.h"

ActionAdd::ActionAdd(ToDraw *item, QGraphicsScene *scene)
 : m_item(item), m_scene(scene), m_done(false), m_shouldAdd(false)
{
	// First m_shouldAdd is false since it was already added
	// in the playground code
}

ActionAdd::~ActionAdd()
{
	if (!m_done) delete m_item;
}

void ActionAdd::redo()
{
	if (m_shouldAdd) {
		m_scene->addItem(m_item);
	}
	m_done = true;
	m_shouldAdd = true;
}

void ActionAdd::undo()
{
	m_scene->removeItem(m_item);
	m_done = false;
}



ActionRemove::ActionRemove(ToDraw *item, const QPointF &oldPos, QGraphicsScene *scene)
 : m_item(item), m_scene(scene), m_done(true)
{
	m_oldPos = QPointF(oldPos.x() / scene->width(), oldPos.y() / scene->height());
}

ActionRemove::~ActionRemove()
{
	if (m_done) delete m_item;
}

void ActionRemove::redo()
{
	m_scene->removeItem(m_item);
	m_done = true;
}

void ActionRemove::undo()
{
	m_item->setPos(m_oldPos.x() * m_scene->width(), m_oldPos.y() * m_scene->height());
	m_scene->addItem(m_item);
	m_done = false;
}



ActionMove::ActionMove(ToDraw *item, const QPointF &oldPos, int zValue, QGraphicsScene *scene)
 : m_item(item), m_zValue(zValue), m_scene(scene)
{
	m_oldPos = QPointF(oldPos.x() / scene->width(), oldPos.y() / scene->height());
	m_newPos = QPointF(m_item->pos().x() / scene->width(), m_item->pos().y() / scene->height());
}

void ActionMove::redo()
{
	qreal zValue = m_item->zValue();
	m_item->setPos(m_newPos.x() * m_scene->width(), m_newPos.y() * m_scene->height());
	m_item->setZValue(m_zValue);
	m_zValue = zValue;
}

void ActionMove::undo()
{
	qreal zValue = m_item->zValue();
	m_item->setPos(m_oldPos.x() * m_scene->width(), m_oldPos.y() * m_scene->height());
	m_item->setZValue(m_zValue);
	m_zValue = zValue;
}