From 1aa5438b2de5de897556830eb7d005ed2fa17a69 Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Thu, 23 May 2024 16:06:23 +1200 Subject: [PATCH] Cleanup: Improve the Use of 'Planned dive' and 'Manually added dive'. - standardise the naming; - use it consistently; - apply the 'samples < 50' only when putting manually added dives into edit mode - everywhere else manually added dives should be treated as such; - do not show a warning before editing a manually added dive in planner. Signed-off-by: Michael Keller --- core/dive.cpp | 16 +++++------ core/divecomputer.c | 28 +++++++++++++------- core/divecomputer.h | 8 +++--- desktop-widgets/mainwindow.cpp | 8 +++--- desktop-widgets/profilewidget.cpp | 2 +- desktop-widgets/tab-widgets/TabDiveNotes.cpp | 2 +- mobile-widgets/qmlmanager.cpp | 8 +++--- profile-widget/profilescene.cpp | 5 ++-- qt-models/diveplannermodel.cpp | 2 +- smtk-import/smartrak.cpp | 2 +- 10 files changed, 47 insertions(+), 34 deletions(-) diff --git a/core/dive.cpp b/core/dive.cpp index 63be1dea4..e5fd35ac4 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -2309,8 +2309,8 @@ static int likely_same_dive(const struct dive *a, const struct dive *b) int match, fuzz = 20 * 60; /* don't merge manually added dives with anything */ - if (is_manually_added_dc(&a->dc) || - is_manually_added_dc(&b->dc)) + if (is_dc_manually_added_dive(&a->dc) || + is_dc_manually_added_dive(&b->dc)) return 0; /* @@ -3244,11 +3244,11 @@ extern "C" int depth_to_mbar(int depth, const struct dive *dive) extern "C" double depth_to_mbarf(int depth, const struct dive *dive) { - // To downloaded and planned dives, use DC's values + // For downloaded and planned dives, use DC's values int salinity = dive->dc.salinity; pressure_t surface_pressure = dive->dc.surface_pressure; - if (is_manually_added_dc(&dive->dc)) { // To manual dives, salinity and pressure in another place... + if (is_dc_manually_added_dive(&dive->dc)) { // For manual dives, salinity and pressure in another place... surface_pressure = dive->surface_pressure; salinity = dive->user_salinity; } @@ -3271,8 +3271,8 @@ extern "C" double depth_to_atm(int depth, const struct dive *dive) * take care of this, but the Uemis we support natively */ extern "C" int rel_mbar_to_depth(int mbar, const struct dive *dive) { - // To downloaded and planned dives, use DC's salinity. Manual dives, use user's salinity - int salinity = is_manually_added_dc(&dive->dc) ? dive->user_salinity : dive->dc.salinity; + // For downloaded and planned dives, use DC's salinity. Manual dives, use user's salinity + int salinity = is_dc_manually_added_dive(&dive->dc) ? dive->user_salinity : dive->dc.salinity; if (!salinity) salinity = SEAWATER_SALINITY; @@ -3283,8 +3283,8 @@ extern "C" int rel_mbar_to_depth(int mbar, const struct dive *dive) extern "C" int mbar_to_depth(int mbar, const struct dive *dive) { - // To downloaded and planned dives, use DC's pressure. Manual dives, use user's pressure - pressure_t surface_pressure = is_manually_added_dc(&dive->dc) + // For downloaded and planned dives, use DC's pressure. Manual dives, use user's pressure + pressure_t surface_pressure = is_dc_manually_added_dive(&dive->dc) ? dive->surface_pressure : dive->dc.surface_pressure; diff --git a/core/divecomputer.c b/core/divecomputer.c index cb3f43937..4cac867ff 100644 --- a/core/divecomputer.c +++ b/core/divecomputer.c @@ -492,11 +492,6 @@ void add_extra_data(struct divecomputer *dc, const char *key, const char *value) } } -bool is_dc_planner(const struct divecomputer *dc) -{ - return same_string(dc->model, "planned dive"); -} - /* * Match two dive computer entries against each other, and * tell if it's the same dive. Return 0 if "don't know", @@ -548,14 +543,27 @@ void free_dc(struct divecomputer *dc) free(dc); } -static const char *manual_dc_name = "manually added dive"; -bool is_manually_added_dc(const struct divecomputer *dc) +static const char *planner_dc_name = "planned dive"; + +bool is_dc_planner(const struct divecomputer *dc) { - return dc && dc->samples <= 50 && - same_string(dc->model, manual_dc_name); + return dc && same_string(dc->model, planner_dc_name); } -void make_manually_added_dc(struct divecomputer *dc) +void make_planner_dc(struct divecomputer *dc) +{ + free((void *)dc->model); + dc->model = strdup(planner_dc_name); +} + +const char *manual_dc_name = "manually added dive"; + +bool is_dc_manually_added_dive(const struct divecomputer *dc) +{ + return dc && same_string(dc->model, manual_dc_name); +} + +void make_manually_added_dive_dc(struct divecomputer *dc) { free((void *)dc->model); dc->model = strdup(manual_dc_name); diff --git a/core/divecomputer.h b/core/divecomputer.h index 3f3d1bf64..a20cd967c 100644 --- a/core/divecomputer.h +++ b/core/divecomputer.h @@ -67,10 +67,12 @@ extern void add_event_to_dc(struct divecomputer *dc, struct event *ev); extern struct event *add_event(struct divecomputer *dc, unsigned int time, int type, int flags, int value, const char *name); extern void remove_event_from_dc(struct divecomputer *dc, struct event *event); extern void add_extra_data(struct divecomputer *dc, const char *key, const char *value); -extern bool is_dc_planner(const struct divecomputer *dc); extern uint32_t calculate_string_hash(const char *str); -extern bool is_manually_added_dc(const struct divecomputer *dc); -extern void make_manually_added_dc(struct divecomputer *dc); +extern bool is_dc_planner(const struct divecomputer *dc); +extern void make_planner_dc(struct divecomputer *dc); +extern const char *manual_dc_name; +extern bool is_dc_manually_added_dive(const struct divecomputer *dc); +extern void make_manually_added_dive_dc(struct divecomputer *dc); /* Check if two dive computer entries are the exact same dive (-1=no/0=maybe/1=yes) */ extern int match_one_dc(const struct divecomputer *a, const struct divecomputer *b); diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 0d5789063..8aea21e1d 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -665,8 +665,10 @@ void MainWindow::on_actionReplanDive_triggered() { if (!plannerStateClean() || !current_dive || !userMayChangeAppState()) return; - else if (!is_dc_planner(get_dive_dc(current_dive, profile->dc))) { - if (QMessageBox::warning(this, tr("Warning"), tr("Trying to replan a dive dive profile that is not a dive plan."), + + const struct divecomputer *dc = get_dive_dc(current_dive, profile->dc); + if (!(is_dc_planner(dc) || is_dc_manually_added_dive(dc))) { + if (QMessageBox::warning(this, tr("Warning"), tr("Trying to replan a dive profile that has not been manually added."), QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Cancel) return; } @@ -707,7 +709,7 @@ void MainWindow::on_actionAddDive_triggered() d.dc.duration.seconds = 40 * 60; d.dc.maxdepth.mm = M_OR_FT(15, 45); d.dc.meandepth.mm = M_OR_FT(13, 39); // this creates a resonable looking safety stop - make_manually_added_dc(&d.dc); + make_manually_added_dive_dc(&d.dc); fake_dc(&d.dc); fixup_dive(&d); diff --git a/desktop-widgets/profilewidget.cpp b/desktop-widgets/profilewidget.cpp index 648dda898..f58c585f7 100644 --- a/desktop-widgets/profilewidget.cpp +++ b/desktop-widgets/profilewidget.cpp @@ -210,7 +210,7 @@ void ProfileWidget::plotDive(dive *dIn, int dcIn) if (d && !editedDive && DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING) { struct divecomputer *comp = get_dive_dc(d, dc); - if (comp && is_manually_added_dc(comp) && comp->samples) + if (comp && is_dc_manually_added_dive(comp) && comp->samples && comp->samples <= 50) editDive(); } diff --git a/desktop-widgets/tab-widgets/TabDiveNotes.cpp b/desktop-widgets/tab-widgets/TabDiveNotes.cpp index 392a38a42..8135cd8af 100644 --- a/desktop-widgets/tab-widgets/TabDiveNotes.cpp +++ b/desktop-widgets/tab-widgets/TabDiveNotes.cpp @@ -254,7 +254,7 @@ void TabDiveNotes::updateData(const std::vector &, dive *currentDive, in ui.LocationLabel->setText(tr("Location")); ui.NotesLabel->setText(tr("Notes")); ui.tagWidget->setText(QString::fromStdString(taglist_get_tagstring(currentDive->tag_list))); - bool isManual = is_manually_added_dc(¤tDive->dc); + bool isManual = is_dc_manually_added_dive(¤tDive->dc); ui.depth->setVisible(isManual); ui.depthLabel->setVisible(isManual); ui.duration->setVisible(isManual); diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index f9f905815..1476b6b75 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -1135,7 +1135,7 @@ bool QMLManager::checkDuration(struct dive *d, QString duration) m = m6.captured(1).toInt(); } d->dc.duration.seconds = d->duration.seconds = h * 3600 + m * 60 + s; - if (is_manually_added_dc(&d->dc)) + if (is_dc_manually_added_dive(&d->dc)) free_samples(&d->dc); else appendTextToLog("Cannot change the duration on a dive that wasn't manually added"); @@ -1153,7 +1153,7 @@ bool QMLManager::checkDepth(dive *d, QString depth) // the depth <= 500m if (0 <= depthValue && depthValue <= 500000) { d->maxdepth.mm = depthValue; - if (is_manually_added_dc(&d->dc)) { + if (is_dc_manually_added_dive(&d->dc)) { d->dc.maxdepth.mm = d->maxdepth.mm; free_samples(&d->dc); } @@ -1359,7 +1359,7 @@ void QMLManager::commitChanges(QString diveId, QString number, QString date, QSt if (diveChanged) { if (d->maxdepth.mm == d->dc.maxdepth.mm && d->maxdepth.mm > 0 && - is_manually_added_dc(&d->dc) && + is_dc_manually_added_dive(&d->dc) && d->dc.samples == 0) { // so we have depth > 0, a manually added dive and no samples // let's create an actual profile so the desktop version can work it @@ -1736,7 +1736,7 @@ int QMLManager::addDive() d.dc.duration.seconds = 40 * 60; d.dc.maxdepth.mm = M_OR_FT(15, 45); d.dc.meandepth.mm = M_OR_FT(13, 39); // this creates a resonable looking safety stop - make_manually_added_dc(&d.dc); + make_manually_added_dive_dc(&d.dc); fake_dc(&d.dc); fixup_dive(&d); diff --git a/profile-widget/profilescene.cpp b/profile-widget/profilescene.cpp index ef083bd32..b1e19ed1c 100644 --- a/profile-widget/profilescene.cpp +++ b/profile-widget/profilescene.cpp @@ -8,6 +8,7 @@ #include "divetextitem.h" #include "tankitem.h" #include "core/device.h" +#include "core/divecomputer.h" #include "core/event.h" #include "core/pref.h" #include "core/profile.h" @@ -579,9 +580,9 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM } QString dcText = get_dc_nickname(currentdc); - if (dcText == "planned dive") + if (is_dc_planner(currentdc)) dcText = tr("Planned dive"); - else if (dcText == "manually added dive") + else if (is_dc_manually_added_dive(currentdc)) dcText = tr("Manually added dive"); else if (dcText.isEmpty()) dcText = tr("Unknown dive computer"); diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp index 36a72ecf7..80ad3d2e6 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -69,7 +69,7 @@ void DivePlannerPointsModel::createSimpleDive(struct dive *dIn) clear_dive(d); d->id = dive_getUniqID(); d->when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset() + 3600; - d->dc.model = strdup("planned dive"); // don't translate! this is stored in the XML file + make_planner_dc(&d->dc); clear(); removeDeco(); diff --git a/smtk-import/smartrak.cpp b/smtk-import/smartrak.cpp index 7afe5dc4a..6566a87a2 100644 --- a/smtk-import/smartrak.cpp +++ b/smtk-import/smartrak.cpp @@ -824,7 +824,7 @@ static dc_status_t prepare_data(int data_model, char *serial, dc_family_t dc_fam dev_data->device = NULL; dev_data->context = NULL; if (!data_model) { - dev_data->model = copy_string("manually added dive"); + dev_data->model = copy_string(manual_dc_name); dev_data->descriptor = NULL; return DC_STATUS_NODEVICE; }