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 /* make sure we skip the selected summary entries */
146 void process_selected_dives(void)
151 memset(&stats_selection, 0, sizeof(stats_selection));
154 for_each_dive(i, dive) {
155 if (dive->selected) {
156 process_dive(dive, &stats_selection);
160 stats_selection.selection_size = nr;
163 static void set_label(GtkWidget *w, const char *fmt, ...)
169 vsnprintf(buf, sizeof(buf), fmt, args);
171 gtk_label_set_text(GTK_LABEL(w), buf);
174 static char * get_time_string(int seconds, int maxdays)
177 if (maxdays && seconds > 3600 * 24 * maxdays)
178 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
180 int days = seconds / 3600 / 24;
181 int hours = (seconds - days * 3600 * 24) / 3600;
182 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
184 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
186 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
191 static void show_single_dive_stats(struct dive *dive)
197 int idx, offset, gas_used;
198 struct dive *prev_dive;
201 process_all_dives(dive, &prev_dive);
203 tm = gmtime(&dive->when);
204 snprintf(buf, sizeof(buf),
205 "%s, %s %d, %d %2d:%02d",
206 weekday(tm->tm_wday),
207 monthname(tm->tm_mon),
208 tm->tm_mday, tm->tm_year + 1900,
209 tm->tm_hour, tm->tm_min);
211 set_label(single_w.date, buf);
212 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
214 set_label(single_w.surf_intv,
215 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
217 set_label(single_w.surf_intv, "unknown");
218 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
219 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
220 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
221 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
222 if (dive->watertemp.mkelvin) {
223 value = get_temp_units(dive->watertemp.mkelvin, &unit);
224 set_label(single_w.water_temp, "%.1f %s", value, unit);
226 set_label(single_w.water_temp, "");
227 value = get_volume_units(dive->sac, &decimals, &unit);
229 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
231 set_label(single_w.sac, "");
232 set_label(single_w.otu, "%d", dive->otu);
236 /* for the O2/He readings just create a list of them */
237 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
238 cylinder_t *cyl = &dive->cylinder[idx];
239 unsigned int start, end;
241 start = cyl->start.mbar ? : cyl->sample_start.mbar;
242 end = cyl->end.mbar ? : cyl->sample_end.mbar;
243 if (!cylinder_none(cyl)) {
244 /* 0% O2 strangely means air, so 21% - I don't like that at all */
245 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
247 snprintf(buf+offset, 80-offset, ", ");
250 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
251 (cyl->gasmix.he.permille + 5) / 10);
252 offset = strlen(buf);
254 /* and if we have size, start and end pressure, we can
255 * calculate the total gas used */
256 if (cyl->type.size.mliter && start && end)
257 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
259 set_label(single_w.o2he, buf);
261 value = get_volume_units(gas_used, &decimals, &unit);
262 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
264 set_label(single_w.gas_used, "");
267 static void show_total_dive_stats(struct dive *dive)
270 int decimals, seconds;
274 stats_ptr = &stats_selection;
276 set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
277 if (stats_ptr->min_temp) {
278 value = get_temp_units(stats_ptr->min_temp, &unit);
279 set_label(stats_w.min_temp, "%.1f %s", value, unit);
281 if (stats_ptr->combined_temp && stats_ptr->combined_count)
282 set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
283 if (stats_ptr->max_temp) {
284 value = get_temp_units(stats_ptr->max_temp, &unit);
285 set_label(stats_w.max_temp, "%.1f %s", value, unit);
287 set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
288 seconds = stats_ptr->total_time.seconds;
289 if (stats_ptr->selection_size)
290 seconds /= stats_ptr->selection_size;
291 set_label(stats_w.avg_time, get_time_string(seconds, 0));
292 set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
293 set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
294 value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
295 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
296 value = get_depth_units(stats_ptr->min_depth.mm, &decimals, &unit);
297 set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
298 value = get_depth_units(stats_ptr->avg_depth.mm, &decimals, &unit);
299 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
300 value = get_volume_units(stats_ptr->max_sac.mliter, &decimals, &unit);
301 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
302 value = get_volume_units(stats_ptr->min_sac.mliter, &decimals, &unit);
303 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
304 value = get_volume_units(stats_ptr->avg_sac.mliter, &decimals, &unit);
305 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
308 void show_dive_stats(struct dive *dive)
310 /* they have to be called in this order, as 'total' depends on
311 * calculations done in 'single' */
312 show_single_dive_stats(dive);
313 show_total_dive_stats(dive);
316 void flush_dive_stats_changes(struct dive *dive)
318 /* We do nothing: we require the "Ok" button press */
321 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
323 GtkWidget *label_widget;
326 frame = gtk_frame_new(label);
327 label_widget = gtk_label_new(NULL);
328 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
329 gtk_container_add(GTK_CONTAINER(frame), label_widget);
334 GtkWidget *total_stats_widget(void)
336 GtkWidget *vbox, *hbox, *statsframe, *framebox;
338 vbox = gtk_vbox_new(FALSE, 3);
340 statsframe = gtk_frame_new("Statistics");
341 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
342 framebox = gtk_vbox_new(FALSE, 3);
343 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
346 hbox = gtk_hbox_new(FALSE, 3);
347 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
348 stats_w.selection_size = new_info_label_in_frame(hbox, "Dives");
349 stats_w.max_temp = new_info_label_in_frame(hbox, "Max Temp");
350 stats_w.min_temp = new_info_label_in_frame(hbox, "Min Temp");
351 stats_w.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
354 hbox = gtk_hbox_new(FALSE, 3);
355 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
357 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
358 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
359 stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
360 stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
363 hbox = gtk_hbox_new(FALSE, 3);
364 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
366 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
367 stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
368 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
371 hbox = gtk_hbox_new(FALSE, 3);
372 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
374 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
375 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
376 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
381 GtkWidget *single_stats_widget(void)
383 GtkWidget *vbox, *hbox, *infoframe, *framebox;
385 vbox = gtk_vbox_new(FALSE, 3);
387 infoframe = gtk_frame_new("Dive Info");
388 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
389 framebox = gtk_vbox_new(FALSE, 3);
390 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
393 hbox = gtk_hbox_new(FALSE, 3);
394 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
396 single_w.date = new_info_label_in_frame(hbox, "Date");
397 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
398 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
401 hbox = gtk_hbox_new(FALSE, 3);
402 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
404 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
405 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
406 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
409 hbox = gtk_hbox_new(FALSE, 3);
410 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
412 single_w.sac = new_info_label_in_frame(hbox, "SAC");
413 single_w.otu = new_info_label_in_frame(hbox, "OTU");
414 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
415 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");