Skip to content

Commit

Permalink
core: move total_weight() into struct dive
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bstoeger committed Aug 13, 2024
1 parent df1affc commit d36fd79
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 34 deletions.
8 changes: 8 additions & 0 deletions core/dive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 }; });
}
5 changes: 1 addition & 4 deletions core/dive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions core/divelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion core/filterconstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 5 additions & 10 deletions core/qthelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion core/qthelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion core/string-format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions qt-models/divetripmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions stats/statsvariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,8 @@ struct WeightBinner : public IntRangeBinner<WeightBinner, IntBin> {
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;
}
};

Expand Down Expand Up @@ -1328,8 +1328,8 @@ struct WeightVariable : public StatsVariableTemplate<StatsVariable::Type::Numeri
return { &weight_binner_2lbs, &weight_binner_5lbs, &weight_binner_10lbs, &weight_binner_20lbs };
}
double toFloat(const dive *d) const override {
return prefs.units.weight == units::KG ? total_weight(d) / 1000.0
: grams_to_lbs(total_weight(d));
return prefs.units.weight == units::KG ? d->total_weight().grams / 1000.0
: grams_to_lbs(d->total_weight().grams);
}
std::vector<StatsOperation> supportedOperations() const override {
return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum, StatsOperation::Min, StatsOperation::Max };
Expand Down

0 comments on commit d36fd79

Please sign in to comment.