aboutsummaryrefslogtreecommitdiffstats
path: root/commands/command_filter.cpp
blob: c79d4eff6dd0e49ac415ed14423e0c89b92dab10 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: GPL-2.0

#include "command_filter.h"
#include "core/filterpreset.h"
#include "core/subsurface-qt/divelistnotifier.h"

namespace Command {

static int createFilterPreset(const QString &name, const FilterData &data)
{
	int index = filter_preset_add(name, data);
	emit diveListNotifier.filterPresetAdded(index);
	return index;
}

static std::pair<QString, FilterData> removeFilterPreset(int index)
{
	QString oldName = filter_preset_name_qstring(index);
	FilterData oldData = filter_preset_get(index);
	filter_preset_delete(index);
	emit diveListNotifier.filterPresetRemoved(index);
	return { oldName, oldData };
}

CreateFilterPreset::CreateFilterPreset(const QString &nameIn, const FilterData &dataIn) :
	name(nameIn), data(dataIn), index(0)
{
	setText(Command::Base::tr("Create filter preset %1").arg(nameIn));
}

bool CreateFilterPreset::workToBeDone()
{
	return true;
}

void CreateFilterPreset::redo()
{
	index = createFilterPreset(name, data);
}

void CreateFilterPreset::undo()
{
	// with std::tie() we can conveniently assign tuples
	std::tie(name, data) = removeFilterPreset(index);
}

RemoveFilterPreset::RemoveFilterPreset(int indexIn) : index(indexIn)
{
	setText(Command::Base::tr("Delete filter preset %1").arg(filter_preset_name_qstring(index)));
}

bool RemoveFilterPreset::workToBeDone()
{
	return true;
}

void RemoveFilterPreset::redo()
{
	// with std::tie() we can conveniently assign tuples
	std::tie(name, data) = removeFilterPreset(index);
}

void RemoveFilterPreset::undo()
{
	index = createFilterPreset(name, data);
}

EditFilterPreset::EditFilterPreset(int indexIn, const FilterData &dataIn) :
	index(indexIn), data(dataIn)
{
	setText(Command::Base::tr("Edit filter preset %1").arg(filter_preset_name_qstring(index)));
}

bool EditFilterPreset::workToBeDone()
{
	return true;
}

void EditFilterPreset::redo()
{
	FilterData oldData = filter_preset_get(index);
	filter_preset_set(index, data);
	data = std::move(oldData);
}

void EditFilterPreset::undo()
{
	redo(); // undo() and redo() do the same thing
}

} // namespace Command