From 0aa4efb3d9dbe82512a5c7940da982fe982b2a7c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 30 Jun 2024 17:38:36 +0200 Subject: [PATCH] core: move divesite_has_gps_information() to struct dive_site Seems logical in a C++ code base. Signed-off-by: Berthold Stoeger --- commands/command_pictures.cpp | 2 +- core/dive.cpp | 7 +------ core/dive.h | 2 -- core/divelist.cpp | 2 +- core/divesite.cpp | 5 +++++ core/divesite.h | 1 + core/load-git.cpp | 2 +- core/worldmap-save.cpp | 2 +- desktop-widgets/locationinformation.cpp | 2 +- desktop-widgets/mapwidget.cpp | 2 +- desktop-widgets/modeldelegates.cpp | 2 +- map-widget/qmlmapwidgethelper.cpp | 8 ++++---- qt-models/divelocationmodel.cpp | 2 +- qt-models/maplocationmodel.cpp | 2 +- 14 files changed, 20 insertions(+), 21 deletions(-) diff --git a/commands/command_pictures.cpp b/commands/command_pictures.cpp index 6044d94f6..9432b6d27 100644 --- a/commands/command_pictures.cpp +++ b/commands/command_pictures.cpp @@ -173,7 +173,7 @@ AddPictures::AddPictures(const std::vector &pictures) : QString name = Command::Base::tr("unnamed dive site"); sitesToAdd.push_back(std::make_unique(qPrintable(name), it->location)); sitesToSet.push_back({ p.d, sitesToAdd.back().get() }); - } else if (!dive_site_has_gps_location(ds)) { + } else if (!ds->has_gps_location()) { // This dive has a dive site, but without coordinates. Let's add them. sitesToEdit.push_back({ ds, it->location }); } diff --git a/core/dive.cpp b/core/dive.cpp index 28ecf1dfd..0bf2209ae 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -2531,14 +2531,9 @@ const struct divecomputer *get_dive_dc(const struct dive *dive, int nr) return get_dive_dc((struct dive *)dive, nr); } -bool dive_site_has_gps_location(const struct dive_site *ds) -{ - return ds && has_location(&ds->location); -} - bool dive::dive_has_gps_location() const { - return dive_site && dive_site_has_gps_location(dive_site); + return dive_site && dive_site->has_gps_location(); } /* Extract GPS location of a dive computer stored in the GPS1 diff --git a/core/dive.h b/core/dive.h index ac9f560a6..4ecc21dde 100644 --- a/core/dive.h +++ b/core/dive.h @@ -167,8 +167,6 @@ extern const struct divecomputer *get_dive_dc(const struct dive *dive, int nr); extern std::unique_ptr clone_make_first_dc(const struct dive &d, int dc_number); -extern bool dive_site_has_gps_location(const struct dive_site *ds); - extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset); extern int save_dives(const char *filename); diff --git a/core/divelist.cpp b/core/divelist.cpp index 601ffa9a0..5caa81bc0 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -1175,7 +1175,7 @@ merge_result dive_table::merge_dives(const struct dive &a_in, const struct dive /* we take the first dive site, unless it's empty */ res.site = a->dive_site && !a->dive_site->is_empty() ? a->dive_site : b->dive_site; - if (!dive_site_has_gps_location(res.site) && dive_site_has_gps_location(b->dive_site)) { + if (res.site && !res.site->has_gps_location() && b->dive_site && b->dive_site->has_gps_location()) { /* we picked the first dive site and that didn't have GPS data, but the new dive has * GPS data (that could be a download from a GPS enabled dive computer). * Keep the dive site, but add the GPS data */ diff --git a/core/divesite.cpp b/core/divesite.cpp index 2133d9497..296d93972 100644 --- a/core/divesite.cpp +++ b/core/divesite.cpp @@ -134,6 +134,11 @@ bool dive_site::is_selected() const [](dive *dive) { return dive->selected; }); } +bool dive_site::has_gps_location() const +{ + return has_location(&location); +} + /* allocate a new site and add it to the table */ dive_site *dive_site_table::create(const std::string &name) { diff --git a/core/divesite.h b/core/divesite.h index 4b542518c..6bbda62f0 100644 --- a/core/divesite.h +++ b/core/divesite.h @@ -26,6 +26,7 @@ struct dive_site size_t nr_of_dives() const; bool is_selected() const; bool is_empty() const; + bool has_gps_location() const; void merge(struct dive_site &b); // Note: b is consumed void add_dive(struct dive *d); }; diff --git a/core/load-git.cpp b/core/load-git.cpp index 8ba091855..b1c2e804d 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -181,7 +181,7 @@ static void parse_dive_gps(char *line, struct git_parser_state *state) ds = state->log->sites.create(std::string(), location); ds->add_dive(state->active_dive.get()); } else { - if (dive_site_has_gps_location(ds) && ds->location != location) { + if (ds->has_gps_location() && ds->location != location) { std::string coords = printGPSCoordsC(&location); // we have a dive site that already has GPS coordinates // note 1: there will be much less copying once the core diff --git a/core/worldmap-save.cpp b/core/worldmap-save.cpp index e6c134c26..a3792e8c5 100644 --- a/core/worldmap-save.cpp +++ b/core/worldmap-save.cpp @@ -36,7 +36,7 @@ static void writeMarkers(struct membuffer *b, bool selected_only) if (selected_only && !dive->selected) continue; struct dive_site *ds = get_dive_site_for_dive(dive.get()); - if (!dive_site_has_gps_location(ds)) + if (!ds || !ds->has_gps_location()) continue; put_degrees(b, ds->location.lat, "temp = new google.maps.Marker({position: new google.maps.LatLng(", ""); put_degrees(b, ds->location.lon, ",", ")});\n"); diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index d61a03741..670cd205f 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -259,7 +259,7 @@ void LocationInformationWidget::initFields(dive_site *ds) if (ds) { filter_model.set(ds, ds->location); updateLabels(); - enableLocationButtons(dive_site_has_gps_location(ds)); + enableLocationButtons(ds->has_gps_location()); DiveFilter::instance()->startFilterDiveSites(std::vector{ ds }); filter_model.invalidate(); } else { diff --git a/desktop-widgets/mapwidget.cpp b/desktop-widgets/mapwidget.cpp index a3bf08670..ec1ba782e 100644 --- a/desktop-widgets/mapwidget.cpp +++ b/desktop-widgets/mapwidget.cpp @@ -61,7 +61,7 @@ void MapWidget::centerOnIndex(const QModelIndex& idx) { CHECK_IS_READY_RETURN_VOID(); dive_site *ds = idx.model()->index(idx.row(), LocationInformationModel::DIVESITE).data().value(); - if (!ds || ds == RECENTLY_ADDED_DIVESITE || !dive_site_has_gps_location(ds)) + if (!ds || ds == RECENTLY_ADDED_DIVESITE || !ds->has_gps_location()) m_mapHelper->centerOnSelectedDiveSite(); else centerOnDiveSite(ds); diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp index aa687b360..8c5870c15 100644 --- a/desktop-widgets/modeldelegates.cpp +++ b/desktop-widgets/modeldelegates.cpp @@ -471,7 +471,7 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem if (bottomText.isEmpty()) bottomText = printGPSCoords(&ds->location); - if (dive_site_has_gps_location(ds) && currentDiveHasGPS) { + if (ds->has_gps_location() && currentDiveHasGPS) { // so we are showing a completion and both the current dive site and the completion // have a GPS fix... so let's show the distance if (ds->location == currentLocation) { diff --git a/map-widget/qmlmapwidgethelper.cpp b/map-widget/qmlmapwidgethelper.cpp index da41426bd..42dfe3174 100644 --- a/map-widget/qmlmapwidgethelper.cpp +++ b/map-widget/qmlmapwidgethelper.cpp @@ -30,14 +30,14 @@ MapWidgetHelper::MapWidgetHelper(QObject *parent) : QObject(parent) QGeoCoordinate MapWidgetHelper::getCoordinates(struct dive_site *ds) { - if (!dive_site_has_gps_location(ds)) + if (!ds || !ds->has_gps_location()) return QGeoCoordinate(0.0, 0.0); return QGeoCoordinate(ds->location.lat.udeg * 0.000001, ds->location.lon.udeg * 0.000001); } void MapWidgetHelper::centerOnDiveSite(struct dive_site *ds) { - if (!dive_site_has_gps_location(ds)) { + if (!ds || !ds->has_gps_location()) { // dive site with no GPS m_mapLocationModel->setSelected(ds); QMetaObject::invokeMethod(m_map, "deselectMapLocation"); @@ -136,7 +136,7 @@ void MapWidgetHelper::selectedLocationChanged(struct dive_site *ds_in) for (auto [idx, dive]: enumerated_range(divelog.dives)) { struct dive_site *ds = get_dive_site_for_dive(dive.get()); - if (!dive_site_has_gps_location(ds)) + if (!ds || !ds->has_gps_location()) continue; #ifndef SUBSURFACE_MOBILE const qreal latitude = ds->location.lat.udeg * 0.000001; @@ -164,7 +164,7 @@ void MapWidgetHelper::selectVisibleLocations() QList selectedDiveIds; for (auto [idx, dive]: enumerated_range(divelog.dives)) { struct dive_site *ds = get_dive_site_for_dive(dive.get()); - if (!dive_site_has_gps_location(ds)) + if (!ds || ds->has_gps_location()) continue; const qreal latitude = ds->location.lat.udeg * 0.000001; const qreal longitude = ds->location.lon.udeg * 0.000001; diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp index ae539c01a..5831426d6 100644 --- a/qt-models/divelocationmodel.cpp +++ b/qt-models/divelocationmodel.cpp @@ -108,7 +108,7 @@ QVariant LocationInformationModel::getDiveSiteData(const struct dive_site &ds, i case EDIT: return editIcon(); case REMOVE: return trashIcon(); #endif - case NAME: return dive_site_has_gps_location(&ds) ? QIcon(":geotag-icon") : QVariant(); + case NAME: return ds.has_gps_location() ? QIcon(":geotag-icon") : QVariant(); } break; case DIVESITE_ROLE: diff --git a/qt-models/maplocationmodel.cpp b/qt-models/maplocationmodel.cpp index c1f5a2e1f..5de18fd39 100644 --- a/qt-models/maplocationmodel.cpp +++ b/qt-models/maplocationmodel.cpp @@ -168,7 +168,7 @@ void MapLocationModel::reload(QObject *map) // Don't show dive sites of hidden dives, unless we're in dive site edit mode. if (!diveSiteMode && !hasVisibleDive(*ds)) continue; - if (!dive_site_has_gps_location(ds.get())) { + if (!ds->has_gps_location()) { // Dive sites that do not have a gps location are not shown in normal mode. // In dive-edit mode, selected sites are placed at the center of the map, // so that the user can drag them somewhere without having to enter coordinates.