[PATCH] Show/save weights up to and including last valid

Anton Lundin glance at acc.umu.se
Wed Nov 27 13:59:17 UTC 2013


Previous show and save code would have aborted at the first invalid
weight system. This makes sure we save and show all weight systems up
until and including the last valid.

If we had:
integrated: 1kg
belt: 2kg
ankle: 3kg

And changed belt to 0 kg, we would have only saved integrated 1kg, and
nothing about the belt or the ankle weights. This will save all of them,
and show all of them.

Signed-off-by: Anton Lundin <glance at acc.umu.se>
---
 qt-ui/models.cpp | 14 +++++---------
 save-xml.c       | 21 ++++++++++++++++-----
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 76ad6e1..b8909da 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -543,20 +543,16 @@ void WeightModel::setDive(dive* d)
 {
 	if (current)
 		clear();
-
-	int amount = MAX_WEIGHTSYSTEMS;
+	rows = 0;
 	for(int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
-		weightsystem_t *weightsystem = &d->weightsystem[i];
-		if (weightsystem_none(weightsystem)) {
-			amount = i;
-			break;
+		if (!weightsystem_none(&d->weightsystem[i])) {
+			rows = i+1;
 		}
 	}
-	rows = amount;
 	current = d;
 	changed = false;
-	if (amount > 0) {
-		beginInsertRows(QModelIndex(), 0, amount - 1);
+	if (rows > 0) {
+		beginInsertRows(QModelIndex(), 0, rows-1);
 		endInsertRows();
 	}
 }
diff --git a/save-xml.c b/save-xml.c
index d16b16d..0b83a02 100644
--- a/save-xml.c
+++ b/save-xml.c
@@ -301,18 +301,29 @@ static void save_cylinder_info(FILE *f, struct dive *dive)
 	}
 }
 
+static int nr_weightsystems(struct dive *dive)
+{
+	int nr;
+
+	for (nr = MAX_WEIGHTSYSTEMS; nr; --nr) {
+		weightsystem_t *ws = dive->weightsystem+nr-1;
+		if (!weightsystem_none(ws))
+			break;
+	}
+	return nr;
+}
+
 static void save_weightsystem_info(FILE *f, struct dive *dive)
 {
-	int i;
+	int i, nr;
 
-	for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
+	nr = nr_weightsystems(dive);
+
+	for (i = 0; i < nr; i++) {
 		weightsystem_t *ws = dive->weightsystem+i;
 		int grams = ws->weight.grams;
 		const char *description = ws->description;
 
-		/* No weight information at all? */
-		if (grams == 0)
-			return;
 		fprintf(f, "  <weightsystem");
 		show_milli(f, " weight='", grams, " kg", "'");
 		show_utf8(f, description, " description='", "'", 1);
-- 
1.8.3.2



More information about the subsurface mailing list