Hello,

On Sat, Feb 28, 2015 at 2:44 PM, Dirk Hohndel <[email protected]> wrote:

>
> Well done. Thanks
>

See the attached patch, which implements undo/redo of when merging dives.


>
> /D
>
From 2a22cfc70dbc06c3090988663ac6eb3939b39f01 Mon Sep 17 00:00:00 2001
From: Grace Karanja <[email protected]>
Date: Mon, 9 Mar 2015 14:46:07 +0300
Subject: [PATCH] Add ability to undo merge dives

Expands the undo/redo feature by adding the option to undo/redo
merging of dives. This patch moves some of the merging code to
the qt-ui/undocommands.cpp file.

Signed-off-by: Grace Karanja <[email protected]>
---
 qt-ui/divelistview.cpp | 28 ++---------------------
 qt-ui/undocommands.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 qt-ui/undocommands.h   | 10 +++++++++
 3 files changed, 73 insertions(+), 26 deletions(-)

diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index e4ccb72..bded95e 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -553,34 +553,10 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS
 		Q_EMIT currentDiveChanged(selected_dive);
 }
 
-static bool can_merge(const struct dive *a, const struct dive *b)
-{
-	if (!a || !b)
-		return false;
-	if (a->when > b->when)
-		return false;
-	/* Don't merge dives if there's more than half an hour between them */
-	if (a->when + a->duration.seconds + 30 * 60 < b->when)
-		return false;
-	return true;
-}
-
 void DiveListView::mergeDives()
 {
-	int i;
-	struct dive *dive, *maindive = NULL;
-
-	for_each_dive (i, dive) {
-		if (dive->selected) {
-			if (!can_merge(maindive, dive)) {
-				maindive = dive;
-			} else {
-				maindive = merge_two_dives(maindive, dive);
-				i--; // otherwise we skip a dive in the freshly changed list
-			}
-		}
-	}
-	MainWindow::instance()->refreshDisplay();
+	UndoMergeDives *undoCommand = new UndoMergeDives();
+	MainWindow::instance()->undoStack->push(undoCommand);
 }
 
 void DiveListView::renumberDives()
diff --git a/qt-ui/undocommands.cpp b/qt-ui/undocommands.cpp
index 6a15f7e..012faaf 100644
--- a/qt-ui/undocommands.cpp
+++ b/qt-ui/undocommands.cpp
@@ -93,3 +93,64 @@ void UndoRenumberDives::redo()
 	mark_divelist_changed(true);
 	MainWindow::instance()->refreshDisplay();
 }
+
+static bool can_merge(const struct dive *a, const struct dive *b)
+{
+	if (!a || !b)
+		return false;
+	if (a->when > b->when)
+		return false;
+	/* Don't merge dives if there's more than half an hour between them */
+	if (a->when + a->duration.seconds + 30 * 60 < b->when)
+		return false;
+	return true;
+}
+
+UndoMergeDives::UndoMergeDives()
+{
+	setText("merge dives");
+	int i;
+	struct dive* dive = NULL;
+
+	for_each_dive(i, dive) {
+		if (dive->selected) {
+			struct dive* d = alloc_dive();
+			copy_dive(dive, d);
+			dives.append(d);
+		}
+	}
+}
+
+void UndoMergeDives::undo()
+{
+	delete_single_dive(get_divenr(mainDive));
+
+	for (int i = 0; i < dives.count(); i++) {
+		struct dive* dive = alloc_dive();
+		copy_dive(dives.at(i), dive);
+		record_dive(dive);
+	}
+
+	mark_divelist_changed(true);
+	MainWindow::instance()->refreshDisplay();
+}
+
+void UndoMergeDives::redo()
+{
+	struct dive* newDive = NULL;
+
+	for (int i = 0; i < dives.count(); i++) {
+		struct dive* dive = alloc_dive();
+		dive = get_dive_by_uniq_id(dives.at(i)->id);
+
+		if (!can_merge(newDive, dive)) {
+			newDive = dive;
+		} else {
+			newDive = merge_two_dives(newDive, dive);
+		}
+	}
+
+	mainDive = newDive;
+	mark_divelist_changed(true);
+	MainWindow::instance()->refreshDisplay();
+}
diff --git a/qt-ui/undocommands.h b/qt-ui/undocommands.h
index 36c789f..a3ff9c2 100644
--- a/qt-ui/undocommands.h
+++ b/qt-ui/undocommands.h
@@ -37,4 +37,14 @@ private:
 	int start;
 };
 
+class UndoMergeDives : public QUndoCommand {
+public:
+	UndoMergeDives();
+	virtual void undo();
+	virtual void redo();
+private:
+	QList<struct dive*> dives;
+	struct dive* mainDive;
+};
+
 #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