]> git.tdb.fi Git - ext/subsurface.git/blob - divelist.c
Merge branch 'stars' of git://git.hohndel.org/subsurface
[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                 frac = -1;
164                 /* Rounding? */
165                 break;
166         case FEET:
167                 integer = mm_to_feet(depth) + 0.5;
168                 frac = -1;
169                 break;
170         default:
171                 return;
172         }
173         len = snprintf(buffer, sizeof(buffer), "%d", integer);
174         if (frac >= 0)
175                 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
176
177         g_object_set(renderer, "text", buffer, NULL);
178 }
179
180 static void duration_data_func(GtkTreeViewColumn *col,
181                                GtkCellRenderer *renderer,
182                                GtkTreeModel *model,
183                                GtkTreeIter *iter,
184                                gpointer data)
185 {
186         unsigned int sec;
187         char buffer[16];
188
189         gtk_tree_model_get(model, iter, DIVE_DURATION, &sec, -1);
190         snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
191
192         g_object_set(renderer, "text", buffer, NULL);
193 }
194
195 static void temperature_data_func(GtkTreeViewColumn *col,
196                                   GtkCellRenderer *renderer,
197                                   GtkTreeModel *model,
198                                   GtkTreeIter *iter,
199                                   gpointer data)
200 {
201         int value;
202         char buffer[80];
203
204         gtk_tree_model_get(model, iter, DIVE_TEMPERATURE, &value, -1);
205
206         *buffer = 0;
207         if (value) {
208                 double deg;
209                 switch (output_units.temperature) {
210                 case CELSIUS:
211                         deg = mkelvin_to_C(value);
212                         break;
213                 case FAHRENHEIT:
214                         deg = mkelvin_to_F(value);
215                         break;
216                 default:
217                         return;
218                 }
219                 snprintf(buffer, sizeof(buffer), "%.1f", deg);
220         }
221
222         g_object_set(renderer, "text", buffer, NULL);
223 }
224
225 static void nitrox_data_func(GtkTreeViewColumn *col,
226                              GtkCellRenderer *renderer,
227                              GtkTreeModel *model,
228                              GtkTreeIter *iter,
229                              gpointer data)
230 {
231         int value;
232         char buffer[80];
233
234         gtk_tree_model_get(model, iter, DIVE_NITROX, &value, -1);
235
236         if (value)
237                 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
238         else
239                 strcpy(buffer, "air");
240
241         g_object_set(renderer, "text", buffer, NULL);
242 }
243
244 /* Render the SAC data (integer value of "ml / min") */
245 static void sac_data_func(GtkTreeViewColumn *col,
246                           GtkCellRenderer *renderer,
247                           GtkTreeModel *model,
248                           GtkTreeIter *iter,
249                           gpointer data)
250 {
251         int value;
252         const char *fmt;
253         char buffer[16];
254         double sac;
255
256         gtk_tree_model_get(model, iter, DIVE_SAC, &value, -1);
257
258         if (!value) {
259                 g_object_set(renderer, "text", "", NULL);
260                 return;
261         }
262
263         sac = value / 1000.0;
264         switch (output_units.volume) {
265         case LITER:
266                 fmt = "%4.1f";
267                 break;
268         case CUFT:
269                 fmt = "%4.2f";
270                 sac = ml_to_cuft(sac * 1000);
271                 break;
272         }
273         snprintf(buffer, sizeof(buffer), fmt, sac);
274
275         g_object_set(renderer, "text", buffer, NULL);
276 }
277
278 /* Render the OTU data (integer value of "OTU") */
279 static void otu_data_func(GtkTreeViewColumn *col,
280                           GtkCellRenderer *renderer,
281                           GtkTreeModel *model,
282                           GtkTreeIter *iter,
283                           gpointer data)
284 {
285         int value;
286         char buffer[16];
287
288         gtk_tree_model_get(model, iter, DIVE_OTU, &value, -1);
289
290         if (!value) {
291                 g_object_set(renderer, "text", "", NULL);
292                 return;
293         }
294
295         snprintf(buffer, sizeof(buffer), "%d", value);
296
297         g_object_set(renderer, "text", buffer, NULL);
298 }
299
300 /* calculate OTU for a dive */
301 static int calculate_otu(struct dive *dive)
302 {
303         int i;
304         double otu = 0.0;
305
306         for (i = 1; i < dive->samples; i++) {
307                 int t;
308                 double po2;
309                 struct sample *sample = dive->sample + i;
310                 struct sample *psample = sample - 1;
311                 t = sample->time.seconds - psample->time.seconds;
312                 po2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille / 1000.0 *
313                         (sample->depth.mm + 10000) / 10000.0;
314                 if (po2 >= 0.5)
315                         otu += pow(po2 - 0.5, 0.83) * t / 30.0;
316         }
317         return otu + 0.5;
318 }
319 /*
320  * Return air usage (in liters).
321  */
322 static double calculate_airuse(struct dive *dive)
323 {
324         double airuse = 0;
325         int i;
326
327         for (i = 0; i < MAX_CYLINDERS; i++) {
328                 pressure_t start, end;
329                 cylinder_t *cyl = dive->cylinder + i;
330                 int size = cyl->type.size.mliter;
331                 double kilo_atm;
332
333                 if (!size)
334                         continue;
335
336                 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
337                 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
338                 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
339
340                 /* Liters of air at 1 atm == milliliters at 1k atm*/
341                 airuse += kilo_atm * size;
342         }
343         return airuse;
344 }
345
346 static int calculate_sac(struct dive *dive)
347 {
348         double airuse, pressure, sac;
349         int duration, i;
350
351         airuse = calculate_airuse(dive);
352         if (!airuse)
353                 return 0;
354         if (!dive->duration.seconds)
355                 return 0;
356
357         /* find and eliminate long surface intervals */
358         duration = dive->duration.seconds;
359         for (i = 0; i < dive->samples; i++) {
360                 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
361                         int end = i + 1;
362                         while (end < dive->samples && dive->sample[end].depth.mm < 100)
363                                 end++;
364                         /* we only want the actual surface time during a dive */
365                         if (end < dive->samples) {
366                                 end--;
367                                 duration -= dive->sample[end].time.seconds -
368                                                 dive->sample[i].time.seconds;
369                                 i = end + 1;
370                         }
371                 }
372         }
373         /* Mean pressure in atm: 1 atm per 10m */
374         pressure = 1 + (dive->meandepth.mm / 10000.0);
375         sac = airuse / pressure * 60 / duration;
376
377         /* milliliters per minute.. */
378         return sac * 1000;
379 }
380
381 void update_cylinder_related_info(struct dive *dive)
382 {
383         if (dive != NULL) {
384                 dive->sac = calculate_sac(dive);
385                 dive->otu = calculate_otu(dive);
386         }
387 }
388
389 static void get_string(char **str, const char *s)
390 {
391         int len;
392         char *n;
393
394         if (!s)
395                 s = "";
396         len = strlen(s);
397         if (len > 60)
398                 len = 60;
399         n = malloc(len+1);
400         memcpy(n, s, len);
401         n[len] = 0;
402         *str = n;
403 }
404
405 static void get_location(struct dive *dive, char **str)
406 {
407         get_string(str, dive->location);
408 }
409
410 static void get_cylinder(struct dive *dive, char **str)
411 {
412         get_string(str, dive->cylinder[0].type.description);
413 }
414
415 static void fill_one_dive(struct dive *dive,
416                           GtkTreeModel *model,
417                           GtkTreeIter *iter)
418 {
419         char *location, *cylinder;
420
421         get_cylinder(dive, &cylinder);
422         get_location(dive, &location);
423
424         /*
425          * We only set the fields that changed: the strings.
426          * The core data itself is unaffected by units
427          */
428         gtk_list_store_set(GTK_LIST_STORE(model), iter,
429                 DIVE_NR, dive->number,
430                 DIVE_LOCATION, location,
431                 DIVE_CYLINDER, cylinder,
432                 DIVE_RATING, dive->rating,
433                 DIVE_SAC, dive->sac,
434                 DIVE_OTU, dive->otu,
435                 -1);
436 }
437
438 static gboolean set_one_dive(GtkTreeModel *model,
439                              GtkTreePath *path,
440                              GtkTreeIter *iter,
441                              gpointer data)
442 {
443         GValue value = {0, };
444         struct dive *dive;
445
446         /* Get the dive number */
447         gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
448         dive = get_dive(g_value_get_int(&value));
449         if (!dive)
450                 return TRUE;
451         if (data && dive != data)
452                 return FALSE;
453
454         fill_one_dive(dive, model, iter);
455         return dive == data;
456 }
457
458 void flush_divelist(struct dive *dive)
459 {
460         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
461
462         gtk_tree_model_foreach(model, set_one_dive, dive);
463 }
464
465 void set_divelist_font(const char *font)
466 {
467         PangoFontDescription *font_desc = pango_font_description_from_string(font);
468         gtk_widget_modify_font(dive_list.tree_view, font_desc);
469         pango_font_description_free(font_desc);
470 }
471
472 void update_dive_list_units(void)
473 {
474         const char *unit;
475         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
476
477         (void) get_depth_units(0, NULL, &unit);
478         gtk_tree_view_column_set_title(dive_list.depth, unit);
479
480         (void) get_temp_units(0, &unit);
481         gtk_tree_view_column_set_title(dive_list.temperature, unit);
482
483         gtk_tree_model_foreach(model, set_one_dive, NULL);
484 }
485
486 void update_dive_list_col_visibility(void)
487 {
488         gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
489         gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
490         gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
491         gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
492         gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
493         return;
494 }
495
496 static void fill_dive_list(void)
497 {
498         int i;
499         GtkTreeIter iter;
500         GtkListStore *store;
501
502         store = GTK_LIST_STORE(dive_list.model);
503
504         i = dive_table.nr;
505         while (--i >= 0) {
506                 struct dive *dive = dive_table.dives[i];
507
508                 update_cylinder_related_info(dive);
509                 gtk_list_store_append(store, &iter);
510                 gtk_list_store_set(store, &iter,
511                         DIVE_INDEX, i,
512                         DIVE_NR, dive->number,
513                         DIVE_DATE, dive->when,
514                         DIVE_DEPTH, dive->maxdepth,
515                         DIVE_DURATION, dive->duration.seconds,
516                         DIVE_LOCATION, "location",
517                         DIVE_TEMPERATURE, dive->watertemp.mkelvin,
518                         DIVE_NITROX, dive->cylinder[0].gasmix.o2,
519                         DIVE_SAC, 0,
520                         -1);
521         }
522
523         update_dive_list_units();
524         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
525                 GtkTreeSelection *selection;
526                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
527                 gtk_tree_selection_select_iter(selection, &iter);
528         }
529 }
530
531 void dive_list_update_dives(void)
532 {
533         gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
534         fill_dive_list();
535         repaint_dive();
536 }
537
538 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
539                                 data_func_t data_func, PangoAlignment align, gboolean visible)
540 {
541         return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
542 }
543
544 /*
545  * This is some crazy crap. The only way to get default focus seems
546  * to be to grab focus as the widget is being shown the first time.
547  */
548 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
549 {
550         gtk_widget_grab_focus(tree_view);
551 }
552
553 static void row_activated_cb(GtkTreeView *tree_view,
554                         GtkTreePath *path,
555                         GtkTreeViewColumn *column,
556                         GtkTreeModel *model)
557 {
558         int index;
559         GtkTreeIter iter;
560
561         if (!gtk_tree_model_get_iter(model, &iter, path))
562                 return;
563         gtk_tree_model_get(model, &iter, DIVE_INDEX, &index, -1);
564         edit_dive_info(get_dive(index));
565 }
566
567 GtkWidget *dive_list_create(void)
568 {
569         GtkTreeSelection  *selection;
570
571         dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
572                                 G_TYPE_INT,                     /* index */
573                                 G_TYPE_INT,                     /* nr */
574                                 G_TYPE_INT,                     /* Date */
575                                 G_TYPE_INT,                     /* Star rating */
576                                 G_TYPE_INT,                     /* Depth */
577                                 G_TYPE_INT,                     /* Duration */
578                                 G_TYPE_INT,                     /* Temperature */
579                                 G_TYPE_STRING,                  /* Cylinder */
580                                 G_TYPE_INT,                     /* Nitrox */
581                                 G_TYPE_INT,                     /* SAC */
582                                 G_TYPE_INT,                     /* OTU */
583                                 G_TYPE_STRING                   /* Location */
584                                 );
585         dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
586         set_divelist_font(divelist_font);
587
588         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
589
590         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
591         gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
592
593         dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE);
594         gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1);
595         dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
596         dive_list.stars = divelist_column(&dive_list, DIVE_RATING, UTF8_BLACKSTAR, star_data_func, PANGO_ALIGN_LEFT, TRUE);
597         dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
598         dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
599         dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature);
600         dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, visible_cols.cylinder);
601         dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, visible_cols.nitrox);
602         dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
603         dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
604         dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
605
606         fill_dive_list();
607
608         g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
609                                           "search-column", DIVE_LOCATION,
610                                           "rules-hint", TRUE,
611                                           NULL);
612
613         g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
614         g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), dive_list.model);
615         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
616
617         dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
618         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
619                                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
620         gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
621
622         dive_list.changed = 0;
623
624         return dive_list.container_widget;
625 }
626
627 void mark_divelist_changed(int changed)
628 {
629         dive_list.changed = changed;
630 }
631
632 int unsaved_changes()
633 {
634         return dive_list.changed;
635 }