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} allign;
62 } text_render_options_t;
64 static void plot_text(struct graphics_context *gc, text_render_options_t *tro,
65 double x, double y, const char *fmt, ...)
68 cairo_text_extents_t extents;
74 vsnprintf(buffer, sizeof(buffer), fmt, args);
77 cairo_text_extents(cr, buffer, &extents);
79 if (tro->allign == CENTER)
80 dx = -(extents.width/2 + extents.x_bearing);
81 dy = extents.height * 1.2;
84 cairo_rel_move_to(cr, dx, dy);
86 cairo_text_path(cr, buffer);
87 cairo_set_source_rgb(cr, 0, 0, 0);
91 cairo_rel_move_to(cr, dx, dy);
93 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
94 cairo_show_text(cr, buffer);
98 * Find the next maximum point in a 10-minute window.
100 * We exit early if we hit "enough" of a depth reversal,
101 * which is roughly 10 feet.
103 static struct sample *next_minmax(struct sample *sample, struct sample *end, int minmax)
105 const int enough = 3000;
106 struct sample *result;
107 int timelimit, depthlimit;
112 timelimit = 24*60*60;
113 depthlimit = sample->depth.mm;
122 time = sample->time.seconds;
123 depth = sample->depth.mm;
124 if (time > timelimit)
128 if (depth <= depthlimit) {
129 if (depthlimit - depth > enough)
134 if (depth >= depthlimit) {
135 if (depth - depthlimit > enough)
143 /* Look up to ten minutes into the future */
144 timelimit = time + 600;
149 void plot_text_samples(struct dive *dive, struct graphics_context *gc,
150 struct sample *a, struct sample *b)
152 struct sample *max, *min;
156 if (b->time.seconds - a->time.seconds < 3*60)
159 max = next_minmax(a, b, 1);
161 text_render_options_t tro = {1.0, 0.2, 0.2, CENTER};
162 int sec = max->time.seconds;
163 depth_t depth = max->depth;
167 min = next_minmax(max, b, 0);
168 plot_text_samples(dive, gc, a, max);
170 plot_text_samples(dive, gc, max, min);
171 plot_text_samples(dive, gc, min, b);
173 plot_text_samples(dive, gc, max, b);
175 switch (output_units.length) {
177 d = depth.mm / 1000.0;
186 plot_text(gc, &tro, sec, depth.mm, fmt, d);
191 static void plot_depth_text(struct dive *dive, struct graphics_context *gc)
193 struct sample *sample, *end;
194 int maxtime, maxdepth;
196 /* Get plot scaling limits */
197 maxtime = round_seconds_up(dive->duration.seconds);
198 maxdepth = round_depth_up(dive->maxdepth);
200 gc->scalex = maxtime;
201 gc->scaley = maxdepth;
203 cairo_set_font_size(gc->cr, 14);
206 * We never take the last sample into account.
207 * It should be a surface event anyway, although
208 * there are buggy cases where it isn't..
210 sample = dive->sample;
211 end = dive->sample + dive->samples - 1;
213 plot_text_samples(dive, gc, sample, end);
216 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc)
218 cairo_t *cr = gc->cr;
219 int begins, sec, depth;
221 struct sample *sample;
222 int maxtime, maxdepth, marker;
224 samples = dive->samples;
228 cairo_set_line_width(gc->cr, 2);
230 /* Get plot scaling limits */
231 maxtime = round_seconds_up(dive->duration.seconds);
232 maxdepth = round_depth_up(dive->maxdepth);
234 /* Time markers: every 5 min */
235 gc->scalex = maxtime;
237 for (i = 5*60; i < maxtime; i += 5*60) {
242 /* Depth markers: every 30 ft or 10 m*/
244 gc->scaley = maxdepth;
245 switch (output_units.length) {
246 case METERS: marker = 10000; break;
247 case FEET: marker = 9144; break; /* 30 ft */
250 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
251 for (i = marker; i < maxdepth; i += marker) {
257 /* Show mean depth */
258 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
259 move_to(gc, 0, dive->meandepth.mm);
260 line_to(gc, 1, dive->meandepth.mm);
263 gc->scalex = maxtime;
265 sample = dive->sample;
266 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
267 begins = sample->time.seconds;
268 move_to(gc, sample->time.seconds, sample->depth.mm);
269 for (i = 1; i < dive->samples; i++) {
271 sec = sample->time.seconds;
272 if (sec <= maxtime) {
273 depth = sample->depth.mm;
274 line_to(gc, sec, depth);
278 line_to(gc, MIN(sec,maxtime), 0);
279 line_to(gc, begins, 0);
280 cairo_close_path(cr);
281 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
282 cairo_fill_preserve(cr);
283 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
287 /* gets both the actual start and end pressure as well as the scaling factors */
288 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
289 pressure_t *startp, pressure_t *endp)
294 gc->scalex = round_seconds_up(dive->duration.seconds);
299 startp->mbar = endp->mbar = 0;
301 for (i = 0; i < dive->samples; i++) {
303 struct sample *sample = dive->sample + i;
305 /* FIXME! We only track cylinder 0 right now */
306 if (sample->cylinderindex)
308 mbar = sample->cylinderpressure.mbar;
322 gc->scaley = max * 1.5;
326 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
330 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
333 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
335 move_to(gc, 0, dive->cylinder[0].start.mbar);
336 for (i = 1; i < dive->samples; i++) {
338 struct sample *sample = dive->sample + i;
340 mbar = sample->cylinderpressure.mbar;
343 sec = sample->time.seconds;
344 if (sec <= dive->duration.seconds)
345 line_to(gc, sec, mbar);
348 * We may have "surface time" events, in which case we don't go
349 * back to dive duration
351 if (sec < dive->duration.seconds)
352 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
353 cairo_stroke(gc->cr);
357 * Return air usage (in liters).
359 static double calculate_airuse(struct dive *dive)
364 for (i = 0; i < MAX_CYLINDERS; i++) {
365 cylinder_t *cyl = dive->cylinder + i;
366 int size = cyl->type.size.mliter;
372 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
374 /* Liters of air at 1 atm == milliliters at 1k atm*/
375 airuse += kilo_atm * size;
380 static void plot_info(struct dive *dive, struct graphics_context *gc)
382 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
383 const double liters_per_cuft = 28.317;
387 airuse = calculate_airuse(dive);
391 /* I really need to start addign some unit setting thing */
392 switch (output_units.volume) {
398 airuse /= liters_per_cuft;
401 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
402 if (dive->duration.seconds) {
403 double pressure = 1 + (dive->meandepth.mm / 10000.0);
404 double sac = airuse / pressure * 60 / dive->duration.seconds;
405 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
409 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
411 pressure_t startp, endp;
413 cairo_set_font_size(gc->cr, 10);
415 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
417 const char *unit = "bar";
419 switch (output_units.pressure) {
421 start = startp.mbar * 100;
422 end = startp.mbar * 100;
426 start = (startp.mbar + 500) / 1000;
427 end = (endp.mbar + 500) / 1000;
431 start = to_PSI(startp);
437 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
438 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
439 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
444 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
450 cairo_translate(gc->cr, topx, topy);
453 * We can use "cairo_translate()" because that doesn't
454 * scale line width etc. But the actual scaling we need
455 * do set up ourselves..
459 gc->maxx = (w - 2*topx);
460 gc->maxy = (h - 2*topy);
462 /* Cylinder pressure plot */
463 plot_cylinder_pressure(dive, gc);
466 plot_depth_profile(dive, gc);
468 /* Text on top of all graphs.. */
469 plot_depth_text(dive, gc);
470 plot_cylinder_pressure_text(dive, gc);
472 /* And info box in the lower right corner.. */
473 gc->scalex = gc->scaley = 1.0;
476 /* Bounding box last */
477 cairo_set_source_rgb(gc->cr, 1, 1, 1);
482 cairo_close_path(gc->cr);
483 cairo_stroke(gc->cr);
487 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
489 struct dive *dive = current_dive;
490 struct graphics_context gc;
493 w = widget->allocation.width;
494 h = widget->allocation.height;
496 gc.cr = gdk_cairo_create(widget->window);
497 cairo_set_source_rgb(gc.cr, 0, 0, 0);
501 plot(&gc, w, h, dive);
503 cairo_destroy(gc.cr);
508 GtkWidget *dive_profile_widget(void)
512 da = gtk_drawing_area_new();
513 gtk_widget_set_size_request(da, 450, 350);
514 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);