-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandmanager.cpp
More file actions
71 lines (54 loc) · 1.23 KB
/
commandmanager.cpp
File metadata and controls
71 lines (54 loc) · 1.23 KB
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
#include "commandmanager.h"
CommandManager::CommandManager(QObject *parent): QObject(parent) {}
CommandManager::~CommandManager()
{
clear();
}
void CommandManager::execute(Command *command)
{
command->execute();
m_undoStack.push(command);
// On command execution
for (Command *cmd : m_redoStack) {
delete cmd;
}
m_redoStack.clear();
emit undoRedoStateChanged();
emit commandExecuted();
}
void CommandManager::undo()
{
if (m_undoStack.isEmpty()) return;
Command *command = m_undoStack.pop();
command->undo();
m_redoStack.push(command);
emit undoRedoStateChanged();
}
void CommandManager::redo()
{
if (m_redoStack.isEmpty()) return;
Command *command = m_redoStack.pop();
command->redo();
m_undoStack.push(command);
emit undoRedoStateChanged();
}
bool CommandManager::canUndo() const
{
return !m_undoStack.isEmpty();
}
bool CommandManager::canRedo() const
{
return !m_redoStack.isEmpty();
}
void CommandManager::clear()
{
for (Command *command : m_undoStack) {
delete command;
}
m_undoStack.clear();
for (Command *command : m_redoStack) {
delete command;
}
m_redoStack.clear();
emit undoRedoStateChanged();
}