aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-03-04 21:10:05 +0100
committerGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-04-07 00:13:35 +0200
commit0bd821183d715f709d378e8ac1a75aa0c3d0f2cc (patch)
treee6d182e6922d7c06bf09b17c9c953f8b5dc78e85 /commands
parentc585fd9f8ee0c1f27f997b4e19984b50d150f6cb (diff)
downloadsubsurface-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')
-rw-r--r--commands/command.cpp5
-rw-r--r--commands/command.h1
-rw-r--r--commands/command_event.cpp62
-rw-r--r--commands/command_event.h13
4 files changed, 81 insertions, 0 deletions
diff --git a/commands/command.cpp b/commands/command.cpp
index 54f9b22f9..c173494c0 100644
--- a/commands/command.cpp
+++ b/commands/command.cpp
@@ -354,4 +354,9 @@ void removeEvent(struct dive *d, int dcNr, struct event *ev)
execute(new RemoveEvent(d, dcNr, ev));
}
+void addGasSwitch(struct dive *d, int dcNr, int seconds, int tank)
+{
+ execute(new AddGasSwitch(d, dcNr, seconds, tank));
+}
+
} // namespace Command
diff --git a/commands/command.h b/commands/command.h
index 82931747c..66122ac99 100644
--- a/commands/command.h
+++ b/commands/command.h
@@ -111,6 +111,7 @@ void addEventDivemodeSwitch(struct dive *d, int dcNr, int seconds, int divemode)
void addEventSetpointChange(struct dive *d, int dcNr, int seconds, pressure_t pO2);
void renameEvent(struct dive *d, int dcNr, struct event *ev, const char *name);
void removeEvent(struct dive *d, int dcNr, struct event *ev);
+void addGasSwitch(struct dive *d, int dcNr, int seconds, int tank);
} // namespace Command
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
diff --git a/commands/command_event.h b/commands/command_event.h
index eec5cb262..8e75dee6c 100644
--- a/commands/command_event.h
+++ b/commands/command_event.h
@@ -82,6 +82,19 @@ private:
event *eventToRemove; // for redo
};
+class AddGasSwitch : public EventBase {
+public:
+ AddGasSwitch(struct dive *d, int dcNr, int seconds, int tank);
+private:
+ bool workToBeDone() override;
+ void undoit() override;
+ void redoit() override;
+
+ std::vector<int> cylinders; // cylinders that are modified
+ std::vector<OwningEventPtr> eventsToAdd;
+ std::vector<event *> eventsToRemove;
+};
+
} // namespace Command
#endif // COMMAND_EVENT_H