aboutsummaryrefslogtreecommitdiffstats
path: root/core/subsurface-qt/DiveObjectHelper.cpp
blob: 2e6d8707bb73c5740c7a643e4b1343d23e6f0d56 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// SPDX-License-Identifier: GPL-2.0
#include "DiveObjectHelper.h"

#include <QDateTime>
#include <QTextDocument>

#include "../qthelper.h"
#include "../helpers.h"
#include "../../qt-models/tankinfomodel.h"

static QString EMPTY_DIVE_STRING = QStringLiteral("");
enum returnPressureSelector {START_PRESSURE, END_PRESSURE};

static QString getFormattedWeight(struct dive *dive, unsigned int idx)
{
	weightsystem_t *weight = &dive->weightsystem[idx];
	if (!weight->description)
		return QString(EMPTY_DIVE_STRING);
	QString fmt = QString(weight->description);
	fmt += ", " + get_weight_string(weight->weight, true);
	return fmt;
}

static QString getFormattedCylinder(struct dive *dive, unsigned int idx)
{
	cylinder_t *cyl = &dive->cylinder[idx];
	const char *desc = cyl->type.description;
	if (!desc && idx > 0)
		return QString(EMPTY_DIVE_STRING);
	QString fmt = desc ? QString(desc) : QObject::tr("unknown");
	fmt += ", " + get_volume_string(cyl->type.size, true);
	fmt += ", " + get_pressure_string(cyl->type.workingpressure, true);
	fmt += ", " + get_pressure_string(cyl->start, false) + " - " + get_pressure_string(cyl->end, true);
	fmt += ", " + get_gas_string(cyl->gasmix);
	return fmt;
}

static QString getPressures(struct dive *dive, enum returnPressureSelector ret)
{
	cylinder_t *cyl = &dive->cylinder[0];
	QString fmt;
	if (ret == START_PRESSURE) {
		if (cyl->start.mbar)
			fmt = get_pressure_string(cyl->start, true);
		else if (cyl->sample_start.mbar)
			fmt = get_pressure_string(cyl->sample_start, true);
	}
	if (ret == END_PRESSURE) {
		if (cyl->end.mbar)
			fmt = get_pressure_string(cyl->end, true);
		else if(cyl->sample_end.mbar)
			fmt = get_pressure_string(cyl->sample_end, true);
	}
	return fmt;
}

DiveObjectHelper::DiveObjectHelper(struct dive *d) :
	m_dive(d)
{
	m_cyls.clear();
	for (int i = 0; i < MAX_CYLINDERS; i++) {
		//Don't add blank cylinders, only those that have been defined.
		if (m_dive->cylinder[i].type.description)
			m_cyls.append(new CylinderObjectHelper(&m_dive->cylinder[i]));
	}
}

DiveObjectHelper::~DiveObjectHelper()
{
while (!m_cyls.isEmpty())
	delete m_cyls.takeFirst();
}

int DiveObjectHelper::number() const
{
	return m_dive->number;
}

int DiveObjectHelper::id() const
{
	return m_dive->id;
}

QString DiveObjectHelper::date() const
{
	QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000*m_dive->when, Qt::UTC);
	localTime.setTimeSpec(Qt::UTC);
	return localTime.date().toString(prefs.date_format_short);
}

timestamp_t DiveObjectHelper::timestamp() const
{
	return m_dive->when;
}

QString DiveObjectHelper::time() const
{
	QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000*m_dive->when, Qt::UTC);
	localTime.setTimeSpec(Qt::UTC);
	return localTime.time().toString(prefs.time_format);
}

QString DiveObjectHelper::location() const
{
	return get_dive_location(m_dive) ? QString::fromUtf8(get_dive_location(m_dive)) : EMPTY_DIVE_STRING;
}

QString DiveObjectHelper::gps() const
{
	struct dive_site *ds = get_dive_site_by_uuid(m_dive->dive_site_uuid);
	return ds ? QString(printGPSCoords(ds->latitude.udeg, ds->longitude.udeg)) : QString();
}

QString DiveObjectHelper::gps_decimal() const
{
	bool savep = prefs.coordinates_traditional;
	QString val;

	prefs.coordinates_traditional = false;
	val = gps();
	prefs.coordinates_traditional = savep;
	return(val);
}

QString DiveObjectHelper::duration() const
{
	return get_dive_duration_string(m_dive->duration.seconds, QObject::tr("h"), QObject::tr("min"));
}

bool DiveObjectHelper::noDive() const
{
	return m_dive->duration.seconds == 0 && m_dive->dc.duration.seconds == 0;
}

QString DiveObjectHelper::depth() const
{
	return get_depth_string(m_dive->dc.maxdepth.mm, true, true);
}

QString DiveObjectHelper::divemaster() const
{
	return m_dive->divemaster ? m_dive->divemaster : EMPTY_DIVE_STRING;
}

QString DiveObjectHelper::buddy() const
{
	return m_dive->buddy ? m_dive->buddy : EMPTY_DIVE_STRING;
}

QString DiveObjectHelper::airTemp() const
{
	QString temp = get_temperature_string(m_dive->airtemp, true);
	if (temp.isEmpty()) {
		temp = EMPTY_DIVE_STRING;
	}
	return temp;
}

QString DiveObjectHelper::waterTemp() const
{
	QString temp = get_temperature_string(m_dive->watertemp, true);
	if (temp.isEmpty()) {
		temp = EMPTY_DIVE_STRING;
	}
	return temp;
}

QString DiveObjectHelper::notes() const
{
	QString tmp = m_dive->notes ? QString::fromUtf8(m_dive->notes) : EMPTY_DIVE_STRING;
	if (same_string(m_dive->dc.model, "planned dive")) {
		QTextDocument notes;
	#define _NOTES_BR "&#92n"
		tmp.replace("<thead>", "<thead>" _NOTES_BR)
			.replace("<br>", "<br>" _NOTES_BR)
			.replace("<tr>", "<tr>" _NOTES_BR)
			.replace("</tr>", "</tr>" _NOTES_BR);
		notes.setHtml(tmp);
		tmp = notes.toPlainText();
		tmp.replace(_NOTES_BR, "<br>");
	#undef _NOTES_BR
	} else {
		tmp.replace("\n", "<br>");
	}
	return tmp;
}

QString DiveObjectHelper::tags() const
{
	static char buffer[256];
	taglist_get_tagstring(m_dive->tag_list, buffer, 256);
	return QString(buffer);
}

QString DiveObjectHelper::gas() const
{
	/*WARNING: here should be the gastlist, returned
	 * from the get_gas_string function or this is correct?
	 */
	QString gas, gases;
	for (int i = 0; i < MAX_CYLINDERS; i++) {
		if (!is_cylinder_used(m_dive, i))
			continue;
		gas = m_dive->cylinder[i].type.description;
		if (!gas.isEmpty())
			gas += QChar(' ');
		gas += gasname(&m_dive->cylinder[i].gasmix);
		// if has a description and if such gas is not already present
		if (!gas.isEmpty() && gases.indexOf(gas) == -1) {
			if (!gases.isEmpty())
				gases += QString(" / ");
			gases += gas;
		}
	}
	return gases;
}

QString DiveObjectHelper::sac() const
{
	if (!m_dive->sac)
		return QString();
	const char *unit;
	int decimal;
	double value = get_volume_units(m_dive->sac, &decimal, &unit);
	return QString::number(value, 'f', decimal).append(unit);
}

QString DiveObjectHelper::weightList() const
{
	QString weights;
	for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
		QString w = getFormattedWeight(m_dive, i);
		if (w == EMPTY_DIVE_STRING)
			continue;
		weights += w + "; ";
	}
	return weights;
}

QStringList DiveObjectHelper::weights() const
{
	QStringList weights;
	for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
		QString w = getFormattedWeight(m_dive, i);
		if (w == EMPTY_DIVE_STRING)
			continue;
		weights << w;
	}
	return weights;
}

bool DiveObjectHelper::singleWeight() const
{
	return weightsystem_none(&m_dive->weightsystem[1]);
}

QString DiveObjectHelper::weight(int idx) const
{
	if ( (idx < 0) || idx > MAX_WEIGHTSYSTEMS )
		return QString(EMPTY_DIVE_STRING);
	return getFormattedWeight(m_dive, idx);
}

QString DiveObjectHelper::suit() const
{
	return m_dive->suit ? m_dive->suit : EMPTY_DIVE_STRING;
}

QStringList DiveObjectHelper::cylinderList() const
{
	QStringList cylinders;
	struct dive *d;
	int i = 0;
	for_each_dive (i, d) {
		for (int j = 0; j < MAX_CYLINDERS; j++) {
			QString cyl = d->cylinder[j].type.description;
			if (cyl == EMPTY_DIVE_STRING)
				continue;
			cylinders << cyl;
		}
	}

	for (unsigned long ti = 0; ti < sizeof(tank_info) && tank_info[ti].name != NULL; ti++) {
		QString cyl = tank_info[ti].name;
		if (cyl == EMPTY_DIVE_STRING)
			continue;
		cylinders << cyl;
	}

	cylinders.removeDuplicates();
	cylinders.sort();
	return cylinders;
}

QStringList DiveObjectHelper::cylinders() const
{
	QStringList cylinders;
	for (int i = 0; i < MAX_CYLINDERS; i++) {
		QString cyl = getFormattedCylinder(m_dive, i);
		if (cyl == EMPTY_DIVE_STRING)
			continue;
		cylinders << cyl;
	}
	return cylinders;
}

QString DiveObjectHelper::cylinder(int idx) const
{
	if ( (idx < 0) || idx > MAX_CYLINDERS)
		return QString(EMPTY_DIVE_STRING);
	return getFormattedCylinder(m_dive, idx);
}

QList<CylinderObjectHelper*> DiveObjectHelper::cylinderObjects() const
{
	return m_cyls;
}

QString DiveObjectHelper::trip() const
{
	return m_dive->divetrip ? m_dive->divetrip->location : EMPTY_DIVE_STRING;
}

// combine the pointer address with the trip title so that
// we detect multiple, destinct trips with the same title
// the trip title is designed to be
// location (# dives)
// or, if there is no location name
// date range (# dives)
// where the date range is given as "month year" or "month-month year" or "month year - month year"
QString DiveObjectHelper::tripMeta() const
{
	QString ret = EMPTY_DIVE_STRING;
	struct dive_trip *dt = m_dive->divetrip;
	if (dt) {
		QString numDives = tr("%1 dive(s)").arg(dt->nrdives);
		QString title(dt->location);
		if (title.isEmpty()) {
			// so use the date range
			QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*dt->when, Qt::UTC);
			QString firstMonth = firstTime.toString("MMM");
			QString firstYear = firstTime.toString("yyyy");
			QDateTime lastTime = QDateTime::fromMSecsSinceEpoch(1000*dt->dives->when, Qt::UTC);
			QString lastMonth = lastTime.toString("MMM");
			QString lastYear = lastTime.toString("yyyy");
			if (lastMonth == firstMonth && lastYear == firstYear)
				title = firstMonth + " " + firstYear;
			else if (lastMonth != firstMonth && lastYear == firstYear)
				title = firstMonth + "-" + lastMonth + " " + firstYear;
			else
				title = firstMonth + " " + firstYear + " - " + lastMonth + " " + lastYear;
		}
		ret = QString::number((quint64)m_dive->divetrip, 16) + QLatin1Literal("::") + QStringLiteral("%1 (%2)").arg(title, numDives);
	}
	return ret;
}

int DiveObjectHelper::maxcns() const
{
	return m_dive->maxcns;
}

int DiveObjectHelper::otu() const
{
	return m_dive->otu;
}

int DiveObjectHelper::rating() const
{
	return m_dive->rating;
}

int DiveObjectHelper::visibility() const
{
	return m_dive->visibility;
}

QString DiveObjectHelper::sumWeight() const
{
	weight_t sum = { 0 };
	for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++){
		sum.grams += m_dive->weightsystem[i].weight.grams;
	}
	return get_weight_string(sum, true);
}

QString DiveObjectHelper::getCylinder() const
{
	QString getCylinder = m_dive->cylinder[0].type.description;
	return getCylinder;
}

QString DiveObjectHelper::startPressure() const
{
	QString startPressure = getPressures(m_dive, START_PRESSURE);
	return startPressure;
}

QString DiveObjectHelper::endPressure() const
{
	QString endPressure = getPressures(m_dive, END_PRESSURE);
	return endPressure;
}

QString DiveObjectHelper::firstGas() const
{
	QString gas;
	gas = get_gas_string(m_dive->cylinder[0].gasmix);
	return gas;
}

QStringList DiveObjectHelper::suitList() const
{
	QStringList suits;
	struct dive *d;
	int i = 0;
	for_each_dive (i, d) {
		QString temp = d->suit;
		if (!temp.isEmpty())
			suits << d->suit;
	}
	suits.removeDuplicates();
	suits.sort();
	return suits;
}

QStringList DiveObjectHelper::buddyList() const
{
	QStringList buddies;
	struct dive *d;
	int i = 0;
	for_each_dive (i, d) {
		QString temp = d->buddy;
		if (!temp.isEmpty() && !temp.contains(",")){
			buddies << d->buddy;
		}
		else if (!temp.isEmpty()){
			QRegExp sep("(,\\s)");
			QStringList tempList = temp.split(sep);
			buddies << tempList;
			buddies << tr("Multiple Buddies");
		}
	}
	buddies.removeDuplicates();
	buddies.sort();
	return buddies;
}

QStringList DiveObjectHelper::divemasterList() const
{
	QStringList divemasters;
	struct dive *d;
	int i = 0;
	for_each_dive (i, d) {
		QString temp = d->divemaster;
		if (!temp.isEmpty())
			divemasters << d->divemaster;
	}
	divemasters.removeDuplicates();
	divemasters.sort();
	return divemasters;
}