-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.cpp
More file actions
94 lines (81 loc) · 2.44 KB
/
commands.cpp
File metadata and controls
94 lines (81 loc) · 2.44 KB
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
93
94
#include "commands.h"
AddNodeCommand::AddNodeCommand(GraphWidget* graphWidget, QPoint initialPosition, QUndoCommand *parent)
: QUndoCommand(parent)
{
this->graphWidget = graphWidget;
this->initialPosition = initialPosition;
node = new Node(graphWidget);
node->setPos(initialPosition);
graphWidget->scene()->addItem(node);
graphWidget->update();
//printf("%le %le\n", this->node->getPosition().x(), this->node->getPosition().y());
QString commandString = QObject::tr("Node at (%1, %2)").arg(initialPosition.x()).arg(initialPosition.y());
setText(QObject::tr("Add %1").arg(commandString));
}
AddNodeCommand::~AddNodeCommand()
{
}
void AddNodeCommand::undo()
{
graphWidget->scene()->removeItem(node);
graphWidget->update();
}
void AddNodeCommand::redo()
{
graphWidget->scene()->addItem(node);
node->setPosition(initialPosition);
graphWidget->scene()->clearSelection();
graphWidget->update();
}
DeleteNodeCommand::DeleteNodeCommand(GraphWidget* graphWidget, Node* node, QUndoCommand *parent)
: QUndoCommand(parent)
{
this->graphWidget = graphWidget;
this->node = node;
graphWidget->scene()->removeItem(node);
graphWidget->update();
QString commandString = QObject::tr("Deleted Node");
setText(QObject::tr("Delete %1").arg(commandString));
}
DeleteNodeCommand::~DeleteNodeCommand()
{
}
void DeleteNodeCommand::undo()
{
graphWidget->scene()->addItem(node);
graphWidget->update();
}
void DeleteNodeCommand::redo()
{
graphWidget->scene()->removeItem(node);
graphWidget->update();
}
AddEdgeCommand::AddEdgeCommand(GraphWidget* graphWidget, Node* source, Node* dest, QUndoCommand *parent)
: QUndoCommand(parent)
{
this->graphWidget = graphWidget;
this->edge = new Edge(source, dest);
source->addEdge(this->edge);
dest->addEdge(this->edge);
graphWidget->scene()->addItem(edge);
graphWidget->update();
QString commandString = QObject::tr("Edge from %1 to %2)").arg(source->type()).arg(dest->type());
setText(QObject::tr("Add %1").arg(commandString));
}
AddEdgeCommand::~AddEdgeCommand()
{
}
void AddEdgeCommand::undo()
{
graphWidget->scene()->removeItem(edge);
edge->sourceNode()->removeEdge(edge);
edge->destNode()->removeEdge(edge);
graphWidget->update();
}
void AddEdgeCommand::redo()
{
graphWidget->scene()->addItem(edge);
edge->sourceNode()->addEdge(edge);
edge->destNode()->addEdge(edge);
graphWidget->update();
}