summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/command_divesite.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-03-15 17:41:31 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 18:19:07 +0300
commit59239cec02aedda8c2651a04c65c580a9ff190a9 (patch)
tree3aa2ce298c132eb6ccacb77d5b2a508e46aa724c /desktop-widgets/command_divesite.cpp
parent7c63956ee4798a835794eee8189b73e3df07594d (diff)
downloadsubsurface-59239cec02aedda8c2651a04c65c580a9ff190a9.tar.gz
Undo: make dive site merging undoable
This one was rather trivial, as there is no actual merging done. Quite simply, a number of dive sites are removed and their dive added to a different dive site. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/command_divesite.cpp')
-rw-r--r--desktop-widgets/command_divesite.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp
index 04ee3b212..a5dce00d1 100644
--- a/desktop-widgets/command_divesite.cpp
+++ b/desktop-widgets/command_divesite.cpp
@@ -266,4 +266,45 @@ void EditDiveSiteTaxonomy::undo()
redo();
}
+MergeDiveSites::MergeDiveSites(dive_site *dsIn, const QVector<dive_site *> &sites) : ds(dsIn)
+{
+ setText(tr("merge dive sites"));
+ sitesToRemove.reserve(sites.size());
+ for (dive_site *site: sites) {
+ if (site != ds)
+ sitesToRemove.push_back(site);
+ }
+}
+
+bool MergeDiveSites::workToBeDone()
+{
+ return !sitesToRemove.empty();
+}
+
+void MergeDiveSites::redo()
+{
+ // First, remove all dive sites
+ sitesToAdd = std::move(removeDiveSites(sitesToRemove));
+
+ // The dives of the above dive sites were reset to no dive sites.
+ // Add them to the merged-into dive site. Thankfully, we remember
+ // the dives in the sitesToAdd vector.
+ for (const OwningDiveSitePtr &site: sitesToAdd) {
+ for (int i = 0; i < site->dives.nr; ++i)
+ add_dive_to_dive_site(site->dives.dives[i], ds); // TODO: send dive changed signal
+ }
+}
+
+void MergeDiveSites::undo()
+{
+ // Before readding the dive sites, unregister the corresponding dives so that they can be
+ // readded to their old dive sites.
+ for (const OwningDiveSitePtr &site: sitesToAdd) {
+ for (int i = 0; i < site->dives.nr; ++i)
+ unregister_dive_from_dive_site(site->dives.dives[i]);
+ }
+
+ sitesToRemove = std::move(addDiveSites(sitesToAdd));
+}
+
} // namespace Command