core: move get_dive_location()/_country() to struct dive

Feels natural in a C++ code base.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-30 20:55:34 +02:00 committed by bstoeger
parent f1f082d86a
commit 76d672210d
9 changed files with 18 additions and 21 deletions

View file

@ -2496,16 +2496,14 @@ depth_t dive::gas_mnd(struct gasmix mix, depth_t end, int roundto) const
return depth_t { (int)lrint(((double)mbar_to_depth(maxambient)) / roundto) * roundto }; return depth_t { (int)lrint(((double)mbar_to_depth(maxambient)) / roundto) * roundto };
} }
std::string get_dive_country(const struct dive *dive) std::string dive::get_country() const
{ {
struct dive_site *ds = dive->dive_site; return dive_site ? taxonomy_get_country(dive_site->taxonomy) : std::string();
return ds ? taxonomy_get_country(ds->taxonomy) : std::string();
} }
std::string get_dive_location(const struct dive *dive) std::string dive::get_location() const
{ {
const struct dive_site *ds = dive->dive_site; return dive_site ? dive_site->name : std::string();
return ds ? ds->name : std::string();
} }
int dive::number_of_computers() const int dive::number_of_computers() const

View file

@ -111,6 +111,8 @@ struct dive {
const cylinder_t *get_cylinder(int idx) const; const cylinder_t *get_cylinder(int idx) const;
weight_t total_weight() const; weight_t total_weight() const;
int get_salinity() const; int get_salinity() const;
std::string get_country() const;
std::string get_location() const;
int depth_to_mbar(int depth) const; int depth_to_mbar(int depth) const;
double depth_to_mbarf(int depth) const; double depth_to_mbarf(int depth) const;
@ -163,9 +165,6 @@ struct dive_components {
extern fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner); extern fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner);
extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2); extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2);
extern std::string get_dive_country(const struct dive *dive);
extern std::string get_dive_location(const struct dive *dive);
extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number); extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number);
extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset); extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset);

View file

@ -1099,7 +1099,7 @@ static void create_commit_message(struct membuffer *msg, bool create_empty)
} else if (!divelog.dives.empty()) { } else if (!divelog.dives.empty()) {
const struct dive &dive = *divelog.dives.back(); const struct dive &dive = *divelog.dives.back();
dive_trip *trip = dive.divetrip; dive_trip *trip = dive.divetrip;
std::string location = get_dive_location(&dive); std::string location = dive.get_location();
if (location.empty()) if (location.empty())
location = "no location"; location = "no location";
const char *sep = "\n"; const char *sep = "\n";

View file

@ -345,7 +345,7 @@ static void write_one_dive(struct membuffer *b, const struct dive &dive, const c
put_format(b, "\"subsurface_number\":%d,", dive.number); put_format(b, "\"subsurface_number\":%d,", dive.number);
put_HTML_date(b, dive, "\"date\":\"", "\","); put_HTML_date(b, dive, "\"date\":\"", "\",");
put_HTML_time(b, dive, "\"time\":\"", "\","); put_HTML_time(b, dive, "\"time\":\"", "\",");
write_attribute(b, "location", get_dive_location(&dive).c_str(), ", "); write_attribute(b, "location", dive.get_location().c_str(), ", ");
put_HTML_coordinates(b, dive); put_HTML_coordinates(b, dive);
put_format(b, "\"rating\":%d,", dive.rating); put_format(b, "\"rating\":%d,", dive.rating);
put_format(b, "\"visibility\":%d,", dive.visibility); put_format(b, "\"visibility\":%d,", dive.visibility);

View file

@ -60,7 +60,7 @@ struct dive_trip *unregister_dive_from_trip(struct dive *dive)
std::unique_ptr<dive_trip> create_trip_from_dive(const struct dive *dive) std::unique_ptr<dive_trip> create_trip_from_dive(const struct dive *dive)
{ {
auto trip = std::make_unique<dive_trip>(); auto trip = std::make_unique<dive_trip>();
trip->location = get_dive_location(dive); trip->location = dive->get_location();
return trip; return trip;
} }
@ -162,7 +162,7 @@ std::vector<dives_to_autogroup_result> get_dives_to_autogroup(const struct dive_
dive->when >= lastdive->when + TRIP_THRESHOLD) dive->when >= lastdive->when + TRIP_THRESHOLD)
break; break;
if (trip->location.empty()) if (trip->location.empty())
trip->location = get_dive_location(dive.get()); trip->location = dive->get_location();
lastdive = dive.get(); lastdive = dive.get();
} }
res.push_back({ i, to, trip, std::move(allocated) }); res.push_back({ i, to, trip, std::move(allocated) });

View file

@ -59,7 +59,7 @@ static void writeMarkers(struct membuffer *b, bool selected_only)
put_HTML_watertemp(b, *dive, " ", "</p>"); put_HTML_watertemp(b, *dive, " ", "</p>");
pre = format_string_std("<p>%s <b>", translate("gettextFromC", "Location:")); pre = format_string_std("<p>%s <b>", translate("gettextFromC", "Location:"));
put_string(b, pre.c_str()); put_string(b, pre.c_str());
put_HTML_quoted(b, get_dive_location(dive.get()).c_str()); put_HTML_quoted(b, dive->get_location().c_str());
put_string(b, "</b></p>"); put_string(b, "</b></p>");
pre = format_string_std("<p> %s ", translate("gettextFromC", "Notes:")); pre = format_string_std("<p> %s ", translate("gettextFromC", "Notes:"));
put_HTML_notes(b, *dive, pre.c_str(), " </p>"); put_HTML_notes(b, *dive, pre.c_str(), " </p>");

View file

@ -526,7 +526,7 @@ QVariant TemplateLayout::getValue(QString list, QString property, const State &s
} else if (property == "timestamp") { } else if (property == "timestamp") {
return QVariant::fromValue(d->when); return QVariant::fromValue(d->when);
} else if (property == "location") { } else if (property == "location") {
return QString::fromStdString(get_dive_location(d)); return QString::fromStdString(d->get_location());
} else if (property == "gps") { } else if (property == "gps") {
return formatDiveGPS(d); return formatDiveGPS(d);
} else if (property == "gps_decimal") { } else if (property == "gps_decimal") {

View file

@ -1066,7 +1066,7 @@ bool QMLManager::checkLocation(DiveSiteChange &res, struct dive *d, QString loca
{ {
struct dive_site *ds = d->dive_site; struct dive_site *ds = d->dive_site;
bool changed = false; bool changed = false;
QString oldLocation = QString::fromStdString(get_dive_location(d)); QString oldLocation = QString::fromStdString(d->get_location());
if (oldLocation != location) { if (oldLocation != location) {
ds = divelog.sites.get_by_name(location.toStdString()); ds = divelog.sites.get_by_name(location.toStdString());
if (!ds && !location.isEmpty()) { if (!ds && !location.isEmpty()) {

View file

@ -279,7 +279,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case MobileListModel::DateTimeRole: return formatDiveDateTime(d); case MobileListModel::DateTimeRole: return formatDiveDateTime(d);
case MobileListModel::IdRole: return d->id; case MobileListModel::IdRole: return d->id;
case MobileListModel::NumberRole: return d->number; case MobileListModel::NumberRole: return d->number;
case MobileListModel::LocationRole: return QString::fromStdString(get_dive_location(d)); case MobileListModel::LocationRole: return QString::fromStdString(d->get_location());
case MobileListModel::DepthRole: return get_depth_string(d->dcs[0].maxdepth.mm, true, true); case MobileListModel::DepthRole: return get_depth_string(d->dcs[0].maxdepth.mm, true, true);
case MobileListModel::DurationRole: return formatDiveDuration(d); case MobileListModel::DurationRole: return formatDiveDuration(d);
case MobileListModel::DepthDurationRole: return QStringLiteral("%1 / %2").arg(get_depth_string(d->dcs[0].maxdepth.mm, true, true), case MobileListModel::DepthDurationRole: return QStringLiteral("%1 / %2").arg(get_depth_string(d->dcs[0].maxdepth.mm, true, true),
@ -350,13 +350,13 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case PHOTOS: case PHOTOS:
break; break;
case COUNTRY: case COUNTRY:
return QString::fromStdString(get_dive_country(d)); return QString::fromStdString(d->get_country());
case BUDDIES: case BUDDIES:
return QString::fromStdString(d->buddy); return QString::fromStdString(d->buddy);
case DIVEGUIDE: case DIVEGUIDE:
return QString::fromStdString(d->diveguide); return QString::fromStdString(d->diveguide);
case LOCATION: case LOCATION:
return QString::fromStdString(get_dive_location(d)); return QString::fromStdString(d->get_location());
case GAS: case GAS:
return formatDiveGasString(d); return formatDiveGasString(d);
case NOTES: case NOTES:
@ -1761,13 +1761,13 @@ bool DiveTripModelList::lessThan(const QModelIndex &i1, const QModelIndex &i2) c
case PHOTOS: case PHOTOS:
return lessThanHelper(countPhotos(d1) - countPhotos(d2), row_diff); return lessThanHelper(countPhotos(d1) - countPhotos(d2), row_diff);
case COUNTRY: case COUNTRY:
return lessThanHelper(strCmp(get_dive_country(d1), get_dive_country(d2)), row_diff); return lessThanHelper(strCmp(d1->get_country(), d2->get_country()), row_diff);
case BUDDIES: case BUDDIES:
return lessThanHelper(strCmp(d1->buddy, d2->buddy), row_diff); return lessThanHelper(strCmp(d1->buddy, d2->buddy), row_diff);
case DIVEGUIDE: case DIVEGUIDE:
return lessThanHelper(strCmp(d1->diveguide, d2->diveguide), row_diff); return lessThanHelper(strCmp(d1->diveguide, d2->diveguide), row_diff);
case LOCATION: case LOCATION:
return lessThanHelper(strCmp(get_dive_location(d1), get_dive_location(d2)), row_diff); return lessThanHelper(strCmp(d1->get_location(), d2->get_location()), row_diff);
case NOTES: case NOTES:
return lessThanHelper(strCmp(d1->notes, d2->notes), row_diff); return lessThanHelper(strCmp(d1->notes, d2->notes), row_diff);
case DIVEMODE: case DIVEMODE: