From 147a36647ce6148ef2eccbe393faa658d0fe6834 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 2 Nov 2019 21:19:29 +0100 Subject: Undo: make adding of weights an undoable action Introduce an AddWeight undo command. This is modelled after the numerous dive-edit undo commands. The redo and undo actions are connected to the WeightModel via two new signals, weightAdded and weightRemoved. Signed-off-by: Berthold Stoeger --- commands/command.cpp | 5 +++++ commands/command.h | 1 + commands/command_edit.cpp | 32 ++++++++++++++++++++++++++++++++ commands/command_edit.h | 9 +++++++++ 4 files changed, 47 insertions(+) (limited to 'commands') diff --git a/commands/command.cpp b/commands/command.cpp index 47595674f..8f3dcc703 100644 --- a/commands/command.cpp +++ b/commands/command.cpp @@ -268,6 +268,11 @@ void editProfile(dive *d) execute(new ReplanDive(d, true)); } +int addWeight(bool currentDiveOnly) +{ + return execute_edit(new AddWeight(currentDiveOnly)); +} + // Trip editing related commands void editTripLocation(dive_trip *trip, const QString &s) { diff --git a/commands/command.h b/commands/command.h index 6446b32fd..85a37534e 100644 --- a/commands/command.h +++ b/commands/command.h @@ -81,6 +81,7 @@ int editDiveMaster(const QStringList &newList, bool currentDiveOnly); void pasteDives(const dive *d, dive_components what); void replanDive(dive *d); // dive computer(s) and cylinder(s) will be reset! void editProfile(dive *d); // dive computer(s) and cylinder(s) will be reset! +int addWeight(bool currentDiveOnly); // 5) Trip editing commands diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 5e02ed80e..541eac61a 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -953,4 +953,36 @@ void ReplanDive::redo() undo(); } +// ***** Add Weight ***** +AddWeight::AddWeight(bool currentDiveOnly) : + EditDivesBase(currentDiveOnly) +{ + //: remove the part in parentheses for %n = 1 + setText(tr("Add weight (%n dive(s))", "", dives.size())); +} + +bool AddWeight::workToBeDone() +{ + return true; +} + +void AddWeight::undo() +{ + for (dive *d: dives) { + if (d->weightsystems.nr <= 0) + continue; + remove_weightsystem(d, d->weightsystems.nr - 1); + emit diveListNotifier.weightRemoved(d, d->weightsystems.nr); + } +} + +void AddWeight::redo() +{ + weightsystem_t ws { {0}, "" }; + for (dive *d: dives) { + add_cloned_weightsystem(&d->weightsystems, ws); + emit diveListNotifier.weightAdded(d, d->weightsystems.nr - 1); + } +} + } // namespace Command diff --git a/commands/command_edit.h b/commands/command_edit.h index c3acf9d48..ecf745070 100644 --- a/commands/command_edit.h +++ b/commands/command_edit.h @@ -330,6 +330,15 @@ private: bool workToBeDone() override; }; +class AddWeight : public EditDivesBase { +public: + AddWeight(bool currentDiveOnly); +private: + void undo() override; + void redo() override; + bool workToBeDone() override; +}; + } // namespace Command #endif -- cgit v1.2.3-70-g09d2