On Tue, Mar 10, 2015 at 11:08 PM, Grace Karanja <[email protected]>
wrote:

>
> On Mon, Mar 9, 2015 at 2:51 PM, Grace Karanja <[email protected]>
> wrote:
>>
>>
>> See the attached patch, which implements undo/redo of when merging dives.
>>
>
> The next attached patch implements undo/redo when downloading from dive
> computers.
>
>

This next one implements undo/redo when removing dives from dive trips.


>
>>
>>>
>>> /D
>>>
>>
>>
>
From 12145d91f0ef51764c9642f232e60dec98c8f2a6 Mon Sep 17 00:00:00 2001
From: Grace Karanja <[email protected]>
Date: Thu, 12 Mar 2015 09:01:53 +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 <[email protected]>
---
 qt-ui/divelistview.cpp | 10 ++++------
 qt-ui/undocommands.cpp | 29 +++++++++++++++++++++++++++++
 qt-ui/undocommands.h   | 10 ++++++++++
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index bded95e..d1c5260 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -598,15 +598,13 @@ void DiveListView::removeFromTrip()
 	//TODO: move this to C-code.
 	int i;
 	struct dive *d;
+	QMap<struct dive*, dive_trip*> dives;
 	for_each_dive (i, d) {
 		if (d->selected)
-			remove_dive_from_trip(d, false);
+			dives.insert(d, d->divetrip);
 	}
-	rememberSelection();
-	reload(currentLayout, false);
-	fixMessyQtModelBehaviour();
-	restoreSelection();
-	mark_divelist_changed(true);
+	UndoRemoveDivesFromTrip *undoCommand = new UndoRemoveDivesFromTrip(dives);
+	MainWindow::instance()->undoStack->push(undoCommand);
 }
 
 void DiveListView::newTripAbove()
diff --git a/qt-ui/undocommands.cpp b/qt-ui/undocommands.cpp
index 758b5eb..cd776e0 100644
--- a/qt-ui/undocommands.cpp
+++ b/qt-ui/undocommands.cpp
@@ -189,3 +189,32 @@ void UndoDownloadDives::redo()
 	mark_divelist_changed(true);
 	MainWindow::instance()->refreshDisplay();
 }
+
+
+UndoRemoveDivesFromTrip::UndoRemoveDivesFromTrip(QMap<dive *, dive_trip *> movedDives)
+{
+	dives = movedDives;
+	setText("remove dives from trip");
+}
+
+void UndoRemoveDivesFromTrip::undo()
+{
+	QMapIterator<dive*, dive_trip*> i(dives);
+	while (i.hasNext()) {
+		i.next();
+		add_dive_to_trip(i.key(), i.value());
+	}
+	mark_divelist_changed(true);
+	MainWindow::instance()->refreshDisplay();
+}
+
+void UndoRemoveDivesFromTrip::redo()
+{
+	QMapIterator<dive*, dive_trip* > i(dives);
+	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 63293b2..143aee4 100644
--- a/qt-ui/undocommands.h
+++ b/qt-ui/undocommands.h
@@ -57,4 +57,14 @@ private:
 	QList<struct dive*> dives;
 };
 
+class UndoRemoveDivesFromTrip : public QUndoCommand {
+public:
+	UndoRemoveDivesFromTrip(QMap<struct dive*, dive_trip*> movedDives);
+	virtual void undo();
+	virtual void redo();
+
+private:
+	QMap<struct dive*, dive_trip*> dives;
+};
+
 #endif // UNDOCOMMANDS_H
-- 
2.1.0

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to