]> git.tdb.fi Git - ext/subsurface.git/blob - divelist.c
Round the maximum depth on dive list
[ext/subsurface.git] / divelist.c
1 /* divelist.c */
2 /* this creates the UI for the dive list -
3  * controlled through the following interfaces:
4  *
5  * void flush_divelist(struct dive *dive)
6  * GtkWidget dive_list_create(void)
7  * void dive_list_update_dives(void)
8  * void update_dive_list_units(void)
9  * void set_divelist_font(const char *font)
10  * void mark_divelist_changed(int changed)
11  * int unsaved_changes()
12  */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <math.h>
18
19 #include "divelist.h"
20 #include "dive.h"
21 #include "display.h"
22 #include "display-gtk.h"
23
24 struct DiveList {
25         GtkWidget    *tree_view;
26         GtkWidget    *container_widget;
27         GtkListStore *model;
28         GtkTreeViewColumn *nr, *date, *stars, *depth, *duration, *location;
29         GtkTreeViewColumn *temperature, *cylinder, *nitrox, *sac, *otu;
30         int changed;
31 };
32
33 static struct DiveList dive_list;
34
35 /*
36  * The dive list has the dive data in both string format (for showing)
37  * and in "raw" format (for sorting purposes)
38  */
39 enum {
40         DIVE_INDEX = 0,
41         DIVE_NR,                /* int: dive->nr */
42         DIVE_DATE,              /* time_t: dive->when */
43         DIVE_RATING,            /* int: 0-5 stars */
44         DIVE_DEPTH,             /* int: dive->maxdepth in mm */
45         DIVE_DURATION,          /* int: in seconds */
46         DIVE_TEMPERATURE,       /* int: in mkelvin */
47         DIVE_CYLINDER,
48         DIVE_NITROX,            /* int: in permille */
49         DIVE_SAC,               /* int: in ml/min */
50         DIVE_OTU,               /* int: in OTUs */
51         DIVE_LOCATION,          /* "2nd Cathedral, Lanai" */
52         DIVELIST_COLUMNS
53 };
54
55 static GList *selected_dives;
56
57 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
58 {
59         GtkTreeIter iter;
60         GValue value = {0, };
61         GtkTreePath *path;
62
63         int nr_selected = gtk_tree_selection_count_selected_rows(selection);
64
65         if (selected_dives) {
66                 g_list_foreach (selected_dives, (GFunc) gtk_tree_path_free, NULL);
67                 g_list_free (selected_dives);
68         }
69         selected_dives = gtk_tree_selection_get_selected_rows(selection, NULL);
70
71         switch (nr_selected) {
72         case 0: /* keep showing the last selected dive */
73                 return;
74         case 1: 
75                 /* just pick that dive as selected */
76                 path = g_list_nth_data(selected_dives, 0);
77                 if (gtk_tree_model_get_iter(model, &iter, path)) {
78                         gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
79                         selected_dive = g_value_get_int(&value);
80                         repaint_dive();
81                 }
82                 return;
83         default: /* multiple selections - what now? At this point I
84                   * don't want to change the selected dive unless
85                   * there is exactly one dive selected; not sure this
86                   * is the most intuitive solution.
87                   * I do however want to keep around which dives have
88                   * been selected */
89                 return;
90         }
91 }
92
93 const char *star_strings[] = {
94         ZERO_STARS,
95         ONE_STARS,
96         TWO_STARS,
97         THREE_STARS,
98         FOUR_STARS,
99         FIVE_STARS
100 };
101
102 static void star_data_func(GtkTreeViewColumn *col,
103                            GtkCellRenderer *renderer,
104                            GtkTreeModel *model,
105                            GtkTreeIter *iter,
106                            gpointer data)
107 {
108         int nr_stars;
109         char buffer[40];
110
111         gtk_tree_model_get(model, iter, DIVE_RATING, &nr_stars, -1);
112         if (nr_stars < 0 || nr_stars > 5)
113                 nr_stars = 0;
114         snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
115         g_object_set(renderer, "text", buffer, NULL);
116 }
117
118 static void date_data_func(GtkTreeViewColumn *col,
119                            GtkCellRenderer *renderer,
120                            GtkTreeModel *model,
121                            GtkTreeIter *iter,
122                            gpointer data)
123 {
124         int val;
125         struct tm *tm;
126         time_t when;
127         char buffer[40];
128
129         gtk_tree_model_get(model, iter, DIVE_DATE, &val, -1);
130
131         /* 2038 problem */
132         when = val;
133
134         tm = gmtime(&when);
135         snprintf(buffer, sizeof(buffer),
136                 "%s, %s %d, %d %02d:%02d",
137                 weekday(tm->tm_wday),
138                 monthname(tm->tm_mon),
139                 tm->tm_mday, tm->tm_year + 1900,
140                 tm->tm_hour, tm->tm_min);
141         g_object_set(renderer, "text", buffer, NULL);
142 }
143
144 static void depth_data_func(GtkTreeViewColumn *col,
145                             GtkCellRenderer *renderer,
146                             GtkTreeModel *model,
147                             GtkTreeIter *iter,
148                             gpointer data)
149 {
150         int depth, integer, frac, len;
151         char buffer[40];
152
153         gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1);
154
155         switch (output_units.length) {
156         case METERS:
157                 /* To tenths of meters */
158                 depth = (depth + 49) / 100;
159                 integer = depth / 10;
160                 frac = depth % 10;
161                 if (integer < 20)
162                         break;
163                 if (frac >= 5)
164                         integer++;
165                 frac = -1;
166                 break;
167         case FEET:
168                 integer = mm_to_feet(depth) + 0.5;
169                 frac = -1;
170                 break;
171         default:
172                 return;
173         }
174         len = snprintf(buffer, sizeof(buffer), "%d", integer);
175         if (frac >= 0)
176                 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
177
178         g_object_set(renderer, "text", buffer, NULL);
179 }
180
181 static void duration_data_func(GtkTreeViewColumn *col,
182                                GtkCellRenderer *renderer,
183                                GtkTreeModel *model,
184                                GtkTreeIter *iter,
185                                gpointer data)
186 {
187         unsigned int sec;
188         char buffer[16];
189
190         gtk_tree_model_get(model, iter, DIVE_DURATION, &sec, -1);
191         snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
192
193         g_object_set(renderer, "text", buffer, NULL);
194 }
195
196 static void temperature_data_func(GtkTreeViewColumn *col,
197                                   GtkCellRenderer *renderer,
198                                   GtkTreeModel *model,
199                                   GtkTreeIter *iter,
200                                   gpointer data)
201 {
202         int value;
203         char buffer[80];
204
205         gtk_tree_model_get(model, iter, DIVE_TEMPERATURE, &value, -1);
206
207         *buffer = 0;
208         if (value) {
209                 double deg;
210                 switch (output_units.temperature) {
211                 case CELSIUS:
212                         deg = mkelvin_to_C(value);
213                         break;
214                 case FAHRENHEIT:
215                         deg = mkelvin_to_F(value);
216                         break;
217                 default:
218                         return;
219                 }
220                 snprintf(buffer, sizeof(buffer), "%.1f", deg);
221         }
222
223         g_object_set(renderer, "text", buffer, NULL);
224 }
225
226 static void nitrox_data_func(GtkTreeViewColumn *col,
227                              GtkCellRenderer *renderer,
228                              GtkTreeModel *model,
229                              GtkTreeIter *iter,
230                              gpointer data)
231 {
232         int value;
233         char buffer[80];
234
235         gtk_tree_model_get(model, iter, DIVE_NITROX, &value, -1);
236
237         if (value)
238                 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
239         else
240                 strcpy(buffer, "air");
241
242         g_object_set(renderer, "text", buffer, NULL);
243 }
244
245 /* Render the SAC data (integer value of "ml / min") */
246 static void sac_data_func(GtkTreeViewColumn *col,
247                           GtkCellRenderer *renderer,
248                           GtkTreeModel *model,
249                           GtkTreeIter *iter,
250                           gpointer data)
251 {
252         int value;
253         const char *fmt;
254         char buffer[16];
255         double sac;
256
257         gtk_tree_model_get(model, iter, DIVE_SAC, &value, -1);
258
259         if (!value) {
260                 g_object_set(renderer, "text", "", NULL);
261                 return;
262         }
263
264         sac = value / 1000.0;
265         switch (output_units.volume) {
266         case LITER:
267                 fmt = "%4.1f";
268                 break;
269         case CUFT:
270                 fmt = "%4.2f";
271                 sac = ml_to_cuft(sac * 1000);
272                 break;
273         }
274         snprintf(buffer, sizeof(buffer), fmt, sac);
275
276         g_object_set(renderer, "text", buffer, NULL);
277 }
278
279 /* Render the OTU data (integer value of "OTU") */
280 static void otu_data_func(GtkTreeViewColumn *col,
281                           GtkCellRenderer *renderer,
282                           GtkTreeModel *model,
283                           GtkTreeIter *iter,
284                           gpointer data)
285 {
286         int value;
287         char buffer[16];
288
289         gtk_tree_model_get(model, iter, DIVE_OTU, &value, -1);
290
291         if (!value) {
292                 g_object_set(renderer, "text", "", NULL);
293                 return;
294         }
295
296         snprintf(buffer, sizeof(buffer), "%d", value);
297
298         g_object_set(renderer, "text", buffer, NULL);
299 }
300
301 /* calculate OTU for a dive */
302 static int calculate_otu(struct dive *dive)
303 {
304         int i;
305         double otu = 0.0;
306
307         for (i = 1; i < dive->samples; i++) {
308                 int t;
309                 double po2;
310                 struct sample *sample = dive->sample + i;
311                 struct sample *psample = sample - 1;
312                 t = sample->time.seconds - psample->time.seconds;
313                 po2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille / 1000.0 *
314                         (sample->depth.mm + 10000) / 10000.0;
315                 if (po2 >= 0.5)
316                         otu += pow(po2 - 0.5, 0.83) * t / 30.0;
317         }
318         return otu + 0.5;
319 }
320 /*
321  * Return air usage (in liters).
322  */
323 static double calculate_airuse(struct dive *dive)
324 {
325         double airuse = 0;
326         int i;
327
328         for (i = 0; i < MAX_CYLINDERS; i++) {
329                 pressure_t start, end;
330                 cylinder_t *cyl = dive->cylinder + i;
331                 int size = cyl->type.size.mliter;
332                 double kilo_atm;
333
334                 if (!size)
335                         continue;
336
337                 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
338                 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
339                 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
340
341                 /* Liters of air at 1 atm == milliliters at 1k atm*/
342                 airuse += kilo_atm * size;
343         }
344         return airuse;
345 }
346
347 static int calculate_sac(struct dive *dive)
348 {
349         double airuse, pressure, sac;
350         int duration, i;
351
352         airuse = calculate_airuse(dive);
353         if (!airuse)
354                 return 0;
355         if (!dive->duration.seconds)
356                 return 0;
357
358         /* find and eliminate long surface intervals */
359         duration = dive->duration.seconds;
360         for (i = 0; i < dive->samples; i++) {
361                 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
362                         int end = i + 1;
363                         while (end < dive->samples && dive->sample[end].depth.mm < 100)
364                                 end++;
365                         /* we only want the actual surface time during a dive */
366                         if (end < dive->samples) {
367                                 end--;
368                                 duration -= dive->sample[end].time.seconds -
369                                                 dive->sample[i].time.seconds;
370                                 i = end + 1;
371                         }
372                 }
373         }
374         /* Mean pressure in atm: 1 atm per 10m */
375         pressure = 1 + (dive->meandepth.mm / 10000.0);
376         sac = airuse / pressure * 60 / duration;
377
378         /* milliliters per minute.. */
379         return sac * 1000;
380 }
381
382 void update_cylinder_related_info(struct dive *dive)
383 {
384         if (dive != NULL) {
385                 dive->sac = calculate_sac(dive);
386                 dive->otu = calculate_otu(dive);
387         }
388 }
389
390 static void get_string(char **str, const char *s)
391 {
392         int len;
393         char *n;
394
395         if (!s)
396                 s = "";
397         len = strlen(s);
398         if (len > 60)
399                 len = 60;
400         n = malloc(len+1);
401         memcpy(n, s, len);
402         n[len] = 0;
403         *str = n;
404 }
405
406 static void get_location(struct dive *dive, char **str)
407 {
408         get_string(str, dive->location);
409 }
410
411 static void get_cylinder(struct dive *dive, char **str)
412 {
413         get_string(str, dive->cylinder[0].type.description);
414 }
415
416 static void fill_one_dive(struct dive *dive,
417                           GtkTreeModel *model,
418                           GtkTreeIter *iter)
419 {
420         char *location, *cylinder;
421
422         get_cylinder(dive, &cylinder);
423         get_location(dive, &location);
424
425         /*
426          * We only set the fields that changed: the strings.
427          * The core data itself is unaffected by units
428          */
429         gtk_list_store_set(GTK_LIST_STORE(model), iter,
430                 DIVE_NR, dive->number,
431                 DIVE_LOCATION, location,
432                 DIVE_CYLINDER, cylinder,
433                 DIVE_RATING, dive->rating,
434                 DIVE_SAC, dive->sac,
435                 DIVE_OTU, dive->otu,
436                 -1);
437 }
438
439 static gboolean set_one_dive(GtkTreeModel *model,
440                              GtkTreePath *path,
441                              GtkTreeIter *iter,
442                              gpointer data)
443 {
444         GValue value = {0, };
445         struct dive *dive;
446
447         /* Get the dive number */
448         gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
449         dive = get_dive(g_value_get_int(&value));
450         if (!dive)
451                 return TRUE;
452         if (data && dive != data)
453                 return FALSE;
454
455         fill_one_dive(dive, model, iter);
456         return dive == data;
457 }
458
459 void flush_divelist(struct dive *dive)
460 {
461         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
462
463         gtk_tree_model_foreach(model, set_one_dive, dive);
464 }
465
466 void set_divelist_font(const char *font)
467 {
468         PangoFontDescription *font_desc = pango_font_description_from_string(font);
469         gtk_widget_modify_font(dive_list.tree_view, font_desc);
470         pango_font_description_free(font_desc);
471 }
472
473 void update_dive_list_units(void)
474 {
475         const char *unit;
476         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
477
478         (void) get_depth_units(0, NULL, &unit);
479         gtk_tree_view_column_set_title(dive_list.depth, unit);
480
481         (void) get_temp_units(0, &unit);
482         gtk_tree_view_column_set_title(dive_list.temperature, unit);
483
484         gtk_tree_model_foreach(model, set_one_dive, NULL);
485 }
486
487 void update_dive_list_col_visibility(void)
488 {
489         gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
490         gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
491         gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
492         gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
493         gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
494         return;
495 }
496
497 static void fill_dive_list(void)
498 {
499         int i;
500         GtkTreeIter iter;
501         GtkListStore *store;
502
503         store = GTK_LIST_STORE(dive_list.model);
504
505         i = dive_table.nr;
506         while (--i >= 0) {
507                 struct dive *dive = dive_table.dives[i];
508
509                 update_cylinder_related_info(dive);
510                 gtk_list_store_append(store, &iter);
511                 gtk_list_store_set(store, &iter,
512                         DIVE_INDEX, i,
513                         DIVE_NR, dive->number,
514                         DIVE_DATE, dive->when,
515                         DIVE_DEPTH, dive->maxdepth,
516                         DIVE_DURATION, dive->duration.seconds,
517                         DIVE_LOCATION, "location",
518                         DIVE_TEMPERATURE, dive->watertemp.mkelvin,
519                         DIVE_NITROX, dive->cylinder[0].gasmix.o2,
520                         DIVE_SAC, 0,
521                         -1);
522         }
523
524         update_dive_list_units();
525         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
526                 GtkTreeSelection *selection;
527                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
528                 gtk_tree_selection_select_iter(selection, &iter);
529         }
530 }
531
532 void dive_list_update_dives(void)
533 {
534         gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
535         fill_dive_list();
536         repaint_dive();
537 }
538
539 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
540                                 data_func_t data_func, PangoAlignment align, gboolean visible)
541 {
542         return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
543 }
544
545 /*
546  * This is some crazy crap. The only way to get default focus seems
547  * to be to grab focus as the widget is being shown the first time.
548  */
549 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
550 {
551         gtk_widget_grab_focus(tree_view);
552 }
553
554 static void row_activated_cb(GtkTreeView *tree_view,
555                         GtkTreePath *path,
556                         GtkTreeViewColumn *column,
557                         GtkTreeModel *model)
558 {
559         int index;
560         GtkTreeIter iter;
561
562         if (!gtk_tree_model_get_iter(model, &iter, path))
563                 return;
564         gtk_tree_model_get(model, &iter, DIVE_INDEX, &index, -1);
565         edit_dive_info(get_dive(index));
566 }
567
568 GtkWidget *dive_list_create(void)
569 {
570         GtkTreeSelection  *selection;
571
572         dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
573                                 G_TYPE_INT,                     /* index */
574                                 G_TYPE_INT,                     /* nr */
575                                 G_TYPE_INT,                     /* Date */
576                                 G_TYPE_INT,                     /* Star rating */
577                                 G_TYPE_INT,                     /* Depth */
578                                 G_TYPE_INT,                     /* Duration */
579                                 G_TYPE_INT,                     /* Temperature */
580                                 G_TYPE_STRING,                  /* Cylinder */
581                                 G_TYPE_INT,                     /* Nitrox */
582                                 G_TYPE_INT,                     /* SAC */
583                                 G_TYPE_INT,                     /* OTU */
584                                 G_TYPE_STRING                   /* Location */
585                                 );
586         dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
587         set_divelist_font(divelist_font);
588
589         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
590
591         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
592         gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
593
594         dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE);
595         gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1);
596         dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
597         dive_list.stars = divelist_column(&dive_list, DIVE_RATING, UTF8_BLACKSTAR, star_data_func, PANGO_ALIGN_LEFT, TRUE);
598         dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
599         dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
600         dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature);
601         dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, visible_cols.cylinder);
602         dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, visible_cols.nitrox);
603         dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
604         dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
605         dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
606
607         fill_dive_list();
608
609         g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
610                                           "search-column", DIVE_LOCATION,
611                                           "rules-hint", TRUE,
612                                           NULL);
613
614         g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
615         g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), dive_list.model);
616         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
617
618         dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
619         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
620                                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
621         gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
622
623         dive_list.changed = 0;
624
625         return dive_list.container_widget;
626 }
627
628 void mark_divelist_changed(int changed)
629 {
630         dive_list.changed = changed;
631 }
632
633 int unsaved_changes()
634 {
635         return dive_list.changed;
636 }