10 int selected_dive = 0;
13 * Cairo scaling really is horribly horribly mis-designed.
15 * Which is sad, because I really like Cairo otherwise. But
16 * the fact that the line width is scaled with the same scale
17 * as the coordinate system is a f*&%ing disaster. So we
18 * can't use it, and instead have this butt-ugly wrapper thing..
20 struct graphics_context {
23 double scalex, scaley;
26 /* Scale to 0,0 -> maxx,maxy */
27 #define SCALE(gc,x,y) (x)*gc->maxx/gc->scalex,(y)*gc->maxy/gc->scaley
29 static void move_to(struct graphics_context *gc, double x, double y)
31 cairo_move_to(gc->cr, SCALE(gc, x, y));
34 static void line_to(struct graphics_context *gc, double x, double y)
36 cairo_line_to(gc->cr, SCALE(gc, x, y));
39 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
42 * When showing dive profiles, we scale things to the
43 * current dive. However, we don't scale past less than
44 * 30 minutes or 90 ft, just so that small dives show
47 static int round_seconds_up(int seconds)
49 return MAX(30*60, ROUND_UP(seconds, 60*10));
52 static int round_depth_up(depth_t depth)
54 unsigned mm = depth.mm;
56 return MAX(30000, ROUND_UP(mm+3000, 10000));
61 enum {CENTER,LEFT} halign;
62 enum {MIDDLE,TOP,BOTTOM} valign;
63 } text_render_options_t;
65 static void plot_text(struct graphics_context *gc, text_render_options_t *tro,
66 double x, double y, const char *fmt, ...)
69 cairo_text_extents_t extents;
75 vsnprintf(buffer, sizeof(buffer), fmt, args);
78 cairo_text_extents(cr, buffer, &extents);
80 switch (tro->halign) {
82 dx = -(extents.width/2 + extents.x_bearing);
88 switch (tro->valign) {
90 dy = extents.height * 1.2;
93 dy = -extents.height * 0.8;
101 cairo_rel_move_to(cr, dx, dy);
103 cairo_text_path(cr, buffer);
104 cairo_set_source_rgb(cr, 0, 0, 0);
108 cairo_rel_move_to(cr, dx, dy);
110 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
111 cairo_show_text(cr, buffer);
115 * Find the next minimum/maximum point.
117 * We exit early if we hit "enough" of a depth reversal,
118 * which is roughly 10 feet.
120 static struct sample *next_minmax(struct sample *sample, struct sample *end, int minmax)
122 const int enough = 3000;
123 struct sample *result;
129 depthlimit = sample->depth.mm;
138 time = sample->time.seconds;
139 depth = sample->depth.mm;
142 if (depth <= depthlimit) {
143 if (depthlimit - depth > enough)
148 if (depth >= depthlimit) {
149 if (depth - depthlimit > enough)
161 static void render_depth_sample(struct graphics_context *gc, struct sample *sample)
163 text_render_options_t tro = {1.0, 0.2, 0.2, CENTER, TOP};
164 int sec = sample->time.seconds;
165 depth_t depth = sample->depth;
169 switch (output_units.length) {
171 d = depth.mm / 1000.0;
179 plot_text(gc, &tro, sec, depth.mm, fmt, d);
182 static void plot_text_samples(struct graphics_context *gc, struct sample *a, struct sample *b)
187 a = next_minmax(a, b, 1);
190 render_depth_sample(gc, a);
191 a = next_minmax(a, b, 0);
197 static void plot_depth_text(struct dive *dive, struct graphics_context *gc)
199 struct sample *sample, *end;
200 int maxtime, maxdepth;
202 /* Get plot scaling limits */
203 maxtime = round_seconds_up(dive->duration.seconds);
204 maxdepth = round_depth_up(dive->maxdepth);
206 gc->scalex = maxtime;
207 gc->scaley = maxdepth;
209 cairo_set_font_size(gc->cr, 14);
211 sample = dive->sample;
212 end = dive->sample + dive->samples;
214 plot_text_samples(gc, sample, end);
217 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc)
219 cairo_t *cr = gc->cr;
220 int begins, sec, depth;
222 struct sample *sample;
223 int maxtime, maxdepth, marker;
225 samples = dive->samples;
229 cairo_set_line_width(gc->cr, 2);
231 /* Get plot scaling limits */
232 maxtime = round_seconds_up(dive->duration.seconds);
233 maxdepth = round_depth_up(dive->maxdepth);
235 /* Time markers: every 5 min */
236 gc->scalex = maxtime;
238 for (i = 5*60; i < maxtime; i += 5*60) {
243 /* Depth markers: every 30 ft or 10 m*/
245 gc->scaley = maxdepth;
246 switch (output_units.length) {
247 case METERS: marker = 10000; break;
248 case FEET: marker = 9144; break; /* 30 ft */
251 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
252 for (i = marker; i < maxdepth; i += marker) {
258 /* Show mean depth */
259 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
260 move_to(gc, 0, dive->meandepth.mm);
261 line_to(gc, 1, dive->meandepth.mm);
264 gc->scalex = maxtime;
266 sample = dive->sample;
267 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
268 begins = sample->time.seconds;
269 move_to(gc, sample->time.seconds, sample->depth.mm);
270 for (i = 1; i < dive->samples; i++) {
272 sec = sample->time.seconds;
273 if (sec <= maxtime) {
274 depth = sample->depth.mm;
275 line_to(gc, sec, depth);
279 line_to(gc, MIN(sec,maxtime), 0);
280 line_to(gc, begins, 0);
281 cairo_close_path(cr);
282 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
283 cairo_fill_preserve(cr);
284 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
288 /* gets both the actual start and end pressure as well as the scaling factors */
289 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
290 pressure_t *startp, pressure_t *endp)
295 gc->scalex = round_seconds_up(dive->duration.seconds);
300 startp->mbar = endp->mbar = 0;
302 for (i = 0; i < dive->samples; i++) {
304 struct sample *sample = dive->sample + i;
306 /* FIXME! We only track cylinder 0 right now */
307 if (sample->cylinderindex)
309 mbar = sample->cylinderpressure.mbar;
323 gc->scaley = max * 1.5;
327 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
331 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
334 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
336 move_to(gc, 0, dive->cylinder[0].start.mbar);
337 for (i = 1; i < dive->samples; i++) {
339 struct sample *sample = dive->sample + i;
341 mbar = sample->cylinderpressure.mbar;
344 sec = sample->time.seconds;
345 if (sec <= dive->duration.seconds)
346 line_to(gc, sec, mbar);
349 * We may have "surface time" events, in which case we don't go
350 * back to dive duration
352 if (sec < dive->duration.seconds)
353 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
354 cairo_stroke(gc->cr);
358 * Return air usage (in liters).
360 static double calculate_airuse(struct dive *dive)
365 for (i = 0; i < MAX_CYLINDERS; i++) {
366 cylinder_t *cyl = dive->cylinder + i;
367 int size = cyl->type.size.mliter;
373 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
375 /* Liters of air at 1 atm == milliliters at 1k atm*/
376 airuse += kilo_atm * size;
381 static void plot_info(struct dive *dive, struct graphics_context *gc)
383 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT, TOP};
384 const double liters_per_cuft = 28.317;
388 airuse = calculate_airuse(dive);
392 /* I really need to start addign some unit setting thing */
393 switch (output_units.volume) {
399 airuse /= liters_per_cuft;
402 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
403 if (dive->duration.seconds) {
404 double pressure = 1 + (dive->meandepth.mm / 10000.0);
405 double sac = airuse / pressure * 60 / dive->duration.seconds;
406 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
410 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
412 pressure_t startp, endp;
414 cairo_set_font_size(gc->cr, 10);
416 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
418 const char *unit = "bar";
420 switch (output_units.pressure) {
422 start = startp.mbar * 100;
423 end = startp.mbar * 100;
427 start = (startp.mbar + 500) / 1000;
428 end = (endp.mbar + 500) / 1000;
432 start = to_PSI(startp);
438 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT, TOP};
439 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
440 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
445 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
451 cairo_translate(gc->cr, topx, topy);
454 * We can use "cairo_translate()" because that doesn't
455 * scale line width etc. But the actual scaling we need
456 * do set up ourselves..
460 gc->maxx = (w - 2*topx);
461 gc->maxy = (h - 2*topy);
463 /* Cylinder pressure plot */
464 plot_cylinder_pressure(dive, gc);
467 plot_depth_profile(dive, gc);
469 /* Text on top of all graphs.. */
470 plot_depth_text(dive, gc);
471 plot_cylinder_pressure_text(dive, gc);
473 /* And info box in the lower right corner.. */
474 gc->scalex = gc->scaley = 1.0;
477 /* Bounding box last */
478 cairo_set_source_rgb(gc->cr, 1, 1, 1);
483 cairo_close_path(gc->cr);
484 cairo_stroke(gc->cr);
488 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
490 struct dive *dive = current_dive;
491 struct graphics_context gc;
494 w = widget->allocation.width;
495 h = widget->allocation.height;
497 gc.cr = gdk_cairo_create(widget->window);
498 cairo_set_source_rgb(gc.cr, 0, 0, 0);
502 plot(&gc, w, h, dive);
504 cairo_destroy(gc.cr);
509 GtkWidget *dive_profile_widget(void)
513 da = gtk_drawing_area_new();
514 gtk_widget_set_size_request(da, 450, 350);
515 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);