From 512a2e6b684e237c5e6aacd3cba42fb7d2093e0a Mon Sep 17 00:00:00 2001
From: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Date: Mon, 28 Jan 2019 21:42:59 +0100
Subject: [PATCH] Undo: implement undo of suit editing

This one was trivially modelled after notes editing. Only difference:
the textChanged() signal was replaced by the editingFinished()
signal so that we're not generating undo-commands on every key-press.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
---
 desktop-widgets/command.cpp             |  5 +++++
 desktop-widgets/command.h               |  1 +
 desktop-widgets/command_edit.cpp        | 22 ++++++++++++++++++++++
 desktop-widgets/command_edit.h          |  9 +++++++++
 desktop-widgets/tab-widgets/maintab.cpp | 14 +++++++-------
 desktop-widgets/tab-widgets/maintab.h   |  2 +-
 6 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp
index 7eff9a4c1..7f18b343c 100644
--- a/desktop-widgets/command.cpp
+++ b/desktop-widgets/command.cpp
@@ -140,4 +140,9 @@ void editMode(const QVector<dive *> dives, int index, int newValue, int oldValue
 	execute(new EditMode(dives, index, newValue, oldValue));
 }
 
+void editSuit(const QVector<dive *> dives, const QString &newValue, const QString &oldValue)
+{
+	execute(new EditSuit(dives, newValue, oldValue));
+}
+
 } // namespace Command
diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h
index e1139bd99..d31098a4b 100644
--- a/desktop-widgets/command.h
+++ b/desktop-widgets/command.h
@@ -53,6 +53,7 @@ void purgeUnusedDiveSites();
 // 4) Dive editing related commands
 
 void editNotes(const QVector<dive *> dives, const QString &newValue, const QString &oldValue);
+void editSuit(const QVector<dive *> dives, const QString &newValue, const QString &oldValue);
 void editMode(const QVector<dive *> dives, int index, int newValue, int oldValue);
 
 } // namespace Command
diff --git a/desktop-widgets/command_edit.cpp b/desktop-widgets/command_edit.cpp
index 5ead59998..80113dbdc 100644
--- a/desktop-widgets/command_edit.cpp
+++ b/desktop-widgets/command_edit.cpp
@@ -100,6 +100,28 @@ DiveField EditNotes::fieldId() const
 	return DiveField::NOTES;
 }
 
+// ***** Suit *****
+void EditSuit::set(struct dive *d, QString s) const
+{
+	free(d->suit);
+	d->suit = strdup(qPrintable(s));
+}
+
+QString EditSuit::data(struct dive *d) const
+{
+	return QString(d->suit);
+}
+
+QString EditSuit::fieldName() const
+{
+	return tr("suit");
+}
+
+DiveField EditSuit::fieldId() const
+{
+	return DiveField::SUIT;
+}
+
 // ***** Mode *****
 // Editing the dive mode has very peculiar semantics for historic reasons:
 // Since the dive-mode depends on the dive computer, the i-th dive computer
diff --git a/desktop-widgets/command_edit.h b/desktop-widgets/command_edit.h
index d91121c7d..0209b7d64 100644
--- a/desktop-widgets/command_edit.h
+++ b/desktop-widgets/command_edit.h
@@ -56,6 +56,15 @@ public:
 	DiveField fieldId() const override;
 };
 
+class EditSuit : public EditBase<QString> {
+public:
+	using EditBase<QString>::EditBase;	// Use constructor of base class.
+	void set(struct dive *d, QString s) const override;
+	QString data(struct dive *d) const override;
+	QString fieldName() const override;
+	DiveField fieldId() const override;
+};
+
 class EditMode : public EditBase<int> {
 	int index;
 public:
diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp
index 49cf5312c..962cdc645 100644
--- a/desktop-widgets/tab-widgets/maintab.cpp
+++ b/desktop-widgets/tab-widgets/maintab.cpp
@@ -342,6 +342,9 @@ void MainTab::divesEdited(const QVector<dive *> &, DiveField field)
 		return;
 
 	switch(field) {
+	case DiveField::SUIT:
+		ui.suit->setText(QString(current_dive->suit));
+		break;
 	case DiveField::NOTES:
 		updateNotes(current_dive);
 		break;
@@ -795,8 +798,6 @@ void MainTab::acceptChanges()
 		struct dive *cd = current_dive;
 		// now check if something has changed and if yes, edit the selected dives that
 		// were identical with the master dive shown (and mark the divelist as changed)
-		if (!same_string(displayed_dive.suit, cd->suit))
-			MODIFY_DIVES(selectedDives, EDIT_TEXT(suit));
 		if (displayed_dive.rating != cd->rating)
 			MODIFY_DIVES(selectedDives, EDIT_VALUE(rating));
 		if (displayed_dive.visibility != cd->visibility)
@@ -1343,13 +1344,12 @@ void MainTab::on_diveTripLocation_textEdited(const QString& text)
 	}
 }
 
-void MainTab::on_suit_textChanged(const QString &text)
+void MainTab::on_suit_editingFinished()
 {
-	if (editMode == IGNORE || acceptingEdit == true)
+	if (editMode == IGNORE || acceptingEdit == true || !current_dive)
 		return;
-	free(displayed_dive.suit);
-	displayed_dive.suit = copy_qstring(text);
-	markChangedWidget(ui.suit);
+
+	Command::editSuit(getSelectedDivesCurrentLast(), ui.suit->text(), QString(current_dive->suit));
 }
 
 void MainTab::on_notes_textChanged()
diff --git a/desktop-widgets/tab-widgets/maintab.h b/desktop-widgets/tab-widgets/maintab.h
index 2ac463aec..14a077ba9 100644
--- a/desktop-widgets/tab-widgets/maintab.h
+++ b/desktop-widgets/tab-widgets/maintab.h
@@ -76,7 +76,7 @@ slots:
 	void on_location_textChanged();
 	void on_divemaster_textChanged();
 	void on_buddy_textChanged();
-	void on_suit_textChanged(const QString &text);
+	void on_suit_editingFinished();
 	void on_diveTripLocation_textEdited(const QString& text);
 	void on_notes_textChanged();
 	void on_notes_editingFinished();