summaryrefslogtreecommitdiffstats
path: root/commands/command_divesite.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-11-16 21:35:26 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-03-09 12:41:57 -0700
commit93bdaa9bb5be80115dc80af569a2c64e0136c5f5 (patch)
tree816d4a03387d211d65b5003d022b8298e87ec0d2 /commands/command_divesite.cpp
parent89047b3541618383ab5b39eeadff7dfcb60e1295 (diff)
downloadsubsurface-93bdaa9bb5be80115dc80af569a2c64e0136c5f5.tar.gz
undo: implement ApplyGPSFixes undo command
This gets a list of dives with GPS fixes and 1) Adds new dive sites if the dive hasn't a dive site set 2) Edits the location of the dive site Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands/command_divesite.cpp')
-rw-r--r--commands/command_divesite.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/commands/command_divesite.cpp b/commands/command_divesite.cpp
index 56372ff41..8c61f224b 100644
--- a/commands/command_divesite.cpp
+++ b/commands/command_divesite.cpp
@@ -391,4 +391,51 @@ void MergeDiveSites::undo()
emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE);
}
+ApplyGPSFixes::ApplyGPSFixes(const std::vector<DiveAndLocation> &fixes)
+{
+ setText(tr("apply GPS fixes"));
+
+ for (const DiveAndLocation &dl: fixes) {
+ struct dive_site *ds = dl.d->dive_site;
+ if (ds) {
+ // Arbitrary choice: if we find multiple fixes for the same dive, we use the first one.
+ if (std::find_if(siteLocations.begin(), siteLocations.end(),
+ [ds] (const SiteAndLocation &sl) { return sl.ds == ds; }) == siteLocations.end()) {
+ siteLocations.push_back({ ds, dl.location });
+ }
+ } else {
+ ds = create_dive_site(qPrintable(dl.name), &dive_site_table);
+ ds->location = dl.location;
+ add_dive_to_dive_site(dl.d, ds);
+ dl.d->dive_site = nullptr; // This will be set on redo()
+ sitesToAdd.emplace_back(ds);
+ }
+ }
+}
+
+bool ApplyGPSFixes::workToBeDone()
+{
+ return !sitesToAdd.empty() || !siteLocations.empty();
+}
+
+void ApplyGPSFixes::editDiveSites()
+{
+ for (SiteAndLocation &sl: siteLocations) {
+ std::swap(sl.location, sl.ds->location);
+ emit diveListNotifier.diveSiteChanged(sl.ds, LocationInformationModel::LOCATION); // Inform frontend of changed dive site.
+ }
+}
+
+void ApplyGPSFixes::redo()
+{
+ sitesToRemove = addDiveSites(sitesToAdd);
+ editDiveSites();
+}
+
+void ApplyGPSFixes::undo()
+{
+ sitesToAdd = removeDiveSites(sitesToRemove);
+ editDiveSites();
+}
+
} // namespace Command