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