From ae713763c79afb483afec12d02730e0c5a8af309 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Fri, 4 Oct 2013 21:39:33 +0300 Subject: Add a helper function to calculate weight display string Add a helper function to unify the calculation of the weight display string, instead of having the same calculation in two places in the code. Signed-off-by: Benjamin Fogel Signed-off-by: Dirk Hohndel --- qt-gui.cpp | 11 +++++------ qt-ui/models.cpp | 12 +----------- qthelper.cpp | 18 ++++++++++++++++++ qthelper.h | 3 +++ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/qt-gui.cpp b/qt-gui.cpp index 78138ad3a..b302e341a 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -216,14 +216,13 @@ QString get_depth_unit() QString get_weight_string(weight_t weight, bool showunit) { - if (prefs.units.weight == units::KG) { - int gr = weight.grams % 1000; - int kg = weight.grams / 1000; - return QString("%1.%2%3").arg(kg).arg((unsigned)(gr) / 100).arg(showunit ? _("kg") : ""); + QString str = weight_string (weight.grams); + if (get_units()->weight == units::KG) { + str = QString ("%1%2").arg(str).arg(showunit ? _("kg") : ""); } else { - double lbs = grams_to_lbs(weight.grams); - return QString("%1%2").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ).arg(showunit ? _("lbs") : ""); + str = QString ("%1%2").arg(str).arg(showunit ? _("lbs") : ""); } + return (str); } QString get_weight_unit() diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 752f3ddf3..62e8d380a 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -1107,17 +1107,7 @@ QString DiveItem::displaySac() const QString DiveItem::displayWeight() const { - QString str; - - if (get_units()->weight == units::KG) { - int gr = weight() % 1000; - int kg = weight() / 1000; - str = QString("%1.%2").arg(kg).arg((unsigned)(gr) / 100); - } else { - double lbs = grams_to_lbs(weight()); - str = QString("%1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ); - } - + QString str = weight_string(weight()); return str; } diff --git a/qthelper.cpp b/qthelper.cpp index 68f8c5058..36d1d02c7 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -76,3 +76,21 @@ void DiveComputerList::rmDC(QString m, uint32_t d) const DiveComputerNode *existNode = this->getExact(m, d); dcMap.remove(m, *existNode); } + +QString weight_string(int weight_in_grams) +{ + QString str; + if (get_units()->weight == units::KG) { + int gr = weight_in_grams % 1000; + int kg = weight_in_grams / 1000; + if (kg >= 20.0) { + str = QString("0"); + } else { + str = QString("%1.%2").arg(kg).arg((unsigned)(gr) / 100); + } + } else { + double lbs = grams_to_lbs(weight_in_grams); + str = QString("%1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ); + } + return (str); +} diff --git a/qthelper.h b/qthelper.h index fcbcbff9a..3ac83b5c4 100644 --- a/qthelper.h +++ b/qthelper.h @@ -4,6 +4,7 @@ #include #include #include +#include "dive.h" class DiveComputerNode { public: @@ -32,4 +33,6 @@ public: QMultiMap dcWorkingMap; }; +QString weight_string(int weight_in_grams); + #endif // QTHELPER_H -- cgit v1.2.3-70-g09d2