]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Make multiple cylinders actually work
[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 struct cylinder_widget {
12         int index, changed;
13         const char *name;
14         GtkComboBox *description;
15         GtkSpinButton *size, *pressure;
16         GtkWidget *o2, *gasmix_button;
17 };
18
19 static struct cylinder_widget gtk_cylinder[MAX_CYLINDERS];
20
21 static void set_cylinder_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
22 {
23         double volume, pressure;
24
25         volume = ml / 1000.0;
26         pressure = mbar / 1000.0;
27         if (mbar) {
28                 if (output_units.volume == CUFT) {
29                         volume /= 28.3168466;   /* Liters to cuft */
30                         volume *= pressure / 1.01325;
31                 }
32                 if (output_units.pressure == PSI) {
33                         pressure *= 14.5037738; /* Bar to PSI */
34                 }
35         }
36
37         gtk_spin_button_set_value(cylinder->size, volume);
38         gtk_spin_button_set_value(cylinder->pressure, pressure);
39 }
40
41 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
42 {
43         GtkTreeIter iter;
44         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
45         GValue value1 = {0, }, value2 = {0,};
46         struct cylinder_widget *cylinder = data;
47         cylinder_t *cyl = current_dive->cylinder + cylinder->index;
48
49         /* Did the user set it to some non-standard value? */
50         if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
51                 cylinder->changed = 1;
52                 return;
53         }
54
55         /*
56          * We get "change" signal callbacks just because we set
57          * the description by hand. Whatever. So ignore them if
58          * they are no-ops.
59          */
60         if (!cylinder->changed && cyl->type.description) {
61                 int same;
62                 char *desc = gtk_combo_box_get_active_text(combo_box);
63
64                 same = !strcmp(desc, cyl->type.description);
65                 g_free(desc);
66                 if (same)
67                         return;
68         }
69         cylinder->changed = 1;
70
71         gtk_tree_model_get_value(model, &iter, 1, &value1);
72         gtk_tree_model_get_value(model, &iter, 2, &value2);
73
74         set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
75 }
76
77 /*
78  * The gtk_tree_model_foreach() interface is bad. It could have
79  * returned whether the callback ever returned true
80  */
81 static int found_match = 0;
82
83 static gboolean match_cylinder(GtkTreeModel *model,
84                                 GtkTreePath *path,
85                                 GtkTreeIter *iter,
86                                 gpointer data)
87 {
88         const char *name;
89         struct cylinder_widget *cylinder = data;
90         GValue value = {0, };
91
92         gtk_tree_model_get_value(model, iter, 0, &value);
93         name = g_value_get_string(&value);
94         if (strcmp(cylinder->name, name))
95                 return FALSE;
96         gtk_combo_box_set_active_iter(cylinder->description, iter);
97         found_match = 1;
98         return TRUE;
99 }
100
101 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
102 {
103         GtkTreeModel *model;
104
105         found_match = 0;
106         model = gtk_combo_box_get_model(cylinder->description);
107         cylinder->name = desc;
108         gtk_tree_model_foreach(model, match_cylinder, cylinder);
109
110         if (!found_match) {
111                 GtkListStore *store = GTK_LIST_STORE(model);
112                 GtkTreeIter iter;
113
114                 gtk_list_store_append(store, &iter);
115                 gtk_list_store_set(store, &iter,
116                         0, desc,
117                         1, ml,
118                         2, mbar,
119                         -1);
120                 gtk_combo_box_set_active_iter(cylinder->description, &iter);
121         }
122 }
123
124 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
125 {
126         const char *desc;
127         int ml, mbar;
128         double o2;
129
130         /* Don't show uninitialized cylinder widgets */
131         if (!cylinder->description)
132                 return;
133
134         desc = cyl->type.description;
135         if (!desc)
136                 desc = "";
137         ml = cyl->type.size.mliter;
138         mbar = cyl->type.workingpressure.mbar;
139         add_cylinder(cylinder, desc, ml, mbar);
140
141         set_cylinder_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar);
142         o2 = cyl->gasmix.o2.permille / 10.0;
143         gtk_widget_set_sensitive(cylinder->o2, !!o2);
144         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
145         if (!o2)
146                 o2 = 21.0;
147         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
148 }
149
150 void show_dive_equipment(struct dive *dive)
151 {
152         int i;
153
154         for (i = 0; i < MAX_CYLINDERS; i++)
155                 show_cylinder(dive->cylinder + i, gtk_cylinder+i);
156 }
157
158 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
159 {
160         GtkWidget *frame, *hbox, *button;
161
162         frame = gtk_frame_new(name);
163         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
164
165         hbox = gtk_hbox_new(FALSE, 3);
166         gtk_container_add(GTK_CONTAINER(frame), hbox);
167
168         button = gtk_spin_button_new_with_range(min, max, incr);
169         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
170
171         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
172
173         return button;
174 }
175
176 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
177 {
178         int mbar, ml;
179
180         if (output_units.pressure == PSI)
181                 pressure /= 14.5037738;
182
183         if (pressure && output_units.volume == CUFT) {
184                 volume *= 28.3168466;   /* CUFT to liter */
185                 volume /= pressure / 1.01325;
186         }
187
188         ml = volume * 1000 + 0.5;
189         mbar = pressure * 1000 + 0.5;
190
191         if (o2 < 211)
192                 o2 = 0;
193         cyl->type.description = desc;
194         cyl->type.size.mliter = ml;
195         cyl->type.workingpressure.mbar = mbar;
196         cyl->gasmix.o2.permille = o2;
197
198         /*
199          * Also, insert it into the model if it doesn't already exist
200          */
201         add_cylinder(cylinder, desc, ml, mbar);
202 }
203
204 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
205 {
206         const gchar *desc;
207         GtkComboBox *box;
208         double volume, pressure;
209         int o2;
210
211         /* Ignore uninitialized cylinder widgets */
212         box = cylinder->description;
213         if (!box)
214                 return;
215
216         desc = gtk_combo_box_get_active_text(box);
217         volume = gtk_spin_button_get_value(cylinder->size);
218         pressure = gtk_spin_button_get_value(cylinder->pressure);
219         o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
220         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
221                 o2 = 0;
222         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, o2);
223 }
224
225 void flush_dive_equipment_changes(struct dive *dive)
226 {
227         int i;
228
229         for (i = 0; i < MAX_CYLINDERS; i++)
230                 record_cylinder_changes(dive->cylinder+i, gtk_cylinder+i);
231 }
232
233 /*
234  * We hardcode the most common standard cylinders,
235  * we should pick up any other names from the dive
236  * logs directly.
237  */
238 static struct tank_info {
239         const char *name;
240         int size;       /* cuft if < 1000, otherwise mliter */
241         int psi;        /* If zero, size is in mliter */
242 } tank_info[100] = {
243         /* Need an empty entry for the no-cylinder case */
244         { "", 0, 0 },
245
246         /* Size-only metric cylinders */
247         { "10.0 l", 10000 },
248         { "11.1 l", 11100 },
249
250         /* Most common AL cylinders */
251         { "AL50",   50, 3000 },
252         { "AL63",   63, 3000 },
253         { "AL72",   72, 3000 },
254         { "AL80",   80, 3000 },
255         { "AL100", 100, 3300 },
256
257         /* Somewhat common LP steel cylinders */
258         { "LP85",   85, 2640 },
259         { "LP95",   95, 2640 },
260         { "LP108", 108, 2640 },
261         { "LP121", 121, 2640 },
262
263         /* Somewhat common HP steel cylinders */
264         { "HP65",   65, 3442 },
265         { "HP80",   80, 3442 },
266         { "HP100", 100, 3442 },
267         { "HP119", 119, 3442 },
268         { "HP130", 130, 3442 },
269
270         /* We'll fill in more from the dive log dynamically */
271         { NULL, }
272 };
273
274 static void fill_tank_list(GtkListStore *store)
275 {
276         GtkTreeIter iter;
277         struct tank_info *info = tank_info;
278
279         while (info->name) {
280                 int size = info->size;
281                 int psi = info->psi;
282                 int mbar = 0, ml = size;
283
284                 /* Is it in cuft and psi? */
285                 if (psi) {
286                         double bar = 0.0689475729 * psi;
287                         double airvolume = 28316.8466 * size;
288                         double atm = bar / 1.01325;
289
290                         ml = airvolume / atm + 0.5;
291                         mbar = bar*1000 + 0.5;
292                 }
293
294                 gtk_list_store_append(store, &iter);
295                 gtk_list_store_set(store, &iter,
296                         0, info->name,
297                         1, ml,
298                         2, mbar,
299                         -1);
300                 info++;
301         }
302 }
303
304 static void nitrox_cb(GtkToggleButton *button, gpointer data)
305 {
306         struct cylinder_widget *cylinder = data;
307         int state;
308
309         state = gtk_toggle_button_get_active(button);
310         gtk_widget_set_sensitive(cylinder->o2, state);
311 }
312
313 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
314 {
315         struct cylinder_widget *cylinder;
316         GtkWidget *frame, *hbox, *hbox2;
317         GtkWidget *widget;
318         char buffer[80];
319
320         cylinder = gtk_cylinder + nr;
321         cylinder->index = nr;
322
323         hbox = gtk_hbox_new(FALSE, 3);
324         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
325
326         snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
327         frame = gtk_frame_new(buffer);
328         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
329
330         hbox2 = gtk_hbox_new(FALSE, 3);
331         gtk_container_add(GTK_CONTAINER(frame), hbox2);
332
333         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
334         gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
335
336         cylinder->description = GTK_COMBO_BOX(widget);
337         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
338
339         widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
340         cylinder->size = GTK_SPIN_BUTTON(widget);
341
342         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
343         cylinder->pressure = GTK_SPIN_BUTTON(widget);
344
345         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
346         cylinder->o2 = widget;
347         cylinder->gasmix_button = gtk_check_button_new();
348         gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
349                 cylinder->gasmix_button, FALSE, FALSE, 3);
350         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
351
352         gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
353 }
354
355 static GtkListStore *create_tank_size_model(void)
356 {
357         GtkListStore *model;
358
359         model = gtk_list_store_new(3,
360                 G_TYPE_STRING,          /* Tank name */
361                 G_TYPE_INT,             /* Tank size in mliter */
362                 G_TYPE_INT,             /* Tank working pressure in mbar */
363                 -1);
364
365         fill_tank_list(model);
366         return model;
367 }
368
369 GtkWidget *equipment_widget(void)
370 {
371         GtkWidget *vbox;
372         GtkListStore *model;
373
374         vbox = gtk_vbox_new(FALSE, 3);
375
376         model = create_tank_size_model();
377         cylinder_widget(vbox, 0, model);
378         cylinder_widget(vbox, 1, model);
379
380         return vbox;
381 }