Undo: remember deleted trip in UndoRemoveDivesFromTrip::undo()

If the last dive of a trip is removed, the trip is deleted.
On redo the dive is added to a non existing trip, leading to a
segfault.

Therefore, keep a copy of the trip to reinstate it on redo.
Note: this cannot work for a sequence of multiple commands.
One would have to rewrite the whole undo-history. Nevertheless,
let's do this as a stop-gap measure.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-18 19:31:59 +02:00 committed by Lubomir I. Ivanov
parent b51e616b6a
commit 325b8bba35
4 changed files with 33 additions and 8 deletions

View file

@ -507,6 +507,7 @@ extern void delete_single_dive(int idx);
extern void add_single_dive(int idx, struct dive *dive); extern void add_single_dive(int idx, struct dive *dive);
extern void insert_trip(dive_trip_t **trip); extern void insert_trip(dive_trip_t **trip);
extern struct dive_trip *clone_empty_trip(struct dive_trip *trip);
extern const struct units SI_units, IMPERIAL_units; extern const struct units SI_units, IMPERIAL_units;

View file

@ -727,6 +727,19 @@ void insert_trip(dive_trip_t **dive_trip_p)
#endif #endif
} }
/* create a copy of a dive trip, but don't add any dives. */
dive_trip_t *clone_empty_trip(dive_trip_t *trip)
{
dive_trip_t *copy = malloc(sizeof(struct dive_trip));
*copy = *trip;
copy->location = copy_string(trip->location);
copy->notes = copy_string(trip->notes);
copy->nrdives = 0;
copy->next = NULL;
copy->dives = NULL;
return copy;
}
static void delete_trip(dive_trip_t *trip) static void delete_trip(dive_trip_t *trip)
{ {
dive_trip_t **p, *tmp; dive_trip_t **p, *tmp;

View file

@ -48,13 +48,7 @@ void UndoDeleteDive::redo()
// check for trip - if this is the last dive in the trip // check for trip - if this is the last dive in the trip
// the trip will get deleted, so we need to remember it as well // the trip will get deleted, so we need to remember it as well
if (d->divetrip && d->divetrip->nrdives == 1) { if (d->divetrip && d->divetrip->nrdives == 1) {
struct dive_trip *undo_trip = (struct dive_trip *)calloc(1, sizeof(struct dive_trip)); dive_trip *undo_trip = clone_empty_trip(d->divetrip);
*undo_trip = *d->divetrip;
undo_trip->location = copy_string(d->divetrip->location);
undo_trip->notes = copy_string(d->divetrip->notes);
undo_trip->nrdives = 0;
undo_trip->next = NULL;
undo_trip->dives = NULL;
// update all the dives who were in this trip to point to the copy of the // update all the dives who were in this trip to point to the copy of the
// trip that we are about to delete implicitly when deleting its last dive below // trip that we are about to delete implicitly when deleting its last dive below
Q_FOREACH(struct dive *inner_dive, newList) { Q_FOREACH(struct dive *inner_dive, newList) {
@ -141,10 +135,15 @@ UndoRemoveDivesFromTrip::UndoRemoveDivesFromTrip(QMap<dive *, dive_trip *> remov
void UndoRemoveDivesFromTrip::undo() void UndoRemoveDivesFromTrip::undo()
{ {
// first bring back the trip(s)
Q_FOREACH(struct dive_trip *trip, tripList)
insert_trip(&trip);
tripList.clear();
QMapIterator<dive*, dive_trip*> i(divesToUndo); QMapIterator<dive*, dive_trip*> i(divesToUndo);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
add_dive_to_trip(i.key (), i.value()); add_dive_to_trip(i.key(), i.value());
} }
mark_divelist_changed(true); mark_divelist_changed(true);
MainWindow::instance()->refreshDisplay(); MainWindow::instance()->refreshDisplay();
@ -155,6 +154,17 @@ void UndoRemoveDivesFromTrip::redo()
QMapIterator<dive*, dive_trip*> i(divesToUndo); QMapIterator<dive*, dive_trip*> i(divesToUndo);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
// If the trip will be deleted, remember it so that we can restore it later.
dive_trip *trip = i.value();
if (trip->nrdives == 1) {
dive_trip *cloned_trip = clone_empty_trip(trip);
tripList.append(cloned_trip);
// Rewrite the dive list, such that the dives will be added to the resurrected trip.
for (dive_trip *&old_trip: divesToUndo) {
if (old_trip == trip)
old_trip = cloned_trip;
}
}
remove_dive_from_trip(i.key(), false); remove_dive_from_trip(i.key(), false);
} }
mark_divelist_changed(true); mark_divelist_changed(true);

View file

@ -45,6 +45,7 @@ public:
private: private:
QMap<struct dive*, dive_trip*> divesToUndo; QMap<struct dive*, dive_trip*> divesToUndo;
QList<struct dive_trip*> tripList;
}; };
#endif // UNDOCOMMANDS_H #endif // UNDOCOMMANDS_H