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 stats->avg_depth.mm = (1.0 * old_tt * stats->avg_depth.mm +
95 dp->duration.seconds * dp->meandepth.mm) / stats->total_time.seconds;
96 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
97 sac_time = stats->total_sac_time + dp->duration.seconds;
98 stats->avg_sac.mliter = (1.0 * stats->total_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;
104 stats->total_sac_time = sac_time;
106 if (dp->watertemp.mkelvin) {
107 if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
108 stats->min_temp = dp->watertemp.mkelvin;
109 if (dp->watertemp.mkelvin > stats->max_temp)
110 stats->max_temp = dp->watertemp.mkelvin;
111 stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
112 stats->combined_count++;
116 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
122 memset(&stats, 0, sizeof(stats));
123 if (dive_table.nr > 0) {
124 stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
125 stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
126 stats.selection_size = dive_table.nr;
128 /* this relies on the fact that the dives in the dive_table
129 * are in chronological order */
130 for (idx = 0; idx < dive_table.nr; idx++) {
131 dp = dive_table.dives[idx];
132 if (dp->when == dive->when) {
133 /* that's the one we are showing */
135 *prev_dive = dive_table.dives[idx-1];
137 process_dive(dp, &stats);
141 void process_selected_dives(GList *selected_dives, GtkTreeModel *model)
148 memset(&stats_selection, 0, sizeof(stats_selection));
149 stats_selection.selection_size = amount_selected;
151 for (i = 0; i < amount_selected; ++i) {
152 GValue value = {0, };
153 path = g_list_nth_data(selected_dives, i);
154 if (gtk_tree_model_get_iter(model, &iter, path)) {
155 gtk_tree_model_get_value(model, &iter, 0, &value);
156 dp = get_dive(g_value_get_int(&value));
158 process_dive(dp, &stats_selection);
162 static void set_label(GtkWidget *w, const char *fmt, ...)
168 vsnprintf(buf, sizeof(buf), fmt, args);
170 gtk_label_set_text(GTK_LABEL(w), buf);
173 static char * get_time_string(int seconds, int maxdays)
176 if (maxdays && seconds > 3600 * 24 * maxdays)
177 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
179 int days = seconds / 3600 / 24;
180 int hours = (seconds - days * 3600 * 24) / 3600;
181 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
183 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
185 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
190 static void show_single_dive_stats(struct dive *dive)
196 int idx, offset, gas_used;
197 struct dive *prev_dive;
200 process_all_dives(dive, &prev_dive);
202 tm = gmtime(&dive->when);
203 snprintf(buf, sizeof(buf),
204 "%s, %s %d, %d %2d:%02d",
205 weekday(tm->tm_wday),
206 monthname(tm->tm_mon),
207 tm->tm_mday, tm->tm_year + 1900,
208 tm->tm_hour, tm->tm_min);
210 set_label(single_w.date, buf);
211 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
213 set_label(single_w.surf_intv,
214 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
216 set_label(single_w.surf_intv, "unknown");
217 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
218 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
219 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
220 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
221 if (dive->watertemp.mkelvin) {
222 value = get_temp_units(dive->watertemp.mkelvin, &unit);
223 set_label(single_w.water_temp, "%.1f %s", value, unit);
225 set_label(single_w.water_temp, "");
226 value = get_volume_units(dive->sac, &decimals, &unit);
228 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
230 set_label(single_w.sac, "");
231 set_label(single_w.otu, "%d", dive->otu);
235 /* for the O2/He readings just create a list of them */
236 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
237 cylinder_t *cyl = &dive->cylinder[idx];
238 unsigned int start, end;
240 start = cyl->start.mbar ? : cyl->sample_start.mbar;
241 end = cyl->end.mbar ? : cyl->sample_end.mbar;
242 if (!cylinder_none(cyl)) {
243 /* 0% O2 strangely means air, so 21% - I don't like that at all */
244 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
246 snprintf(buf+offset, 80-offset, ", ");
249 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
250 (cyl->gasmix.he.permille + 5) / 10);
251 offset = strlen(buf);
253 /* and if we have size, start and end pressure, we can
254 * calculate the total gas used */
255 if (cyl->type.size.mliter && start && end)
256 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
258 set_label(single_w.o2he, buf);
260 value = get_volume_units(gas_used, &decimals, &unit);
261 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
263 set_label(single_w.gas_used, "");
266 static void show_total_dive_stats(struct dive *dive)
273 if (amount_selected < 2)
276 stats_ptr = &stats_selection;
278 set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
279 if (stats_ptr->min_temp) {
280 value = get_temp_units(stats_ptr->min_temp, &unit);
281 set_label(stats_w.min_temp, "%.1f %s", value, unit);
283 if (stats_ptr->combined_temp && stats_ptr->combined_count)
284 set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
285 if (stats_ptr->max_temp) {
286 value = get_temp_units(stats_ptr->max_temp, &unit);
287 set_label(stats_w.max_temp, "%.1f %s", value, unit);
289 set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
290 set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds / stats_ptr->selection_size, 0));
291 set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
292 set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
293 value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
294 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
295 value = get_depth_units(stats_ptr->min_depth.mm, &decimals, &unit);
296 set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
297 value = get_depth_units(stats_ptr->avg_depth.mm, &decimals, &unit);
298 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
299 value = get_volume_units(stats_ptr->max_sac.mliter, &decimals, &unit);
300 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
301 value = get_volume_units(stats_ptr->min_sac.mliter, &decimals, &unit);
302 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
303 value = get_volume_units(stats_ptr->avg_sac.mliter, &decimals, &unit);
304 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
307 void show_dive_stats(struct dive *dive)
309 /* they have to be called in this order, as 'total' depends on
310 * calculations done in 'single' */
311 show_single_dive_stats(dive);
312 show_total_dive_stats(dive);
315 void flush_dive_stats_changes(struct dive *dive)
317 /* We do nothing: we require the "Ok" button press */
320 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
322 GtkWidget *label_widget;
325 frame = gtk_frame_new(label);
326 label_widget = gtk_label_new(NULL);
327 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
328 gtk_container_add(GTK_CONTAINER(frame), label_widget);
333 GtkWidget *total_stats_widget(void)
335 GtkWidget *vbox, *hbox, *statsframe, *framebox;
337 vbox = gtk_vbox_new(FALSE, 3);
339 statsframe = gtk_frame_new("Statistics");
340 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
341 framebox = gtk_vbox_new(FALSE, 3);
342 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
345 hbox = gtk_hbox_new(FALSE, 3);
346 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
347 stats_w.selection_size = new_info_label_in_frame(hbox, "Dives");
348 stats_w.max_temp = new_info_label_in_frame(hbox, "Max Temp");
349 stats_w.min_temp = new_info_label_in_frame(hbox, "Min Temp");
350 stats_w.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
353 hbox = gtk_hbox_new(FALSE, 3);
354 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
356 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
357 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
358 stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
359 stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
362 hbox = gtk_hbox_new(FALSE, 3);
363 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
365 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
366 stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
367 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
370 hbox = gtk_hbox_new(FALSE, 3);
371 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
373 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
374 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
375 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
380 GtkWidget *single_stats_widget(void)
382 GtkWidget *vbox, *hbox, *infoframe, *framebox;
384 vbox = gtk_vbox_new(FALSE, 3);
386 infoframe = gtk_frame_new("Dive Info");
387 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
388 framebox = gtk_vbox_new(FALSE, 3);
389 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
392 hbox = gtk_hbox_new(FALSE, 3);
393 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
395 single_w.date = new_info_label_in_frame(hbox, "Date");
396 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
397 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
400 hbox = gtk_hbox_new(FALSE, 3);
401 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
403 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
404 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
405 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
408 hbox = gtk_hbox_new(FALSE, 3);
409 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
411 single_w.sac = new_info_label_in_frame(hbox, "SAC");
412 single_w.otu = new_info_label_in_frame(hbox, "OTU");
413 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
414 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");