aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--desktop-widgets/command.cpp5
-rw-r--r--desktop-widgets/command.h1
-rw-r--r--desktop-widgets/command_divesite.cpp36
-rw-r--r--desktop-widgets/command_divesite.h18
4 files changed, 59 insertions, 1 deletions
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp
index ccfcbc524..00bb49872 100644
--- a/desktop-widgets/command.cpp
+++ b/desktop-widgets/command.cpp
@@ -120,6 +120,11 @@ void addDiveSite(const QString &name)
execute(new AddDiveSite(name));
}
+void importDiveSites(struct dive_site_table *sites, const QString &source)
+{
+ execute(new ImportDiveSites(sites, source));
+}
+
void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites)
{
execute(new MergeDiveSites(ds, sites));
diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h
index 9e08b9a5e..8e8157acf 100644
--- a/desktop-widgets/command.h
+++ b/desktop-widgets/command.h
@@ -48,6 +48,7 @@ void editDiveSiteCountry(dive_site *ds, const QString &value);
void editDiveSiteLocation(dive_site *ds, location_t value);
void editDiveSiteTaxonomy(dive_site *ds, taxonomy_data &value); // value is consumed (i.e. will be erased after call)!
void addDiveSite(const QString &name);
+void importDiveSites(struct dive_site_table *sites, const QString &source);
void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites);
void purgeUnusedDiveSites();
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()));
diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h
index 9661bbffd..3fcf085ae 100644
--- a/desktop-widgets/command_divesite.h
+++ b/desktop-widgets/command_divesite.h
@@ -21,10 +21,26 @@ private:
// Note: we only add one dive site. Nevertheless, we use vectors so that we
// can reuse the dive site deletion code.
- // For redo
+ // For undo
std::vector<dive_site *> sitesToRemove;
+ // For redo
+ std::vector<OwningDiveSitePtr> sitesToAdd;
+};
+
+class ImportDiveSites : public Base {
+public:
+ // Note: the dive site table is consumed after the call it will be empty.
+ ImportDiveSites(struct dive_site_table *sites, const QString &source);
+private:
+ bool workToBeDone() override;
+ void undo() override;
+ void redo() override;
+
// For undo
+ std::vector<dive_site *> sitesToRemove;
+
+ // For redo
std::vector<OwningDiveSitePtr> sitesToAdd;
};