-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchangeslog.cpp
242 lines (217 loc) · 5.61 KB
/
changeslog.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* PGR 2013 project
* Used for tracking changes for redo and undo buttons
*
* @author xskota05 Klara Vankova
* xsimet00 Vojtech Simetka
* @date 2013/11/26
* @version 1
* @file changeslog.cpp
*/
#include "changeslog.h"
#include "mainwindow.h"
ChangesLog *ChangesLog::changesLogInstance = new ChangesLog();
/**
* @brief ChangesLog destructor
*/
ChangesLog::~ChangesLog()
{
// TODO: Dealocate everything
}
/**
* @brief ChangesLog singleton constructor
* @return Singleton reference
*/
ChangesLog *ChangesLog::sharedInstance()
{
return changesLogInstance;
}
/**
* @brief Initializes changeslog
* @param d Data reference
*/
void ChangesLog::init(Data *d, Selection *selection)
{
this->data = d;
this->selection = selection;
this->lastChange = 0;
}
/**
* @brief Log one step
* @param a Action to log
* @param offsetX X offset if object was moved.
* @param offsetY Y offset if object was moved.
* @param object Object ID
*/
void ChangesLog::doStep(Actions a, int offsetX, int offsetY, void *object)
{
// Remove all possible redo
while ((int)changes.size() != lastChange)
changes.pop_back();
// Create new step
Step *s = new Step();
s->action = a;
s->offsetX = offsetX;
s->offsetY = offsetY;
s->object = object;
// Add step to list of changes
changes.push_back(s);
lastChange++;
}
/**
* @brief Log one step
* @param a Action to log
* @param offsetX X offset if object was moved.
* @param offsetY Y offset if object was moved.
* @param object Object ID
*/
void ChangesLog::doStep(Actions a, Point *origin, Point *changed, Qt::Corner orientation, void *object)
{
// Remove all possible redo
while ((int)changes.size() != lastChange)
changes.pop_back();
// Create new step
Step *s = new Step();
s->action = a;
s->origin = origin;
s->changed = changed;
s->orientation = orientation;
s->object = object;
// Add step to list of changes
changes.push_back(s);
lastChange++;
}
/**
* @brief Log one step
* @param a Action to log
* @param cor Centre of rotation
* @param angle How much should the object be rotated in radians
* @param object Object ID
*/
void ChangesLog::doStep(Actions a, Point *cor, float angle, void *object)
{
// Remove all possible redo
while ((int)changes.size() != lastChange)
changes.pop_back();
// Create new step
Step *s = new Step();
s->action = a;
s->cor = cor;
s->angle = angle;
s->object = object;
// Add step to list of changes
changes.push_back(s);
lastChange++;
}
/**
* @brief Undo step
*/
void ChangesLog::undoStep()
{
// Nothing to undo
if (!this->canUndo())
return;
// Point to last change
lastChange--;
std::vector<Element *> *list;
// Undo action
Step *s = (Step *)changes.at(lastChange);
switch (s->action)
{
case ADD:
this->selection->remove((Element*)s->object);
data->remove((Element*)s->object);
this->selection->calculateBoundingRectangle();
break;
case DELETE:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
data->add(e);
}
break;
case MOVE:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
e->translatef(-s->offsetX, -s->offsetY);
}
this->selection->calculateBoundingRectangle();
break;
case ROTATION:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
e->rotate(*s->cor,-s->angle);
}
this->selection->calculateBoundingRectangle();
break;
case RESIZE:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
this->selection->addBack(e);
}
this->selection->calculateBoundingRectangle();
this->selection->storeDistancesToFixedPoint();
this->selection->resizeSelectedItems(s->origin->getX(),
s->origin->getY(),
s->changed->getX(),
s->changed->getY(),
s->orientation);
break;
default:
break;
}
}
bool ChangesLog::canUndo()
{
if (changes.empty() || lastChange == 0)
return false;
return true;
}
/**
* @brief Redo step
*/
void ChangesLog::redoStep()
{
// Nothing to redo
if (!this->canRedo())
return;
std::vector<Element *> *list;
// Undo action
Step *s = (Step *)changes.at(lastChange);
switch (s->action)
{
case ADD:
data->add((Element*)s->object);
this->selection->addBack((Element*)s->object);
this->selection->calculateBoundingRectangle();
break;
case DELETE:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
data->remove(e);
}
break;
case MOVE:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
e->translatef(s->offsetX, s->offsetY);
}
this->selection->calculateBoundingRectangle();
break;
case ROTATION:
list = (std::vector<Element *> *) s->object;
foreach (Element *e, *list) {
e->rotate(*s->cor,s->angle);
}
this->selection->calculateBoundingRectangle();
break;
default:
break;
}
lastChange++;
}
bool ChangesLog::canRedo()
{
if (changes.empty() || ((int)changes.size() == lastChange))
return false;
return true;
}