On Wed, Feb 11, 2015 at 7:20 PM, Dirk Hohndel <[email protected]> wrote:

>
> Nice - I haven't looked at what that framework has to offer - I guess I
> need to investigate that :-)
> A quick reading of the overview doc seems to indicate this is quite
> useful. It does seem to implement an unlimited undo buffer, though...
>
> I played around with your patches a bit and they seem to work really well.
> Subsurface behaves just as one might expect, doing the right thing about
> disabling the menu, showing the correct operations, etc.
>
> Very nicely done.
>
> Thanks!
>
> /D
>

The attached patch now implements undo for time shifts in dives.
From 0e3102fa9631551cd7f4dd4a74159685a157eab5 Mon Sep 17 00:00:00 2001
From: Grace Karanja <[email protected]>
Date: Sat, 14 Feb 2015 20:12:05 +0300
Subject: [PATCH] Add ability to undo shifting of dive time

Adds the ability to undo shifting of dive times. The change is captured
at simplewidgets.cpp and an undo command is created.
---
 qt-ui/simplewidgets.cpp | 12 +++++++++++-
 qt-ui/undocommands.cpp  | 28 ++++++++++++++++++++++++++++
 qt-ui/undocommands.h    | 11 +++++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/qt-ui/simplewidgets.cpp b/qt-ui/simplewidgets.cpp
index 5c08a3f..22e589e 100644
--- a/qt-ui/simplewidgets.cpp
+++ b/qt-ui/simplewidgets.cpp
@@ -14,6 +14,7 @@
 #include "divelistview.h"
 #include "display.h"
 #include "profile/profilewidget2.h"
+#include "undocommands.h"
 
 class MinMaxAvgWidgetPrivate {
 public:
@@ -200,7 +201,16 @@ void ShiftTimesDialog::buttonClicked(QAbstractButton *button)
 			amount *= -1;
 		if (amount != 0) {
 			// DANGER, DANGER - this could get our dive_table unsorted...
-			shift_times(amount);
+			int i;
+			struct dive *dive;
+			QList<int> affectedDives;
+			for_each_dive (i, dive) {
+				if (!dive->selected)
+					continue;
+
+				affectedDives.append(dive->id);
+			}
+			MainWindow::instance()->undoStack->push(new UndoShiftTime(affectedDives, amount));
 			sort_table(&dive_table);
 			mark_divelist_changed(true);
 			MainWindow::instance()->dive_list()->rememberSelection();
diff --git a/qt-ui/undocommands.cpp b/qt-ui/undocommands.cpp
index 8c58c7c..316def4 100644
--- a/qt-ui/undocommands.cpp
+++ b/qt-ui/undocommands.cpp
@@ -34,3 +34,31 @@ void UndoDeleteDive::redo()
 	dives.clear();
 	dives = newList;
 }
+
+
+UndoShiftTime::UndoShiftTime(QList<int> diveList, int amount)
+{
+	setText("shift time");
+	dives = diveList;
+	timeChanged = amount;
+}
+
+void UndoShiftTime::undo()
+{
+	for (int i = 0; i < dives.count(); i++) {
+		struct dive* d = get_dive_by_uniq_id(dives.at(i));
+		d->when -= timeChanged;
+	}
+	mark_divelist_changed(true);
+	MainWindow::instance()->refreshDisplay();
+}
+
+void UndoShiftTime::redo()
+{
+	for (int i = 0; i < dives.count(); i++) {
+		struct dive* d = get_dive_by_uniq_id(dives.at(i));
+		d->when += timeChanged;
+	}
+	mark_divelist_changed(true);
+	MainWindow::instance()->refreshDisplay();
+}
diff --git a/qt-ui/undocommands.h b/qt-ui/undocommands.h
index 9a7803f..addef81 100644
--- a/qt-ui/undocommands.h
+++ b/qt-ui/undocommands.h
@@ -14,4 +14,15 @@ private:
 	QList<struct dive*> dives;
 };
 
+class UndoShiftTime : public QUndoCommand {
+public:
+	UndoShiftTime(QList<int> diveList, int amount);
+	virtual void undo();
+	virtual void redo();
+
+private:
+	QList<int> dives;
+	int timeChanged;
+};
+
 #endif // UNDOCOMMANDS_H
-- 
2.3.0

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

Reply via email to