diff options
Diffstat (limited to 'commands/command_edit.cpp')
-rw-r--r-- | commands/command_edit.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 541eac61a..bfa02a55d 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -985,4 +985,75 @@ void AddWeight::redo() } } +static int find_weightsystem_index(const struct dive *d, weightsystem_t ws) +{ + for (int idx = 0; idx < d->weightsystems.nr; ++idx) { + if (same_weightsystem(d->weightsystems.weightsystems[idx], ws)) + return idx; + } + return -1; +} + +RemoveWeight::RemoveWeight(int index, bool currentDiveOnly) : + EditDivesBase(currentDiveOnly), + ws{ {0}, nullptr } +{ + // Get the old weightsystem, bail if index is invalid + if (!current || index < 0 || index >= current->weightsystems.nr) { + dives.clear(); + return; + } + ws = clone_weightsystem(current->weightsystems.weightsystems[index]); + + // Deleting a weightsystem from multiple dives is semantically ill-defined. + // What we will do is trying to delete the same weightsystem if it exists. + // For that purpose, we will determine the indices of the same weightsystem. + std::vector<dive *> divesNew; + divesNew.reserve(dives.size()); + indexes.reserve(dives.size()); + + for (dive *d: dives) { + if (d == current) { + divesNew.push_back(d); + indexes.push_back(index); + continue; + } + int idx = find_weightsystem_index(d, ws); + if (idx >= 0) { + divesNew.push_back(d); + indexes.push_back(idx); + } + } + dives = std::move(divesNew); + + //: remove the part in parentheses for %n = 1 + setText(tr("Remove weight (%n dive(s))", "", dives.size())); +} + +RemoveWeight::~RemoveWeight() +{ + free((void *)ws.description); +} + +bool RemoveWeight::workToBeDone() +{ + return !dives.empty(); +} + +void RemoveWeight::undo() +{ + for (size_t i = 0; i < dives.size(); ++i) { + add_to_weightsystem_table(&dives[i]->weightsystems, indexes[i], clone_weightsystem(ws)); + emit diveListNotifier.weightAdded(dives[i], indexes[i]); + } +} + +void RemoveWeight::redo() +{ + for (size_t i = 0; i < dives.size(); ++i) { + remove_weightsystem(dives[i], indexes[i]); + emit diveListNotifier.weightRemoved(dives[i], indexes[i]); + } +} + } // namespace Command |