aboutsummaryrefslogtreecommitdiffstats
path: root/core/filterpreset.cpp
blob: e7534cbc7d620ea7f00d68d48e7aa347db9efb95 (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
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
// SPDX-License-Identifier: GPL-2.0
#include "filterpreset.h"
#include "qthelper.h"
#include "subsurface-string.h"

struct filter_preset_table filter_preset_table;

extern "C" void clear_filter_presets(void)
{
	filter_preset_table.clear();
}

extern "C" int filter_presets_count(void)
{
	return (int)filter_preset_table.size();
}

extern "C" char *filter_preset_name(int preset)
{
	return copy_qstring(filter_preset_name_qstring(preset));
}

extern "C" char *filter_preset_fulltext_query(int preset)
{
	return copy_qstring(filter_preset_table[preset].data.fullText.originalQuery);
}

extern "C" const char *filter_preset_fulltext_mode(int preset)
{
	switch (filter_preset_table[preset].data.fulltextStringMode) {
	default:
	case StringFilterMode::SUBSTRING:
		return "substring";
	case StringFilterMode::STARTSWITH:
		return "startswith";
	case StringFilterMode::EXACT:
		return "exact";
	}
}

extern "C" void filter_preset_set_fulltext(struct filter_preset *preset, const char *fulltext, const char *fulltext_string_mode)
{
	if (same_string(fulltext_string_mode, "substring"))
		preset->data.fulltextStringMode = StringFilterMode::SUBSTRING;
	else if (same_string(fulltext_string_mode, "startswith"))
		preset->data.fulltextStringMode = StringFilterMode::STARTSWITH;
	else // if (same_string(fulltext_string_mode, "exact"))
		preset->data.fulltextStringMode = StringFilterMode::EXACT;
	preset->data.fullText = fulltext;
}

extern "C" int filter_preset_constraint_count(int preset)
{
	return (int)filter_preset_table[preset].data.constraints.size();
}

extern "C" const filter_constraint *filter_preset_constraint(int preset, int constraint)
{
	return &filter_preset_table[preset].data.constraints[constraint];
}

extern "C" struct filter_preset *alloc_filter_preset()
{
	return new filter_preset;
}

extern "C" void free_filter_preset(const struct filter_preset *preset)
{
	delete preset;
}

extern "C" void filter_preset_set_name(struct filter_preset *preset, const char *name)
{
	preset->name = name;
}

static int filter_preset_add_to_table(const QString &name, const FilterData &d, struct filter_preset_table &table)
{
	// std::lower_bound does a binary search - the vector must be sorted.
	filter_preset newEntry { name, d };
	auto it = std::lower_bound(table.begin(), table.end(), newEntry,
				   [](const filter_preset &p1, const filter_preset &p2)
				   { return p1.name < p2.name; });
	it = table.insert(it, newEntry);
	return it - table.begin();
}

// Take care that the name doesn't already exist by adding numbers
static QString get_unique_preset_name(const QString &orig, const struct filter_preset_table &table)
{
	QString res = orig;
	int count = 2;
	while (std::find_if(table.begin(), table.end(),
			    [&res](const filter_preset &preset)
			    { return preset.name == res; }) != table.end()) {
		res = orig + "#" + QString::number(count);
		++count;
	}
	return res;
}

extern "C" void add_filter_preset_to_table(const struct filter_preset *preset, struct filter_preset_table *table)
{
	QString name = get_unique_preset_name(preset->name, *table);
	filter_preset_add_to_table(name, preset->data, *table);
}

extern "C" void filter_preset_add_constraint(struct filter_preset *preset, const char *type, const char *string_mode,
					     const char *range_mode, bool negate, const char *data)
{
	preset->data.constraints.emplace_back(type, string_mode, range_mode, negate, data);
}

int filter_preset_id(const QString &name)
{
	auto it = std::find_if(filter_preset_table.begin(), filter_preset_table.end(),
			       [&name] (filter_preset &p) { return p.name == name; });
	return it != filter_preset_table.end() ? it - filter_preset_table.begin() : -1;
}

QString filter_preset_name_qstring(int preset)
{
	return filter_preset_table[preset].name;
}

void filter_preset_set(int preset, const FilterData &data)
{
	filter_preset_table[preset].data = data;
}

FilterData filter_preset_get(int preset)
{
	return filter_preset_table[preset].data;
}

int filter_preset_add(const QString &nameIn, const FilterData &d)
{
	QString name = get_unique_preset_name(nameIn, filter_preset_table);
	return filter_preset_add_to_table(name, d, filter_preset_table);
}

void filter_preset_delete(int preset)
{
	filter_preset_table.erase(filter_preset_table.begin() + preset);
}