diff options
Diffstat (limited to 'qt-ui/undocommands.cpp')
-rw-r--r-- | qt-ui/undocommands.cpp | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/qt-ui/undocommands.cpp b/qt-ui/undocommands.cpp index d7bae75a9..dbfc77472 100644 --- a/qt-ui/undocommands.cpp +++ b/qt-ui/undocommands.cpp @@ -2,8 +2,7 @@ #include "mainwindow.h" #include "divelist.h" -UndoDeleteDive::UndoDeleteDive(QList<dive *> deletedDives) - : diveList(deletedDives) +UndoDeleteDive::UndoDeleteDive(QList<dive *> deletedDives) : diveList(deletedDives) { setText("delete dive"); if (diveList.count() > 1) @@ -12,8 +11,25 @@ UndoDeleteDive::UndoDeleteDive(QList<dive *> deletedDives) void UndoDeleteDive::undo() { - for (int i = 0; i < diveList.count(); i++) + // first bring back the trip(s) + Q_FOREACH(struct dive_trip *trip, tripList) + insert_trip(&trip); + + // now walk the list of deleted dives + for (int i = 0; i < diveList.count(); i++) { + struct dive *d = diveList.at(i); + // we adjusted the divetrip to point to the "new" divetrip + if (d->divetrip) { + struct dive_trip *trip = d->divetrip; + tripflag_t tripflag = d->tripflag; // this gets overwritten in add_dive_to_trip() + d->divetrip = NULL; + d->next = NULL; + d->pprev = NULL; + add_dive_to_trip(d, trip); + d->tripflag = tripflag; + } record_dive(diveList.at(i)); + } mark_divelist_changed(true); MainWindow::instance()->refreshDisplay(); } @@ -22,10 +38,28 @@ void UndoDeleteDive::redo() { QList<struct dive*> newList; for (int i = 0; i < diveList.count(); i++) { - //make a copy of the dive before deleting it + // make a copy of the dive before deleting it struct dive* d = alloc_dive(); copy_dive(diveList.at(i), d); newList.append(d); + // 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 + if (d->divetrip && d->divetrip->nrdives == 1) { + struct dive_trip *undo_trip = (struct dive_trip *)calloc(1, sizeof(struct dive_trip)); + *undo_trip = *d->divetrip; + undo_trip->location = strdup(d->divetrip->location); + undo_trip->notes = strdup(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 + // trip that we are about to delete implicitly when deleting its last dive below + Q_FOREACH(struct dive *inner_dive, newList) + if (inner_dive->divetrip == d->divetrip) + inner_dive->divetrip = undo_trip; + d->divetrip = undo_trip; + tripList.append(undo_trip); + } //delete the dive delete_single_dive(get_divenr(diveList.at(i))); } |