2016-01-07 18:01:24 +00:00
|
|
|
#include "DiveObjectHelper.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
|
|
#include "../qthelper.h"
|
|
|
|
#include "../helpers.h"
|
|
|
|
|
|
|
|
static QString EMPTY_DIVE_STRING = QStringLiteral("--");
|
2016-02-09 16:19:06 +00:00
|
|
|
enum returnPressureSelector {START_PRESSURE, END_PRESSURE};
|
2016-01-07 18:01:24 +00:00
|
|
|
|
|
|
|
static QString getFormattedWeight(struct dive *dive, unsigned int idx)
|
|
|
|
{
|
2016-01-11 14:14:45 +00:00
|
|
|
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;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QString getFormattedCylinder(struct dive *dive, unsigned int idx)
|
|
|
|
{
|
2016-01-11 14:14:45 +00:00
|
|
|
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, 0);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-09 16:19:06 +00:00
|
|
|
static QString getPressures(struct dive *dive, enum returnPressureSelector ret)
|
|
|
|
{
|
|
|
|
cylinder_t *cyl = &dive->cylinder[0];
|
|
|
|
QString fmt;
|
|
|
|
if (ret == START_PRESSURE)
|
|
|
|
fmt = get_pressure_string(cyl->start, true);
|
|
|
|
if (ret == END_PRESSURE)
|
|
|
|
fmt = get_pressure_string(cyl->end, true);
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:02:13 +00:00
|
|
|
DiveObjectHelper::DiveObjectHelper(struct dive *d) :
|
2016-01-07 18:01:24 +00:00
|
|
|
m_dive(d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveObjectHelper::~DiveObjectHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int DiveObjectHelper::number() const
|
|
|
|
{
|
2016-01-11 17:02:13 +00:00
|
|
|
return m_dive->number;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveObjectHelper::id() const
|
|
|
|
{
|
2016-01-11 17:04:21 +00:00
|
|
|
return m_dive->id;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::date() const
|
|
|
|
{
|
2016-01-11 18:40:10 +00:00
|
|
|
QDateTime localTime = QDateTime::fromTime_t(m_dive->when - gettimezoneoffset(m_dive->when));
|
|
|
|
localTime.setTimeSpec(Qt::UTC);
|
|
|
|
return localTime.date().toString(prefs.date_format);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timestamp_t DiveObjectHelper::timestamp() const
|
|
|
|
{
|
2016-01-11 17:34:53 +00:00
|
|
|
return m_dive->when;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::time() const
|
|
|
|
{
|
2016-01-11 18:40:10 +00:00
|
|
|
QDateTime localTime = QDateTime::fromTime_t(m_dive->when - gettimezoneoffset(m_dive->when));
|
|
|
|
localTime.setTimeSpec(Qt::UTC);
|
|
|
|
return localTime.time().toString(prefs.time_format);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::location() const
|
|
|
|
{
|
2016-01-11 17:35:21 +00:00
|
|
|
return get_dive_location(m_dive) ? QString::fromUtf8(get_dive_location(m_dive)) : EMPTY_DIVE_STRING;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::gps() const
|
|
|
|
{
|
2016-01-11 18:13:23 +00:00
|
|
|
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();
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
QString DiveObjectHelper::duration() const
|
|
|
|
{
|
2016-01-11 17:36:12 +00:00
|
|
|
return get_dive_duration_string(m_dive->duration.seconds, QObject::tr("h:"), QObject::tr("min"));
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::depth() const
|
|
|
|
{
|
2016-01-11 17:36:12 +00:00
|
|
|
return get_depth_string(m_dive->dc.maxdepth.mm, true, true);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::divemaster() const
|
|
|
|
{
|
2016-01-11 17:39:04 +00:00
|
|
|
return m_dive->divemaster ? m_dive->divemaster : EMPTY_DIVE_STRING;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::buddy() const
|
|
|
|
{
|
2016-01-11 17:40:10 +00:00
|
|
|
return m_dive->buddy ? m_dive->buddy : EMPTY_DIVE_STRING;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::airTemp() const
|
|
|
|
{
|
2016-01-11 17:40:10 +00:00
|
|
|
QString temp = get_temperature_string(m_dive->airtemp, true);
|
|
|
|
if (temp.isEmpty()) {
|
|
|
|
temp = EMPTY_DIVE_STRING;
|
|
|
|
}
|
|
|
|
return temp;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::waterTemp() const
|
|
|
|
{
|
2016-01-11 17:46:30 +00:00
|
|
|
QString temp = get_temperature_string(m_dive->watertemp, true);
|
|
|
|
if (temp.isEmpty()) {
|
|
|
|
temp = EMPTY_DIVE_STRING;
|
|
|
|
}
|
|
|
|
return temp;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::notes() const
|
|
|
|
{
|
2016-01-11 18:04:46 +00:00
|
|
|
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 "\n"
|
|
|
|
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;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::tags() const
|
|
|
|
{
|
2016-01-11 18:29:49 +00:00
|
|
|
static char buffer[256];
|
|
|
|
taglist_get_tagstring(m_dive->tag_list, buffer, 256);
|
|
|
|
return QString(buffer);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::gas() const
|
|
|
|
{
|
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++) {
|
|
|
|
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;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::sac() const
|
|
|
|
{
|
2016-01-11 18:16:48 +00:00
|
|
|
if (!m_dive->sac)
|
|
|
|
return QString();
|
|
|
|
const char *unit;
|
|
|
|
int decimal;
|
|
|
|
double value = get_volume_units(m_dive->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
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DiveObjectHelper::weights() const
|
|
|
|
{
|
2016-01-11 18:48:46 +00:00
|
|
|
QStringList weights;
|
|
|
|
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
|
|
|
|
weights << getFormattedWeight(m_dive, i);
|
|
|
|
return weights;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 06:53:12 +00:00
|
|
|
bool DiveObjectHelper::singleWeight() const
|
|
|
|
{
|
|
|
|
return weightsystem_none(&m_dive->weightsystem[1]);
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:01:24 +00:00
|
|
|
QString DiveObjectHelper::weight(int idx) const
|
|
|
|
{
|
2016-01-11 18:48:46 +00:00
|
|
|
if ( (idx < 0) || idx > MAX_WEIGHTSYSTEMS )
|
2016-01-07 18:01:24 +00:00
|
|
|
return QString(EMPTY_DIVE_STRING);
|
2016-01-11 18:48:46 +00:00
|
|
|
return getFormattedWeight(m_dive, idx);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::suit() const
|
|
|
|
{
|
2016-01-11 17:49:06 +00:00
|
|
|
return m_dive->suit ? m_dive->suit : EMPTY_DIVE_STRING;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DiveObjectHelper::cylinders() const
|
|
|
|
{
|
2016-01-11 18:48:46 +00:00
|
|
|
QStringList cylinders;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++)
|
|
|
|
cylinders << getFormattedCylinder(m_dive, i);
|
|
|
|
return cylinders;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::cylinder(int idx) const
|
|
|
|
{
|
2016-01-11 18:48:46 +00:00
|
|
|
if ( (idx < 0) || idx > MAX_CYLINDERS)
|
2016-01-07 18:01:24 +00:00
|
|
|
return QString(EMPTY_DIVE_STRING);
|
2016-01-11 18:48:46 +00:00
|
|
|
return getFormattedCylinder(m_dive, idx);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::trip() const
|
|
|
|
{
|
2016-01-11 17:49:06 +00:00
|
|
|
return m_dive->divetrip ? m_dive->divetrip->location : EMPTY_DIVE_STRING;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 04:06:30 +00:00
|
|
|
// combine the pointer address with the trip location so that
|
|
|
|
// we detect multiple, destinct trips to the same location
|
|
|
|
QString DiveObjectHelper::tripMeta() const
|
|
|
|
{
|
|
|
|
QString ret = EMPTY_DIVE_STRING;
|
|
|
|
if (m_dive->divetrip)
|
|
|
|
ret = QString::number((quint64)m_dive->divetrip, 16) + QLatin1Literal("::") + m_dive->divetrip->location;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:01:24 +00:00
|
|
|
QString DiveObjectHelper::maxcns() const
|
|
|
|
{
|
2016-01-11 17:56:50 +00:00
|
|
|
return QString(m_dive->maxcns);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::otu() const
|
|
|
|
{
|
2016-01-11 17:56:50 +00:00
|
|
|
return QString(m_dive->otu);
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveObjectHelper::rating() const
|
|
|
|
{
|
2016-01-11 17:05:07 +00:00
|
|
|
return m_dive->rating;
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
2016-01-27 17:34:34 +00:00
|
|
|
|
|
|
|
QString DiveObjectHelper::sumWeight() const
|
|
|
|
{
|
2016-02-06 06:52:30 +00:00
|
|
|
weight_t sum = { 0 };
|
2016-01-27 17:34:34 +00:00
|
|
|
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++){
|
2016-02-06 06:52:30 +00:00
|
|
|
sum.grams += m_dive->weightsystem[i].weight.grams;
|
2016-01-27 17:34:34 +00:00
|
|
|
}
|
2016-02-06 06:52:30 +00:00
|
|
|
return get_weight_string(sum, true);
|
2016-01-27 17:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveObjectHelper::getCylinder() const
|
|
|
|
{
|
|
|
|
QString getCylinder;
|
|
|
|
if (m_dive->cylinder[1].type.description != NULL){
|
|
|
|
getCylinder = QObject::tr("Multiple");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
getCylinder = m_dive->cylinder[0].type.description;
|
|
|
|
}
|
|
|
|
return getCylinder;
|
|
|
|
}
|
2016-02-09 16:19:06 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|