2017-04-27 20:24:53 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2020-02-03 19:33:06 +01:00
|
|
|
#include "diveobjecthelper.h"
|
2016-01-07 16:01:24 -02:00
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
2020-05-01 13:43:52 +02:00
|
|
|
#include "core/dive.h"
|
2018-05-11 08:25:41 -07:00
|
|
|
#include "core/qthelper.h"
|
2019-03-04 23:20:29 +01:00
|
|
|
#include "core/divesite.h"
|
2019-05-31 16:09:14 +02:00
|
|
|
#include "core/trip.h"
|
2018-05-11 08:25:41 -07:00
|
|
|
#include "core/subsurface-string.h"
|
2020-12-14 22:42:07 +01:00
|
|
|
#include "core/string-format.h"
|
2018-05-11 08:25:41 -07:00
|
|
|
#include "qt-models/tankinfomodel.h"
|
2016-01-07 16:01:24 -02:00
|
|
|
|
2019-10-19 17:54:56 -04:00
|
|
|
#if defined(DEBUG_DOH)
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
static int callCounter = 0;
|
|
|
|
#endif /* defined(DEBUG_DOH) */
|
|
|
|
|
|
|
|
|
2019-08-04 19:19:18 +02:00
|
|
|
static QString getFormattedWeight(const struct dive *dive, int idx)
|
2016-01-07 16:01:24 -02:00
|
|
|
{
|
2019-08-13 22:48:18 +02:00
|
|
|
const weightsystem_t *weight = &dive->weightsystems.weightsystems[idx];
|
2016-01-11 06:14:45 -08:00
|
|
|
if (!weight->description)
|
2018-11-24 08:40:30 +01:00
|
|
|
return QString();
|
2016-01-11 06:14:45 -08:00
|
|
|
QString fmt = QString(weight->description);
|
|
|
|
fmt += ", " + get_weight_string(weight->weight, true);
|
|
|
|
return fmt;
|
2016-01-07 16:01:24 -02:00
|
|
|
}
|
|
|
|
|
2019-08-13 22:48:18 +02:00
|
|
|
static QString formatGas(const dive *d)
|
2016-01-07 16:01:24 -02:00
|
|
|
{
|
2016-01-11 16:37:10 -02:00
|
|
|
/*WARNING: here should be the gastlist, returned
|
|
|
|
* from the get_gas_string function or this is correct?
|
|
|
|
*/
|
|
|
|
QString gas, gases;
|
2019-08-04 18:44:57 +02:00
|
|
|
for (int i = 0; i < d->cylinders.nr; i++) {
|
2019-08-13 22:48:18 +02:00
|
|
|
if (!is_cylinder_used(d, i))
|
2016-01-11 16:37:10 -02:00
|
|
|
continue;
|
2019-08-04 22:13:49 +02:00
|
|
|
gas = get_cylinder(d, i)->type.description;
|
2016-01-11 16:37:10 -02:00
|
|
|
if (!gas.isEmpty())
|
|
|
|
gas += QChar(' ');
|
2019-08-04 22:13:49 +02:00
|
|
|
gas += gasname(get_cylinder(d, i)->gasmix);
|
2016-01-11 16:37:10 -02:00
|
|
|
// 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;
|
2016-01-07 16:01:24 -02:00
|
|
|
}
|
|
|
|
|
2019-08-13 22:48:18 +02:00
|
|
|
static QString formatWeightList(const dive *d)
|
2016-02-29 16:42:07 +02:00
|
|
|
{
|
|
|
|
QString weights;
|
2019-08-13 22:48:18 +02:00
|
|
|
for (int i = 0; i < d->weightsystems.nr; i++) {
|
|
|
|
QString w = getFormattedWeight(d, i);
|
2018-11-24 08:40:30 +01:00
|
|
|
if (w.isEmpty())
|
2016-02-29 16:42:07 +02:00
|
|
|
continue;
|
|
|
|
weights += w + "; ";
|
|
|
|
}
|
|
|
|
return weights;
|
|
|
|
}
|
|
|
|
|
2019-08-13 22:48:18 +02:00
|
|
|
static QStringList formatWeights(const dive *d)
|
2016-01-07 16:01:24 -02:00
|
|
|
{
|
2016-01-11 16:48:46 -02:00
|
|
|
QStringList weights;
|
2019-08-13 22:48:18 +02:00
|
|
|
for (int i = 0; i < d->weightsystems.nr; i++) {
|
|
|
|
QString w = getFormattedWeight(d, i);
|
2018-11-24 08:40:30 +01:00
|
|
|
if (w.isEmpty())
|
2016-07-19 06:55:21 +01:00
|
|
|
continue;
|
|
|
|
weights << w;
|
|
|
|
}
|
2016-01-11 16:48:46 -02:00
|
|
|
return weights;
|
2016-01-07 16:01:24 -02:00
|
|
|
}
|
|
|
|
|
2020-05-04 08:54:58 -05:00
|
|
|
QString formatDiveSalinity(const dive *d)
|
|
|
|
{
|
|
|
|
int salinity = get_dive_salinity(d);
|
|
|
|
if (!salinity)
|
|
|
|
return QString();
|
|
|
|
return get_salinity_string(salinity);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString formatDiveWaterType(const dive *d)
|
|
|
|
{
|
|
|
|
return get_water_type_string(get_dive_salinity(d));
|
|
|
|
}
|
|
|
|
|
2019-08-13 22:48:18 +02:00
|
|
|
// Qt's metatype system insists on generating a default constructed object, even if that makes no sense.
|
|
|
|
DiveObjectHelper::DiveObjectHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveObjectHelper::DiveObjectHelper(const struct dive *d) :
|
|
|
|
number(d->number),
|
|
|
|
id(d->id),
|
|
|
|
rating(d->rating),
|
|
|
|
visibility(d->visibility),
|
2020-12-11 17:31:04 +01:00
|
|
|
wavesize(d->wavesize),
|
|
|
|
current(d->current),
|
|
|
|
surge(d->surge),
|
|
|
|
chill(d->chill),
|
2019-08-13 22:48:18 +02:00
|
|
|
timestamp(d->when),
|
|
|
|
location(get_dive_location(d) ? QString::fromUtf8(get_dive_location(d)) : QString()),
|
|
|
|
gps(d->dive_site ? printGPSCoords(&d->dive_site->location) : QString()),
|
|
|
|
gps_decimal(format_gps_decimal(d)),
|
|
|
|
dive_site(QVariant::fromValue(d->dive_site)),
|
|
|
|
duration(get_dive_duration_string(d->duration.seconds, gettextFromC::tr("h"), gettextFromC::tr("min"))),
|
|
|
|
noDive(d->duration.seconds == 0 && d->dc.duration.seconds == 0),
|
|
|
|
depth(get_depth_string(d->dc.maxdepth.mm, true, true)),
|
|
|
|
divemaster(d->divemaster ? d->divemaster : QString()),
|
|
|
|
buddy(d->buddy ? d->buddy : QString()),
|
|
|
|
airTemp(get_temperature_string(d->airtemp, true)),
|
|
|
|
waterTemp(get_temperature_string(d->watertemp, true)),
|
|
|
|
notes(formatNotes(d)),
|
|
|
|
tags(get_taglist_string(d->tag_list)),
|
|
|
|
gas(formatGas(d)),
|
|
|
|
sac(formatSac(d)),
|
|
|
|
weightList(formatWeightList(d)),
|
|
|
|
weights(formatWeights(d)),
|
|
|
|
singleWeight(d->weightsystems.nr <= 1),
|
|
|
|
suit(d->suit ? d->suit : QString()),
|
|
|
|
cylinders(formatCylinders(d)),
|
|
|
|
maxcns(d->maxcns),
|
|
|
|
otu(d->otu),
|
|
|
|
sumWeight(get_weight_string(weight_t { total_weight(d) }, true)),
|
|
|
|
getCylinder(formatGetCylinder(d)),
|
2020-12-14 22:42:07 +01:00
|
|
|
startPressure(formatStartPressure(d)),
|
|
|
|
endPressure(formatEndPressure(d)),
|
|
|
|
firstGas(formatFirstGas(d)),
|
2020-05-04 08:54:58 -05:00
|
|
|
salinity(formatDiveSalinity(d)),
|
|
|
|
waterType(formatDiveWaterType(d))
|
2019-08-13 22:48:18 +02:00
|
|
|
{
|
2019-10-19 17:54:56 -04:00
|
|
|
#if defined(DEBUG_DOH)
|
|
|
|
void *array[4];
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
// get void*'s for all entries on the stack
|
|
|
|
size = backtrace(array, 4);
|
|
|
|
|
|
|
|
// print out all the frames to stderr
|
|
|
|
fprintf(stderr, "\n\nCalling DiveObjectHelper constructor for dive %d - call #%d\n", d->number, ++callCounter);
|
|
|
|
backtrace_symbols_fd(array, size, STDERR_FILENO);
|
|
|
|
#endif /* defined(DEBUG_DOH) */
|
2019-08-13 22:48:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::date() const
|
|
|
|
{
|
2020-05-22 18:02:15 +02:00
|
|
|
QDateTime localTime = timestampToDateTime(timestamp);
|
2019-08-13 22:48:18 +02:00
|
|
|
return localTime.date().toString(prefs.date_format_short);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::time() const
|
|
|
|
{
|
2020-05-22 18:02:15 +02:00
|
|
|
QDateTime localTime = timestampToDateTime(timestamp);
|
2019-08-13 22:48:18 +02:00
|
|
|
return localTime.time().toString(prefs.time_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DiveObjectHelper::cylinderList() const
|
|
|
|
{
|
2020-12-14 22:42:07 +01:00
|
|
|
return formatFullCylinderList();
|
2019-08-13 22:48:18 +02:00
|
|
|
}
|