undo: reorder cylinders on remove-cylinder undo/redo

The cylinders in the events must be reordered if we remove
a cylinder. To avoid duplication of code, move the reordering
function into qthelper.cpp, though it might not be ideal
there.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-02-23 19:24:06 +01:00
parent a37939889b
commit 5b7a316593
4 changed files with 39 additions and 9 deletions

View file

@ -1106,6 +1106,7 @@ RemoveCylinder::RemoveCylinder(int index, bool currentDiveOnly) :
void RemoveCylinder::undo() void RemoveCylinder::undo()
{ {
for (size_t i = 0; i < dives.size(); ++i) { for (size_t i = 0; i < dives.size(); ++i) {
std::vector<int> mapping = get_cylinder_map_for_add(dives[i]->cylinders.nr, indexes[i]);
add_to_cylinder_table(&dives[i]->cylinders, indexes[i], clone_cylinder(cyl)); add_to_cylinder_table(&dives[i]->cylinders, indexes[i], clone_cylinder(cyl));
emit diveListNotifier.cylinderAdded(dives[i], indexes[i]); emit diveListNotifier.cylinderAdded(dives[i], indexes[i]);
} }
@ -1114,7 +1115,9 @@ void RemoveCylinder::undo()
void RemoveCylinder::redo() void RemoveCylinder::redo()
{ {
for (size_t i = 0; i < dives.size(); ++i) { for (size_t i = 0; i < dives.size(); ++i) {
std::vector<int> mapping = get_cylinder_map_for_remove(dives[i]->cylinders.nr, indexes[i]);
remove_cylinder(dives[i], indexes[i]); remove_cylinder(dives[i], indexes[i]);
cylinder_renumber(dives[i], &mapping[0]);
emit diveListNotifier.cylinderRemoved(dives[i], indexes[i]); emit diveListNotifier.cylinderRemoved(dives[i], indexes[i]);
} }
} }

View file

@ -1661,3 +1661,36 @@ extern "C" char *get_changes_made()
else else
return nullptr; return nullptr;
} }
// Generate a cylinder-renumber map for use when the n-th cylinder
// of a dive with count cylinders is removed. It fills an int vector
// with 0..n, -1, n..count-1. Each entry in the vector represents
// the new id of the cylinder, whereby <0 means that this particular
// cylinder does not get any new id. This should probably be moved
// to the C-core, but using std::vector is simply more convenient.
// The function assumes that n < count!
std::vector<int> get_cylinder_map_for_remove(int count, int n)
{
// 1) Fill mapping[0]..mapping[n-1] with 0..n-1
// 2) Set mapping[n] to -1
// 3) Fill mapping[n+1]..mapping[count-1] with n..count-2
std::vector<int> mapping(count);
std::iota(mapping.begin(), mapping.begin() + n, 0);
mapping[n] = -1;
std::iota(mapping.begin() + n + 1, mapping.end(), n);
return mapping;
}
// Generate a cylinder-renumber map for use when a cylinder is added
// before the n-th cylinder. It fills an int vector with
// with 0..n-1, n+1..count. Each entry in the vector represents
// the new id of the cylinder. This probably should be moved
// to the C-core, but using std::vector is simply more convenient.
// This function assumes that that n <= count!
std::vector<int> get_cylinder_map_for_add(int count, int n)
{
std::vector<int> mapping(count);
std::iota(mapping.begin(), mapping.begin() + n, 0);
std::iota(mapping.begin() + n, mapping.end(), n + 1);
return mapping;
}

View file

@ -83,6 +83,8 @@ QLocale getLocale();
QVector<QPair<QString, int>> selectedDivesGasUsed(); QVector<QPair<QString, int>> selectedDivesGasUsed();
QString getUserAgent(); QString getUserAgent();
QString printGPSCoords(const location_t *loc); QString printGPSCoords(const location_t *loc);
std::vector<int> get_cylinder_map_for_remove(int count, int n);
std::vector<int> get_cylinder_map_for_add(int count, int n);
extern QString (*changesCallback)(); extern QString (*changesCallback)();
void uiNotification(const QString &msg); void uiNotification(const QString &msg);

View file

@ -513,15 +513,7 @@ void CylindersModel::remove(QModelIndex index)
changed = true; changed = true;
endRemoveRows(); endRemoveRows();
// Create a mapping of cylinder indices: std::vector<int> mapping = get_cylinder_map_for_remove(d->cylinders.nr + 1, index.row());
// 1) Fill mapping[0]..mapping[index-1] with 0..index
// 2) Set mapping[index] to -1
// 3) Fill mapping[index+1]..mapping[end] with index..
std::vector<int> mapping(d->cylinders.nr + 1);
std::iota(mapping.begin(), mapping.begin() + index.row(), 0);
mapping[index.row()] = -1;
std::iota(mapping.begin() + index.row() + 1, mapping.end(), index.row());
cylinder_renumber(d, &mapping[0]); cylinder_renumber(d, &mapping[0]);
if (in_planner()) if (in_planner())
DivePlannerPointsModel::instance()->cylinderRenumber(&mapping[0]); DivePlannerPointsModel::instance()->cylinderRenumber(&mapping[0]);