-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
108 lines (92 loc) · 2.02 KB
/
log.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
#include "log.h"
Log::Log(){
curPos = -1;
commits.clear();
}
Log::~Log(){
int size = commits.size();
for(int i = 0; i < size; i++){
delete commits[i];
}
commits.clear();
}
void Log::commit(QImage image, QString str){
if(redoEnabled()) {
freeCommits(curPos + 1);
}
Commit *newCommit = new Commit(image, str);
addCommit(newCommit);
}
QImage Log::undo(QImage *curImg){
if(!undoEnabled()){ //不能撤销
return QImage();
}
if(!redoEnabled()){ //记录最后一次操作后的图像
imgFinalState = *curImg;
}
QImage res = getImgBeforeOperation();
curPos--;
return res;
}
QImage Log::redo(){
if(!redoEnabled()){
return QImage();
}
curPos++;
return getImgAfterOperation();
}
QString Log::getUndoMsg(){
if(!undoEnabled()){
return NULL;
}
return getLastOperation();
}
QString Log::getRedoMsg(){
if(!redoEnabled()){
return NULL;
}
return getNextOperation();
}
bool Log::undoEnabled(){
return curPos >= 0;
}
bool Log::redoEnabled(){
return commits.size() > (unsigned)curPos + 1;
}
void Log::freeCommits(int i){
int size = commits.size();
for(int j = i; j < size; j++){
delete commits[j];
}
commits.erase(commits.begin() + i, commits.end());
}
void Log::addCommit(Commit *commit){
commits.push_back(commit);
curPos++;
}
QImage Log::getImgBeforeOperation(){
Commit *commit = commits[curPos];
return commit->before;
}
QImage Log::getImgAfterOperation(){
if((unsigned)curPos + 1 < commits.size()){
Commit *commit = commits[curPos + 1];
return commit->before;
}
return imgFinalState;
}
QString Log::getLastOperation(){
Commit *commit = commits[curPos];
return commit->info;
}
QString Log::getNextOperation(){
if((unsigned)curPos + 1 < commits.size()){
Commit *commit = commits[curPos + 1];
return commit->info;
}
return NULL;
}
void Log::clear(){
curPos = -1;
commits.clear();
}