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 <nystire@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Benjamin 2013-10-04 21:39:33 +03:00 committed by Dirk Hohndel
parent f63605e02e
commit ae713763c7
4 changed files with 27 additions and 17 deletions

View file

@ -216,14 +216,13 @@ QString get_depth_unit()
QString get_weight_string(weight_t weight, bool showunit) QString get_weight_string(weight_t weight, bool showunit)
{ {
if (prefs.units.weight == units::KG) { QString str = weight_string (weight.grams);
int gr = weight.grams % 1000; if (get_units()->weight == units::KG) {
int kg = weight.grams / 1000; str = QString ("%1%2").arg(str).arg(showunit ? _("kg") : "");
return QString("%1.%2%3").arg(kg).arg((unsigned)(gr) / 100).arg(showunit ? _("kg") : "");
} else { } else {
double lbs = grams_to_lbs(weight.grams); str = QString ("%1%2").arg(str).arg(showunit ? _("lbs") : "");
return QString("%1%2").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ).arg(showunit ? _("lbs") : "");
} }
return (str);
} }
QString get_weight_unit() QString get_weight_unit()

View file

@ -1107,17 +1107,7 @@ QString DiveItem::displaySac() const
QString DiveItem::displayWeight() const QString DiveItem::displayWeight() const
{ {
QString str; QString str = weight_string(weight());
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 );
}
return str; return str;
} }

View file

@ -76,3 +76,21 @@ void DiveComputerList::rmDC(QString m, uint32_t d)
const DiveComputerNode *existNode = this->getExact(m, d); const DiveComputerNode *existNode = this->getExact(m, d);
dcMap.remove(m, *existNode); 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);
}

View file

@ -4,6 +4,7 @@
#include <QMultiMap> #include <QMultiMap>
#include <QString> #include <QString>
#include <stdint.h> #include <stdint.h>
#include "dive.h"
class DiveComputerNode { class DiveComputerNode {
public: public:
@ -32,4 +33,6 @@ public:
QMultiMap<QString, struct DiveComputerNode> dcWorkingMap; QMultiMap<QString, struct DiveComputerNode> dcWorkingMap;
}; };
QString weight_string(int weight_in_grams);
#endif // QTHELPER_H #endif // QTHELPER_H