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));
62 enum {CENTER,LEFT} halign;
63 enum {MIDDLE,TOP,BOTTOM} valign;
64 } text_render_options_t;
66 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
67 double x, double y, const char *fmt, ...)
70 cairo_text_extents_t extents;
76 vsnprintf(buffer, sizeof(buffer), fmt, args);
79 cairo_set_font_size(cr, tro->size);
80 cairo_text_extents(cr, buffer, &extents);
82 switch (tro->halign) {
84 dx = -(extents.width/2 + extents.x_bearing);
90 switch (tro->valign) {
92 dy = extents.height * 1.2;
95 dy = -extents.height * 0.8;
103 cairo_rel_move_to(cr, dx, dy);
105 cairo_text_path(cr, buffer);
106 cairo_set_source_rgb(cr, 0, 0, 0);
110 cairo_rel_move_to(cr, dx, dy);
112 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
113 cairo_show_text(cr, buffer);
116 static void render_depth_sample(struct graphics_context *gc, struct sample *sample, const text_render_options_t *tro)
118 int sec = sample->time.seconds;
119 depth_t depth = sample->depth;
123 switch (output_units.length) {
125 d = depth.mm / 1000.0;
133 plot_text(gc, tro, sec, depth.mm, fmt, d);
137 * Find the next minimum/maximum point.
139 * We exit early if we hit "enough" of a depth reversal,
140 * which is roughly 10 feet.
142 static struct sample *next_minmax(struct sample *sample, struct sample *end, int minmax)
144 const int enough = 3000;
145 struct sample *result;
151 depthlimit = sample->depth.mm;
160 depth = sample->depth.mm;
163 if (depth <= depthlimit) {
164 if (depthlimit - depth > enough)
169 if (depth >= depthlimit) {
170 if (depth - depthlimit > enough)
182 static void plot_text_samples(struct graphics_context *gc, struct sample *a, struct sample *b)
184 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
185 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
190 a = next_minmax(a, b, 1);
193 render_depth_sample(gc, a, &deep);
194 a = next_minmax(a, b, 0);
197 if (a->depth.mm < 2500)
199 render_depth_sample(gc, a, &shallow);
203 static void plot_depth_text(struct dive *dive, struct graphics_context *gc)
205 struct sample *sample, *end;
206 int maxtime, maxdepth;
208 /* Get plot scaling limits */
209 maxtime = round_seconds_up(dive->duration.seconds);
210 maxdepth = round_depth_up(dive->maxdepth);
212 gc->scalex = maxtime;
213 gc->scaley = maxdepth;
215 sample = dive->sample;
216 end = dive->sample + dive->samples;
218 plot_text_samples(gc, sample, end);
221 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc)
223 cairo_t *cr = gc->cr;
224 int begins, sec, depth;
226 struct sample *sample;
227 int maxtime, maxdepth, marker;
229 samples = dive->samples;
233 cairo_set_line_width(gc->cr, 2);
235 /* Get plot scaling limits */
236 maxtime = round_seconds_up(dive->duration.seconds);
237 maxdepth = round_depth_up(dive->maxdepth);
239 /* Time markers: every 5 min */
240 gc->scalex = maxtime;
242 for (i = 5*60; i < maxtime; i += 5*60) {
247 /* Depth markers: every 30 ft or 10 m*/
249 gc->scaley = maxdepth;
250 switch (output_units.length) {
251 case METERS: marker = 10000; break;
252 case FEET: marker = 9144; break; /* 30 ft */
255 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
256 for (i = marker; i < maxdepth; i += marker) {
262 /* Show mean depth */
263 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
264 move_to(gc, 0, dive->meandepth.mm);
265 line_to(gc, 1, dive->meandepth.mm);
268 gc->scalex = maxtime;
270 sample = dive->sample;
271 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
272 begins = sample->time.seconds;
273 move_to(gc, sample->time.seconds, sample->depth.mm);
274 for (i = 1; i < dive->samples; i++) {
276 sec = sample->time.seconds;
277 if (sec <= maxtime) {
278 depth = sample->depth.mm;
279 line_to(gc, sec, depth);
283 line_to(gc, MIN(sec,maxtime), 0);
284 line_to(gc, begins, 0);
285 cairo_close_path(cr);
286 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
287 cairo_fill_preserve(cr);
288 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
292 /* gets both the actual start and end pressure as well as the scaling factors */
293 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
294 pressure_t *startp, pressure_t *endp)
299 gc->scalex = round_seconds_up(dive->duration.seconds);
304 startp->mbar = endp->mbar = 0;
306 for (i = 0; i < dive->samples; i++) {
308 struct sample *sample = dive->sample + i;
310 /* FIXME! We only track cylinder 0 right now */
311 if (sample->cylinderindex)
313 mbar = sample->cylinderpressure.mbar;
327 gc->scaley = max * 1.5;
331 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
335 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
338 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
340 move_to(gc, 0, dive->cylinder[0].start.mbar);
341 for (i = 1; i < dive->samples; i++) {
343 struct sample *sample = dive->sample + i;
345 mbar = sample->cylinderpressure.mbar;
348 sec = sample->time.seconds;
349 if (sec <= dive->duration.seconds)
350 line_to(gc, sec, mbar);
353 * We may have "surface time" events, in which case we don't go
354 * back to dive duration
356 if (sec < dive->duration.seconds)
357 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
358 cairo_stroke(gc->cr);
362 * Return air usage (in liters).
364 static double calculate_airuse(struct dive *dive)
369 for (i = 0; i < MAX_CYLINDERS; i++) {
370 cylinder_t *cyl = dive->cylinder + i;
371 int size = cyl->type.size.mliter;
377 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
379 /* Liters of air at 1 atm == milliliters at 1k atm*/
380 airuse += kilo_atm * size;
385 static void plot_info(struct dive *dive, struct graphics_context *gc)
387 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
388 const double liters_per_cuft = 28.317;
392 airuse = calculate_airuse(dive);
396 /* I really need to start addign some unit setting thing */
397 switch (output_units.volume) {
403 airuse /= liters_per_cuft;
406 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
407 if (dive->duration.seconds) {
408 double pressure = 1 + (dive->meandepth.mm / 10000.0);
409 double sac = airuse / pressure * 60 / dive->duration.seconds;
410 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
414 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
416 pressure_t startp, endp;
418 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
420 const char *unit = "bar";
422 switch (output_units.pressure) {
424 start = startp.mbar * 100;
425 end = startp.mbar * 100;
429 start = (startp.mbar + 500) / 1000;
430 end = (endp.mbar + 500) / 1000;
434 start = to_PSI(startp);
440 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
441 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
442 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
447 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
453 cairo_translate(gc->cr, topx, topy);
456 * We can use "cairo_translate()" because that doesn't
457 * scale line width etc. But the actual scaling we need
458 * do set up ourselves..
462 gc->maxx = (w - 2*topx);
463 gc->maxy = (h - 2*topy);
465 /* Cylinder pressure plot */
466 plot_cylinder_pressure(dive, gc);
469 plot_depth_profile(dive, gc);
471 /* Text on top of all graphs.. */
472 plot_depth_text(dive, gc);
473 plot_cylinder_pressure_text(dive, gc);
475 /* And info box in the lower right corner.. */
476 gc->scalex = gc->scaley = 1.0;
479 /* Bounding box last */
480 cairo_set_source_rgb(gc->cr, 1, 1, 1);
485 cairo_close_path(gc->cr);
486 cairo_stroke(gc->cr);
490 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
492 struct dive *dive = current_dive;
493 struct graphics_context gc;
496 w = widget->allocation.width;
497 h = widget->allocation.height;
499 gc.cr = gdk_cairo_create(widget->window);
500 cairo_set_source_rgb(gc.cr, 0, 0, 0);
504 plot(&gc, w, h, dive);
506 cairo_destroy(gc.cr);
511 GtkWidget *dive_profile_widget(void)
515 da = gtk_drawing_area_new();
516 gtk_widget_set_size_request(da, 450, 350);
517 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);