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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-30 11:13:39 +02:00 committed by bstoeger
parent df1affc25b
commit d36fd79527
9 changed files with 22 additions and 34 deletions

View file

@ -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 }; });
}

View file

@ -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

View file

@ -105,17 +105,6 @@ std::unique_ptr<dive> 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);

View file

@ -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:

View file

@ -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)

View file

@ -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);

View file

@ -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)