diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-03-04 17:35:02 +0100 |
---|---|---|
committer | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-04-07 00:13:35 +0200 |
commit | f9fe6d759f33c36f1c0c0d20a591f6517fc9071f (patch) | |
tree | db068ff9743b8f30b03cd1d73439bef5c0cc6873 /commands | |
parent | 1971cfad547792ba961f804605ccb12780e17de0 (diff) | |
download | subsurface-f9fe6d759f33c36f1c0c0d20a591f6517fc9071f.tar.gz |
undo: split out EventBase class
All event-based commands will work on a dive computer and
need to replot the profile, etc. Therefore, in analogy to
the dive-list commands create a base class with two virtual
functions undoit() and redoit() that must be defined in the
derived classes that do the actual work.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/command_event.cpp | 31 | ||||
-rw-r--r-- | commands/command_event.h | 19 |
2 files changed, 37 insertions, 13 deletions
diff --git a/commands/command_event.cpp b/commands/command_event.cpp index a1e13f252..7ecd4f070 100644 --- a/commands/command_event.cpp +++ b/commands/command_event.cpp @@ -8,8 +8,27 @@ namespace Command { -AddEventBase::AddEventBase(struct dive *dIn, int dcNrIn, struct event *ev) : - d(dIn), dcNr(dcNrIn), eventToAdd(ev) +EventBase::EventBase(struct dive *dIn, int dcNrIn) : + d(dIn), dcNr(dcNrIn) +{ +} + +void EventBase::redo() +{ + redoit(); // Call actual function in base class + invalidate_dive_cache(d); + emit diveListNotifier.eventsChanged(d); +} + +void EventBase::undo() +{ + undoit(); // Call actual function in base class + invalidate_dive_cache(d); + emit diveListNotifier.eventsChanged(d); +} + +AddEventBase::AddEventBase(struct dive *d, int dcNr, struct event *ev) : EventBase(d, dcNr), + eventToAdd(ev) { } @@ -18,23 +37,19 @@ bool AddEventBase::workToBeDone() return true; } -void AddEventBase::redo() +void AddEventBase::redoit() { struct divecomputer *dc = get_dive_dc(d, dcNr); eventToRemove = eventToAdd.get(); add_event_to_dc(dc, eventToAdd.release()); // return ownership to backend - invalidate_dive_cache(d); - emit diveListNotifier.eventsChanged(d); } -void AddEventBase::undo() +void AddEventBase::undoit() { struct divecomputer *dc = get_dive_dc(d, dcNr); remove_event_from_dc(dc, eventToRemove); eventToAdd.reset(eventToRemove); // take ownership of event eventToRemove = nullptr; - invalidate_dive_cache(d); - emit diveListNotifier.eventsChanged(d); } AddEventBookmark::AddEventBookmark(struct dive *d, int dcNr, int seconds) : diff --git a/commands/command_event.h b/commands/command_event.h index 8cceaeb25..a674e258b 100644 --- a/commands/command_event.h +++ b/commands/command_event.h @@ -16,19 +16,28 @@ namespace Command { // Therefore, the undo commands work on events as they do with dives: using // owning pointers. See comments in command_base.h -class AddEventBase : public Base { -public: - AddEventBase(struct dive *d, int dcNr, struct event *ev); // Takes ownership of event! -private: - bool workToBeDone() override; +class EventBase : public Base { +protected: + EventBase(struct dive *d, int dcNr); void undo() override; void redo() override; + virtual void redoit() = 0; + virtual void undoit() = 0; // Note: we store dive and the divecomputer-number instead of a pointer to the divecomputer. // Since one divecomputer is integrated into the dive structure, pointers to divecomputers // are probably not stable. struct dive *d; int dcNr; +}; + +class AddEventBase : public EventBase { +public: + AddEventBase(struct dive *d, int dcNr, struct event *ev); // Takes ownership of event! +private: + bool workToBeDone() override; + void undoit() override; + void redoit() override; OwningEventPtr eventToAdd; // for redo event *eventToRemove; // for undo |