aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models/completionmodels.cpp
blob: b3d633c40b651bc615a053e862280ad33701fa5c (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
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/completionmodels.h"
#include "core/dive.h"
#include "core/tag.h"
#include <QSet>

#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
#define SKIP_EMPTY Qt::SkipEmptyParts
#else
#define SKIP_EMPTY QString::SkipEmptyParts
#endif

CompletionModelBase::CompletionModelBase()
{
	connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &CompletionModelBase::updateModel);
	connect(&diveListNotifier, &DiveListNotifier::divesImported, this, &CompletionModelBase::updateModel);
	connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &CompletionModelBase::divesChanged);
}

void CompletionModelBase::updateModel()
{
	setStringList(getStrings());
}

void CompletionModelBase::divesChanged(const QVector<dive *> &, DiveField field)
{
	if (relevantDiveField(field))
		updateModel();
}

static QStringList getCSVList(char *dive::*item)
{
	QSet<QString> set;
	struct dive *dive;
	int i = 0;
	for_each_dive (i, dive) {
		QString str(dive->*item);
		for (const QString &value: str.split(",", SKIP_EMPTY))
			set.insert(value.trimmed());
	}
	QStringList setList = set.values();
	std::sort(setList.begin(), setList.end());
	return setList;
}

QStringList BuddyCompletionModel::getStrings()
{
	return getCSVList(&dive::buddy);
}

bool BuddyCompletionModel::relevantDiveField(const DiveField &f)
{
	return f.buddy;
}

QStringList DiveMasterCompletionModel::getStrings()
{
	return getCSVList(&dive::divemaster);
}

bool DiveMasterCompletionModel::relevantDiveField(const DiveField &f)
{
	return f.divemaster;
}

QStringList SuitCompletionModel::getStrings()
{
	QStringList list;
	struct dive *dive;
	int i = 0;
	for_each_dive (i, dive) {
		QString suit(dive->suit);
		if (!list.contains(suit))
			list.append(suit);
	}
	std::sort(list.begin(), list.end());
	return list;
}

bool SuitCompletionModel::relevantDiveField(const DiveField &f)
{
	return f.suit;
}

QStringList TagCompletionModel::getStrings()
{
	if (g_tag_list == NULL)
		return {};
	QStringList list;
	struct tag_entry *current_tag_entry = g_tag_list;
	while (current_tag_entry != NULL) {
		list.append(QString(current_tag_entry->tag->name));
		current_tag_entry = current_tag_entry->next;
	}
	std::sort(list.begin(), list.end());
	return list;
}

bool TagCompletionModel::relevantDiveField(const DiveField &f)
{
	return f.tags;
}