diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-03-04 21:10:05 +0100 |
---|---|---|
committer | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-04-07 00:13:35 +0200 |
commit | 0bd821183d715f709d378e8ac1a75aa0c3d0f2cc (patch) | |
tree | e6d182e6922d7c06bf09b17c9c953f8b5dc78e85 /commands/command_event.cpp | |
parent | c585fd9f8ee0c1f27f997b4e19984b50d150f6cb (diff) | |
download | subsurface-0bd821183d715f709d378e8ac1a75aa0c3d0f2cc.tar.gz |
undo: implement gas switch
This is a bit hairy as - in theory - one gas switch can remove
other gas switch(es) at the same timestamp. However, I did not
find a way to test it. Moreover, it is not clear whether the
dive-tabs are properly updated on undo/redo.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands/command_event.cpp')
-rw-r--r-- | commands/command_event.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/commands/command_event.cpp b/commands/command_event.cpp index e32edfc41..6d217da65 100644 --- a/commands/command_event.cpp +++ b/commands/command_event.cpp @@ -5,6 +5,7 @@ #include "core/subsurface-qt/divelistnotifier.h" #include "core/libdivecomputer.h" #include "core/gettextfromc.h" +#include <QVector> namespace Command { @@ -123,4 +124,65 @@ void RemoveEvent::undoit() add_event_to_dc(dc, eventToAdd.release()); // return ownership to backend } +AddGasSwitch::AddGasSwitch(struct dive *d, int dcNr, int seconds, int tank) : EventBase(d, dcNr) +{ + // If there is a gas change at this time stamp, remove it before adding the new one. + // There shouldn't be more than one gas change per time stamp. Just in case we'll + // support that anyway. + struct divecomputer *dc = get_dive_dc(d, dcNr); + struct event *gasChangeEvent = dc->events; + while ((gasChangeEvent = get_next_event_mutable(gasChangeEvent, "gaschange")) != NULL) { + if (gasChangeEvent->time.seconds == seconds) { + eventsToRemove.push_back(gasChangeEvent); + int idx = gasChangeEvent->gas.index; + if (std::find(cylinders.begin(), cylinders.end(), idx) == cylinders.end()) + cylinders.push_back(idx); // cylinders might have changed their status + } + gasChangeEvent = gasChangeEvent->next; + } + + eventsToAdd.emplace_back(create_gas_switch_event(d, dc, seconds, tank)); +} + +bool AddGasSwitch::workToBeDone() +{ + return true; +} + +void AddGasSwitch::redoit() +{ + std::vector<OwningEventPtr> newEventsToAdd; + std::vector<event *> newEventsToRemove; + newEventsToAdd.reserve(eventsToRemove.size()); + newEventsToRemove.reserve(eventsToAdd.size()); + struct divecomputer *dc = get_dive_dc(d, dcNr); + + for (event *ev: eventsToRemove) { + remove_event_from_dc(dc, ev); + newEventsToAdd.emplace_back(ev); // take ownership of event + } + for (OwningEventPtr &ev: eventsToAdd) { + newEventsToRemove.push_back(ev.get()); + add_event_to_dc(dc, ev.release()); // return ownership to backend + } + eventsToAdd = std::move(newEventsToAdd); + eventsToRemove = std::move(newEventsToRemove); + + // this means we potentially have a new tank that is being used and needs to be shown + fixup_dive(d); + + for (int idx: cylinders) + emit diveListNotifier.cylinderEdited(d, idx); + + // TODO: This is silly we send a DURATION change event so that the statistics are recalculated. + // We should instead define a proper DiveField that expresses the change caused by a gas switch. + emit diveListNotifier.divesChanged(QVector<dive *>{ d }, DiveField::DURATION | DiveField::DEPTH); +} + +void AddGasSwitch::undoit() +{ + // Undo and redo do the same thing, as the dives-to-be-added and dives-to-be-removed are exchanged. + redoit(); +} + } // namespace Command |