From 96d87273995a6af35d90efb1190ff653e4c03d02 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 30 Jul 2018 15:55:29 +0200 Subject: Undo: use dive * instead of unique index in renumber-dives Now, that pointers to dives are stable, we might just as well use dive * instead of the unique-id. This also affects the merge-dive command, as this uses the same renumbering machinery. Signed-off-by: Berthold Stoeger --- desktop-widgets/command.cpp | 2 +- desktop-widgets/command.h | 2 +- desktop-widgets/command_divelist.cpp | 10 +++++----- desktop-widgets/command_divelist.h | 6 +++--- desktop-widgets/simplewidgets.cpp | 12 ++++++------ 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index fe16f1898..54ca2d39f 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -21,7 +21,7 @@ void shiftTime(const QVector &changedDives, int amount) execute(new ShiftTime(changedDives, amount)); } -void renumberDives(const QVector> &divesToRenumber) +void renumberDives(const QVector> &divesToRenumber) { execute(new RenumberDives(divesToRenumber)); } diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index 924365806..9a8ccfd6d 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -18,7 +18,7 @@ QAction *redoAction(QObject *parent); // Create an redo action. void addDive(dive *d, bool autogroup); void deleteDive(const QVector &divesToDelete); void shiftTime(const QVector &changedDives, int amount); -void renumberDives(const QVector> &divesToRenumber); +void renumberDives(const QVector> &divesToRenumber); void removeDivesFromTrip(const QVector &divesToRemove); void removeAutogenTrips(); void addDivesToTrip(const QVector &divesToAddIn, dive_trip *trip); diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp index 732932de3..17d5f00e4 100644 --- a/desktop-widgets/command_divelist.cpp +++ b/desktop-widgets/command_divelist.cpp @@ -151,10 +151,10 @@ static std::vector addDives(std::vector &divesToAdd) // This helper function renumbers dives according to an array of id/number pairs. // The old numbers are stored in the array, thus calling this function twice has no effect. // TODO: switch from uniq-id to indexes once all divelist-actions are controlled by undo-able commands -static void renumberDives(QVector> &divesToRenumber) +static void renumberDives(QVector> &divesToRenumber) { for (auto &pair: divesToRenumber) { - dive *d = get_dive_by_uniq_id(pair.first); + dive *d = pair.first; if (!d) continue; std::swap(d->number, pair.second); @@ -165,7 +165,7 @@ static void renumberDives(QVector> &divesToRenumber) std::vector> dives; dives.reserve(divesToRenumber.size()); for (const auto &pair: divesToRenumber) { - dive *d = get_dive_by_uniq_id(pair.first); + dive *d = pair.first; dives.push_back({ d->divetrip, d }); } @@ -413,7 +413,7 @@ void ShiftTime::undo() } -RenumberDives::RenumberDives(const QVector> &divesToRenumberIn) : divesToRenumber(divesToRenumberIn) +RenumberDives::RenumberDives(const QVector> &divesToRenumberIn) : divesToRenumber(divesToRenumberIn) { setText(tr("renumber %n dive(s)", "", divesToRenumber.count())); } @@ -641,7 +641,7 @@ MergeDives::MergeDives(const QVector &dives) // Stop renumbering if stuff isn't in order (see also core/divelist.c) if (newnr <= previousnr) break; - divesToRenumber.append(QPair(dive_table.dives[i]->id, newnr)); + divesToRenumber.append(QPair(dive_table.dives[i], newnr)); previousnr = newnr; } } diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h index 69824ce9f..e679369af 100644 --- a/desktop-widgets/command_divelist.h +++ b/desktop-widgets/command_divelist.h @@ -85,14 +85,14 @@ private: class RenumberDives : public Base { public: - RenumberDives(const QVector> &divesToRenumber); + RenumberDives(const QVector> &divesToRenumber); private: void undo() override; void redo() override; bool workToBeDone() override; // For redo and undo: pairs of dive-id / new number - QVector> divesToRenumber; + QVector> divesToRenumber; }; // The classes RemoveDivesFromTrip, RemoveAutogenTrips, CreateTrip, AutogroupDives @@ -166,7 +166,7 @@ private: std::vector unmergedDives; // For undo and redo - QVector> divesToRenumber; + QVector> divesToRenumber; }; } // namespace Command diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index 1df69e7bb..6d15da475 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -161,14 +161,14 @@ void RenumberDialog::buttonClicked(QAbstractButton *button) if (ui.buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) { MainWindow::instance()->dive_list()->rememberSelection(); // we remember a list from dive uuid to a new number - QVector> renumberedDives; + QVector> renumberedDives; int i; int newNr = ui.spinBox->value(); - struct dive *dive = NULL; - for_each_dive (i, dive) { - if (!selectedOnly || dive->selected) { - invalidate_dive_cache(dive); - renumberedDives.append(QPair(dive->id, newNr++)); + struct dive *d; + for_each_dive (i, d) { + if (!selectedOnly || d->selected) { + invalidate_dive_cache(d); + renumberedDives.append({ d, newNr++ }); } } Command::renumberDives(renumberedDives); -- cgit v1.2.3-70-g09d2