This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
session.cpp
149 lines (114 loc) · 2.9 KB
/
session.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
#include "session.h"
#include "terrainedit.h"
#include "terrain.h"
Session::Session(QObject *parent) :
QObject(parent),
nextEditHistoryIndex_(0),
primaryColor_(0x7f7f7f)
{
}
Session::~Session()
{
}
QSharedPointer<TerrainEdit> Session::beginEdit()
{
if (isEditActive()) {
return QSharedPointer<TerrainEdit>();
}
Q_ASSERT(terrain_);
QSharedPointer<TerrainEdit> edit = QSharedPointer<TerrainEdit>::create();
connect(edit.data(), SIGNAL(edited(QRect)), SLOT(edited(QRect)));
currentEdit_ = edit;
emit canUndoChanged(canUndo());
emit canRedoChanged(canRedo());
return edit;
}
void Session::endEdit()
{
Q_ASSERT(currentEdit_);
Q_ASSERT(terrain_);
auto edit = currentEdit_;
edit->done();
// add edit to history
while (editHistory_.size() > nextEditHistoryIndex_) {
editHistory_.pop_back();
}
editHistory_.push_back(edit);
++nextEditHistoryIndex_;
if (editHistory_.size() > 100) {
editHistory_.pop_front();;
--nextEditHistoryIndex_;
}
disconnect(edit.data(), SIGNAL(edited(QRect)), this, SLOT(edited(QRect)));
currentEdit_.reset();
emit canUndoChanged(canUndo());
emit canRedoChanged(canRedo());
}
void Session::cancelEdit()
{
Q_ASSERT(currentEdit_);
Q_ASSERT(terrain_);
auto edit = currentEdit_;
edit->undo(terrain_.data());
emit terrainUpdated(edit->modifiedBounds());
disconnect(edit.data(), SIGNAL(edited(QRect)), this, SLOT(edited(QRect)));
currentEdit_.reset();
emit canUndoChanged(canUndo());
emit canRedoChanged(canRedo());
}
void Session::setTool(QSharedPointer<ToolController> t)
{
tool_ = t;
emit toolChanged();
}
void Session::setPrimaryColor(quint32 color)
{
if (color == primaryColor_) {
return;
}
primaryColor_ = color;
emit primaryColorChanged();
}
bool Session::canUndo()
{
return !isEditActive() && nextEditHistoryIndex_ > 0;
}
bool Session::canRedo()
{
return !isEditActive() && nextEditHistoryIndex_ < editHistory_.size();
}
void Session::terrainUpdate(const QRect &rect)
{
if (rect.isEmpty())
emit terrainUpdated(QRect(QPoint(0, 0), terrain_->size()));
else
emit terrainUpdated(rect);
}
void Session::undo()
{
if (!canUndo()) {
return;
}
--nextEditHistoryIndex_;
const auto& edit = editHistory_[nextEditHistoryIndex_];
edit->undo(terrain_.data());
emit canUndoChanged(canUndo());
emit canRedoChanged(canRedo());
emit terrainUpdated(edit->modifiedBounds());
}
void Session::redo()
{
if (!canRedo()) {
return;
}
const auto &edit = editHistory_[nextEditHistoryIndex_];
edit->apply(terrain_.data());
++nextEditHistoryIndex_;
emit canUndoChanged(canUndo());
emit canRedoChanged(canRedo());
emit terrainUpdated(edit->modifiedBounds());
}
void Session::edited(const QRect &rt)
{
emit terrainUpdated(rt);
}