mirror of
https://github.com/subsurface/subsurface.git
synced 2024-12-11 03:21:29 +00:00
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>
This commit is contained in:
parent
1971cfad54
commit
f9fe6d759f
2 changed files with 37 additions and 13 deletions
|
@ -8,8 +8,27 @@
|
||||||
|
|
||||||
namespace Command {
|
namespace Command {
|
||||||
|
|
||||||
AddEventBase::AddEventBase(struct dive *dIn, int dcNrIn, struct event *ev) :
|
EventBase::EventBase(struct dive *dIn, int dcNrIn) :
|
||||||
d(dIn), dcNr(dcNrIn), eventToAdd(ev)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddEventBase::redo()
|
void AddEventBase::redoit()
|
||||||
{
|
{
|
||||||
struct divecomputer *dc = get_dive_dc(d, dcNr);
|
struct divecomputer *dc = get_dive_dc(d, dcNr);
|
||||||
eventToRemove = eventToAdd.get();
|
eventToRemove = eventToAdd.get();
|
||||||
add_event_to_dc(dc, eventToAdd.release()); // return ownership to backend
|
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);
|
struct divecomputer *dc = get_dive_dc(d, dcNr);
|
||||||
remove_event_from_dc(dc, eventToRemove);
|
remove_event_from_dc(dc, eventToRemove);
|
||||||
eventToAdd.reset(eventToRemove); // take ownership of event
|
eventToAdd.reset(eventToRemove); // take ownership of event
|
||||||
eventToRemove = nullptr;
|
eventToRemove = nullptr;
|
||||||
invalidate_dive_cache(d);
|
|
||||||
emit diveListNotifier.eventsChanged(d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AddEventBookmark::AddEventBookmark(struct dive *d, int dcNr, int seconds) :
|
AddEventBookmark::AddEventBookmark(struct dive *d, int dcNr, int seconds) :
|
||||||
|
|
|
@ -16,19 +16,28 @@ namespace Command {
|
||||||
// Therefore, the undo commands work on events as they do with dives: using
|
// Therefore, the undo commands work on events as they do with dives: using
|
||||||
// owning pointers. See comments in command_base.h
|
// owning pointers. See comments in command_base.h
|
||||||
|
|
||||||
class AddEventBase : public Base {
|
class EventBase : public Base {
|
||||||
public:
|
protected:
|
||||||
AddEventBase(struct dive *d, int dcNr, struct event *ev); // Takes ownership of event!
|
EventBase(struct dive *d, int dcNr);
|
||||||
private:
|
|
||||||
bool workToBeDone() override;
|
|
||||||
void undo() override;
|
void undo() override;
|
||||||
void redo() 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.
|
// 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
|
// Since one divecomputer is integrated into the dive structure, pointers to divecomputers
|
||||||
// are probably not stable.
|
// are probably not stable.
|
||||||
struct dive *d;
|
struct dive *d;
|
||||||
int dcNr;
|
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
|
OwningEventPtr eventToAdd; // for redo
|
||||||
event *eventToRemove; // for undo
|
event *eventToRemove; // for undo
|
||||||
|
|
Loading…
Reference in a new issue