-
Notifications
You must be signed in to change notification settings - Fork 5
/
levelundostack.cpp
90 lines (75 loc) · 2.04 KB
/
levelundostack.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
#include "levelundostack.h"
//LevelUndoStack::LevelUndoStack(QObject *parent) : QObject(parent)
//{
//}
LevelUndoStack::LevelUndoStack(EntireLevel *lev)
{
Level = lev;
UndoTimer = new QTimer(this);
connect(UndoTimer,SIGNAL(timeout()),this,SLOT(UndoTimerTimeout()));
undoPosition = 0;
isModified = false;
UndoTimer->start(UNDO_CHECK_INTERVAL);
}
LevelUndoStack::~LevelUndoStack()
{
UndoTimer->stop();
delete UndoTimer;
qDeleteAll(UndoStack);
UndoStack.clear();
}
void LevelUndoStack::notifyModified()
{
isModified = true;
isSaved = false;
}
void LevelUndoStack::notifySaved()
{
isSaved = true;
}
void LevelUndoStack::undo()
{
UndoTimerTimeout();
if (undoPosition <= 0) return;
undoPosition--;
QDataStream st( /*qUncompress(*/ UndoStack.at(undoPosition) , QIODevice::ReadWrite);
Level->loadFromStream( st );
qDebug() << "UNDO, pos: " << undoPosition << ", count: " << UndoStack.count();
}
void LevelUndoStack::redo()
{
UndoTimerTimeout();
if (undoPosition >= UndoStack.count() - 1) return;
undoPosition++;
QDataStream st( /*qUncompress(*/ UndoStack.at(undoPosition) , QIODevice::ReadWrite);
Level->loadFromStream( st );
qDebug() << "REDO, pos: " << undoPosition << ", count: " << UndoStack.count();
}
bool LevelUndoStack::getIsSaved()
{
return isSaved;
}
void LevelUndoStack::UndoTimerTimeout()
{
if (!isModified) return;
if ( UndoStack.count() )
{
while (undoPosition < UndoStack.count() - 1)
{
delete UndoStack.last();
UndoStack.removeLast();
}
}
if (UndoStack.count() >= MAX_UNDO_STACK_SIZE)
{
delete UndoStack.first();
UndoStack.removeFirst();
}
QByteArray *buf = new QByteArray();
QDataStream st(buf, QIODevice::ReadWrite);
Level->saveToStream( st );
UndoStack.append( /*qCompress( */ buf/*, 4)*/ );
undoPosition = UndoStack.count() - 1;
qDebug() << "MODIFY, pos: " << undoPosition << ", count: " << UndoStack.count();
isModified = false;
}