forked from Vadixem/qAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapmanager.cpp
341 lines (290 loc) · 8.77 KB
/
swapmanager.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "swapmanager.h"
#include "swapanimation.h"
SwapManager::SwapManager(GraphicsView &window, QMap<QString, void (*)(GraphicsView&, SwapManager&)> *sortFunctions ,unsigned int actionInterval,QObject *par) :
QObject(par), actionInterval(actionInterval) ,window(window), sortFunctions(sortFunctions), currentSortType(QString("Insertion Sort"))
{
setLastAction(Actions::Back);
currentPairIndex = -1;
nextPairActionTimer = new QTimer(this);
nextPairActionTimer->setTimerType(Qt::PreciseTimer);
nextPairActionTimer->setInterval(actionInterval);
nextPairActionTimer->setSingleShot(false);
connect(nextPairActionTimer, SIGNAL(timeout()), this, SLOT(slotStepForward()));
// When index reaches the end of action then stop the timer
connect(this, SIGNAL(indexReachedEnd()), this, SLOT(stopNextPairActionTimer()));
}
bool SwapManager::addPair(int i, int j, AnimationType atype)
{
if (i != j)
actionPairs.append(QPair<QPair<int,int>, AnimationType>(QPair<int,int>(i, j), atype));
return true;
}
int SwapManager::getTimerInterval() const
{
return nextPairActionTimer->interval();
}
QPair<QPair<int,int>, AnimationType> SwapManager::getCurrentPair()
{
if (currentPairIndex < 0)
return actionPairs[0];
else if (currentPairIndex > actionPairs.size() - 1)
return actionPairs[actionPairs.size() - 1];
return actionPairs[currentPairIndex];
}
int SwapManager::getCurrentPairIndex() const
{
return currentPairIndex;
}
void SwapManager::setCurrentPairIndex(int index)
{
currentPairIndex = index;
}
Actions SwapManager::getLastAction() const
{
return lastAction;
}
void SwapManager::setLastAction(Actions action)
{
this->lastAction = action;
}
bool SwapManager::isAnimationActive()
{
return nextPairActionTimer->isActive();
}
void SwapManager::startNextPairActionTimer()
{
if (actionPairs.isEmpty() || currentPairIndex == actionPairs.size())
return;
nextPairActionTimer->start();
emit actionTimerStarted();
nextPairActionTimer->setInterval(actionInterval + 30);
}
void SwapManager::stopNextPairActionTimer(bool b)
{
if (b)
{
if (currentPairIndex != actionPairs.size())
emit actionTimerStoppedInMiddle();
nextPairActionTimer->stop();
}
}
/**
* @brief SwapManager::slotStartAction performs action depending on action mode
* @param actMode can be animation or no animation
*/
void SwapManager::slotStartAction(ActionMode actMode)
{
slotPutItemsInStr8Line();
int i = getCurrentPair().first.first;
int j = getCurrentPair().first.second;
auto actionType = getCurrentPair().second;
auto w1 = static_cast<QGraphicsRectWidget*>(window.items().at(i));
auto w2 = static_cast<QGraphicsRectWidget*>(window.items().at(j));
switch (actionType)
{
case AnimationType::Comparison:
{
switch(actMode)
{
case ActionMode::Animation:
{
QParallelAnimationGroup *pparAnim = new QParallelAnimationGroup(this);
QPropertyAnimation *panim = new QPropertyAnimation(w1, "pos");
panim->setDuration(actionInterval);
panim->setKeyValueAt(0.5, QPoint(w1->pos().x(), w1->pos().y() - w1->geometry().y() * 0.1));
panim->setEndValue(w1->pos());
pparAnim->addAnimation(panim);
panim = new QPropertyAnimation(w2, "pos");
panim->setDuration(actionInterval);
panim->setKeyValueAt(0.5, QPoint(w2->pos().x(), w2->pos().y() - w2->geometry().y() * 0.1));
panim->setEndValue(w2->pos());
pparAnim->addAnimation(panim);
pparAnim->start();
break;
}
default: // If mode is no animation then do nothing
break;
}
break;
}
case AnimationType::Swap:
{
/* SWAP Z VALUES OF THOSE ITEMS in order to prevent calling wrong item via window.items().at()*/
auto zVal = w1->zValue();
w1->setZValue(w2->zValue());
w2->setZValue(zVal);
switch (actMode)
{
case ActionMode::Animation:
{
double swapHeightRatio;
if (abs(w1->zValue() - w2->zValue()) == 1)
swapHeightRatio = 0.75;
else swapHeightRatio = 1.2;
auto swapAnim = getSwapAnimation(w1, w2, actionInterval, swapHeightRatio);
swapAnim->setParent(this);
swapAnim->start();
break;
}
default:
auto pos = w1->pos();
w1->setPos(w2->pos());
w2->setPos(pos);
break;
}
break;
}
}
}
/**
* @brief slotCheckIndex Checks position of index. If it reached end or beginning of history emits corresponding signal
*/
void SwapManager::slotCheckIndex()
{
if (currentPairIndex == -1)
{
emit indexReachedBegin(); // Disable back button etc.
setLastAction(Actions::Back);
}
else if (currentPairIndex == actionPairs.size())
{
emit indexReachedEnd(); // Disable forward button etc.
setLastAction(Actions::Forward);
}
}
/**
* @brief slotStepForward Allows to perform single swap(or other animation) forward
* @brief Must be connected to its's stackPopTimer timeout() signal
*/
void SwapManager::slotStepForward(ActionMode actmode)
{
slotCheckIndex();
// If swap consequence is empty, then generate new
if (actionPairs.isEmpty())
{
slotFillActionPairs();
}
if (getLastAction() == Actions::Back)
{
++currentPairIndex;
slotStartAction(actmode);
++currentPairIndex;
setLastAction(Actions::Forward);
}
else // LastAction == Forward
{
slotStartAction(actmode);
++currentPairIndex;
setLastAction(Actions::Forward);
}
slotCheckIndex();
}
/**
* @brief slotStepBack Allows to perform single animation back
*/
void SwapManager::slotStepBack(ActionMode actmode)
{
slotCheckIndex();
if (getLastAction() == Actions::Forward)
{
--currentPairIndex;
slotStartAction(actmode);
--currentPairIndex;
setLastAction(Actions::Back);
}
else // LastAction == Back
{
slotStartAction(actmode);
--currentPairIndex;
setLastAction(Actions::Back);
}
slotCheckIndex();
}
/**
* @brief slotDropToDefault called to remove all actionPairs, drop current Pair index to default(-1), set last action to back
*/
void SwapManager::slotDropToDefault()
{
// Restore initial order
if (!actionPairs.isEmpty())
{
setCurrentPairIndex(-1);
actionPairs.clear();
setLastAction(Actions::Back);
emit droppedToDefault();
slotPutItemsInStr8Line();
}
}
/**
* @brief SetTimerInterval Changes speed of animation
* @param msec time in ms
*/
void SwapManager::setTimerInterval(int msec)
{
actionInterval = abs(msec);
nextPairActionTimer->setInterval(actionInterval + 30);
}
void SwapManager::slotHandleSpeedSlider(int val)
{
qreal min, max, maxVal;
min = 100.0;
max = 3000.0;
maxVal = 7.0;
setTimerInterval(max - (max * qreal(val)/maxVal) + min);
}
void SwapManager::slotFillActionPairs()
{
slotRestoreInitialOrder();
void (*func)(GraphicsView&, SwapManager&) = sortFunctions->find(currentSortType).value();
// Reset all data
slotDropToDefault();
// Generate animation sequence
(*func)(window, *this);
}
void SwapManager::slotRestoreInitialOrder(ActionMode actMode)
{
if (!currentPairIndex || actionPairs.isEmpty()) return;
else if (!actionPairs.isEmpty())
{
while (currentPairIndex)
slotStepBack(actMode);
}
slotPutItemsInStr8Line();
emit droppedToDefault();
}
void SwapManager::slotPutItemsInStr8Line()
{
auto items = window.items();
int noItems = items.size();
int noSpace = noItems + 1;
double viewWidth = window.width();
double viewHeight = window.height();
double elementWidth = viewWidth * 0.8 / (double)noItems;
double spaceWidth = viewWidth * 0.2 / (double)noSpace;
QPointF startPoint(spaceWidth, viewHeight/2.0 - elementWidth/2.0);
for (auto item : items)
{
QGraphicsWidget *wgt = static_cast<QGraphicsWidget*>(item);
wgt->resize(elementWidth, elementWidth);
wgt->setPos(startPoint.x(), startPoint.y());
wgt->setVisible(true);
startPoint.setX(startPoint.x() + spaceWidth + elementWidth);
}
}
// Must be connected with start/stop toggle button
void SwapManager::slotHandleToggleStartStop(bool checked)
{
if (checked) // If button is pressed that means that animation sequence must run on it's own
{
slotStepForward();
startNextPairActionTimer();
}
else
{
stopNextPairActionTimer();
}
}
void SwapManager::slotSetCurrentSortType(QString stype)
{
currentSortType = stype;
}