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,
45 } total_stats_widget_t;
47 static total_stats_widget_t stats_w;
50 duration_t total_time;
51 /* avg_time is simply total_time / nr -- let's not keep this */
61 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
65 int old_tt, sac_time = 0;
68 memset(&stats, 0, sizeof(stats));
69 /* this relies on the fact that the dives in the dive_table
70 * are in chronological order */
71 for (idx = 0; idx < dive_table.nr; idx++) {
72 dp = dive_table.dives[idx];
73 if (dp->when == dive->when) {
74 /* that's the one we are showing */
76 *prev_dive = dive_table.dives[idx-1];
78 old_tt = stats.total_time.seconds;
79 stats.total_time.seconds += dp->duration.seconds;
80 if (dp->maxdepth.mm > stats.max_depth.mm)
81 stats.max_depth.mm = dp->maxdepth.mm;
82 stats.avg_depth.mm = (1.0 * old_tt * stats.avg_depth.mm +
83 dp->duration.seconds * dp->meandepth.mm) / stats.total_time.seconds;
84 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
85 int old_sac_time = sac_time;
86 sac_time += dp->duration.seconds;
87 stats.avg_sac.mliter = (1.0 * old_sac_time * stats.avg_sac.mliter +
88 dp->duration.seconds * dp->sac) / sac_time ;
89 if (dp->sac > stats.max_sac.mliter)
90 stats.max_sac.mliter = dp->sac;
91 if (stats.min_sac.mliter == 0 || dp->sac < stats.min_sac.mliter)
92 stats.min_sac.mliter = dp->sac;
97 static void set_label(GtkWidget *w, const char *fmt, ...)
103 vsnprintf(buf, sizeof(buf), fmt, args);
105 gtk_label_set_text(GTK_LABEL(w), buf);
108 static char * get_time_string(int seconds, int maxdays)
111 if (maxdays && seconds > 3600 * 24 * maxdays)
112 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
114 int days = seconds / 3600 / 24;
115 int hours = (seconds - days * 3600 * 24) / 3600;
116 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
118 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
120 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
125 static void show_single_dive_stats(struct dive *dive)
131 int idx, offset, gas_used;
132 struct dive *prev_dive;
135 process_all_dives(dive, &prev_dive);
137 tm = gmtime(&dive->when);
138 snprintf(buf, sizeof(buf),
139 "%s, %s %d, %d %2d:%02d",
140 weekday(tm->tm_wday),
141 monthname(tm->tm_mon),
142 tm->tm_mday, tm->tm_year + 1900,
143 tm->tm_hour, tm->tm_min);
145 set_label(single_w.date, buf);
146 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
148 set_label(single_w.surf_intv,
149 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
151 set_label(single_w.surf_intv, "unknown");
152 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
153 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
154 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
155 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
156 if (dive->watertemp.mkelvin) {
157 value = get_temp_units(dive->watertemp.mkelvin, &unit);
158 set_label(single_w.water_temp, "%.1f %s", value, unit);
160 set_label(single_w.water_temp, "");
161 value = get_volume_units(dive->sac, &decimals, &unit);
163 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
165 set_label(single_w.sac, "");
166 set_label(single_w.otu, "%d", dive->otu);
170 /* for the O2/He readings just create a list of them */
171 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
172 cylinder_t *cyl = &dive->cylinder[idx];
173 unsigned int start, end;
175 start = cyl->start.mbar ? : cyl->sample_start.mbar;
176 end = cyl->end.mbar ? : cyl->sample_end.mbar;
177 if (!cylinder_none(cyl)) {
178 /* 0% O2 strangely means air, so 21% - I don't like that at all */
179 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
181 snprintf(buf+offset, 80-offset, ", ");
184 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
185 (cyl->gasmix.he.permille + 5) / 10);
186 offset = strlen(buf);
188 /* and if we have size, start and end pressure, we can
189 * calculate the total gas used */
190 if (cyl->type.size.mliter && start && end)
191 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
193 set_label(single_w.o2he, buf);
195 value = get_volume_units(gas_used, &decimals, &unit);
196 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
198 set_label(single_w.gas_used, "");
201 static void show_total_dive_stats(struct dive *dive)
207 set_label(stats_w.total_time, get_time_string(stats.total_time.seconds, 0));
208 set_label(stats_w.avg_time, get_time_string(stats.total_time.seconds / dive_table.nr, 0));
209 value = get_depth_units(stats.max_depth.mm, &decimals, &unit);
210 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
211 value = get_depth_units(stats.avg_depth.mm, &decimals, &unit);
212 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
213 value = get_volume_units(stats.max_sac.mliter, &decimals, &unit);
214 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
215 value = get_volume_units(stats.min_sac.mliter, &decimals, &unit);
216 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
217 value = get_volume_units(stats.avg_sac.mliter, &decimals, &unit);
218 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
221 void show_dive_stats(struct dive *dive)
223 show_single_dive_stats(dive);
224 show_total_dive_stats(dive);
227 void flush_dive_stats_changes(struct dive *dive)
229 /* We do nothing: we require the "Ok" button press */
232 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
234 GtkWidget *label_widget;
237 frame = gtk_frame_new(label);
238 label_widget = gtk_label_new(NULL);
239 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
240 gtk_container_add(GTK_CONTAINER(frame), label_widget);
245 GtkWidget *total_stats_widget(void)
247 GtkWidget *vbox, *hbox, *statsframe, *framebox;
249 vbox = gtk_vbox_new(FALSE, 3);
251 statsframe = gtk_frame_new("Statistics");
252 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
253 framebox = gtk_vbox_new(FALSE, 3);
254 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
257 hbox = gtk_hbox_new(FALSE, 3);
258 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
260 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
261 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
262 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
263 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
266 hbox = gtk_hbox_new(FALSE, 3);
267 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
269 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
270 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
271 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
276 GtkWidget *single_stats_widget(void)
278 GtkWidget *vbox, *hbox, *infoframe, *framebox;
280 vbox = gtk_vbox_new(FALSE, 3);
282 infoframe = gtk_frame_new("Dive Info");
283 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
284 framebox = gtk_vbox_new(FALSE, 3);
285 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
288 hbox = gtk_hbox_new(FALSE, 3);
289 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
291 single_w.date = new_info_label_in_frame(hbox, "Date");
292 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
293 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
296 hbox = gtk_hbox_new(FALSE, 3);
297 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
299 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
300 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
301 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
304 hbox = gtk_hbox_new(FALSE, 3);
305 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
307 single_w.sac = new_info_label_in_frame(hbox, "SAC");
308 single_w.otu = new_info_label_in_frame(hbox, "OTU");
309 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
310 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");