From 193edd9f13b4a1a6cbddd494de6b7aa38709be95 Mon Sep 17 00:00:00 2001 From: Grace Karanja Date: Fri, 10 Apr 2015 09:39:51 +0300 Subject: [PATCH] Add ability to undo removing of dives from trips Add the functionality to undo/redo removing of dives from trips. The code calling remove_dive_from_trip has moved to the UndoCommands class. Signed-off-by: Grace Karanja Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 6 +++++- qt-ui/undocommands.cpp | 29 +++++++++++++++++++++++++++++ qt-ui/undocommands.h | 10 ++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index bacac71db..b65b88e3a 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -622,10 +622,14 @@ void DiveListView::removeFromTrip() //TODO: move this to C-code. int i; struct dive *d; + QMap divesToRemove; for_each_dive (i, d) { if (d->selected) - remove_dive_from_trip(d, false); + divesToRemove.insert(d, d->divetrip); } + UndoRemoveDivesFromTrip *undoCommand = new UndoRemoveDivesFromTrip(divesToRemove); + MainWindow::instance()->undoStack->push(undoCommand); + rememberSelection(); reload(currentLayout, false); fixMessyQtModelBehaviour(); diff --git a/qt-ui/undocommands.cpp b/qt-ui/undocommands.cpp index be27e27dc..aad264e24 100644 --- a/qt-ui/undocommands.cpp +++ b/qt-ui/undocommands.cpp @@ -92,3 +92,32 @@ void UndoRenumberDives::redo() mark_divelist_changed(true); MainWindow::instance()->refreshDisplay(); } + + +UndoRemoveDivesFromTrip::UndoRemoveDivesFromTrip(QMap removedDives) +{ + divesToUndo = removedDives; + setText("remove dive(s) from trip"); +} + +void UndoRemoveDivesFromTrip::undo() +{ + QMapIterator i(divesToUndo); + while (i.hasNext()) { + i.next(); + add_dive_to_trip(i.key (), i.value()); + } + mark_divelist_changed(true); + MainWindow::instance()->refreshDisplay(); +} + +void UndoRemoveDivesFromTrip::redo() +{ + QMapIterator i(divesToUndo); + while (i.hasNext()) { + i.next(); + remove_dive_from_trip(i.key(), false); + } + mark_divelist_changed(true); + MainWindow::instance()->refreshDisplay(); +} diff --git a/qt-ui/undocommands.h b/qt-ui/undocommands.h index e4f2da8e1..bd8530d77 100644 --- a/qt-ui/undocommands.h +++ b/qt-ui/undocommands.h @@ -37,4 +37,14 @@ private: int start; }; +class UndoRemoveDivesFromTrip : public QUndoCommand { +public: + UndoRemoveDivesFromTrip(QMap removedDives); + virtual void undo(); + virtual void redo(); + +private: + QMap divesToUndo; +}; + #endif // UNDOCOMMANDS_H