]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Actually connect the cylinder configuration with the dive
[ext/subsurface.git] / equipment.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <time.h>
6
7 #include "dive.h"
8 #include "display.h"
9 #include "divelist.h"
10
11 static int cylinder_changed;
12 static GtkComboBox *cylinder_description;
13 static GtkSpinButton *cylinder_size, *cylinder_pressure, *nitrox_value;
14
15 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
16 {
17         GtkTreeIter iter;
18         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
19         GValue value1 = {0, }, value2 = {0,};
20         int volume, pressure;
21         cylinder_t *cyl = current_dive->cylinder + 0;
22
23         /* Did the user set it to some non-standard value? */
24         if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
25                 cylinder_changed = 1;
26                 return;
27         }
28
29         /*
30          * We get "change" signal callbacks just because we set
31          * the description by hand. Whatever. So ignore them if
32          * they are no-ops.
33          */
34         if (!cylinder_changed && cyl->type.description) {
35                 int same;
36                 char *desc = gtk_combo_box_get_active_text(combo_box);
37
38                 same = !strcmp(desc, cyl->type.description);
39                 g_free(desc);
40                 if (same)
41                         return;
42         }
43         cylinder_changed = 1;
44
45         gtk_tree_model_get_value(model, &iter, 1, &value1);
46         volume = g_value_get_int(&value1);
47         gtk_tree_model_get_value(model, &iter, 2, &value2);
48         pressure = g_value_get_int(&value2);
49
50         gtk_spin_button_set_value(cylinder_size,
51                         volume / 1000.0);
52         gtk_spin_button_set_value(cylinder_pressure,
53                         pressure / 1000.0);
54 }
55
56 static gboolean match_cylinder(GtkTreeModel *model,
57                                 GtkTreePath *path,
58                                 GtkTreeIter *iter,
59                                 gpointer data)
60 {
61         const char *name;
62         const char *desc = data;
63         GValue value = {0, };
64
65         gtk_tree_model_get_value(model, iter, 0, &value);
66         name = g_value_get_string(&value);
67         if (strcmp(desc, name))
68                 return FALSE;
69         gtk_combo_box_set_active_iter(cylinder_description, iter);
70         return TRUE;
71 }
72
73 void show_dive_equipment(struct dive *dive)
74 {
75         cylinder_t *cyl = &dive->cylinder[0];
76         const char *desc = cyl->type.description;
77         GtkTreeModel *model = gtk_combo_box_get_model(cylinder_description);
78         double o2;
79
80         if (desc)
81                 gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
82         gtk_spin_button_set_value(cylinder_size,
83                         cyl->type.size.mliter / 1000.0);
84         gtk_spin_button_set_value(cylinder_pressure,
85                         cyl->type.workingpressure.mbar / 1000.0);
86         o2 = cyl->gasmix.o2.permille / 10.0;
87         if (!o2)
88                 o2 = 21.0;
89         gtk_spin_button_set_value(nitrox_value, o2);
90 }
91
92 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
93 {
94         GtkWidget *frame, *button;
95
96         frame = gtk_frame_new(name);
97         gtk_container_add(GTK_CONTAINER(vbox), frame);
98
99         button = gtk_spin_button_new_with_range(min, max, incr);
100         gtk_container_add(GTK_CONTAINER(frame), button);
101
102         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
103
104         return button;
105 }
106
107 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, int mliter, int mbar, int o2)
108 {
109         if (o2 < 211)
110                 o2 = 0;
111         cyl->type.description = desc;
112         cyl->type.size.mliter = mliter;
113         cyl->type.workingpressure.mbar = mbar;
114         cyl->gasmix.o2.permille = o2;
115 }
116
117 static void record_cylinder_changes(struct dive *dive)
118 {
119         const gchar *desc;
120         GtkComboBox *box = cylinder_description;
121         int volume, pressure, o2;
122
123         desc = gtk_combo_box_get_active_text(box);
124         volume = gtk_spin_button_get_value(cylinder_size) * 1000 + 0.5;
125         pressure = gtk_spin_button_get_value(cylinder_pressure) * 1000 + 0.5;
126         o2 = gtk_spin_button_get_value(nitrox_value)*10 + 0.5;
127         fill_cylinder_info(dive->cylinder+0, desc, volume, pressure, o2);
128 }
129
130 void flush_dive_equipment_changes(struct dive *dive)
131 {
132         record_cylinder_changes(dive);
133 }
134
135 /* We should take these from the dive list instead */
136 static struct tank_info {
137         const char *name;
138         int size;       /* cuft or mliter depending on psi */
139         int psi;        /* If zero, size is in mliter */
140 } tank_info[] = {
141         { "None", 0, 0 },
142         { "10.0 l", 10000 },
143         { "11.1 l", 11100 },
144         { "AL72", 72, 3000 },
145         { "AL80", 80, 3000 },
146         { "LP85", 85, 2400 },
147         { "LP95", 95, 2400 },
148         { "LP85+", 85, 2640 },
149         { "LP95+", 95, 2640 },
150         { "HP100", 100, 3442 },
151         { "HP119", 119, 3442 },
152         { NULL, }
153 };
154
155 static void fill_tank_list(GtkListStore *store)
156 {
157         GtkTreeIter iter;
158
159         struct tank_info *info = tank_info;
160
161         while (info->name) {
162                 int size = info->size;
163                 int psi = info->psi;
164                 int mbar = 0, ml = size;
165
166                 /* Is it in cuft and psi? */
167                 if (psi) {
168                         double bar = 0.0689475729 * psi;
169                         double airvolume = 28316.8466 * size;
170                         double atm = bar / 1.01325;
171
172                         ml = airvolume / atm + 0.5;
173                         mbar = bar*1000 + 0.5;
174                 }
175
176                 gtk_list_store_append(store, &iter);
177                 gtk_list_store_set(store, &iter,
178                         0, info->name,
179                         1, ml,
180                         2, mbar,
181                         -1);
182                 info++;
183         }
184 }
185
186 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
187 {
188         GtkWidget *frame, *hbox;
189         GtkWidget *widget;
190         char buffer[80];
191
192         snprintf(buffer, sizeof(buffer), "Cylinder %d", nr);
193         frame = gtk_frame_new(buffer);
194         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0);
195
196         hbox = gtk_hbox_new(TRUE, 3);
197         gtk_container_add(GTK_CONTAINER(frame), hbox);
198
199         frame = gtk_frame_new("Description");
200         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, FALSE, 0);
201
202         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
203         gtk_container_add(GTK_CONTAINER(frame), widget);
204
205         cylinder_description = GTK_COMBO_BOX(widget);
206         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), NULL);
207
208         widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
209         cylinder_size = GTK_SPIN_BUTTON(widget);
210
211         widget = create_spinbutton(hbox, "Working Pressure", 0, 5000, 1);
212         cylinder_pressure = GTK_SPIN_BUTTON(widget);
213
214         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
215         nitrox_value = GTK_SPIN_BUTTON(widget);
216         gtk_spin_button_set_range(nitrox_value, 21.0, 100.0);
217 }
218
219 static GtkListStore *create_tank_size_model(void)
220 {
221         GtkListStore *model;
222
223         model = gtk_list_store_new(3,
224                 G_TYPE_STRING,          /* Tank name */
225                 G_TYPE_INT,             /* Tank size in mliter */
226                 G_TYPE_INT,             /* Tank working pressure in mbar */
227                 -1);
228
229         fill_tank_list(model);
230         return model;
231 }
232
233 GtkWidget *equipment_widget(void)
234 {
235         GtkWidget *vbox;
236         GtkListStore *model;
237
238         vbox = gtk_vbox_new(FALSE, 3);
239
240         model = create_tank_size_model();
241         cylinder_widget(vbox, 0, model);
242
243         return vbox;
244 }