2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-01-07 18:01:24 +00:00
|
|
|
#include "DiveObjectHelper.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/qthelper.h"
|
2019-03-04 22:20:29 +00:00
|
|
|
#include "core/divesite.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "core/trip.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.h"
|
|
|
|
#include "qt-models/tankinfomodel.h"
|
2016-01-07 18:01:24 +00:00
|
|
|
|
2016-02-09 16:19:06 +00:00
|
|
|
enum returnPressureSelector {START_PRESSURE, END_PRESSURE};
|
2016-01-07 18:01:24 +00:00
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString getFormattedWeight(const struct dive *dive, unsigned int idx)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2019-08-13 20:48:18 +00:00
|
|
|
const weightsystem_t *weight = &dive->weightsystems.weightsystems[idx];
|
2016-01-11 14:14:45 +00:00
|
|
|
if (!weight->description)
|
2018-11-24 07:40:30 +00:00
|
|
|
return QString();
|
2016-01-11 14:14:45 +00:00
|
|
|
QString fmt = QString(weight->description);
|
|
|
|
fmt += ", " + get_weight_string(weight->weight, true);
|
|
|
|
return fmt;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString getFormattedCylinder(const struct dive *dive, unsigned int idx)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2019-08-13 20:48:18 +00:00
|
|
|
const cylinder_t *cyl = &dive->cylinder[idx];
|
2016-01-11 14:14:45 +00:00
|
|
|
const char *desc = cyl->type.description;
|
|
|
|
if (!desc && idx > 0)
|
2018-11-24 07:40:30 +00:00
|
|
|
return QString();
|
2018-07-03 14:52:20 +00:00
|
|
|
QString fmt = desc ? QString(desc) : gettextFromC::tr("unknown");
|
Don't use "get_volume_string()" for cylinder size string
We had two totally different usage cases for "get_volume_string()": one
that did the obvious "show this volume as a string", and one that tried
to show a cylinder size.
The function used a magic third argument (the working pressure of the
cylinder) to distinguish between the two cases, but it still got it
wrong.
A metric cylinder doesn't necessarily have a working pressure at all,
and the size is a wet size in liters. We'd pass in zero as the working
pressure, and if the volume units were set to cubic feet, the logic in
"get_volume_string()" would happily convert the metric wet size into the
wet size in cubic feet.
But that's completely wrong. An imperial cylinder size simply isn't a
wet size. If you don't have a working pressure, you cannot convert the
cylinder size to cubic feet. End of story.
So instead of having "get_volume_string()" have magical behavior
depending on working pressure, and getting it wrong anyway, just make
get_volume_string do a pure volume conversion, and create a whole new
function for showing the size of a cylinder.
Now, if the cylinder doesn't have a working pressure, we just show the
metric size, even if the user had asked for cubic feet.
[Dirk Hohndel: added call to translation functions for the units]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-02-24 22:42:56 +00:00
|
|
|
fmt += ", " + get_volume_string(cyl->type.size, true);
|
2016-01-11 14:14:45 +00:00
|
|
|
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;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString getPressures(const struct dive *dive, int i, enum returnPressureSelector ret)
|
2016-02-09 16:19:06 +00:00
|
|
|
{
|
2019-08-13 20:48:18 +00:00
|
|
|
const cylinder_t *cyl = &dive->cylinder[i];
|
2016-02-09 16:19:06 +00:00
|
|
|
QString fmt;
|
2016-03-03 21:16:12 +00:00
|
|
|
if (ret == START_PRESSURE) {
|
2016-02-14 15:33:18 +00:00
|
|
|
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);
|
2016-03-03 21:16:12 +00:00
|
|
|
}
|
|
|
|
if (ret == END_PRESSURE) {
|
2016-02-14 15:33:18 +00:00
|
|
|
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);
|
2016-03-03 21:16:12 +00:00
|
|
|
}
|
2016-02-09 16:19:06 +00:00
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString format_gps_decimal(const dive *d)
|
2017-03-21 16:52:29 +00:00
|
|
|
{
|
|
|
|
bool savep = prefs.coordinates_traditional;
|
|
|
|
|
|
|
|
prefs.coordinates_traditional = false;
|
2019-08-13 20:48:18 +00:00
|
|
|
QString val = d->dive_site ? printGPSCoords(&d->dive_site->location) : QString();
|
2017-03-21 16:52:29 +00:00
|
|
|
prefs.coordinates_traditional = savep;
|
2018-02-17 20:21:16 +00:00
|
|
|
return val;
|
2017-03-21 16:52:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString formatNotes(const dive *d)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2019-08-13 20:48:18 +00:00
|
|
|
QString tmp = d->notes ? QString::fromUtf8(d->notes) : QString();
|
|
|
|
if (is_dc_planner(&d->dc)) {
|
2016-01-11 18:04:46 +00:00
|
|
|
QTextDocument notes;
|
|
|
|
#define _NOTES_BR "\n"
|
|
|
|
tmp.replace("<thead>", "<thead>" _NOTES_BR)
|
|
|
|
.replace("<br>", "<br>" _NOTES_BR)
|
2019-09-06 22:07:26 +00:00
|
|
|
.replace("<br/>", "<br/>" _NOTES_BR)
|
|
|
|
.replace("<br />", "<br />" _NOTES_BR)
|
2016-01-11 18:04:46 +00:00
|
|
|
.replace("<tr>", "<tr>" _NOTES_BR)
|
|
|
|
.replace("</tr>", "</tr>" _NOTES_BR);
|
|
|
|
notes.setHtml(tmp);
|
|
|
|
tmp = notes.toPlainText();
|
2019-09-06 22:07:26 +00:00
|
|
|
tmp.replace(_NOTES_BR, "<br/>");
|
2016-01-11 18:04:46 +00:00
|
|
|
#undef _NOTES_BR
|
|
|
|
} else {
|
2019-09-06 22:07:26 +00:00
|
|
|
tmp.replace("\n", "<br/>");
|
2016-01-11 18:04:46 +00:00
|
|
|
}
|
|
|
|
return tmp;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString formatGas(const dive *d)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2016-01-11 18:37:10 +00:00
|
|
|
/*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++) {
|
2019-08-13 20:48:18 +00:00
|
|
|
if (!is_cylinder_used(d, i))
|
2016-01-11 18:37:10 +00:00
|
|
|
continue;
|
2019-08-13 20:48:18 +00:00
|
|
|
gas = d->cylinder[i].type.description;
|
2016-01-11 18:37:10 +00:00
|
|
|
if (!gas.isEmpty())
|
|
|
|
gas += QChar(' ');
|
2019-08-13 20:48:18 +00:00
|
|
|
gas += gasname(d->cylinder[i].gasmix);
|
2016-01-11 18:37:10 +00: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 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString formatSac(const dive *d)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2019-08-13 20:48:18 +00:00
|
|
|
if (!d->sac)
|
2016-01-11 18:16:48 +00:00
|
|
|
return QString();
|
|
|
|
const char *unit;
|
|
|
|
int decimal;
|
2019-08-13 20:48:18 +00:00
|
|
|
double value = get_volume_units(d->sac, &decimal, &unit);
|
2016-01-21 20:41:40 +00:00
|
|
|
return QString::number(value, 'f', decimal).append(unit);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QString formatWeightList(const dive *d)
|
2016-02-29 14:42:07 +00:00
|
|
|
{
|
|
|
|
QString weights;
|
2019-08-13 20:48:18 +00:00
|
|
|
for (int i = 0; i < d->weightsystems.nr; i++) {
|
|
|
|
QString w = getFormattedWeight(d, i);
|
2018-11-24 07:40:30 +00:00
|
|
|
if (w.isEmpty())
|
2016-02-29 14:42:07 +00:00
|
|
|
continue;
|
|
|
|
weights += w + "; ";
|
|
|
|
}
|
|
|
|
return weights;
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QStringList formatWeights(const dive *d)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2016-01-11 18:48:46 +00:00
|
|
|
QStringList weights;
|
2019-08-13 20:48:18 +00:00
|
|
|
for (int i = 0; i < d->weightsystems.nr; i++) {
|
|
|
|
QString w = getFormattedWeight(d, i);
|
2018-11-24 07:40:30 +00:00
|
|
|
if (w.isEmpty())
|
2016-07-19 05:55:21 +00:00
|
|
|
continue;
|
|
|
|
weights << w;
|
|
|
|
}
|
2016-01-11 18:48:46 +00:00
|
|
|
return weights;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QStringList formatCylinders(const dive *d)
|
2016-01-07 18:01:24 +00:00
|
|
|
{
|
2016-01-11 18:48:46 +00:00
|
|
|
QStringList cylinders;
|
2016-07-18 23:06:10 +00:00
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
2019-08-13 20:48:18 +00:00
|
|
|
QString cyl = getFormattedCylinder(d, i);
|
2018-11-24 07:40:30 +00:00
|
|
|
if (cyl.isEmpty())
|
2016-07-18 23:06:10 +00:00
|
|
|
continue;
|
|
|
|
cylinders << cyl;
|
|
|
|
}
|
2016-01-11 18:48:46 +00:00
|
|
|
return cylinders;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QVector<CylinderObjectHelper> makeCylinderObjects(const dive *d)
|
2016-07-31 21:27:07 +00:00
|
|
|
{
|
2019-08-12 16:43:21 +00:00
|
|
|
QVector<CylinderObjectHelper> res;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
|
|
|
//Don't add blank cylinders, only those that have been defined.
|
2019-08-13 20:48:18 +00:00
|
|
|
if (d->cylinder[i].type.description)
|
|
|
|
res.append(CylinderObjectHelper(&d->cylinder[i])); // no emplace for QVector. :(
|
2019-08-12 16:43:21 +00:00
|
|
|
}
|
|
|
|
return res;
|
2016-07-31 21:27:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QStringList formatGetCylinder(const dive *d)
|
2016-01-27 17:34:34 +00:00
|
|
|
{
|
2018-07-12 09:57:21 +00:00
|
|
|
QStringList getCylinder;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
2019-08-13 20:48:18 +00:00
|
|
|
if (is_cylinder_used(d, i))
|
|
|
|
getCylinder << d->cylinder[i].type.description;
|
2018-07-12 09:57:21 +00:00
|
|
|
}
|
2016-01-27 17:34:34 +00:00
|
|
|
return getCylinder;
|
|
|
|
}
|
2016-02-09 16:19:06 +00:00
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QStringList getStartPressure(const dive *d)
|
2016-02-09 16:19:06 +00:00
|
|
|
{
|
2018-07-17 09:49:42 +00:00
|
|
|
QStringList startPressure;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
2019-08-13 20:48:18 +00:00
|
|
|
if (is_cylinder_used(d, i))
|
|
|
|
startPressure << getPressures(d, i, START_PRESSURE);
|
2018-07-17 09:49:42 +00:00
|
|
|
}
|
2016-02-09 16:19:06 +00:00
|
|
|
return startPressure;
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QStringList getEndPressure(const dive *d)
|
2016-02-09 16:19:06 +00:00
|
|
|
{
|
2018-07-17 09:49:42 +00:00
|
|
|
QStringList endPressure;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
2019-08-13 20:48:18 +00:00
|
|
|
if (is_cylinder_used(d, i))
|
|
|
|
endPressure << getPressures(d, i, END_PRESSURE);
|
2018-07-17 09:49:42 +00:00
|
|
|
}
|
2016-02-09 16:19:06 +00:00
|
|
|
return endPressure;
|
|
|
|
}
|
2016-02-13 17:34:29 +00:00
|
|
|
|
2019-08-13 20:48:18 +00:00
|
|
|
static QStringList getFirstGas(const dive *d)
|
2016-02-13 17:34:29 +00:00
|
|
|
{
|
2018-07-16 16:25:21 +00:00
|
|
|
QStringList gas;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
2019-08-13 20:48:18 +00:00
|
|
|
if (is_cylinder_used(d, i))
|
|
|
|
gas << get_gas_string(d->cylinder[i].gasmix);
|
2018-07-16 16:25:21 +00:00
|
|
|
}
|
2016-02-13 17:34:29 +00:00
|
|
|
return gas;
|
|
|
|
}
|
2019-08-13 20:48:18 +00: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),
|
|
|
|
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)),
|
|
|
|
cylinderObjects(makeCylinderObjects(d)),
|
|
|
|
maxcns(d->maxcns),
|
|
|
|
otu(d->otu),
|
|
|
|
sumWeight(get_weight_string(weight_t { total_weight(d) }, true)),
|
|
|
|
getCylinder(formatGetCylinder(d)),
|
|
|
|
startPressure(getStartPressure(d)),
|
|
|
|
endPressure(getEndPressure(d)),
|
|
|
|
firstGas(getFirstGas(d))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::date() const
|
|
|
|
{
|
|
|
|
QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000 * timestamp, Qt::UTC);
|
|
|
|
localTime.setTimeSpec(Qt::UTC);
|
|
|
|
return localTime.date().toString(prefs.date_format_short);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::time() const
|
|
|
|
{
|
|
|
|
QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000 * timestamp, Qt::UTC);
|
|
|
|
localTime.setTimeSpec(Qt::UTC);
|
|
|
|
return localTime.time().toString(prefs.time_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DiveObjectHelper::cylinderList() const
|
|
|
|
{
|
|
|
|
QStringList cylinders;
|
|
|
|
int i = 0;
|
|
|
|
struct dive *d;
|
|
|
|
for_each_dive (i, d) {
|
|
|
|
for (int j = 0; j < MAX_CYLINDERS; j++) {
|
|
|
|
QString cyl = d->cylinder[j].type.description;
|
|
|
|
if (cyl.isEmpty())
|
|
|
|
continue;
|
|
|
|
cylinders << cyl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int ti = 0; ti < MAX_TANK_INFO && tank_info[ti].name != NULL; ti++) {
|
|
|
|
QString cyl = tank_info[ti].name;
|
|
|
|
if (cyl.isEmpty())
|
|
|
|
continue;
|
|
|
|
cylinders << cyl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cylinders.removeDuplicates();
|
|
|
|
cylinders.sort();
|
|
|
|
return cylinders;
|
|
|
|
}
|