summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/command_divesite.cpp
diff options
context:
space:
mode:
authorGravatar Doug Junkins <junkins@foghead.com>2019-05-04 20:40:27 -0700
committerGravatar bstoeger <32835590+bstoeger@users.noreply.github.com>2019-05-06 10:48:44 +0200
commit704ff9f82e35699dcd5ba260cda798195b0860a5 (patch)
tree590a3456015417a582f1de06051b864e0a109e3e /desktop-widgets/command_divesite.cpp
parentc38a86bebf5d031d8c830813d0e4107bcd9e098c (diff)
downloadsubsurface-704ff9f82e35699dcd5ba260cda798195b0860a5.tar.gz
Add undo/redo commands for importing dive sites
ImportDiveSites adds the provided dive sites to the core dive site table and stores the source data so it can be undone. Signed-off-by: Doug Junkins <junkins@foghead.com>
Diffstat (limited to 'desktop-widgets/command_divesite.cpp')
-rw-r--r--desktop-widgets/command_divesite.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp
index e18a7f3c1..386159a7a 100644
--- a/desktop-widgets/command_divesite.cpp
+++ b/desktop-widgets/command_divesite.cpp
@@ -100,6 +100,42 @@ void AddDiveSite::undo()
sitesToAdd = std::move(removeDiveSites(sitesToRemove));
}
+ImportDiveSites::ImportDiveSites(struct dive_site_table *sites, const QString &source)
+{
+ setText(tr("import dive sites from %1").arg(source));
+
+ for (int i = 0; i < sites->nr; ++i) {
+ struct dive_site *new_ds = sites->dive_sites[i];
+
+ // Don't import dive sites that already exist. Currently we only check for
+ // the same name. We might want to be smarter here and merge dive site data, etc.
+ struct dive_site *old_ds = get_same_dive_site(new_ds);
+ if (old_ds) {
+ free_dive_site(new_ds);
+ continue;
+ }
+ sitesToAdd.emplace_back(new_ds);
+ }
+
+ // All site have been consumed
+ sites->nr = 0;
+}
+
+bool ImportDiveSites::workToBeDone()
+{
+ return !sitesToAdd.empty();
+}
+
+void ImportDiveSites::redo()
+{
+ sitesToRemove = std::move(addDiveSites(sitesToAdd));
+}
+
+void ImportDiveSites::undo()
+{
+ sitesToAdd = std::move(removeDiveSites(sitesToRemove));
+}
+
DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(sites.toStdVector())
{
setText(tr("delete %n dive site(s)", "", sites.size()));