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,
48 } total_stats_widget_t;
50 static total_stats_widget_t stats_w;
53 duration_t total_time;
54 /* avg_time is simply total_time / nr -- let's not keep this */
55 duration_t shortest_time;
56 duration_t longest_time;
67 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
71 int old_tt, sac_time = 0;
74 memset(&stats, 0, sizeof(stats));
75 if (dive_table.nr > 0) {
76 stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
77 stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
79 /* this relies on the fact that the dives in the dive_table
80 * are in chronological order */
81 for (idx = 0; idx < dive_table.nr; idx++) {
82 dp = dive_table.dives[idx];
83 if (dp->when == dive->when) {
84 /* that's the one we are showing */
86 *prev_dive = dive_table.dives[idx-1];
88 old_tt = stats.total_time.seconds;
89 stats.total_time.seconds += dp->duration.seconds;
90 if (dp->duration.seconds > stats.longest_time.seconds)
91 stats.longest_time.seconds = dp->duration.seconds;
92 if (dp->duration.seconds < stats.shortest_time.seconds)
93 stats.shortest_time.seconds = dp->duration.seconds;
94 if (dp->maxdepth.mm > stats.max_depth.mm)
95 stats.max_depth.mm = dp->maxdepth.mm;
96 if (dp->maxdepth.mm < stats.min_depth.mm)
97 stats.min_depth.mm = dp->maxdepth.mm;
98 stats.avg_depth.mm = (1.0 * old_tt * stats.avg_depth.mm +
99 dp->duration.seconds * dp->meandepth.mm) / stats.total_time.seconds;
100 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
101 int old_sac_time = sac_time;
102 sac_time += dp->duration.seconds;
103 stats.avg_sac.mliter = (1.0 * old_sac_time * stats.avg_sac.mliter +
104 dp->duration.seconds * dp->sac) / sac_time ;
105 if (dp->sac > stats.max_sac.mliter)
106 stats.max_sac.mliter = dp->sac;
107 if (stats.min_sac.mliter == 0 || dp->sac < stats.min_sac.mliter)
108 stats.min_sac.mliter = dp->sac;
113 static void set_label(GtkWidget *w, const char *fmt, ...)
119 vsnprintf(buf, sizeof(buf), fmt, args);
121 gtk_label_set_text(GTK_LABEL(w), buf);
124 static char * get_time_string(int seconds, int maxdays)
127 if (maxdays && seconds > 3600 * 24 * maxdays)
128 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
130 int days = seconds / 3600 / 24;
131 int hours = (seconds - days * 3600 * 24) / 3600;
132 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
134 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
136 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
141 static void show_single_dive_stats(struct dive *dive)
147 int idx, offset, gas_used;
148 struct dive *prev_dive;
151 process_all_dives(dive, &prev_dive);
153 tm = gmtime(&dive->when);
154 snprintf(buf, sizeof(buf),
155 "%s, %s %d, %d %2d:%02d",
156 weekday(tm->tm_wday),
157 monthname(tm->tm_mon),
158 tm->tm_mday, tm->tm_year + 1900,
159 tm->tm_hour, tm->tm_min);
161 set_label(single_w.date, buf);
162 set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
164 set_label(single_w.surf_intv,
165 get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
167 set_label(single_w.surf_intv, "unknown");
168 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
169 set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
170 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
171 set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
172 if (dive->watertemp.mkelvin) {
173 value = get_temp_units(dive->watertemp.mkelvin, &unit);
174 set_label(single_w.water_temp, "%.1f %s", value, unit);
176 set_label(single_w.water_temp, "");
177 value = get_volume_units(dive->sac, &decimals, &unit);
179 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
181 set_label(single_w.sac, "");
182 set_label(single_w.otu, "%d", dive->otu);
186 /* for the O2/He readings just create a list of them */
187 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
188 cylinder_t *cyl = &dive->cylinder[idx];
189 unsigned int start, end;
191 start = cyl->start.mbar ? : cyl->sample_start.mbar;
192 end = cyl->end.mbar ? : cyl->sample_end.mbar;
193 if (!cylinder_none(cyl)) {
194 /* 0% O2 strangely means air, so 21% - I don't like that at all */
195 int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
197 snprintf(buf+offset, 80-offset, ", ");
200 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
201 (cyl->gasmix.he.permille + 5) / 10);
202 offset = strlen(buf);
204 /* and if we have size, start and end pressure, we can
205 * calculate the total gas used */
206 if (cyl->type.size.mliter && start && end)
207 gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
209 set_label(single_w.o2he, buf);
211 value = get_volume_units(gas_used, &decimals, &unit);
212 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
214 set_label(single_w.gas_used, "");
217 static void show_total_dive_stats(struct dive *dive)
223 set_label(stats_w.total_time, get_time_string(stats.total_time.seconds, 0));
224 set_label(stats_w.avg_time, get_time_string(stats.total_time.seconds / dive_table.nr, 0));
225 set_label(stats_w.longest_time, get_time_string(stats.longest_time.seconds, 0));
226 set_label(stats_w.shortest_time, get_time_string(stats.shortest_time.seconds, 0));
227 value = get_depth_units(stats.max_depth.mm, &decimals, &unit);
228 set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
229 value = get_depth_units(stats.min_depth.mm, &decimals, &unit);
230 set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
231 value = get_depth_units(stats.avg_depth.mm, &decimals, &unit);
232 set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
233 value = get_volume_units(stats.max_sac.mliter, &decimals, &unit);
234 set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
235 value = get_volume_units(stats.min_sac.mliter, &decimals, &unit);
236 set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
237 value = get_volume_units(stats.avg_sac.mliter, &decimals, &unit);
238 set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
241 void show_dive_stats(struct dive *dive)
243 /* they have to be called in this order, as 'total' depends on
244 * calculations done in 'single' */
245 show_single_dive_stats(dive);
246 show_total_dive_stats(dive);
249 void flush_dive_stats_changes(struct dive *dive)
251 /* We do nothing: we require the "Ok" button press */
254 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
256 GtkWidget *label_widget;
259 frame = gtk_frame_new(label);
260 label_widget = gtk_label_new(NULL);
261 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
262 gtk_container_add(GTK_CONTAINER(frame), label_widget);
267 GtkWidget *total_stats_widget(void)
269 GtkWidget *vbox, *hbox, *statsframe, *framebox;
271 vbox = gtk_vbox_new(FALSE, 3);
273 statsframe = gtk_frame_new("Statistics");
274 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
275 framebox = gtk_vbox_new(FALSE, 3);
276 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
279 hbox = gtk_hbox_new(FALSE, 3);
280 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
282 stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
283 stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
284 stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
285 stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
288 hbox = gtk_hbox_new(FALSE, 3);
289 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
291 stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
292 stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
293 stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
296 hbox = gtk_hbox_new(FALSE, 3);
297 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
299 stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
300 stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
301 stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
306 GtkWidget *single_stats_widget(void)
308 GtkWidget *vbox, *hbox, *infoframe, *framebox;
310 vbox = gtk_vbox_new(FALSE, 3);
312 infoframe = gtk_frame_new("Dive Info");
313 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
314 framebox = gtk_vbox_new(FALSE, 3);
315 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
318 hbox = gtk_hbox_new(FALSE, 3);
319 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
321 single_w.date = new_info_label_in_frame(hbox, "Date");
322 single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
323 single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
326 hbox = gtk_hbox_new(FALSE, 3);
327 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
329 single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
330 single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
331 single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
334 hbox = gtk_hbox_new(FALSE, 3);
335 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
337 single_w.sac = new_info_label_in_frame(hbox, "SAC");
338 single_w.otu = new_info_label_in_frame(hbox, "OTU");
339 single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
340 single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");