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;
75 static stats_t stats_selection;
78 static void process_dive(struct dive *dp, stats_t *stats)
80 int old_tt, sac_time = 0;
83 old_tt = stats->total_time.seconds;
84 stats->total_time.seconds += dp->duration.seconds;
85 if (dp->duration.seconds > stats->longest_time.seconds)
86 stats->longest_time.seconds = dp->duration.seconds;
87 if (stats->shortest_time.seconds == 0 || dp->duration.seconds < stats->shortest_time.seconds)
88 stats->shortest_time.seconds = dp->duration.seconds;
89 if (dp->maxdepth.mm > stats->max_depth.mm)
90 stats->max_depth.mm = dp->maxdepth.mm;
91 if (stats->min_depth.mm == 0 || dp->maxdepth.mm < stats->min_depth.mm)
92 stats->min_depth.mm = dp->maxdepth.mm;
93 stats->avg_depth.mm = (1.0 * old_tt * stats->avg_depth.mm +
94 dp->duration.seconds * dp->meandepth.mm) / stats->total_time.seconds;
95 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
96 int old_sac_time = sac_time;
97 sac_time += dp->duration.seconds;
98 stats->avg_sac.mliter = (1.0 * old_sac_time * stats->avg_sac.mliter +
99 dp->duration.seconds * dp->sac) / sac_time ;
100 if (dp->sac > stats->max_sac.mliter)
101 stats->max_sac.mliter = dp->sac;
102 if (stats->min_sac.mliter == 0 || dp->sac < stats->min_sac.mliter)
103 stats->min_sac.mliter = dp->sac;
105 if (dp->watertemp.mkelvin) {
106 if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
107 stats->min_temp = dp->watertemp.mkelvin;
108 if (dp->watertemp.mkelvin > stats->max_temp)
109 stats->max_temp = dp->watertemp.mkelvin;
110 stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
111 stats->combined_count++;
115 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
121 memset(&stats, 0, sizeof(stats));
122 if (dive_table.nr > 0) {
123 stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
124 stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
125 stats.selection_size = dive_table.nr;
127 /* this relies on the fact that the dives in the dive_table
128 * are in chronological order */
129 for (idx = 0; idx < dive_table.nr; idx++) {
130 dp = dive_table.dives[idx];
131 if (dp->when == dive->when) {
132 /* that's the one we are showing */
134 *prev_dive = dive_table.dives[idx-1];
136 process_dive(dp, &stats);
140 void process_selected_dives(GList *selected_dives, GtkTreeModel *model)
147 memset(&stats_selection, 0, sizeof(stats_selection));
148 stats_selection.selection_size = amount_selected;
150 for (i = 0; i < amount_selected; ++i) {
151 GValue value = {0, };
152 path = g_list_nth_data(selected_dives, i);
153 if (gtk_tree_model_get_iter(model, &iter, path)) {
154 gtk_tree_model_get_value(model, &iter, 0, &value);
155 dp = get_dive(g_value_get_int(&value));
157 process_dive(dp, &stats_selection);
161 static void set_label(GtkWidget *w, const char *fmt, ...)
167 vsnprintf(buf, sizeof(buf), fmt, args);
169 gtk_label_set_text(GTK_LABEL(w), buf);
172 static char * get_time_string(int seconds, int maxdays)
175 if (maxdays && seconds > 3600 * 24 * maxdays)
176 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
178 int days = seconds / 3600 / 24;
179 int hours = (seconds - days * 3600 * 24) / 3600;
180 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
182 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
184 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
189 static void show_single_dive_stats(struct dive *dive)
195 int idx, offset, gas_used;
196 struct dive *prev_dive;
199 process_all_dives(dive, &prev_dive);
201 tm = gmtime(&dive->when);
202 snprintf(buf, sizeof(buf),
203 "%s, %s %d, %d %2d:%02d",
204 weekday(tm->tm_wday),
205 monthname(tm->tm_mon),
206 tm->tm_mday, tm->tm_year + 1900,
207 tm->tm_hour, tm->tm_min);
209 set_label(single_w.date, buf);
210 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
212 set_label(single_w.surf_intv,
213 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
215 set_label(single_w.surf_intv, "unknown");
216 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
217 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
218 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
219 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
220 if (dive->watertemp.mkelvin) {
221 value = get_temp_units(dive->watertemp.mkelvin, &unit);
222 set_label(single_w.water_temp, "%.1f %s", value, unit);
224 set_label(single_w.water_temp, "");
225 value = get_volume_units(dive->sac, &decimals, &unit);
227 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
229 set_label(single_w.sac, "");
230 set_label(single_w.otu, "%d", dive->otu);
234 /* for the O2/He readings just create a list of them */
235 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
236 cylinder_t *cyl = &dive->cylinder[idx];
237 unsigned int start, end;
239 start = cyl->start.mbar ? : cyl->sample_start.mbar;
240 end = cyl->end.mbar ? : cyl->sample_end.mbar;
241 if (!cylinder_none(cyl)) {
242 /* 0% O2 strangely means air, so 21% - I don't like that at all */
243 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
245 snprintf(buf+offset, 80-offset, ", ");
248 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
249 (cyl->gasmix.he.permille + 5) / 10);
250 offset = strlen(buf);
252 /* and if we have size, start and end pressure, we can
253 * calculate the total gas used */
254 if (cyl->type.size.mliter && start && end)
255 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
257 set_label(single_w.o2he, buf);
259 value = get_volume_units(gas_used, &decimals, &unit);
260 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
262 set_label(single_w.gas_used, "");
265 static void show_total_dive_stats(struct dive *dive)
272 if (amount_selected < 2)
275 stats_ptr = &stats_selection;
277 set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
278 if (stats_ptr->min_temp) {
279 value = get_temp_units(stats_ptr->min_temp, &unit);
280 set_label(stats_w.min_temp, "%.1f %s", value, unit);
282 if (stats_ptr->combined_temp && stats_ptr->combined_count)
283 set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
284 if (stats_ptr->max_temp) {
285 value = get_temp_units(stats_ptr->max_temp, &unit);
286 set_label(stats_w.max_temp, "%.1f %s", value, unit);
288 set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
289 set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds / stats_ptr->selection_size, 0));
290 set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
291 set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
292 value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
293 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
294 value = get_depth_units(stats_ptr->min_depth.mm, &decimals, &unit);
295 set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
296 value = get_depth_units(stats_ptr->avg_depth.mm, &decimals, &unit);
297 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
298 value = get_volume_units(stats_ptr->max_sac.mliter, &decimals, &unit);
299 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
300 value = get_volume_units(stats_ptr->min_sac.mliter, &decimals, &unit);
301 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
302 value = get_volume_units(stats_ptr->avg_sac.mliter, &decimals, &unit);
303 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
306 void show_dive_stats(struct dive *dive)
308 /* they have to be called in this order, as 'total' depends on
309 * calculations done in 'single' */
310 show_single_dive_stats(dive);
311 show_total_dive_stats(dive);
314 void flush_dive_stats_changes(struct dive *dive)
316 /* We do nothing: we require the "Ok" button press */
319 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
321 GtkWidget *label_widget;
324 frame = gtk_frame_new(label);
325 label_widget = gtk_label_new(NULL);
326 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
327 gtk_container_add(GTK_CONTAINER(frame), label_widget);
332 GtkWidget *total_stats_widget(void)
334 GtkWidget *vbox, *hbox, *statsframe, *framebox;
336 vbox = gtk_vbox_new(FALSE, 3);
338 statsframe = gtk_frame_new("Statistics");
339 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
340 framebox = gtk_vbox_new(FALSE, 3);
341 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
344 hbox = gtk_hbox_new(FALSE, 3);
345 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
346 stats_w.selection_size = new_info_label_in_frame(hbox, "Dives");
347 stats_w.max_temp = new_info_label_in_frame(hbox, "Max Temp");
348 stats_w.min_temp = new_info_label_in_frame(hbox, "Min Temp");
349 stats_w.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
352 hbox = gtk_hbox_new(FALSE, 3);
353 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
355 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
356 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
357 stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
358 stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
361 hbox = gtk_hbox_new(FALSE, 3);
362 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
364 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
365 stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
366 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
369 hbox = gtk_hbox_new(FALSE, 3);
370 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
372 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
373 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
374 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
379 GtkWidget *single_stats_widget(void)
381 GtkWidget *vbox, *hbox, *infoframe, *framebox;
383 vbox = gtk_vbox_new(FALSE, 3);
385 infoframe = gtk_frame_new("Dive Info");
386 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
387 framebox = gtk_vbox_new(FALSE, 3);
388 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
391 hbox = gtk_hbox_new(FALSE, 3);
392 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
394 single_w.date = new_info_label_in_frame(hbox, "Date");
395 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
396 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
399 hbox = gtk_hbox_new(FALSE, 3);
400 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
402 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
403 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
404 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
407 hbox = gtk_hbox_new(FALSE, 3);
408 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
410 single_w.sac = new_info_label_in_frame(hbox, "SAC");
411 single_w.otu = new_info_label_in_frame(hbox, "OTU");
412 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
413 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");