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"
42 static info_stat_widget_t info_stat_w;
45 duration_t total_time;
46 /* avg_time is simply total_time / nr -- let's not keep this */
54 static info_stat_t info_stat;
56 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
60 int old_tt, sac_time = 0;
63 memset(&info_stat, 0, sizeof(info_stat));
64 /* this relies on the fact that the dives in the dive_table
65 * are in chronological order */
66 for (idx = 0; idx < dive_table.nr; idx++) {
67 dp = dive_table.dives[idx];
68 if (dp->when == dive->when) {
69 /* that's the one we are showing */
71 *prev_dive = dive_table.dives[idx-1];
73 old_tt = info_stat.total_time.seconds;
74 info_stat.total_time.seconds += dp->duration.seconds;
75 if (dp->maxdepth.mm > info_stat.max_depth.mm)
76 info_stat.max_depth.mm = dp->maxdepth.mm;
77 info_stat.avg_depth.mm = (1.0 * old_tt * info_stat.avg_depth.mm +
78 dp->duration.seconds * dp->meandepth.mm) / info_stat.total_time.seconds;
80 int old_sac_time = sac_time;
81 sac_time += dp->duration.seconds;
82 info_stat.avg_sac.mliter = (1.0 * old_sac_time * info_stat.avg_sac.mliter +
83 dp->duration.seconds * dp->sac) / sac_time ;
84 if (dp->sac > info_stat.max_sac.mliter)
85 info_stat.max_sac.mliter = dp->sac;
86 if (info_stat.min_sac.mliter == 0 || dp->sac < info_stat.max_sac.mliter)
87 info_stat.min_sac.mliter = dp->sac;
92 static void set_label(GtkWidget *w, const char *fmt, ...)
98 vsnprintf(buf, sizeof(buf), fmt, args);
100 gtk_label_set_text(GTK_LABEL(w), buf);
103 static char * get_time_string(int seconds, int maxdays)
106 if (maxdays && seconds > 3600 * 24 * maxdays)
107 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
109 int days = seconds / 3600 / 24;
110 int hours = (seconds - days * 3600 * 24) / 3600;
111 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
113 snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
115 snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
120 void show_dive_stats(struct dive *dive)
126 int idx, offset, gas_used;
127 struct dive *prev_dive;
129 process_all_dives(dive, &prev_dive);
131 strftime(buf, 80, "%a, %b %d, %Y, %k:%M", gmtime(&dive->when));
132 set_label(info_stat_w.date, buf);
133 set_label(info_stat_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
135 set_label(info_stat_w.surf_intv, get_time_string(dive->when - prev_dive->when, 4));
137 set_label(info_stat_w.surf_intv, "unknown");
138 value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
139 set_label(info_stat_w.max_depth, "%.*f %s", decimals, value, unit);
140 value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
141 set_label(info_stat_w.avg_depth, "%.*f %s", decimals, value, unit);
142 value = get_temp_units(dive->watertemp.mkelvin, &unit);
143 set_label(info_stat_w.water_temp, "%.1f %s", value, unit);
144 value = get_volume_units(dive->sac, &decimals, &unit);
146 set_label(info_stat_w.sac, "%.*f %s/min", decimals, value, unit);
148 set_label(info_stat_w.sac, "");
149 set_label(info_stat_w.otu, "%d", dive->otu);
153 /* for the O2/He readings just create a list of them */
154 for (idx = 0; idx < MAX_CYLINDERS; idx++) {
155 cylinder_t *cyl = &dive->cylinder[idx];
156 /* we assume that every valid cylinder has either a working pressure
157 * or a size; but for good measure let's also accept cylinders with
158 * a starting or ending pressure*/
159 if (cyl->type.workingpressure.mbar || cyl->type.size.mliter ||
160 cyl->start.mbar || cyl->end.mbar) {
161 /* 0% O2 strangely means air, so 21% - I don't like that at all */
162 int o2 = cyl->gasmix.o2.permille ? : 209;
164 snprintf(buf+offset, 80-offset, ", ");
167 snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
168 (cyl->gasmix.he.permille + 5) / 10);
169 offset = strlen(buf);
171 /* and if we have size, start and end pressure, we can
172 * calculate the total gas used */
173 if (cyl->type.size.mliter && cyl->start.mbar && cyl->end.mbar)
174 gas_used += cyl->type.size.mliter / 1000.0 *
175 (cyl->start.mbar - cyl->end.mbar);
178 set_label(info_stat_w.o2he, buf);
180 value = get_volume_units(gas_used, &decimals, &unit);
181 set_label(info_stat_w.gas_used, "%.*f %s", decimals, value, unit);
183 set_label(info_stat_w.gas_used, "");
184 /* and now do the statistics */
185 set_label(info_stat_w.total_time, get_time_string(info_stat.total_time.seconds, 0));
186 set_label(info_stat_w.avg_time, get_time_string(info_stat.total_time.seconds / dive_table.nr, 0));
187 value = get_depth_units(info_stat.max_depth.mm, &decimals, &unit);
188 set_label(info_stat_w.max_overall_depth, "%.*f %s", decimals, value, unit);
189 value = get_depth_units(info_stat.avg_depth.mm, &decimals, &unit);
190 set_label(info_stat_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
191 value = get_volume_units(info_stat.max_sac.mliter, &decimals, &unit);
192 set_label(info_stat_w.max_sac, "%.*f %s/min", decimals, value, unit);
193 value = get_volume_units(info_stat.min_sac.mliter, &decimals, &unit);
194 set_label(info_stat_w.min_sac, "%.*f %s/min", decimals, value, unit);
195 value = get_volume_units(info_stat.avg_sac.mliter, &decimals, &unit);
196 set_label(info_stat_w.avg_sac, "%.*f %s/min", decimals, value, unit);
199 void flush_dive_stats_changes(struct dive *dive)
201 /* We do nothing: we require the "Ok" button press */
204 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
206 GtkWidget *label_widget;
209 frame = gtk_frame_new(label);
210 label_widget = gtk_label_new(NULL);
211 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
212 gtk_container_add(GTK_CONTAINER(frame), label_widget);
217 GtkWidget *stats_widget(void)
220 GtkWidget *vbox, *hbox, *infoframe, *statsframe, *framebox;
222 vbox = gtk_vbox_new(FALSE, 3);
224 infoframe = gtk_frame_new("Dive Info");
225 gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
226 framebox = gtk_vbox_new(FALSE, 3);
227 gtk_container_add(GTK_CONTAINER(infoframe), framebox);
230 hbox = gtk_hbox_new(FALSE, 3);
231 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
233 info_stat_w.date = new_info_label_in_frame(hbox, "Date");
234 info_stat_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
235 info_stat_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
238 hbox = gtk_hbox_new(FALSE, 3);
239 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
241 info_stat_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
242 info_stat_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
243 info_stat_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
246 hbox = gtk_hbox_new(FALSE, 3);
247 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
249 info_stat_w.sac = new_info_label_in_frame(hbox, "SAC");
250 info_stat_w.otu = new_info_label_in_frame(hbox, "OTU");
251 info_stat_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
252 info_stat_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");
254 statsframe = gtk_frame_new("Statistics");
255 gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
256 framebox = gtk_vbox_new(FALSE, 3);
257 gtk_container_add(GTK_CONTAINER(statsframe), framebox);
260 hbox = gtk_hbox_new(FALSE, 3);
261 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
263 info_stat_w.total_time = new_info_label_in_frame(hbox, "Total Time");
264 info_stat_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
265 info_stat_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
266 info_stat_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
269 hbox = gtk_hbox_new(FALSE, 3);
270 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
272 info_stat_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
273 info_stat_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
274 info_stat_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");