summaryrefslogtreecommitdiffstats
path: root/subsurface-core
diff options
context:
space:
mode:
authorGravatar Salvador Cuñat <salvador.cunat@gmail.com>2016-01-09 16:21:08 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2016-01-10 14:11:32 -0800
commit09c854fee030d7f2626a843c45f8e58a19bccb8c (patch)
tree7e843d80e1d8edfbae1bb7def052a53fd5aae8dc /subsurface-core
parentaa6aa416bf1772348b799bc599823ecb44cdcf3e (diff)
downloadsubsurface-09c854fee030d7f2626a843c45f8e58a19bccb8c.tar.gz
Renumber dive list after manual dive merging
As Linus pointed out in mail list, user is forced to manually renumber his dives after doing a merge, unless the merged dives were those at the list tail. This patch try to manage the more usual cases, letting the user to deal with those more complex, based on some assumptions: 1.- We are working on an time ordered list of type: dive_table.nr ... 100 -- 101 -- 102 -- 103 -- 104 ... dive_table.dives.number ... 234 -- 235 -- 236 -- 245 -- 246 ... 2.- It's unlikely to merge no consecutive dives, as merging is time based. 3.- It's unlikely (although possible) to find consecutive dives with no consecutive numbers. 4.- It would be rather bizarre to find that newer dive,of those to merge, has lower number than older. 5.- It can be found that one (or both) dives to merge are zero numbered. 6.- There is only need to renumber from merged dives in advance. A variable, "factor", is fixed before reworking the dive table. This number will be substracted from the original dive number. If we are in point 5.- case, "factor" will be set to zero, meaning that dive numbers will not change (if older dive is zero, merged one will be numbered zero too and will let the user to manage this; if newer dive is zero there won't be need of renumbering as following dives will be correctly numbered, e.g. after splitting a dive which is not at the tail of the table). In most cases, "factor" *should* be set to 1. While renumbering it can be found a dive with it's number set to zero, this won't be changed and will remain zeroed to avoid negative numbers. It, mostly, means that the user has pending work on his dives. I don't know why I've written such a big explanation for such a tiny patch :-) Signed-off-by: Salvador Cuñat <salvador.cunat@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'subsurface-core')
-rw-r--r--subsurface-core/divelist.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/subsurface-core/divelist.c b/subsurface-core/divelist.c
index a2e94c067..7ae5e8373 100644
--- a/subsurface-core/divelist.c
+++ b/subsurface-core/divelist.c
@@ -827,7 +827,7 @@ bool consecutive_selected()
struct dive *merge_two_dives(struct dive *a, struct dive *b)
{
struct dive *res;
- int i, j;
+ int i, j, factor;
int id;
if (!a || !b)
@@ -843,6 +843,7 @@ struct dive *merge_two_dives(struct dive *a, struct dive *b)
if (!res)
return NULL;
+ factor = (a->number == 0 || b->number == 0) ? 0 : abs(b->number - a->number);
add_single_dive(i, res);
delete_single_dive(i + 1);
delete_single_dive(j);
@@ -850,6 +851,13 @@ struct dive *merge_two_dives(struct dive *a, struct dive *b)
// why?
// because this way one of the previously selected ids is still around
res->id = id;
+
+ // renumber dives from merged one in advance by difference between
+ // merged dives numbers. Do not renumber if actual number is zero.
+ for (j; j < dive_table.nr; j++)
+ if (!dive_table.dives[j]->number == 0)
+ dive_table.dives[j]->number -= factor;
+
mark_divelist_changed(true);
return res;
}