summaryrefslogtreecommitdiffstats
path: root/qt-ui/undobuffer.cpp
blob: 4ee0cf608996b04ed3209b655207c151ae8a75ab (plain) (blame)
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
#include "undobuffer.h"
#include "mainwindow.h"

UndoBuffer::UndoBuffer(QObject *parent) : QObject(parent)
{
	curIdx = 0;
}

UndoBuffer::~UndoBuffer()
{

}

bool UndoBuffer::canUndo()
{
	return curIdx > 0;
}

bool UndoBuffer::canRedo()
{
	return curIdx < list.count();
}

void UndoBuffer::redo()
{
	current()->redo();
	curIdx++;
	if (curIdx > list.count())
		curIdx = list.count() - 1;
}

void UndoBuffer::undo()
{
	current()->undo();
	curIdx = list.indexOf(current());
}

void UndoBuffer::recordbefore(QString commandName, dive *affectedDive)
{
	UndoCommand *cmd = new UndoCommand(commandName, affectedDive);
	//If we are within the list, clear the extra UndoCommands.
	if (list.count() > 0) {
		if (curIdx + 1 < list.count()) {
			for (int i = curIdx + 1; i < list.count(); i++) {
				list.removeAt(i);
			}
		}
	}
	list.append(cmd);
	curIdx = list.count();
}

void UndoBuffer::recordAfter(dive *affectedDive)
{
	list.at(curIdx - 1)->setStateAfter(affectedDive);
}



UndoCommand::UndoCommand(QString commandName, dive *affectedDive)
{
	name = commandName;
	stateBefore = affectedDive;
}

void UndoCommand::undo()
{
	if (name == "Delete Dive") {
		record_dive(stateBefore);
		MainWindow::instance()->recreateDiveList();
	}
}

void UndoCommand::redo()
{
	//To be implemented
}