From 60a7404ed4c79398c08a9d83d6575a3381c999a1 Mon Sep 17 00:00:00 2001 From: Grace Karanja Date: Wed, 11 Feb 2015 10:57:56 +0300 Subject: Add option to undo deleted dives Add ability to undo deleted dives by storing a list of the deleted dives in a QUndoCommand. Signed-off-by: Grace Karanja Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 7 +++++++ qt-ui/undocommands.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'qt-ui') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index b866a1aa1..e4ccb7208 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -14,6 +14,7 @@ #include #include #include "qthelper.h" +#include "undocommands.h" // # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500}; @@ -731,13 +732,19 @@ void DiveListView::deleteDive() // so instead of using the for_each_dive macro I'm using an explicit for loop // to make this easier to understand int lastDiveNr = -1; + QList deletedDives; //a list of all deleted dives to be stored in the undo command for_each_dive (i, d) { if (!d->selected) continue; + struct dive* undo_entry = alloc_dive(); + copy_dive(get_dive(i), undo_entry); + deletedDives.append(undo_entry); delete_single_dive(i); i--; // so the next dive isn't skipped... it's now #i lastDiveNr = i; } + UndoDeleteDive *undoEntry = new UndoDeleteDive(deletedDives); + MainWindow::instance()->undoStack->push(undoEntry); if (amount_selected == 0) { MainWindow::instance()->cleanUpEmpty(); } diff --git a/qt-ui/undocommands.cpp b/qt-ui/undocommands.cpp index f76f021b3..8c58c7c96 100644 --- a/qt-ui/undocommands.cpp +++ b/qt-ui/undocommands.cpp @@ -1,16 +1,36 @@ #include "undocommands.h" +#include "mainwindow.h" +#include "divelist.h" UndoDeleteDive::UndoDeleteDive(QList diveList) { - + dives = diveList; + setText("delete dive"); + if (dives.count() > 1) + setText(QString("delete %1 dives").arg(QString::number(dives.count()))); } void UndoDeleteDive::undo() { - + for (int i = 0; i < dives.count(); i++) + record_dive(dives.at(i)); + mark_divelist_changed(true); + MainWindow::instance()->refreshDisplay(); } void UndoDeleteDive::redo() { - + QList newList; + for (int i = 0; i < dives.count(); i++) { + //make a copy of the dive before deleting it + struct dive* d = alloc_dive(); + copy_dive(dives.at(i), d); + newList.append(d); + //delete the dive + delete_single_dive(get_divenr(dives.at(i))); + } + mark_divelist_changed(true); + MainWindow::instance()->refreshDisplay(); + dives.clear(); + dives = newList; } -- cgit v1.2.3-70-g09d2