From a2be015a43b9d0de710539ee3838bb3aafe6bb2c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 3 Mar 2019 17:10:09 +0100 Subject: Undo: consider dive site in AddDive() If a dive site was added for a new dive, remove it on undo. Signed-off-by: Berthold Stoeger --- core/divesite.c | 18 +++++++++++++++--- core/divesite.h | 1 + desktop-widgets/command.cpp | 4 ++-- desktop-widgets/command.h | 11 +++++++---- desktop-widgets/command_divelist.cpp | 11 ++++++++++- desktop-widgets/command_divelist.h | 2 +- desktop-widgets/tab-widgets/maintab.cpp | 14 +++++++++++--- qt-models/diveplannermodel.cpp | 4 ++-- 8 files changed, 49 insertions(+), 16 deletions(-) diff --git a/core/divesite.c b/core/divesite.c index d77e6ef72..f944d760a 100644 --- a/core/divesite.c +++ b/core/divesite.c @@ -140,6 +140,19 @@ void add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_tab ds_table->nr = nr + 1; } +struct dive_site *alloc_dive_site() +{ + struct dive_site *ds; + ds = calloc(1, sizeof(*ds)); + if (!ds) + exit(1); + ds->uuid = rand() & 0xff; + ds->uuid |= (rand() & 0xff) << 8; + ds->uuid |= (rand() & 0xff) << 16; + ds->uuid |= (rand() & 0xff) << 24; + return ds; +} + /* we never allow a second dive site with the same uuid */ struct dive_site *alloc_or_get_dive_site(uint32_t uuid, struct dive_site_table *ds_table) { @@ -148,9 +161,8 @@ struct dive_site *alloc_or_get_dive_site(uint32_t uuid, struct dive_site_table * if (uuid && (ds = get_dive_site_by_uuid(uuid, ds_table)) != NULL) return ds; - ds = calloc(1, sizeof(*ds)); - if (!ds) - exit(1); + ds = alloc_dive_site(); + add_dive_site_to_table(ds, ds_table); // we should always be called with a valid uuid except in the special diff --git a/core/divesite.h b/core/divesite.h index 1f841a7d2..b31bf3940 100644 --- a/core/divesite.h +++ b/core/divesite.h @@ -50,6 +50,7 @@ void remove_dive_site_from_table(struct dive_site *ds, struct dive_site_table *d void register_dive_site(struct dive_site *ds); void unregister_dive_site(struct dive_site *ds); struct dive_site *alloc_or_get_dive_site(uint32_t uuid, struct dive_site_table *ds_table); +struct dive_site *alloc_dive_site(); int nr_of_dives_at_dive_site(struct dive_site *ds, bool select_only); bool is_dive_site_used(struct dive_site *ds, bool select_only); void free_dive_site(struct dive_site *ds); diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index d8ec0812f..2e2982860 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -6,9 +6,9 @@ namespace Command { // Dive-list related commands -void addDive(dive *d, bool autogroup, bool newNumber) +void addDive(dive *d, const QString &newDS, bool autogroup, bool newNumber) { - execute(new AddDive(d, autogroup, newNumber)); + execute(new AddDive(d, newDS, autogroup, newNumber)); } void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source) diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index f2594787d..dfc79a9e6 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -17,10 +17,13 @@ QAction *redoAction(QObject *parent); // Create an redo action. // 2) Dive-list related commands -void addDive(dive *d, bool autogroup, bool newNumber); // If d->dive_trip is null and autogroup is true, dives within the auto-group - // distance are added to a trip. dive d is consumed (the structure is reset)! - // If newNumber is true, the dive is assigned a new number, depending on the - // insertion position. +// If d->dive_trip is null and autogroup is true, dives within the auto-group +// distance are added to a trip. dive d is consumed (the structure is reset)! +// If newNumber is true, the dive is assigned a new number, depending on the +// insertion position. +// Id newDS is not empty, a dive site with that name will be created. d->dive_site +// should be null in this case. +void addDive(dive *d, const QString &newDS, bool autogroup, bool newNumber); void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source); void deleteDive(const QVector &divesToDelete); void shiftTime(const QVector &changedDives, int amount); diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp index 9a5a6d398..a6f67b090 100644 --- a/desktop-widgets/command_divelist.cpp +++ b/desktop-widgets/command_divelist.cpp @@ -5,6 +5,7 @@ #include "desktop-widgets/divelistview.h" #include "core/divelist.h" #include "core/display.h" // for amount_selected +#include "core/qthelper.h" #include "core/subsurface-qt/DiveListNotifier.h" #include "qt-models/filtermodels.h" @@ -489,7 +490,7 @@ void DiveListBase::redo() finishWork(); } -AddDive::AddDive(dive *d, bool autogroup, bool newNumber) +AddDive::AddDive(dive *d, const QString &newDS, bool autogroup, bool newNumber) { setText(tr("add dive")); // By convention, d is "displayed dive" and can be overwritten. @@ -497,6 +498,14 @@ AddDive::AddDive(dive *d, bool autogroup, bool newNumber) d->dc.maxdepth.mm = 0; fixup_dive(d); + // Create new dive site if requested. + if (!newDS.isEmpty()) { + struct dive_site *ds = alloc_dive_site(); + ds->name = copy_qstring(newDS); + d->dive_site = ds; + divesToAdd.sites.emplace_back(ds); + } + // Get an owning pointer to a copied or moved dive // Note: if move is true, this destroys the old dive! OwningDivePtr divePtr(clone_dive(d)); diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h index 8a70c69d3..1672c92f9 100644 --- a/desktop-widgets/command_divelist.h +++ b/desktop-widgets/command_divelist.h @@ -85,7 +85,7 @@ private: class AddDive : public DiveListBase { public: - AddDive(dive *dive, bool autogroup, bool newNumber); + AddDive(dive *dive, const QString &newDS, bool autogroup, bool newNumber); private: void undoit() override; void redoit() override; diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index edabd41fe..18d424437 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -712,11 +712,19 @@ void MainTab::acceptChanges() hideMessage(); ui.equipmentTab->setEnabled(true); if (editMode == ADD) { - // make sure that the dive site is handled as well - updateDiveSite(ui.location->currDiveSite(), &displayed_dive); + // Handle dive site + struct dive_site *pickedDs = ui.location->currDiveSite(); + QString newDiveSiteName; + if (pickedDs == RECENTLY_ADDED_DIVESITE) { + newDiveSiteName = ui.location->text(); + displayed_dive.dive_site = nullptr; + } else { + displayed_dive.dive_site = pickedDs; + } + copyTagsToDisplayedDive(); - Command::addDive(&displayed_dive, autogroup, true); + Command::addDive(&displayed_dive, newDiveSiteName, autogroup, true); editMode = NONE; MainWindow::instance()->exitEditState(); diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp index 2dfeff7eb..35146f9ef 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -1145,11 +1145,11 @@ void DivePlannerPointsModel::createPlan(bool replanCopy) if (!current_dive || displayed_dive.id != current_dive->id) { // we were planning a new dive, not re-planning an existing one displayed_dive.divetrip = nullptr; // Should not be necessary, just in case! - Command::addDive(&displayed_dive, autogroup, true); + Command::addDive(&displayed_dive, QString(), autogroup, true); } else if (replanCopy) { // we were planning an old dive and save as a new dive displayed_dive.id = dive_getUniqID(); // Things will break horribly if we create dives with the same id. - Command::addDive(&displayed_dive, false, false); + Command::addDive(&displayed_dive, QString(), false, false); } else { // we were planning an old dive and rewrite the plan mark_divelist_changed(true); -- cgit v1.2.3-70-g09d2