[PATCH] Show statistics of selected dives

Miika Turkia miika.turkia at gmail.com
Mon Mar 12 12:18:23 PDT 2012


If at least 2 dives are selected, show statistics of these dives. This
includes temperatures, depths, durations and air consumption.

Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
 Makefile      |    4 +-
 display-gtk.h |    1 +
 divelist.c    |    3 +
 gtk-gui.c     |    4 +
 statistics.c  |  180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 statistics.h  |    4 +
 6 files changed, 194 insertions(+), 2 deletions(-)
 create mode 100644 statistics.h

diff --git a/Makefile b/Makefile
index 20dff4c..19fccfc 100644
--- a/Makefile
+++ b/Makefile
@@ -179,10 +179,10 @@ info.o: info.c dive.h display.h display-gtk.h divelist.h
 equipment.o: equipment.c dive.h display.h divelist.h
 	$(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) -c equipment.c
 
-statistics.o: statistics.c dive.h display.h divelist.h
+statistics.o: statistics.c dive.h display.h divelist.h statistics.h
 	$(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) -c statistics.c
 
-divelist.o: divelist.c dive.h display.h divelist.h
+divelist.o: divelist.c dive.h display.h divelist.h statistics.h
 	$(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) -c divelist.c
 
 print.o: print.c dive.h display.h display-gtk.h
diff --git a/display-gtk.h b/display-gtk.h
index f43e374..abd9ee9 100644
--- a/display-gtk.h
+++ b/display-gtk.h
@@ -62,6 +62,7 @@ extern GtkWidget *dive_info_frame(void);
 extern GtkWidget *extended_dive_info_widget(void);
 extern GtkWidget *equipment_widget(void);
 extern GtkWidget *single_stats_widget(void);
+extern GtkWidget *selection_stats_widget(void);
 extern GtkWidget *total_stats_widget(void);
 extern GtkWidget *cylinder_list_widget(void);
 
diff --git a/divelist.c b/divelist.c
index b9388f8..9b52e86 100644
--- a/divelist.c
+++ b/divelist.c
@@ -20,6 +20,7 @@
 #include "dive.h"
 #include "display.h"
 #include "display-gtk.h"
+#include "statistics.h"
 
 struct DiveList {
 	GtkWidget    *tree_view;
@@ -86,6 +87,8 @@ static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
 		  * is the most intuitive solution.
 		  * I do however want to keep around which dives have
 		  * been selected */
+		process_selected_dives(selected_dives, model);
+		repaint_dive();
 		return;
 	}
 }
diff --git a/gtk-gui.c b/gtk-gui.c
index 5b89265..3d111cc 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -744,6 +744,10 @@ void init_ui(int *argcp, char ***argvp)
 	nb_page = single_stats_widget();
 	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
 
+	/* Frame for selection dive statistics */
+	nb_page = selection_stats_widget();
+	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Selection Stats"));
+
 	/* Frame for total dive statistics */
 	nb_page = total_stats_widget();
 	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Overall Stats"));
diff --git a/statistics.c b/statistics.c
index 2669d53..076b55b 100644
--- a/statistics.c
+++ b/statistics.c
@@ -18,6 +18,7 @@
 #include "display.h"
 #include "display-gtk.h"
 #include "divelist.h"
+#include "statistics.h"
 
 typedef struct {
 	GtkWidget *date,
@@ -50,6 +51,25 @@ typedef struct {
 static total_stats_widget_t stats_w;
 
 typedef struct {
+	GtkWidget *selection_size,
+		*total_time,
+		*avg_time,
+		*shortest_time,
+		*longest_time,
+		*max_overall_depth,
+		*min_overall_depth,
+		*avg_overall_depth,
+		*min_sac,
+		*avg_sac,
+		*max_sac,
+		*max_temp,
+		*avg_temp,
+		*min_temp;
+} selection_stats_widget_t;
+
+static selection_stats_widget_t stats_s;
+
+typedef struct {
 	duration_t total_time;
 	/* avg_time is simply total_time / nr -- let's not keep this */
 	duration_t shortest_time;
@@ -64,6 +84,27 @@ typedef struct {
 
 static stats_t stats;
 
+typedef struct {
+	duration_t total_time;
+	/* avg_time is simply total_time / nr -- let's not keep this */
+	duration_t shortest_time;
+	duration_t longest_time;
+	depth_t max_depth;
+	depth_t min_depth;
+	depth_t avg_depth;
+	volume_t max_sac;
+	volume_t min_sac;
+	volume_t avg_sac;
+	int max_temp;
+	int min_temp;
+	char *temp_unit;
+} selection_stats_t;
+
+static selection_stats_t stats_selection;
+guint selection_size = 1;
+unsigned int combined_temp = 0;
+unsigned int combined_count = 0;
+
 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
 {
 	int idx;
@@ -110,6 +151,64 @@ static void process_all_dives(struct dive *dive, struct dive **prev_dive)
 	}
 }
 
+void process_selected_dives(GList *selected_dives, GtkTreeModel *model)
+{
+	struct dive *dp;
+	int old_tt, sac_time = 0;
+	unsigned int iter_dive;
+	guint i;
+	GtkTreeIter iter;
+	GtkTreePath *path;
+	const char *unit;
+
+	memset(&stats_selection, 0, sizeof(stats_selection));
+	selection_size = g_list_length(selected_dives);
+	combined_temp = 0;
+	combined_count = 0;
+
+	for (i = 0; i < g_list_length(selected_dives); ++i) {
+		GValue value = {0, };
+		path = g_list_nth_data(selected_dives, i);
+		if (gtk_tree_model_get_iter(model, &iter, path)) {
+			gtk_tree_model_get_value(model, &iter, 0, &value);
+			iter_dive = g_value_get_int(&value);
+			dp = get_dive(iter_dive);
+			old_tt = stats_selection.total_time.seconds;
+			stats_selection.total_time.seconds += dp->duration.seconds;
+			if (dp->duration.seconds > stats_selection.longest_time.seconds)
+				stats_selection.longest_time.seconds = dp->duration.seconds;
+			if (stats_selection.shortest_time.seconds == 0 || dp->duration.seconds < stats_selection.shortest_time.seconds)
+				stats_selection.shortest_time.seconds = dp->duration.seconds;
+			if (dp->maxdepth.mm > stats_selection.max_depth.mm)
+				stats_selection.max_depth.mm = dp->maxdepth.mm;
+			if (stats_selection.min_depth.mm == 0 || dp->maxdepth.mm < stats_selection.min_depth.mm)
+				stats_selection.min_depth.mm = dp->maxdepth.mm;
+			stats_selection.avg_depth.mm = (1.0 * old_tt * stats_selection.avg_depth.mm +
+					dp->duration.seconds * dp->meandepth.mm) / stats_selection.total_time.seconds;
+			if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
+				int old_sac_time = sac_time;
+				sac_time += dp->duration.seconds;
+				stats_selection.avg_sac.mliter = (1.0 * old_sac_time * stats_selection.avg_sac.mliter +
+						dp->duration.seconds * dp->sac) / sac_time ;
+				if (dp->sac > stats_selection.max_sac.mliter)
+					stats_selection.max_sac.mliter = dp->sac;
+				if (stats_selection.min_sac.mliter == 0 || dp->sac < stats_selection.min_sac.mliter)
+					stats_selection.min_sac.mliter = dp->sac;
+			}
+			if (dp->watertemp.mkelvin) {
+				if (stats_selection.min_temp == 0 || dp->watertemp.mkelvin < stats_selection.min_temp)
+					stats_selection.min_temp = dp->watertemp.mkelvin;
+				if (dp->watertemp.mkelvin > stats_selection.max_temp)
+					stats_selection.max_temp = dp->watertemp.mkelvin;
+				combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
+				combined_count++;
+			}
+		}
+
+
+	}
+}
+
 static void set_label(GtkWidget *w, const char *fmt, ...)
 {
 	char buf[80];
@@ -238,11 +337,47 @@ static void show_total_dive_stats(struct dive *dive)
 	set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
 }
 
+static void show_selection_dive_stats()
+{
+	double value;
+	int decimals;
+	const char *unit;
+
+	set_label(stats_s.selection_size, "%d", selection_size);
+	if (stats_selection.min_temp) {
+		value = get_temp_units(stats_selection.min_temp, &unit);
+		set_label(stats_s.min_temp, "%.1f %s", value, unit);
+	}
+	if (combined_temp && combined_count)
+		set_label(stats_s.avg_temp, "%.1f %s", combined_temp / (combined_count * 1.0), unit);
+	if (stats_selection.max_temp) {
+		value = get_temp_units(stats_selection.max_temp, &unit);
+		set_label(stats_s.max_temp, "%.1f %s", value, unit);
+	}
+	set_label(stats_s.total_time, get_time_string(stats_selection.total_time.seconds, 0));
+	set_label(stats_s.avg_time, get_time_string(stats_selection.total_time.seconds / selection_size, 0));
+	set_label(stats_s.longest_time, get_time_string(stats_selection.longest_time.seconds, 0));
+	set_label(stats_s.shortest_time, get_time_string(stats_selection.shortest_time.seconds, 0));
+	value = get_depth_units(stats_selection.max_depth.mm, &decimals, &unit);
+	set_label(stats_s.max_overall_depth, "%.*f %s", decimals, value, unit);
+	value = get_depth_units(stats_selection.min_depth.mm, &decimals, &unit);
+	set_label(stats_s.min_overall_depth, "%.*f %s", decimals, value, unit);
+	value = get_depth_units(stats_selection.avg_depth.mm, &decimals, &unit);
+	set_label(stats_s.avg_overall_depth, "%.*f %s", decimals, value, unit);
+	value = get_volume_units(stats_selection.max_sac.mliter, &decimals, &unit);
+	set_label(stats_s.max_sac, "%.*f %s/min", decimals, value, unit);
+	value = get_volume_units(stats_selection.min_sac.mliter, &decimals, &unit);
+	set_label(stats_s.min_sac, "%.*f %s/min", decimals, value, unit);
+	value = get_volume_units(stats_selection.avg_sac.mliter, &decimals, &unit);
+	set_label(stats_s.avg_sac, "%.*f %s/min", decimals, value, unit);
+}
+
 void show_dive_stats(struct dive *dive)
 {
 	/* they have to be called in this order, as 'total' depends on
 	 * calculations done in 'single' */
 	show_single_dive_stats(dive);
+	show_selection_dive_stats();
 	show_total_dive_stats(dive);
 }
 
@@ -303,6 +438,51 @@ GtkWidget *total_stats_widget(void)
 	return vbox;
 }
 
+GtkWidget *selection_stats_widget(void)
+{
+	GtkWidget *vbox, *hbox, *statsframe, *framebox;
+
+	vbox = gtk_vbox_new(FALSE, 3);
+
+	statsframe = gtk_frame_new("Statistics");
+	gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
+	framebox = gtk_vbox_new(FALSE, 3);
+	gtk_container_add(GTK_CONTAINER(statsframe), framebox);
+
+	hbox = gtk_hbox_new(FALSE, 3);
+	gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
+	stats_s.selection_size = new_info_label_in_frame(hbox, "Dives");
+	stats_s.max_temp = new_info_label_in_frame(hbox, "Max Temp");
+	stats_s.min_temp = new_info_label_in_frame(hbox, "Min Temp");
+	stats_s.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
+	/* first row */
+	hbox = gtk_hbox_new(FALSE, 3);
+	gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
+
+	stats_s.total_time = new_info_label_in_frame(hbox, "Total Time");
+	stats_s.avg_time = new_info_label_in_frame(hbox, "Avg Time");
+	stats_s.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
+	stats_s.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
+
+	/* second row */
+	hbox = gtk_hbox_new(FALSE, 3);
+	gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
+
+	stats_s.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
+	stats_s.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
+	stats_s.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
+
+	/* third row */
+	hbox = gtk_hbox_new(FALSE, 3);
+	gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
+
+	stats_s.max_sac = new_info_label_in_frame(hbox, "Max SAC");
+	stats_s.min_sac = new_info_label_in_frame(hbox, "Min SAC");
+	stats_s.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
+
+	return vbox;
+}
+
 GtkWidget *single_stats_widget(void)
 {
 	GtkWidget *vbox, *hbox, *infoframe, *framebox;
diff --git a/statistics.h b/statistics.h
new file mode 100644
index 0000000..87d2e75
--- /dev/null
+++ b/statistics.h
@@ -0,0 +1,4 @@
+#ifndef STATISTICS_H
+#define STATISTICS_H
+extern void process_selected_dives(GList *, GtkTreeModel *);
+#endif
-- 
1.7.5.4



More information about the subsurface mailing list