From d36fd79527f4032f1ea0bbb70cc529620528ce92 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 30 Jun 2024 11:13:39 +0200 Subject: [PATCH] core: move total_weight() into struct dive Feels natural in a C++ code base. Change the function to return a weight_t. Sadly, use of the units.h types is very inconsistent and many parts of the code use int or double instead. So let's try to make this consistent. Signed-off-by: Berthold Stoeger --- core/dive.cpp | 8 ++++++++ core/dive.h | 5 +---- core/divelist.cpp | 11 ----------- core/filterconstraint.cpp | 2 +- core/qthelper.cpp | 15 +++++---------- core/qthelper.h | 1 - core/string-format.cpp | 2 +- qt-models/divetripmodel.cpp | 4 ++-- stats/statsvariables.cpp | 8 ++++---- 9 files changed, 22 insertions(+), 34 deletions(-) diff --git a/core/dive.cpp b/core/dive.cpp index 581f0d771..7d20de637 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -2763,3 +2763,11 @@ bool dive::is_cylinder_prot(int idx) const [this, idx](auto &dc) { return has_gaschange_event(&dc, idx); }); } + +weight_t dive::total_weight() const +{ + // TODO: implement addition for units.h types + return std::accumulate(weightsystems.begin(), weightsystems.end(), weight_t(), + [] (weight_t w, const weightsystem_t &ws) + { return weight_t{ w.grams + ws.weight.grams }; }); +} diff --git a/core/dive.h b/core/dive.h index 616ec679f..62599903d 100644 --- a/core/dive.h +++ b/core/dive.h @@ -104,6 +104,7 @@ struct dive { struct gasmix get_gasmix_at_time(const struct divecomputer &dc, duration_t time) const; cylinder_t *get_cylinder(int idx); const cylinder_t *get_cylinder(int idx) const; + weight_t total_weight() const; int depth_to_mbar(int depth) const; double depth_to_mbarf(int depth) const; @@ -202,10 +203,6 @@ extern struct event create_gas_switch_event(struct dive *dive, struct divecomput extern void per_cylinder_mean_depth(const struct dive *dive, struct divecomputer *dc, int *mean, int *duration); extern bool cylinder_with_sensor_sample(const struct dive *dive, int cylinder_id); -/* UI related protopypes */ - -extern int total_weight(const struct dive *); - extern void update_setpoint_events(const struct dive *dive, struct divecomputer *dc); /* Make pointers to dive and dive_trip "Qt metatypes" so that they can be passed through diff --git a/core/divelist.cpp b/core/divelist.cpp index c2972aa8b..4e1d6aa6f 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -105,17 +105,6 @@ std::unique_ptr dive_table::default_dive() return d; } -int total_weight(const struct dive *dive) -{ - int total_grams = 0; - - if (dive) { - for (auto &ws: dive->weightsystems) - total_grams += ws.weight.grams; - } - return total_grams; -} - static int active_o2(const struct dive &dive, const struct divecomputer *dc, duration_t time) { struct gasmix gas = dive.get_gasmix_at_time(*dc, time); diff --git a/core/filterconstraint.cpp b/core/filterconstraint.cpp index 0de99fe9c..36c2d3256 100644 --- a/core/filterconstraint.cpp +++ b/core/filterconstraint.cpp @@ -1056,7 +1056,7 @@ bool filter_constraint_match_dive(const filter_constraint &c, const struct dive case FILTER_CONSTRAINT_DURATION: return check_numerical_range(c, d->duration.seconds); case FILTER_CONSTRAINT_WEIGHT: - return check_numerical_range(c, total_weight(d)); + return check_numerical_range(c, d->total_weight().grams); case FILTER_CONSTRAINT_WATER_TEMP: return check_numerical_range(c, d->watertemp.mkelvin); case FILTER_CONSTRAINT_AIR_TEMP: diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 1b4337875..b3f446223 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -51,14 +51,14 @@ static inline QString degreeSigns() return QStringLiteral("dD\u00b0"); } -QString weight_string(int weight_in_grams) +static QString weight_string(weight_t weight) { QString str; if (get_units()->weight == units::KG) { - double kg = (double) weight_in_grams / 1000.0; + double kg = (double) weight.grams / 1000.0; str = QString("%L1").arg(kg, 0, 'f', kg >= 20.0 ? 0 : 1); } else { - double lbs = grams_to_lbs(weight_in_grams); + double lbs = grams_to_lbs(weight.grams); str = QString("%L1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1); } return str; @@ -521,13 +521,8 @@ QString get_depth_unit() QString get_weight_string(weight_t weight, bool showunit) { - QString str = weight_string(weight.grams); - if (get_units()->weight == units::KG) { - str = QString("%1%2").arg(str, showunit ? gettextFromC::tr("kg") : QString()); - } else { - str = QString("%1%2").arg(str, showunit ? gettextFromC::tr("lbs") : QString()); - } - return str; + QString str = weight_string(weight); + return showunit ? str + get_weight_unit() : str; } QString get_weight_unit(bool metric) diff --git a/core/qthelper.h b/core/qthelper.h index 70b5a8e57..92d9daa22 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -25,7 +25,6 @@ enum watertypes {FRESHWATER, BRACKISHWATER, EN13319WATER, SALTWATER, DC_WATERTYP #define SKIP_EMPTY QString::SkipEmptyParts #endif -QString weight_string(int weight_in_grams); QString distance_string(int distanceInMeters); bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_text, bool *parsed_out = 0); QString get_gas_string(struct gasmix gas); diff --git a/core/string-format.cpp b/core/string-format.cpp index f3d5b6ce6..b9b2bb1a3 100644 --- a/core/string-format.cpp +++ b/core/string-format.cpp @@ -192,7 +192,7 @@ QString formatGas(const dive *d) QString formatSumWeight(const dive *d) { - return get_weight_string(weight_t { total_weight(d) }, true); + return get_weight_string(d->total_weight(), true); } static QString getFormattedWeight(const weightsystem_t &weight) diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 3deb4a81c..120d49b03 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -183,7 +183,7 @@ static QString displaySac(const struct dive *d, bool units) static QString displayWeight(const struct dive *d, bool units) { - QString s = weight_string(total_weight(d)); + QString s = get_weight_string(d->total_weight(), false); if (!units) return s; else if (get_units()->weight == units::KG) @@ -1737,7 +1737,7 @@ bool DiveTripModelList::lessThan(const QModelIndex &i1, const QModelIndex &i2) c case TEMPERATURE: return lessThanHelper(d1->watertemp.mkelvin - d2->watertemp.mkelvin, row_diff); case TOTALWEIGHT: - return lessThanHelper(total_weight(d1) - total_weight(d2), row_diff); + return lessThanHelper(d1->total_weight().grams - d2->total_weight().grams, row_diff); case SUIT: return lessThanHelper(strCmp(d1->suit, d2->suit), row_diff); case CYLINDER: diff --git a/stats/statsvariables.cpp b/stats/statsvariables.cpp index 86de9244c..4f223b134 100644 --- a/stats/statsvariables.cpp +++ b/stats/statsvariables.cpp @@ -1297,8 +1297,8 @@ struct WeightBinner : public IntRangeBinner { return get_weight_unit(metric); } int to_bin_value(const dive *d) const { - return metric ? total_weight(d) / 1000 / bin_size - : lrint(grams_to_lbs(total_weight(d))) / bin_size; + return metric ? d->total_weight().grams / 1000 / bin_size + : lrint(grams_to_lbs(d->total_weight().grams)) / bin_size; } }; @@ -1328,8 +1328,8 @@ struct WeightVariable : public StatsVariableTemplatetotal_weight().grams / 1000.0 + : grams_to_lbs(d->total_weight().grams); } std::vector supportedOperations() const override { return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum, StatsOperation::Min, StatsOperation::Max };