forked from x90/IRC-Paint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text_Command.cpp
42 lines (37 loc) · 1.18 KB
/
Text_Command.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
#include "Text_Command.h"
#include "MainWidget.h"
Text_Command::Text_Command(MainWidget *w, int x, int y, QChar c) : widget(w) {
chars << std::make_pair(QPoint(x, y), c);
}
void Text_Command::undo() {
foreach (cell c, changed) {
widget->text[c.first.y()][c.first.x()] = c.second;
widget->update(widget->pixelRect(c.first));
}
}
void Text_Command::redo() {
changed.clear();
foreach (cell c, chars) {
changed << std::make_pair(c.first, widget->text[c.first.y()][c.first.x()]);
widget->text[c.first.y()][c.first.x()] = c.second;
widget->update(widget->pixelRect(c.first));
}
}
bool Text_Command::mergeWith(const QUndoCommand* other) {
Text_Command* cmd = dynamic_cast<Text_Command*>(const_cast<QUndoCommand*>(other));
if (chars.size() > 500 || cmd->id() != id() || !cmd)
return false;
foreach (cell c, cmd->chars) {
foreach (cell h, chars) {
if (c.first == h.first) {
if (c.second == h.second)
return true;
return false;
}
}
chars << c;
}
foreach (cell c, cmd->changed)
changed << c;
return true;
}