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(GList *selected_dives, int *selectiontracker, GtkTreeModel *model)
152 memset(&stats_selection, 0, sizeof(stats_selection));
154 for (i = 0; i < amount_selected; ++i) {
155 idx = selectiontracker[i];
159 process_dive(dp, &stats_selection);
163 stats_selection.selection_size = amount_selected;
166 static void set_label(GtkWidget *w, const char *fmt, ...)
172 vsnprintf(buf, sizeof(buf), fmt, args);
174 gtk_label_set_text(GTK_LABEL(w), buf);
177 static char * get_time_string(int seconds, int maxdays)
180 if (maxdays && seconds > 3600 * 24 * maxdays)
181 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
183 int days = seconds / 3600 / 24;
184 int hours = (seconds - days * 3600 * 24) / 3600;
185 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
187 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
189 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
194 static void show_single_dive_stats(struct dive *dive)
200 int idx, offset, gas_used;
201 struct dive *prev_dive;
204 process_all_dives(dive, &prev_dive);
206 tm = gmtime(&dive->when);
207 snprintf(buf, sizeof(buf),
208 "%s, %s %d, %d %2d:%02d",
209 weekday(tm->tm_wday),
210 monthname(tm->tm_mon),
211 tm->tm_mday, tm->tm_year + 1900,
212 tm->tm_hour, tm->tm_min);
214 set_label(single_w.date, buf);
215 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
217 set_label(single_w.surf_intv,
218 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
220 set_label(single_w.surf_intv, "unknown");
221 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
222 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
223 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
224 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
225 if (dive->watertemp.mkelvin) {
226 value = get_temp_units(dive->watertemp.mkelvin, &unit);
227 set_label(single_w.water_temp, "%.1f %s", value, unit);
229 set_label(single_w.water_temp, "");
230 value = get_volume_units(dive->sac, &decimals, &unit);
232 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
234 set_label(single_w.sac, "");
235 set_label(single_w.otu, "%d", dive->otu);
239 /* for the O2/He readings just create a list of them */
240 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
241 cylinder_t *cyl = &dive->cylinder[idx];
242 unsigned int start, end;
244 start = cyl->start.mbar ? : cyl->sample_start.mbar;
245 end = cyl->end.mbar ? : cyl->sample_end.mbar;
246 if (!cylinder_none(cyl)) {
247 /* 0% O2 strangely means air, so 21% - I don't like that at all */
248 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
250 snprintf(buf+offset, 80-offset, ", ");
253 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
254 (cyl->gasmix.he.permille + 5) / 10);
255 offset = strlen(buf);
257 /* and if we have size, start and end pressure, we can
258 * calculate the total gas used */
259 if (cyl->type.size.mliter && start && end)
260 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
262 set_label(single_w.o2he, buf);
264 value = get_volume_units(gas_used, &decimals, &unit);
265 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
267 set_label(single_w.gas_used, "");
270 static void show_total_dive_stats(struct dive *dive)
273 int decimals, seconds;
277 if (amount_selected < 2)
280 stats_ptr = &stats_selection;
282 set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
283 if (stats_ptr->min_temp) {
284 value = get_temp_units(stats_ptr->min_temp, &unit);
285 set_label(stats_w.min_temp, "%.1f %s", value, unit);
287 if (stats_ptr->combined_temp && stats_ptr->combined_count)
288 set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
289 if (stats_ptr->max_temp) {
290 value = get_temp_units(stats_ptr->max_temp, &unit);
291 set_label(stats_w.max_temp, "%.1f %s", value, unit);
293 set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
294 seconds = stats_ptr->total_time.seconds;
295 if (stats_ptr->selection_size)
296 seconds /= stats_ptr->selection_size;
297 set_label(stats_w.avg_time, get_time_string(seconds, 0));
298 set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
299 set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
300 value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
301 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
302 value = get_depth_units(stats_ptr->min_depth.mm, &decimals, &unit);
303 set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
304 value = get_depth_units(stats_ptr->avg_depth.mm, &decimals, &unit);
305 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
306 value = get_volume_units(stats_ptr->max_sac.mliter, &decimals, &unit);
307 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
308 value = get_volume_units(stats_ptr->min_sac.mliter, &decimals, &unit);
309 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
310 value = get_volume_units(stats_ptr->avg_sac.mliter, &decimals, &unit);
311 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
314 void show_dive_stats(struct dive *dive)
316 /* they have to be called in this order, as 'total' depends on
317 * calculations done in 'single' */
318 show_single_dive_stats(dive);
319 show_total_dive_stats(dive);
322 void flush_dive_stats_changes(struct dive *dive)
324 /* We do nothing: we require the "Ok" button press */
327 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
329 GtkWidget *label_widget;
332 frame = gtk_frame_new(label);
333 label_widget = gtk_label_new(NULL);
334 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
335 gtk_container_add(GTK_CONTAINER(frame), label_widget);
340 GtkWidget *total_stats_widget(void)
342 GtkWidget *vbox, *hbox, *statsframe, *framebox;
344 vbox = gtk_vbox_new(FALSE, 3);
346 statsframe = gtk_frame_new("Statistics");
347 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
348 framebox = gtk_vbox_new(FALSE, 3);
349 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
352 hbox = gtk_hbox_new(FALSE, 3);
353 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
354 stats_w.selection_size = new_info_label_in_frame(hbox, "Dives");
355 stats_w.max_temp = new_info_label_in_frame(hbox, "Max Temp");
356 stats_w.min_temp = new_info_label_in_frame(hbox, "Min Temp");
357 stats_w.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
360 hbox = gtk_hbox_new(FALSE, 3);
361 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
363 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
364 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
365 stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
366 stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
369 hbox = gtk_hbox_new(FALSE, 3);
370 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
372 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
373 stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
374 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
377 hbox = gtk_hbox_new(FALSE, 3);
378 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
380 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
381 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
382 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
387 GtkWidget *single_stats_widget(void)
389 GtkWidget *vbox, *hbox, *infoframe, *framebox;
391 vbox = gtk_vbox_new(FALSE, 3);
393 infoframe = gtk_frame_new("Dive Info");
394 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
395 framebox = gtk_vbox_new(FALSE, 3);
396 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
399 hbox = gtk_hbox_new(FALSE, 3);
400 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
402 single_w.date = new_info_label_in_frame(hbox, "Date");
403 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
404 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
407 hbox = gtk_hbox_new(FALSE, 3);
408 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
410 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
411 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
412 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
415 hbox = gtk_hbox_new(FALSE, 3);
416 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
418 single_w.sac = new_info_label_in_frame(hbox, "SAC");
419 single_w.otu = new_info_label_in_frame(hbox, "OTU");
420 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
421 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");