-
Notifications
You must be signed in to change notification settings - Fork 11
/
historylist.cpp
115 lines (102 loc) · 2.57 KB
/
historylist.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
// (c) 2015 David Vyvlečka, AGPLv3
#include "historylist.h"
historyList::historyList()
{
current_index = 0;
max_index = 0;
history.push_back(QList<MeshObject*>());
historySize = 0;
sizeLimit = 0;
}
historyList::~historyList()
{
while(max_index != 0){
for(QList<MeshObject*>::size_type i = 0; i < history[max_index].size();i++){
if(history[max_index][i]->hasReferences()){
history[max_index][i]->removeReference();
}else{
delete history[max_index][i];
history[max_index][i]=NULL;
}
}
history.pop_back();
--max_index;
}
}
void historyList::setLimitSize(int limit)
{
sizeLimit = (unsigned long long)limit*1000000; //convert to MB
}
void historyList::add(QList <MeshObject*> item, unsigned long size)
{
if(current_index != max_index){ // not on the end of history
cutRedos();
}
cutOldest();
historySize += size;
history.push_back(item);
current_index++;
max_index++;
}
QList <MeshObject*> historyList::current()
{
return history[current_index];
}
QList <MeshObject*> historyList::undo()
{
if(current_index == 0 || current_index == 1){
return current();
}else{
--current_index;
return current();
}
}
QList <MeshObject*> historyList::redo()
{
if(current_index < max_index) ++current_index;
return current();
}
void historyList::deleteRow(QList <QList <MeshObject*> >::size_type index)
{
for(QList<MeshObject*>::size_type i = 0; i < history[index].size();i++){
if(history[index][i]->hasReferences()){
history[index][i]->removeReference();
}else{
if(history[index][i]->getSize() > historySize){
historySize = 0;
}else{
historySize -= history[index][i]->getSize();
}
delete history[index][i];
history[index][i]=NULL;
}
}
}
void historyList::cutOldest()
{
QList <QList <MeshObject*> >::size_type index = 1;
while(historySize > sizeLimit && current_index > 1){
deleteRow(index);
--current_index;
--max_index;
history.erase(history.begin() + index);
}
}
void historyList::cutRedos()
{
while(max_index != current_index){
deleteRow(max_index);
history.pop_back();
--max_index;
}
}
bool historyList::hasUndos()
{
if(current_index > 1) return true;
else return false;
}
bool historyList::hasRedos()
{
if(max_index != current_index) return true;
else return false;
}