summaryrefslogtreecommitdiffstats
path: root/qt-models/divelistmodel.cpp
blob: 1786c85e1870cbd37aad802ca9b986ae3e6a9cc4 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "divelistmodel.h"
#include "helpers.h"

Dive::Dive(dive *d)
{
	m_thisDive = d;
	setDiveNumber(QString::number(d->number));
	setDate(get_dive_date_string(d->when));
	setDepth(get_depth_string(d->maxdepth));
	setDuration(get_dive_duration_string(d->duration.seconds, "h:","min"));

	if (!d->watertemp.mkelvin)
		m_depth = "";

	if (get_units()->temperature == units::CELSIUS)
		m_depth = QString::number(mkelvin_to_C(d->watertemp.mkelvin), 'f', 1);
	else
		m_depth = QString::number(mkelvin_to_F(d->watertemp.mkelvin), 'f', 1);

	weight_t tw = { total_weight(d) };
	setWeight(weight_string(tw.grams));

	setSuit(QString(d->suit));
	setCylinder(QString(d->cylinder[0].type.description));
	setSac(QString::number(d->sac));
	setLocation(get_dive_location(d));
}

QString Dive::date() const
{
	return m_date;
}

void Dive::setDate(const QString &date)
{
	m_date = date;
}
QString Dive::location() const
{
	return m_location;
}

void Dive::setLocation(const QString &location)
{
	m_location = location;
}
QString Dive::sac() const
{
	return m_sac;
}

void Dive::setSac(const QString &sac)
{
	m_sac = sac;
}
QString Dive::gas() const
{
	return m_gas;
}

void Dive::setGas(const QString &gas)
{
	m_gas = gas;
}
QString Dive::cylinder() const
{
	return m_cylinder;
}

void Dive::setCylinder(const QString &cylinder)
{
	m_cylinder = cylinder;
}
QString Dive::suit() const
{
	return m_suit;
}

void Dive::setSuit(const QString &suit)
{
	m_suit = suit;
}
QString Dive::weight() const
{
	return m_weight;
}

void Dive::setWeight(const QString &weight)
{
	m_weight = weight;
}
QString Dive::temp() const
{
	return m_temp;
}

void Dive::setTemp(const QString &temp)
{
	m_temp = temp;
}
QString Dive::duration() const
{
	return m_duration;
}

void Dive::setDuration(const QString &duration)
{
	m_duration = duration;
}
QString Dive::depth() const
{
	return m_depth;
}

void Dive::setDepth(const QString &depth)
{
	m_depth = depth;
}
QString Dive::rating() const
{
	return m_rating;
}

void Dive::setRating(const QString &rating)
{
	m_rating = rating;
}
dive *Dive::thisDive() const
{
	return m_thisDive;
}

void Dive::setThisDive(dive *thisDive)
{
	m_thisDive = thisDive;
}
QString Dive::diveNumber() const
{
	return m_diveNumber;
}

void Dive::setDiveNumber(const QString &diveNumber)
{
	m_diveNumber = diveNumber;
}


DiveListModel *DiveListModel::m_instance = NULL;

DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
{
	m_instance = this;
}

void DiveListModel::addDive(dive *d)
{
	beginInsertRows(QModelIndex(), rowCount(), rowCount());
	m_dives.append(Dive(d));
	endInsertRows();
}

int DiveListModel::rowCount(const QModelIndex &) const
{
	return m_dives.count();
}

QVariant DiveListModel::data(const QModelIndex &index, int role) const
{
	if(index.row() < 0 || index.row() > m_dives.count())
		return QVariant();

	const Dive &dive = m_dives[index.row()];

	if (role == DiveNumberRole)
		return dive.diveNumber();
	else if (role == DiveDateRole)
		return dive.date();
	else if (role == DiveRatingRole)
		return dive.rating();
	else if (role == DiveDepthRole)
		return dive.depth();
	else if (role == DiveDurationRole)
		return dive.duration();
	else if (role == DiveTemperatureRole)
		return dive.temp();
	else if (role == DiveWeightRole)
		return dive.weight();
	else if (role == DiveSuitRole)
		return dive.suit();
	else if (role == DiveCylinderRole)
		return dive.cylinder();
	else if (role == DiveGasRole)
		return dive.gas();
	else if (role == DiveSacRole)
		return dive.sac();
	else if (role == DiveLocationRole)
		return dive.location();
	return QVariant();


}

QHash<int, QByteArray> DiveListModel::roleNames() const
{
	QHash<int, QByteArray> roles;
	roles[DiveNumberRole] = "diveNumber";
	roles[DiveDateRole] = "date";
	roles[DiveRatingRole] = "rating";
	roles[DiveDepthRole] = "depth";
	roles[DiveDurationRole] = "duration";
	roles[DiveTemperatureRole] = "temp";
	roles[DiveWeightRole] = "weight";
	roles[DiveSuitRole] = "suit";
	roles[DiveCylinderRole] = "cylinder";
	roles[DiveGasRole] = "gas";
	roles[DiveSacRole] = "sac";
	roles[DiveLocationRole] = "location";

	return roles;
}

DiveListModel *DiveListModel::instance()
{
	return m_instance;
}