2 /* creates the UI for the Info & Stats page -
3 * controlled through the following interfaces:
5 * void show_dive_stats(struct dive *dive)
6 * void flush_dive_stats_changes(struct dive *dive)
9 * GtkWidget *stats_widget(void)
19 #include "display-gtk.h"
33 } single_stat_widget_t;
35 static single_stat_widget_t single_w;
38 GtkWidget *total_time,
52 } total_stats_widget_t;
54 static total_stats_widget_t stats_w;
57 duration_t total_time;
58 /* avg_time is simply total_time / nr -- let's not keep this */
59 duration_t shortest_time;
60 duration_t longest_time;
69 unsigned int combined_temp;
70 unsigned int combined_count;
71 unsigned int selection_size;
72 unsigned int total_sac_time;
76 static stats_t stats_selection;
79 static void process_dive(struct dive *dp, stats_t *stats)
81 int old_tt, sac_time = 0;
84 old_tt = stats->total_time.seconds;
85 stats->total_time.seconds += dp->duration.seconds;
86 if (dp->duration.seconds > stats->longest_time.seconds)
87 stats->longest_time.seconds = dp->duration.seconds;
88 if (stats->shortest_time.seconds == 0 || dp->duration.seconds < stats->shortest_time.seconds)
89 stats->shortest_time.seconds = dp->duration.seconds;
90 if (dp->maxdepth.mm > stats->max_depth.mm)
91 stats->max_depth.mm = dp->maxdepth.mm;
92 if (stats->min_depth.mm == 0 || dp->maxdepth.mm < stats->min_depth.mm)
93 stats->min_depth.mm = dp->maxdepth.mm;
94 if (dp->watertemp.mkelvin) {
95 if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
96 stats->min_temp = dp->watertemp.mkelvin;
97 if (dp->watertemp.mkelvin > stats->max_temp)
98 stats->max_temp = dp->watertemp.mkelvin;
99 stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
100 stats->combined_count++;
103 /* Maybe we should drop zero-duration dives */
104 if (!dp->duration.seconds)
106 stats->avg_depth.mm = (1.0 * old_tt * stats->avg_depth.mm +
107 dp->duration.seconds * dp->meandepth.mm) / stats->total_time.seconds;
108 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
109 sac_time = stats->total_sac_time + dp->duration.seconds;
110 stats->avg_sac.mliter = (1.0 * stats->total_sac_time * stats->avg_sac.mliter +
111 dp->duration.seconds * dp->sac) / sac_time ;
112 if (dp->sac > stats->max_sac.mliter)
113 stats->max_sac.mliter = dp->sac;
114 if (stats->min_sac.mliter == 0 || dp->sac < stats->min_sac.mliter)
115 stats->min_sac.mliter = dp->sac;
116 stats->total_sac_time = sac_time;
120 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
126 memset(&stats, 0, sizeof(stats));
127 if (dive_table.nr > 0) {
128 stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
129 stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
130 stats.selection_size = dive_table.nr;
132 /* this relies on the fact that the dives in the dive_table
133 * are in chronological order */
134 for (idx = 0; idx < dive_table.nr; idx++) {
135 dp = dive_table.dives[idx];
136 if (dp->when == dive->when) {
137 /* that's the one we are showing */
139 *prev_dive = dive_table.dives[idx-1];
141 process_dive(dp, &stats);
145 void process_selected_dives(GList *selected_dives, int *selectiontracker, GtkTreeModel *model)
153 memset(&stats_selection, 0, sizeof(stats_selection));
154 stats_selection.selection_size = amount_selected;
156 for (i = 0; i < amount_selected; ++i) {
157 GValue value = {0, };
158 path = g_list_nth_data(selected_dives, i);
159 if (gtk_tree_model_get_iter(model, &iter, path)) {
160 gtk_tree_model_get_value(model, &iter, 0, &value);
161 idx = g_value_get_int(&value);
164 selectiontracker[i] = idx;
165 process_dive(dp, &stats_selection);
171 static void set_label(GtkWidget *w, const char *fmt, ...)
177 vsnprintf(buf, sizeof(buf), fmt, args);
179 gtk_label_set_text(GTK_LABEL(w), buf);
182 static char * get_time_string(int seconds, int maxdays)
185 if (maxdays && seconds > 3600 * 24 * maxdays)
186 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
188 int days = seconds / 3600 / 24;
189 int hours = (seconds - days * 3600 * 24) / 3600;
190 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
192 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
194 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
199 static void show_single_dive_stats(struct dive *dive)
205 int idx, offset, gas_used;
206 struct dive *prev_dive;
209 process_all_dives(dive, &prev_dive);
211 tm = gmtime(&dive->when);
212 snprintf(buf, sizeof(buf),
213 "%s, %s %d, %d %2d:%02d",
214 weekday(tm->tm_wday),
215 monthname(tm->tm_mon),
216 tm->tm_mday, tm->tm_year + 1900,
217 tm->tm_hour, tm->tm_min);
219 set_label(single_w.date, buf);
220 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
222 set_label(single_w.surf_intv,
223 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
225 set_label(single_w.surf_intv, "unknown");
226 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
227 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
228 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
229 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
230 if (dive->watertemp.mkelvin) {
231 value = get_temp_units(dive->watertemp.mkelvin, &unit);
232 set_label(single_w.water_temp, "%.1f %s", value, unit);
234 set_label(single_w.water_temp, "");
235 value = get_volume_units(dive->sac, &decimals, &unit);
237 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
239 set_label(single_w.sac, "");
240 set_label(single_w.otu, "%d", dive->otu);
244 /* for the O2/He readings just create a list of them */
245 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
246 cylinder_t *cyl = &dive->cylinder[idx];
247 unsigned int start, end;
249 start = cyl->start.mbar ? : cyl->sample_start.mbar;
250 end = cyl->end.mbar ? : cyl->sample_end.mbar;
251 if (!cylinder_none(cyl)) {
252 /* 0% O2 strangely means air, so 21% - I don't like that at all */
253 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
255 snprintf(buf+offset, 80-offset, ", ");
258 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
259 (cyl->gasmix.he.permille + 5) / 10);
260 offset = strlen(buf);
262 /* and if we have size, start and end pressure, we can
263 * calculate the total gas used */
264 if (cyl->type.size.mliter && start && end)
265 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
267 set_label(single_w.o2he, buf);
269 value = get_volume_units(gas_used, &decimals, &unit);
270 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
272 set_label(single_w.gas_used, "");
275 static void show_total_dive_stats(struct dive *dive)
278 int decimals, seconds;
282 if (amount_selected < 2)
285 stats_ptr = &stats_selection;
287 set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
288 if (stats_ptr->min_temp) {
289 value = get_temp_units(stats_ptr->min_temp, &unit);
290 set_label(stats_w.min_temp, "%.1f %s", value, unit);
292 if (stats_ptr->combined_temp && stats_ptr->combined_count)
293 set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
294 if (stats_ptr->max_temp) {
295 value = get_temp_units(stats_ptr->max_temp, &unit);
296 set_label(stats_w.max_temp, "%.1f %s", value, unit);
298 set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
299 seconds = stats_ptr->total_time.seconds;
300 if (stats_ptr->selection_size)
301 seconds /= stats_ptr->selection_size;
302 set_label(stats_w.avg_time, get_time_string(seconds, 0));
303 set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
304 set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
305 value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
306 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
307 value = get_depth_units(stats_ptr->min_depth.mm, &decimals, &unit);
308 set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
309 value = get_depth_units(stats_ptr->avg_depth.mm, &decimals, &unit);
310 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
311 value = get_volume_units(stats_ptr->max_sac.mliter, &decimals, &unit);
312 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
313 value = get_volume_units(stats_ptr->min_sac.mliter, &decimals, &unit);
314 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
315 value = get_volume_units(stats_ptr->avg_sac.mliter, &decimals, &unit);
316 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
319 void show_dive_stats(struct dive *dive)
321 /* they have to be called in this order, as 'total' depends on
322 * calculations done in 'single' */
323 show_single_dive_stats(dive);
324 show_total_dive_stats(dive);
327 void flush_dive_stats_changes(struct dive *dive)
329 /* We do nothing: we require the "Ok" button press */
332 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
334 GtkWidget *label_widget;
337 frame = gtk_frame_new(label);
338 label_widget = gtk_label_new(NULL);
339 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
340 gtk_container_add(GTK_CONTAINER(frame), label_widget);
345 GtkWidget *total_stats_widget(void)
347 GtkWidget *vbox, *hbox, *statsframe, *framebox;
349 vbox = gtk_vbox_new(FALSE, 3);
351 statsframe = gtk_frame_new("Statistics");
352 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
353 framebox = gtk_vbox_new(FALSE, 3);
354 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
357 hbox = gtk_hbox_new(FALSE, 3);
358 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
359 stats_w.selection_size = new_info_label_in_frame(hbox, "Dives");
360 stats_w.max_temp = new_info_label_in_frame(hbox, "Max Temp");
361 stats_w.min_temp = new_info_label_in_frame(hbox, "Min Temp");
362 stats_w.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
365 hbox = gtk_hbox_new(FALSE, 3);
366 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
368 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
369 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
370 stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
371 stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
374 hbox = gtk_hbox_new(FALSE, 3);
375 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
377 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
378 stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
379 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
382 hbox = gtk_hbox_new(FALSE, 3);
383 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
385 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
386 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
387 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
392 GtkWidget *single_stats_widget(void)
394 GtkWidget *vbox, *hbox, *infoframe, *framebox;
396 vbox = gtk_vbox_new(FALSE, 3);
398 infoframe = gtk_frame_new("Dive Info");
399 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
400 framebox = gtk_vbox_new(FALSE, 3);
401 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
404 hbox = gtk_hbox_new(FALSE, 3);
405 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
407 single_w.date = new_info_label_in_frame(hbox, "Date");
408 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
409 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
412 hbox = gtk_hbox_new(FALSE, 3);
413 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
415 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
416 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
417 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
420 hbox = gtk_hbox_new(FALSE, 3);
421 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
423 single_w.sac = new_info_label_in_frame(hbox, "SAC");
424 single_w.otu = new_info_label_in_frame(hbox, "OTU");
425 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
426 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");