summaryrefslogtreecommitdiffstats
path: root/qt-ui/undocommands.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-10-02 14:50:12 -0400
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-10-02 14:50:12 -0400
commita94c84d5982a10cd9adfd421aed41cf159dd3303 (patch)
treeedbaea3f89cad4e03f4ed2126de3e0c4bb07c2a7 /qt-ui/undocommands.cpp
parent7c427dcc020064e0ea867c77ee86db6e1f4755bb (diff)
downloadsubsurface-a94c84d5982a10cd9adfd421aed41cf159dd3303.tar.gz
Undo/redo of dive deletion needs to handle trips as well
If we delete dives that were part of a trip, that trip may get deleted as well. So if we undo that operation we need to bring back the trip, too. This also deals with a bug in the original code that did the delete both in calling code (in divelistview.cpp) and in the redo function. Because of the nature of the delete this didn't really matter but it is of course wrong and with the new code it would in fact cause an issue. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/undocommands.cpp')
-rw-r--r--qt-ui/undocommands.cpp42
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)));
}