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>
This commit is contained in:
Berthold Stoeger 2020-03-04 21:10:05 +01:00
parent c585fd9f8e
commit 0bd821183d
6 changed files with 83 additions and 20 deletions

View file

@ -16,6 +16,7 @@ Mobile: make sure filter header and virtual keyboard are shown when tapping on f
Mobile: fix issue where in some circumstances changes weren't written to storage
Mobile: fix issues with filtering while editing dives
Desktop: implement dive invalidation
Undo: implement undo of event handling (viz. bookmarks, setpoints, gas switches)
Desktop: fix tab-order in filter widget
Desktop: implement fulltext search
Desktop: add starts-with and exact filter modes for textual search

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1645,26 +1645,7 @@ void ProfileWidget2::changeGas(int tank, int seconds)
if (!current_dive || tank < 0 || tank >= current_dive->cylinders.nr)
return;
// if there is a gas change at this time stamp, remove it before adding the new one
struct event *gasChangeEvent = current_dc->events;
while ((gasChangeEvent = get_next_event_mutable(gasChangeEvent, "gaschange")) != NULL) {
if (gasChangeEvent->time.seconds == seconds) {
remove_event(gasChangeEvent);
gasChangeEvent = current_dc->events;
} else {
gasChangeEvent = gasChangeEvent->next;
}
}
add_gas_switch_event(current_dive, current_dc, seconds, tank);
// this means we potentially have a new tank that is being used and needs to be shown
fixup_dive(current_dive);
invalidate_dive_cache(current_dive);
// FIXME - this no longer gets written to the dive list - so we need to enableEdition() here
emit updateDiveInfo();
mark_divelist_changed(true);
replot();
Command::addGasSwitch(current_dive, dc_number, seconds, tank);
}
#endif