diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-03-12 22:35:43 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-04-12 18:19:07 +0300 |
commit | 8e1f736d2b608784e1ea942ec8579d2361691c43 (patch) | |
tree | c6c9c60d112e95158a69b692a1ca32b0923a39d3 /desktop-widgets/command_divesite.cpp | |
parent | e99c4c90592c9dba17d5cbb9a99d0bf458fb53d2 (diff) | |
download | subsurface-8e1f736d2b608784e1ea942ec8579d2361691c43.tar.gz |
Undo: make dive site removal undoable
Create a new undo-command for deleting dive sites. If there are dives
associated with that site, the dives will be removed. The frontend
is not yet updated in such a case, as that infrastructure is in a
different PR.
Connect the trashcan icon of the dive site table to the undo command.
Currently, this code is in the dive site model, which makes little
sense, but is how the TableView class works. We might want to change
that when cylinder and weight editing are made undoable.
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.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp new file mode 100644 index 000000000..d4c5d87b8 --- /dev/null +++ b/desktop-widgets/command_divesite.cpp @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "command_divesite.h" +#include "core/divesite.h" +#include "core/subsurface-qt/DiveListNotifier.h" + +namespace Command { + +// Helper functions to add / remove a set of dive sites + +// Add a set of dive sites to the core. The dives that were associated with +// that dive site will be restored to that dive site. +static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sites) +{ + std::vector<dive_site *> res; + res.reserve(sites.size()); + + for (OwningDiveSitePtr &ds: sites) { + // Readd the dives that belonged to this site + for (int i = 0; i < ds->dives.nr; ++i) { + // TODO: send dive site changed signal + ds->dives.dives[i]->dive_site = ds.get(); + } + + // Add dive site to core, but remember a non-owning pointer first. + res.push_back(ds.get()); + int idx = register_dive_site(ds.release()); // Return ownership to backend. + emit diveListNotifier.diveSiteAdded(res.back(), idx); // Inform frontend of new dive site. + } + + // Clear vector of unused owning pointers + sites.clear(); + + return res; +} + +// Remove a set of dive sites. Get owning pointers to them. The dives are set to +// being at no dive site, but the dive site will retain a list of dives, so +// that the dives can be readded to the site on undo. +static std::vector<OwningDiveSitePtr> removeDiveSites(std::vector<dive_site *> &sites) +{ + std::vector<OwningDiveSitePtr> res; + res.reserve(sites.size()); + + for (dive_site *ds: sites) { + // Reset the dive_site field of the affected dives + for (int i = 0; i < ds->dives.nr; ++i) { + // TODO: send dive site changed signal + ds->dives.dives[i]->dive_site = nullptr; + } + + // Remove dive site from core and take ownership. + int idx = unregister_dive_site(ds); + res.emplace_back(ds); + emit diveListNotifier.diveSiteDeleted(ds, idx); // Inform frontend of removed dive site. + } + + sites.clear(); + + return res; +} + +DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(sites.toStdVector()) +{ + setText(tr("delete %n dive site(s)", "", sites.size())); +} + +bool DeleteDiveSites::workToBeDone() +{ + return !sitesToRemove.empty(); +} + +void DeleteDiveSites::redo() +{ + sitesToAdd = std::move(removeDiveSites(sitesToRemove)); +} + +void DeleteDiveSites::undo() +{ + sitesToRemove = std::move(addDiveSites(sitesToAdd)); +} + +} // namespace Command |