Move Dive class from qthelper.h/cpp to it's own file

and rename it to DiveObjectHelper, since it should be an QObject
based class to make it easier on the QML, grantlee and widgets
side to display the dive's internal data.

each Q_PROPERTY defined in the DiveObjectHelper.h file
can be acessed directly via it's name.

So, if you are on a model that  returns a dive, acess it's name
by dive.name

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2016-01-07 16:01:24 -02:00 committed by Dirk Hohndel
parent c022f5c4f7
commit 25aa80846b
10 changed files with 403 additions and 564 deletions

View file

@ -69,7 +69,6 @@ QString TemplateLayout::generate()
m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser());
m_engine->addTemplateLoader(m_templateLoader); m_engine->addTemplateLoader(m_templateLoader);
Grantlee::registerMetaType<Dive>();
Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<template_options>();
Grantlee::registerMetaType<print_options>(); Grantlee::registerMetaType<print_options>();
@ -81,7 +80,7 @@ QString TemplateLayout::generate()
//TODO check for exporting selected dives only //TODO check for exporting selected dives only
if (!dive->selected && PrintOptions->print_selected) if (!dive->selected && PrintOptions->print_selected)
continue; continue;
Dive d(dive); DiveObjectHelper *d = new DiveObjectHelper(dive);
diveList.append(QVariant::fromValue(d)); diveList.append(QVariant::fromValue(d));
progress++; progress++;
emit progressUpdated(progress * 100.0 / totalWork); emit progressUpdated(progress * 100.0 / totalWork);

View file

@ -7,6 +7,7 @@
#include "statistics.h" #include "statistics.h"
#include "qthelper.h" #include "qthelper.h"
#include "helpers.h" #include "helpers.h"
#include "subsurface-core/subsurface-qt/DiveObjectHelper.h"
int getTotalWork(print_options *printOptions); int getTotalWork(print_options *printOptions);
void find_all_templates(); void find_all_templates();
@ -44,7 +45,6 @@ public:
~YearInfo(); ~YearInfo();
}; };
Q_DECLARE_METATYPE(Dive)
Q_DECLARE_METATYPE(template_options) Q_DECLARE_METATYPE(template_options)
Q_DECLARE_METATYPE(print_options) Q_DECLARE_METATYPE(print_options)
Q_DECLARE_METATYPE(YearInfo) Q_DECLARE_METATYPE(YearInfo)
@ -59,7 +59,7 @@ Q_DECLARE_METATYPE(YearInfo)
#define _RETURN_DIVE_PROPERTY_IDX(prop, idx) \ #define _RETURN_DIVE_PROPERTY_IDX(prop, idx) \
if (property == _CONC_STR(prop, idx)) return object.prop(idx) if (property == _CONC_STR(prop, idx)) return object.prop(idx)
GRANTLEE_BEGIN_LOOKUP(Dive) GRANTLEE_BEGIN_LOOKUP(DiveObjectHelper)
_RETURN_DIVE_PROPERTY(number); _RETURN_DIVE_PROPERTY(number);
else _RETURN_DIVE_PROPERTY(id); else _RETURN_DIVE_PROPERTY(id);
else _RETURN_DIVE_PROPERTY(date); else _RETURN_DIVE_PROPERTY(date);

View file

@ -14,7 +14,6 @@ MobileComponents.Page {
Component { Component {
id: diveDelegate id: diveDelegate
MobileComponents.ListItem { MobileComponents.ListItem {
id: dive
enabled: true enabled: true
checked: diveListView.currentIndex == model.index checked: diveListView.currentIndex == model.index
width: parent.width width: parent.width
@ -27,22 +26,22 @@ MobileComponents.Page {
onClicked: { onClicked: {
diveListView.currentIndex = model.index diveListView.currentIndex = model.index
detailsWindow.width = parent.width detailsWindow.width = parent.width
detailsWindow.location = location detailsWindow.location = dive.location
detailsWindow.gps = gps detailsWindow.gps = dive.gps
detailsWindow.dive_id = id detailsWindow.dive_id = dive.id
detailsWindow.diveNumber = diveNumber detailsWindow.diveNumber = dive.number
detailsWindow.duration = duration detailsWindow.duration = dive.duration
detailsWindow.depth = depth detailsWindow.depth = dive.depth
detailsWindow.rating = rating detailsWindow.rating = dive.rating
detailsWindow.buddy = buddy detailsWindow.buddy = dive.buddy
detailsWindow.suit = suit detailsWindow.suit = dive.suit
detailsWindow.airtemp = airtemp detailsWindow.airtemp = dive.airTemp
detailsWindow.watertemp = watertemp detailsWindow.watertemp = dive.waterTemp
detailsWindow.divemaster = divemaster detailsWindow.divemaster = dive.divemaster
detailsWindow.notes = notes detailsWindow.notes = dive.notes
detailsWindow.number = diveNumber detailsWindow.number = dive.number
detailsWindow.date = date detailsWindow.date = dive.date
detailsWindow.weight = weight // detailsWindow.weight = dive.weights
stackView.push(detailsWindow) stackView.push(detailsWindow)
} }
@ -52,7 +51,7 @@ MobileComponents.Page {
MobileComponents.Label { MobileComponents.Label {
id: locationText id: locationText
text: location text: dive.location
font.weight: Font.Light font.weight: Font.Light
elide: Text.ElideRight elide: Text.ElideRight
maximumLineCount: 1 // needed for elide to work at all maximumLineCount: 1 // needed for elide to work at all
@ -65,7 +64,7 @@ MobileComponents.Page {
} }
MobileComponents.Label { MobileComponents.Label {
id: dateLabel id: dateLabel
text: date text: dive.date
opacity: 0.6 opacity: 0.6
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
anchors { anchors {
@ -87,7 +86,7 @@ MobileComponents.Page {
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
} }
MobileComponents.Label { MobileComponents.Label {
text: depth text: dive.depth
width: Math.max(MobileComponents.Units.gridUnit * 3, paintedWidth) // helps vertical alignment throughout listview width: Math.max(MobileComponents.Units.gridUnit * 3, paintedWidth) // helps vertical alignment throughout listview
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
} }
@ -97,13 +96,13 @@ MobileComponents.Page {
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
} }
MobileComponents.Label { MobileComponents.Label {
text: duration text: dive.duration
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
} }
} }
MobileComponents.Label { MobileComponents.Label {
id: numberText id: numberText
text: "#" + diveNumber text: "#" + dive.location
color: MobileComponents.Theme.textColor color: MobileComponents.Theme.textColor
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
opacity: 0.6 opacity: 0.6

View file

@ -12,15 +12,15 @@ DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
void DiveListModel::addDive(dive *d) void DiveListModel::addDive(dive *d)
{ {
beginInsertRows(QModelIndex(), rowCount(), rowCount()); beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_dives.append(Dive(d)); m_dives.append(new DiveObjectHelper(d));
endInsertRows(); endInsertRows();
} }
void DiveListModel::updateDive(dive *d) void DiveListModel::updateDive(dive *d)
{ {
for (int i = 0; i < m_dives.count(); i++) { for (int i = 0; i < m_dives.count(); i++) {
if (m_dives.at(i).id() == d->id) { if (m_dives.at(i)->id() == d->id) {
Dive newDive(d); DiveObjectHelper *newDive = new DiveObjectHelper(d);
m_dives.replace(i, newDive); m_dives.replace(i, newDive);
break; break;
} }
@ -31,6 +31,7 @@ void DiveListModel::clear()
{ {
if (m_dives.count()) { if (m_dives.count()) {
beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1); beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1);
qDeleteAll(m_dives);
m_dives.clear(); m_dives.clear();
endRemoveRows(); endRemoveRows();
} }
@ -46,76 +47,20 @@ QVariant DiveListModel::data(const QModelIndex &index, int role) const
if(index.row() < 0 || index.row() > m_dives.count()) if(index.row() < 0 || index.row() > m_dives.count())
return QVariant(); return QVariant();
const Dive &dive = m_dives[index.row()]; DiveObjectHelper *curr_dive = m_dives[index.row()];
switch(role) {
if (role == DiveNumberRole) case DiveRole: return QVariant::fromValue<QObject*>(curr_dive);
return dive.number(); case DiveDateRole: return (qlonglong)curr_dive->timestamp();
else if (role == DiveTripRole) }
return dive.trip();
else if (role == DiveDateRole)
return (qlonglong)dive.timestamp();
else if (role == DiveDateStringRole)
return dive.date() + " " + dive.time();
else if (role == DiveRatingRole)
return QString::number(dive.rating());
else if (role == DiveDepthRole)
return dive.depth();
else if (role == DiveDurationRole)
return dive.duration();
else if (role == DiveAirTemperatureRole)
return dive.airTemp();
else if (role == DiveWaterTemperatureRole)
return dive.waterTemp();
else if (role == DiveWeightRole)
return dive.weight(0);
else if (role == DiveSuitRole)
return dive.suit();
else if (role == DiveCylinderRole)
return dive.cylinder(0);
else if (role == DiveGasRole)
return dive.gas();
else if (role == DiveSacRole)
return dive.sac();
else if (role == DiveLocationRole)
return dive.location();
else if (role == DiveGPSRole)
return dive.gps();
else if (role == DiveNotesRole)
return dive.notes();
else if (role == DiveBuddyRole)
return dive.buddy();
else if (role == DiveMasterRole)
return dive.divemaster();
else if (role == DiveIdRole)
return QString::number(dive.id());
return QVariant(); return QVariant();
} }
QHash<int, QByteArray> DiveListModel::roleNames() const QHash<int, QByteArray> DiveListModel::roleNames() const
{ {
QHash<int, QByteArray> roles; QHash<int, QByteArray> roles;
roles[DiveNumberRole] = "diveNumber"; roles[DiveRole] = "dive";
roles[DiveTripRole] = "trip"; roles[DiveDateRole] = "date";
roles[DiveDateStringRole] = "date";
roles[DiveRatingRole] = "rating";
roles[DiveDepthRole] = "depth";
roles[DiveDurationRole] = "duration";
roles[DiveAirTemperatureRole] = "airtemp";
roles[DiveWaterTemperatureRole] = "watertemp";
roles[DiveWeightRole] = "weight";
roles[DiveSuitRole] = "suit";
roles[DiveCylinderRole] = "cylinder";
roles[DiveGasRole] = "gas";
roles[DiveSacRole] = "sac";
roles[DiveLocationRole] = "location";
roles[DiveGPSRole] = "gps";
roles[DiveNotesRole] = "notes";
roles[DiveBuddyRole] = "buddy";
roles[DiveMasterRole] = "divemaster";
roles[DiveIdRole] = "id";
return roles; return roles;
} }
@ -140,3 +85,7 @@ DiveListModel *DiveListModel::instance()
{ {
return m_instance; return m_instance;
} }
DiveObjectHelper* DiveListModel::at(int i){
return m_dives.at(i);
}

View file

@ -2,8 +2,10 @@
#define DIVELISTMODEL_H #define DIVELISTMODEL_H
#include <QAbstractListModel> #include <QAbstractListModel>
#include "dive.h" #include "dive.h"
#include "helpers.h" #include "helpers.h"
#include "subsurface-qt/DiveObjectHelper.h"
class DiveListModel : public QAbstractListModel class DiveListModel : public QAbstractListModel
{ {
@ -11,26 +13,8 @@ class DiveListModel : public QAbstractListModel
public: public:
enum DiveListRoles { enum DiveListRoles {
DiveNumberRole = Qt::UserRole + 1, DiveRole = Qt::UserRole + 1,
DiveTripRole, DiveDateRole
DiveDateRole,
DiveDateStringRole,
DiveRatingRole,
DiveDepthRole,
DiveDurationRole,
DiveWaterTemperatureRole,
DiveAirTemperatureRole,
DiveWeightRole,
DiveSuitRole,
DiveCylinderRole,
DiveGasRole,
DiveSacRole,
DiveLocationRole,
DiveGPSRole,
DiveNotesRole,
DiveBuddyRole,
DiveMasterRole,
DiveIdRole
}; };
static DiveListModel *instance(); static DiveListModel *instance();
@ -42,8 +26,9 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
QString startAddDive(); QString startAddDive();
Q_INVOKABLE DiveObjectHelper* at(int i);
private: private:
QList<Dive> m_dives; QList<DiveObjectHelper*> m_dives;
static DiveListModel *m_instance; static DiveListModel *m_instance;
}; };

View file

@ -81,6 +81,9 @@ set(SUBSURFACE_CORE_LIB_SRCS
imagedownloader.cpp imagedownloader.cpp
isocialnetworkintegration.cpp isocialnetworkintegration.cpp
gpslocation.cpp gpslocation.cpp
#Subsurface Qt have the Subsurface structs QObjectified for easy access via QML.
subsurface-qt/DiveObjectHelper.cpp
${SERIAL_FTDI} ${SERIAL_FTDI}
${PLATFORM_SRC} ${PLATFORM_SRC}
${BT_CORE_SRC_FILES} ${BT_CORE_SRC_FILES}

View file

@ -38,353 +38,6 @@ static QLocale loc;
#define translate(_context, arg) trGettext(arg) #define translate(_context, arg) trGettext(arg)
static const QString DEGREE_SIGNS("dD" UTF8_DEGREE); static const QString DEGREE_SIGNS("dD" UTF8_DEGREE);
#define EMPTY_DIVE_STRING "--"
Dive::Dive() :
m_number(-1),
dive(NULL)
{
}
Dive::~Dive()
{
}
int Dive::number() const
{
return m_number;
}
int Dive::id() const
{
return m_id;
}
QString Dive::date() const
{
return m_date;
}
timestamp_t Dive::timestamp() const
{
return m_timestamp;
}
QString Dive::time() const
{
return m_time;
}
QString Dive::location() const
{
return m_location;
}
QString Dive::gps() const
{
return m_gps;
}
QString Dive::duration() const
{
return m_duration;
}
QString Dive::depth() const
{
return m_depth;
}
QString Dive::divemaster() const
{
return m_divemaster;
}
QString Dive::buddy() const
{
return m_buddy;
}
QString Dive::airTemp() const
{
return m_airTemp;
}
QString Dive::waterTemp() const
{
return m_waterTemp;
}
QString Dive::notes() const
{
return m_notes;
}
QString Dive::tags() const
{
return m_tags;
}
QString Dive::gas() const
{
return m_gas;
}
QString Dive::sac() const
{
return m_sac;
}
QString Dive::weights() const
{
QString str = "";
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
QString entry = m_weights.at(i);
if (entry == EMPTY_DIVE_STRING)
continue;
str += QObject::tr("Weight %1: ").arg(i + 1) + entry + "; ";
}
return str;
}
QString Dive::weight(int idx) const
{
if (idx < 0 || idx > m_weights.size() - 1)
return QString(EMPTY_DIVE_STRING);
return m_weights.at(idx);
}
QString Dive::suit() const
{
return m_suit;
}
QString Dive::cylinders() const
{
QString str = "";
for (int i = 0; i < MAX_CYLINDERS; i++) {
QString entry = m_cylinders.at(i);
if (entry == EMPTY_DIVE_STRING)
continue;
str += QObject::tr("Cylinder %1: ").arg(i + 1) + entry + "; ";
}
return str;
}
QString Dive::cylinder(int idx) const
{
if (idx < 0 || idx > m_cylinders.size() - 1)
return QString(EMPTY_DIVE_STRING);
return m_cylinders.at(idx);
}
QString Dive::trip() const
{
return m_trip;
}
QString Dive::maxcns() const
{
return m_maxcns;
}
QString Dive::otu() const
{
return m_otu;
}
int Dive::rating() const
{
return m_rating;
}
void Dive::put_divemaster()
{
if (!dive->divemaster)
m_divemaster = EMPTY_DIVE_STRING;
else
m_divemaster = dive->divemaster;
}
void Dive::put_date_time()
{
QDateTime localTime = QDateTime::fromTime_t(dive->when - gettimezoneoffset(dive->when));
localTime.setTimeSpec(Qt::UTC);
m_date = localTime.date().toString(prefs.date_format);
m_time = localTime.time().toString(prefs.time_format);
}
void Dive::put_timestamp()
{
m_timestamp = dive->when;
}
void Dive::put_location()
{
m_location = QString::fromUtf8(get_dive_location(dive));
if (m_location.isEmpty()) {
m_location = EMPTY_DIVE_STRING;
}
}
void Dive::put_gps()
{
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (ds)
m_gps = QString("%1,%2").arg(ds->latitude.udeg / 1000000.0).arg(ds->longitude.udeg / 1000000.0);
else
m_gps = QString();
}
void Dive::put_depth()
{
m_depth = get_depth_string(dive->dc.maxdepth.mm, true, true);
}
void Dive::put_duration()
{
m_duration = get_dive_duration_string(dive->duration.seconds, QObject::tr("h:"), QObject::tr("min"));
}
void Dive::put_buddy()
{
if (!dive->buddy)
m_buddy = EMPTY_DIVE_STRING;
else
m_buddy = dive->buddy;
}
void Dive::put_temp()
{
m_airTemp = get_temperature_string(dive->airtemp, true);
m_waterTemp = get_temperature_string(dive->watertemp, true);
if (m_airTemp.isEmpty()) {
m_airTemp = EMPTY_DIVE_STRING;
}
if (m_waterTemp.isEmpty()) {
m_waterTemp = EMPTY_DIVE_STRING;
}
}
void Dive::put_notes()
{
m_notes = QString::fromUtf8(dive->notes);
if (m_notes.isEmpty()) {
m_notes = EMPTY_DIVE_STRING;
return;
}
if (same_string(dive->dc.model, "planned dive")) {
QTextDocument notes;
QString notesFormatted = m_notes;
#define _NOTES_BR "&#92n"
notesFormatted = notesFormatted.replace("<thead>", "<thead>" _NOTES_BR);
notesFormatted = notesFormatted.replace("<br>", "<br>" _NOTES_BR);
notesFormatted = notesFormatted.replace("<tr>", "<tr>" _NOTES_BR);
notesFormatted = notesFormatted.replace("</tr>", "</tr>" _NOTES_BR);
notes.setHtml(notesFormatted);
m_notes = notes.toPlainText();
m_notes.replace(_NOTES_BR, "<br>");
#undef _NOTES_BR
} else {
m_notes.replace("\n", "<br>");
}
}
void Dive::put_tags()
{
char buffer[256];
taglist_get_tagstring(dive->tag_list, buffer, 256);
m_tags = QString(buffer);
}
void Dive::put_gas()
{
int added = 0;
QString gas, gases;
for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!is_cylinder_used(dive, i))
continue;
gas = dive->cylinder[i].type.description;
gas += QString(!gas.isEmpty() ? " " : "") + gasname(&dive->cylinder[i].gasmix);
// if has a description and if such gas is not already present
if (!gas.isEmpty() && gases.indexOf(gas) == -1) {
if (added > 0)
gases += QString(" / ");
gases += gas;
added++;
}
}
m_gas = gases;
}
void Dive::put_sac()
{
if (dive->sac) {
const char *unit;
int decimal;
double value = get_volume_units(dive->sac, &decimal, &unit);
m_sac = QString::number(value, 'f', decimal).append(unit);
}
}
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;
}
void Dive::put_weight()
{
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
m_weights << getFormattedWeight(dive, i);
}
void Dive::put_suit()
{
m_suit = QString(dive->suit);
}
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, 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;
}
void Dive::put_cylinder()
{
for (int i = 0; i < MAX_CYLINDERS; i++)
m_cylinders << getFormattedCylinder(dive, i);
}
void Dive::put_trip()
{
dive_trip *trip = dive->divetrip;
if (trip) {
m_trip = QString(trip->location);
}
}
void Dive::put_maxcns()
{
m_maxcns = QString::number(dive->maxcns);
}
void Dive::put_otu()
{
m_otu = QString::number(dive->otu);
}
QString weight_string(int weight_in_grams) QString weight_string(int weight_in_grams)
{ {
QString str; QString str;

View file

@ -9,110 +9,6 @@
#include <QTranslator> #include <QTranslator>
#include <QDir> #include <QDir>
class Dive {
private:
int m_number;
int m_id;
int m_rating;
QString m_date;
timestamp_t m_timestamp;
QString m_time;
QString m_location;
QString m_gps;
QString m_duration;
QString m_depth;
QString m_divemaster;
QString m_buddy;
QString m_airTemp;
QString m_waterTemp;
QString m_notes;
QString m_tags;
QString m_gas;
QString m_sac;
QStringList m_weights;
QString m_suit;
QStringList m_cylinders;
QString m_trip;
QString m_maxcns;
QString m_otu;
struct dive *dive;
void put_date_time();
void put_timestamp();
void put_location();
void put_gps();
void put_duration();
void put_depth();
void put_divemaster();
void put_buddy();
void put_temp();
void put_notes();
void put_tags();
void put_gas();
void put_sac();
void put_weight();
void put_suit();
void put_cylinder();
void put_trip();
void put_maxcns();
void put_otu();
public:
Dive(struct dive *dive)
: dive(dive)
{
m_number = dive->number;
m_id = dive->id;
m_rating = dive->rating;
put_date_time();
put_location();
put_gps();
put_duration();
put_depth();
put_divemaster();
put_buddy();
put_temp();
put_notes();
put_tags();
put_gas();
put_sac();
put_timestamp();
put_weight();
put_suit();
put_cylinder();
put_trip();
put_maxcns();
put_otu();
}
Dive();
~Dive();
int number() const;
int id() const;
int rating() const;
QString date() const;
timestamp_t timestamp() const;
QString time() const;
QString location() const;
QString gps() const;
QString duration() const;
QString depth() const;
QString divemaster() const;
QString buddy() const;
QString airTemp() const;
QString waterTemp() const;
QString notes() const;
QString tags() const;
QString gas() const;
QString sac() const;
QString weights() const;
QString weight(int idx) const;
QString suit() const;
QString cylinders() const;
QString cylinder(int idx) const;
QString trip() const;
QString maxcns() const;
QString otu() const;
};
// global pointers for our translation // global pointers for our translation
extern QTranslator *qtTranslator, *ssrfTranslator; extern QTranslator *qtTranslator, *ssrfTranslator;

View file

@ -0,0 +1,263 @@
#include "DiveObjectHelper.h"
#include <QDateTime>
#include <QTextDocument>
#include "../qthelper.h"
#include "../helpers.h"
static QString EMPTY_DIVE_STRING = QStringLiteral("--");
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, 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;
}
DiveObjectHelper::DiveObjectHelper(struct dive *d) :
m_number(d->number),
m_id(d->id),
m_rating(d->rating),
m_timestamp(d->when),
m_location(get_dive_location(d) ? QString::fromUtf8(get_dive_location(d)) : EMPTY_DIVE_STRING),
m_duration(get_dive_duration_string(d->duration.seconds, QObject::tr("h:"), QObject::tr("min"))),
m_depth(get_depth_string(d->dc.maxdepth.mm, true, true)),
m_divemaster(d->divemaster ? d->divemaster : EMPTY_DIVE_STRING),
m_buddy(d->buddy ? d->buddy : EMPTY_DIVE_STRING),
m_airTemp(get_temperature_string(d->airtemp, true)),
m_waterTemp(get_temperature_string(d->watertemp, true)),
m_suit(d->suit ? d->suit : EMPTY_DIVE_STRING),
m_trip(d->divetrip ? d->divetrip->location : EMPTY_DIVE_STRING),
m_maxcns(d->maxcns),
m_otu(d->otu),
m_dive(d)
{
struct dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (ds)
m_gps = QString("%1,%2").arg(ds->latitude.udeg / 1000000.0).arg(ds->longitude.udeg / 1000000.0);
if (m_airTemp.isEmpty()) {
m_airTemp = EMPTY_DIVE_STRING;
}
if (m_waterTemp.isEmpty()) {
m_waterTemp = EMPTY_DIVE_STRING;
}
m_notes = QString::fromUtf8(d->notes);
if (m_notes.isEmpty()) {
m_notes = EMPTY_DIVE_STRING;
return;
}
if (same_string(d->dc.model, "planned dive")) {
QTextDocument notes;
QString notesFormatted = m_notes;
#define _NOTES_BR "&#92n"
notesFormatted = notesFormatted.replace("<thead>", "<thead>" _NOTES_BR);
notesFormatted = notesFormatted.replace("<br>", "<br>" _NOTES_BR);
notesFormatted = notesFormatted.replace("<tr>", "<tr>" _NOTES_BR);
notesFormatted = notesFormatted.replace("</tr>", "</tr>" _NOTES_BR);
notes.setHtml(notesFormatted);
m_notes = notes.toPlainText();
m_notes.replace(_NOTES_BR, "<br>");
#undef _NOTES_BR
} else {
m_notes.replace("\n", "<br>");
}
char buffer[256];
taglist_get_tagstring(d->tag_list, buffer, 256);
m_tags = QString(buffer);
int added = 0;
QString gas, gases;
for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!is_cylinder_used(d, i))
continue;
gas = d->cylinder[i].type.description;
gas += QString(!gas.isEmpty() ? " " : "") + gasname(&d->cylinder[i].gasmix);
// if has a description and if such gas is not already present
if (!gas.isEmpty() && gases.indexOf(gas) == -1) {
if (added > 0)
gases += QString(" / ");
gases += gas;
added++;
}
}
m_gas = gases;
if (d->sac) {
const char *unit;
int decimal;
double value = get_volume_units(d->sac, &decimal, &unit);
m_sac = QString::number(value, 'f', decimal).append(unit);
}
for (int i = 0; i < MAX_CYLINDERS; i++)
m_cylinders << getFormattedCylinder(d, i);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
m_weights << getFormattedWeight(d, i);
QDateTime localTime = QDateTime::fromTime_t(d->when - gettimezoneoffset(d->when));
localTime.setTimeSpec(Qt::UTC);
m_date = localTime.date().toString(prefs.date_format);
m_time = localTime.time().toString(prefs.time_format);
}
DiveObjectHelper::~DiveObjectHelper()
{
}
int DiveObjectHelper::number() const
{
return m_number;
}
int DiveObjectHelper::id() const
{
return m_id;
}
QString DiveObjectHelper::date() const
{
return m_date;
}
timestamp_t DiveObjectHelper::timestamp() const
{
return m_timestamp;
}
QString DiveObjectHelper::time() const
{
return m_time;
}
QString DiveObjectHelper::location() const
{
return m_location;
}
QString DiveObjectHelper::gps() const
{
return m_gps;
}
QString DiveObjectHelper::duration() const
{
return m_duration;
}
QString DiveObjectHelper::depth() const
{
return m_depth;
}
QString DiveObjectHelper::divemaster() const
{
return m_divemaster;
}
QString DiveObjectHelper::buddy() const
{
return m_buddy;
}
QString DiveObjectHelper::airTemp() const
{
return m_airTemp;
}
QString DiveObjectHelper::waterTemp() const
{
return m_waterTemp;
}
QString DiveObjectHelper::notes() const
{
return m_notes;
}
QString DiveObjectHelper::tags() const
{
return m_tags;
}
QString DiveObjectHelper::gas() const
{
return m_gas;
}
QString DiveObjectHelper::sac() const
{
return m_sac;
}
QStringList DiveObjectHelper::weights() const
{
return m_weights;
}
QString DiveObjectHelper::weight(int idx) const
{
if (idx < 0 || idx > m_weights.size() - 1)
return QString(EMPTY_DIVE_STRING);
return m_weights.at(idx);
}
QString DiveObjectHelper::suit() const
{
return m_suit;
}
QStringList DiveObjectHelper::cylinders() const
{
return m_cylinders;
}
QString DiveObjectHelper::cylinder(int idx) const
{
if (idx < 0 || idx > m_cylinders.size() - 1)
return QString(EMPTY_DIVE_STRING);
return m_cylinders.at(idx);
}
QString DiveObjectHelper::trip() const
{
return m_trip;
}
QString DiveObjectHelper::maxcns() const
{
return m_maxcns;
}
QString DiveObjectHelper::otu() const
{
return m_otu;
}
int DiveObjectHelper::rating() const
{
return m_rating;
}

View file

@ -0,0 +1,92 @@
#ifndef DIVE_QOBJECT_H
#define DIVE_QOBJECT_H
#include "../dive.h"
#include <QObject>
#include <QString>
class DiveObjectHelper : public QObject {
Q_OBJECT
Q_PROPERTY(int number READ number CONSTANT)
Q_PROPERTY(int id READ id CONSTANT)
Q_PROPERTY(int rating READ rating CONSTANT)
Q_PROPERTY(QString date READ date CONSTANT)
Q_PROPERTY(QString time READ time CONSTANT)
Q_PROPERTY(QString location READ location CONSTANT)
Q_PROPERTY(QString gps READ gps CONSTANT)
Q_PROPERTY(QString duration READ duration CONSTANT)
Q_PROPERTY(QString depth READ depth CONSTANT)
Q_PROPERTY(QString divemaster READ divemaster CONSTANT)
Q_PROPERTY(QString buddy READ buddy CONSTANT)
Q_PROPERTY(QString airTemp READ airTemp CONSTANT)
Q_PROPERTY(QString waterTemp READ waterTemp CONSTANT)
Q_PROPERTY(QString notes READ notes CONSTANT)
Q_PROPERTY(QString tags READ tags CONSTANT)
Q_PROPERTY(QString gas READ gas CONSTANT)
Q_PROPERTY(QString sac READ sac CONSTANT)
Q_PROPERTY(QStringList weights READ weights CONSTANT)
Q_PROPERTY(QString suit READ suit CONSTANT)
Q_PROPERTY(QStringList cylinders READ cylinders CONSTANT)
Q_PROPERTY(QString trip READ trip CONSTANT)
Q_PROPERTY(QString maxcns READ maxcns CONSTANT)
Q_PROPERTY(QString otu READ otu CONSTANT)
public:
DiveObjectHelper(struct dive *dive = NULL);
~DiveObjectHelper();
int number() const;
int id() const;
int rating() const;
QString date() const;
timestamp_t timestamp() const;
QString time() const;
QString location() const;
QString gps() const;
QString duration() const;
QString depth() const;
QString divemaster() const;
QString buddy() const;
QString airTemp() const;
QString waterTemp() const;
QString notes() const;
QString tags() const;
QString gas() const;
QString sac() const;
QStringList weights() const;
QString weight(int idx) const;
QString suit() const;
QStringList cylinders() const;
QString cylinder(int idx) const;
QString trip() const;
QString maxcns() const;
QString otu() const;
private:
int m_number;
int m_id;
int m_rating;
QString m_date;
timestamp_t m_timestamp;
QString m_time;
QString m_location;
QString m_gps;
QString m_duration;
QString m_depth;
QString m_divemaster;
QString m_buddy;
QString m_airTemp;
QString m_waterTemp;
QString m_notes;
QString m_tags;
QString m_gas;
QString m_sac;
QStringList m_weights;
QString m_suit;
QStringList m_cylinders;
QString m_trip;
QString m_maxcns;
QString m_otu;
struct dive *m_dive;
};
Q_DECLARE_METATYPE(DiveObjectHelper*)
#endif