summaryrefslogtreecommitdiffstats
path: root/divelist.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2012-11-11 07:49:19 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2012-11-11 10:48:14 +0100
commit8514ec8723542b11a555af2d204a4a7b66b062cc (patch)
treef4217ee9f8254a9c87a021ed989e5088c59873da /divelist.c
parentcda1b73bf68d1802c32416bce3be0ef84137002e (diff)
downloadsubsurface-8514ec8723542b11a555af2d204a4a7b66b062cc.tar.gz
Support merging of two adjacent dives
This introduces the notion of merging two disjoint dives: you can select two dives from the dive list, and if the selection is exactly two dives, and they are adjacent (and share the same dive trip), we support the notion of merging the dives into one dive. The most common reason for this is an extended surface event, which made the dive computer decide that the dive was ended, but maybe you were just waiting for a buddy or a student at the surface, and you want to stitch together two dives into one. There are still details to be sorted out: my Suunto dive computers don't actually do surface samples at the beginning or end of the dive, so when you stitch two dives together, the profile ends up being this odd "a couple of feet under water between the two parts of the dive" thing. But that's an independent thing from the actual merging logic, and I'll work on that separately. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'divelist.c')
-rw-r--r--divelist.c69
1 files changed, 68 insertions, 1 deletions
diff --git a/divelist.c b/divelist.c
index 6aa26c2bd..8dab5b4cb 100644
--- a/divelist.c
+++ b/divelist.c
@@ -1919,7 +1919,7 @@ void merge_trips_cb(GtkWidget *menuitem, GtkTreePath *trippath)
/* this implements the mechanics of removing the dive from the table,
* but doesn't deal with updating dive trips, etc */
-static void delete_single_dive(int idx)
+void delete_single_dive(int idx)
{
int i;
struct dive *dive = get_dive(idx);
@@ -1934,6 +1934,17 @@ static void delete_single_dive(int idx)
free(dive);
}
+void add_single_dive(int idx, struct dive *dive)
+{
+ int i;
+ dive_table.nr++;
+ for (i = idx; i < dive_table.nr ; i++) {
+ struct dive *tmp = dive_table.dives[i];
+ dive_table.dives[i] = dive;
+ dive = tmp;
+ }
+}
+
/* remember expanded state */
static void remember_tree_state()
{
@@ -2070,6 +2081,58 @@ static void delete_dive_cb(GtkWidget *menuitem, GtkTreePath *path)
mark_divelist_changed(TRUE);
}
+static void merge_dive_index(int i, struct dive *a)
+{
+ struct dive *b = get_dive(i+1);
+ struct dive *res;
+
+ res = merge_dives(a, b, b->when - a->when);
+ if (!res)
+ return;
+
+ add_single_dive(i, res);
+ delete_single_dive(i+1);
+ delete_single_dive(i+1);
+
+ dive_list_update_dives();
+ restore_tree_state();
+ mark_divelist_changed(TRUE);
+}
+
+static void merge_dives_cb(GtkWidget *menuitem, void *unused)
+{
+ int i;
+ struct dive *dive;
+
+ for_each_dive(i, dive) {
+ if (dive->selected) {
+ merge_dive_index(i, dive);
+ return;
+ }
+ }
+}
+
+/* Called if there are exactly two selected dives and the dive at idx is one of them */
+static void add_dive_merge_label(int idx, GtkMenuShell *menu)
+{
+ struct dive *d;
+ GtkWidget *menuitem;
+
+ /* The other selected dive must be next to it.. */
+ if (!((d = get_dive(idx-1)) && d->selected) &&
+ !((d = get_dive(idx+1)) && d->selected))
+ return;
+
+ /* .. and they had better be in the same dive trip */
+ if (d->divetrip != get_dive(idx)->divetrip)
+ return;
+
+ /* If so, we can add a "merge dive" menu entry */
+ menuitem = gtk_menu_item_new_with_label(_("Merge dives"));
+ g_signal_connect(menuitem, "activate", G_CALLBACK(merge_dives_cb), NULL);
+ gtk_menu_shell_append(menu, menuitem);
+}
+
static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int button, GdkEventButton *event)
{
GtkWidget *menu, *menuitem, *image;
@@ -2142,6 +2205,10 @@ static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int
menuitem = gtk_menu_item_new_with_label(editlabel);
g_signal_connect(menuitem, "activate", G_CALLBACK(edit_selected_dives_cb), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
+
+ /* Two contiguous selected dives? */
+ if (amount_selected == 2)
+ add_dive_merge_label(idx, GTK_MENU_SHELL(menu));
} else {
deletelabel = _(deletesinglelabel);
menuitem = gtk_menu_item_new_with_label(deletelabel);