diff options
author | Tomaz Canabrava <tomaz.canabrava@intel.com> | 2016-01-07 16:01:24 -0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2016-01-08 08:04:44 -0800 |
commit | 25aa80846b84c0b3cd3e0b0256a046e1e92fb1db (patch) | |
tree | c9934f39f2e039e5877af7ee0d3b0d96c5a1debf /subsurface-core | |
parent | c022f5c4f71f3e7670ed458d714d96fd6e88c55b (diff) | |
download | subsurface-25aa80846b84c0b3cd3e0b0256a046e1e92fb1db.tar.gz |
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>
Diffstat (limited to 'subsurface-core')
-rw-r--r-- | subsurface-core/CMakeLists.txt | 3 | ||||
-rw-r--r-- | subsurface-core/qthelper.cpp | 347 | ||||
-rw-r--r-- | subsurface-core/qthelper.h | 104 | ||||
-rw-r--r-- | subsurface-core/subsurface-qt/DiveObjectHelper.cpp | 263 | ||||
-rw-r--r-- | subsurface-core/subsurface-qt/DiveObjectHelper.h | 92 |
5 files changed, 358 insertions, 451 deletions
diff --git a/subsurface-core/CMakeLists.txt b/subsurface-core/CMakeLists.txt index b8c3ed8a0..decc94d5f 100644 --- a/subsurface-core/CMakeLists.txt +++ b/subsurface-core/CMakeLists.txt @@ -81,6 +81,9 @@ set(SUBSURFACE_CORE_LIB_SRCS imagedownloader.cpp isocialnetworkintegration.cpp gpslocation.cpp + + #Subsurface Qt have the Subsurface structs QObjectified for easy access via QML. + subsurface-qt/DiveObjectHelper.cpp ${SERIAL_FTDI} ${PLATFORM_SRC} ${BT_CORE_SRC_FILES} diff --git a/subsurface-core/qthelper.cpp b/subsurface-core/qthelper.cpp index b89fac4c3..4efe57bd4 100644 --- a/subsurface-core/qthelper.cpp +++ b/subsurface-core/qthelper.cpp @@ -38,353 +38,6 @@ static QLocale loc; #define translate(_context, arg) trGettext(arg) 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 "\n" - 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 str; diff --git a/subsurface-core/qthelper.h b/subsurface-core/qthelper.h index 0049e9bbd..842e40c36 100644 --- a/subsurface-core/qthelper.h +++ b/subsurface-core/qthelper.h @@ -9,110 +9,6 @@ #include <QTranslator> #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 extern QTranslator *qtTranslator, *ssrfTranslator; diff --git a/subsurface-core/subsurface-qt/DiveObjectHelper.cpp b/subsurface-core/subsurface-qt/DiveObjectHelper.cpp new file mode 100644 index 000000000..03e63786a --- /dev/null +++ b/subsurface-core/subsurface-qt/DiveObjectHelper.cpp @@ -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 "\n" + 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; +} diff --git a/subsurface-core/subsurface-qt/DiveObjectHelper.h b/subsurface-core/subsurface-qt/DiveObjectHelper.h new file mode 100644 index 000000000..5e7858b24 --- /dev/null +++ b/subsurface-core/subsurface-qt/DiveObjectHelper.h @@ -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
\ No newline at end of file |